Exemplo n.º 1
0
 def load_sites(self):
     """
     Instructs the SiteHierarchy object to load all sites from the Cisco DNA Center's site design hierarchy.  This
     method can be used to load a new hierarchy object or to refresh one that has sites which have been added or
     deleted.
     :return: dict
     """
     self.get_all_sites()
     # find the site hierarchy's root: siteNameHierarchy = "Global"
     global_site_node = None
     for site in self.__all_sites:
         if site['siteNameHierarchy'] != GLOBAL_SITE:
             continue
         else:  # found the root
             global_site = Site(self.dnac, GLOBAL_SITE)
             global_site_node = SiteNode(global_site)
             self.add_site_node(global_site_node)
             break
     if global_site_node is None:
         raise DnacApiError(MODULE, 'load_sites', NO_GLOBAL_SITE_ERROR, '',
                            '', str(global_site_node), '', '')
     # starting from the global site, load all children recursively
     #
     #  need a throttle here; this is where the majority of the site calls are being made
     #  perhaps the throttle should be part of a SiteHierarchy instance?
     #
     self.__load_children__(global_site_node)
     return self.__site_nodes
Exemplo n.º 2
0
def get_site(device, cluster):
    details = device.get_device_detail_by_name(device.devices['id'])
    location = details['location']
    if location in cluster.api.keys():
        location_id = cluster.api[location].id
    else:
        site = Site(cluster, location)
        location_id = site.id
    return location_id
Exemplo n.º 3
0
def find_device_by_ip():
    ip = request.forms.get('ip')
    device = None
    target_cluster = None
    cluster_id = None
    inventory_url = ''
    device360_url = ''
    for cluster in clusters:
        # if the device has been cached, return it
        if ip in cluster.api.keys():
            device = cluster.api[ip]
            target_cluster = cluster
            cluster_id = get_cluster_id(cluster)
            break
        # otherwise, try finding it in the current cluster
        else:
            try:
                # an exception will be thrown if it's not in the cluster
                result = cluster.api[STUB_DEVICE].get_device_by_ip(ip)
                # otherwise, it does exist in the physical cluster; cache it in the cluster and return it
                device = NetworkDevice(cluster, result['managementIpAddress'])
                target_cluster = cluster
                cluster_id = get_cluster_id(cluster)
                break
            except DnacApiError:
                # device not found; try the next cluster
                continue
    # if the device was found, get the associated site info in case the user wants to cross-launch into inventory
    if bool(device):
        device_by_ip = device.get_device_by_ip(ip)
        details = device.get_device_detail_by_name(device_by_ip['hostname'])
        location = details['location']
        if location in target_cluster.api.keys():
            site = target_cluster.api[location]
        else:
            site = Site(target_cluster, location)
        inventory_url = '%s%s%s%s' % (HTTPS, cluster_id, INVENTORY_URI,
                                      site.id)
        device360_url = '%s%s%s%s' % (HTTPS, cluster_id, DEVICE360_URI,
                                      device.devices['id'])
    return template('results',
                    target=ip,
                    cluster=cluster_id,
                    device=device,
                    inventory_url=inventory_url,
                    device360_url=device360_url,
                    method='GET')
Exemplo n.º 4
0
    def __load_children__(self, parent_node):
        """
        A hidden method used by the load_sites method to build the site hierarchy.  If a site doesn't exist in the
        Dnac object's api dictionary, this method creates one, installs it and adds a SiteNode to it in the
        SiteHierarchy.  If the site does exist but not in the SiteHierarchy, it installs a SiteNode into the
        SiteHierarchy.

        This method use a recursive call to build the SiteHierarchy.
        :param parent_node: The new site's parent SiteNode.
            type: SiteNode
            required: yes
            default: none
        :return: none
        """
        for site in self.__all_sites:
            if site['siteNameHierarchy'] == GLOBAL_SITE:
                continue
            if site['parentId'] != parent_node.site.id:
                continue
            else:
                if site['siteNameHierarchy'] not in self.dnac.api.keys():
                    # site does not exist; create it now
                    child_site = Site(self.dnac, site['siteNameHierarchy'])
                else:
                    # site exists; get it from Dnac.api
                    child_site = self.dnac.api[site['siteNameHierarchy']]
                    if site['siteNameHierarchy'] in self.__site_nodes.keys():
                        # the site and its site node both exist; nothing to do
                        continue
                # the site's site node does not exist in the hierarchy; create a new site node
                child_node = SiteNode(child_site)
                # add the site node to the parent's children
                parent_node.add_child(child_node)
                # add the site node to the site hierarchy
                self.add_site_node(child_node)
                # find the child node's children (recursive)
                # if the child has no children, there will be no hits on any other site's parentId
                # the method call for the node ends with no action and falls back to the previous call in the stack
                self.__load_children__(child_node)
        return
Exemplo n.º 5
0
# Main Program ########################################################################################################

if __name__ == '__main__':
    """
    usage: site_heirarchy_replicator <clusters_file>
    """

    # read the json encoded config file passed from the CLI
    clusters_file = open(sys.argv[1], mode='r')
    clusters_from_config_file = json.load(clusters_file)
    clusters_file.close()

    # create all the Dnac objects from the clusters that were listed in the config file
    clusters = []
    for cluster in clusters_from_config_file:
        # create a Dnac object
        dnac = Dnac(version=cluster['version'],
                    name=cluster['name'],
                    ip=cluster['ip'],
                    port=cluster['port'],
                    user=cluster['user'],
                    passwd=cluster['passwd'],
                    content_type=cluster['content_type'])
        # create a stub site for adding new sites
        stub_site = Site(dnac, STUB_SITE)
        stub_site.timeout = 60  # my lab's DNAC server is responding slowly; others may not need this
        # add the new Dnac instance to the global clusters list
        clusters.append(dnac)

    run(replicator, host='localhost', port=8088, reloader=True, debug=True)