Example #1
0
 def __init__(self, max_threads, scan_vlan, snmp_community, snmp_version,
              *args, **kwargs):
     super(DeviceScannerDriver, self).__init__(*args, **kwargs)
     self.site_id = self.click_ctx.obj['SITE_ID']
     self.logger = logging.getLogger(__name__)
     self.c = get_api_client()
     self.devices_to_update = []
     self.exit_app = False
     self.scan_vlan = scan_vlan
     self.snmp_community = snmp_community
     self.snmp_version = snmp_version
     self.max_threads = max_threads
     try:
         self.logger.info('Getting networks for site: %s', self.site_id)
         self.networks = self.c.sites(self.site_id).networks.get()
         self.logger.info('Getting devices for site: %s', self.site_id)
         self.devices = self.c.sites(self.site_id).devices.get()
     except ConnectionError:
         self.click_ctx.fail('Cannot connect to NSoT server')
         raise
     except HttpClientError as e:
         self.handle_pynsot_err(e)
         raise
     except Exception:
         self.logger.exception(
             'DeviceScannerDriver, Getting networks and devices.')
         raise
Example #2
0
    def __init__(self, max_threads, scan_all, scan_all_update, scan_vlan,
                 *args, **kwargs):
        super(IpScannerDriver, self).__init__(*args, **kwargs)
        self.site_id = self.click_ctx.obj['SITE_ID']
        self.logger = logging.getLogger(__name__)
        self.c = get_api_client()
        self.network_to_update = []
        self.exit_app = False
        self.scan_all = scan_all
        self.scan_all_update = scan_all_update
        self.max_threads = max_threads
        self.scan_vlan = scan_vlan

        try:
            self.logger.info('Getting networks for site: %s', self.site_id)
            self.networks = self.c.sites(self.site_id).networks.get()
        except ConnectionError:
            self.click_ctx.fail('Cannot connect to NSoT server')
            raise
        except HttpClientError as e:
            self.handle_pynsot_err(e)
            raise
        except Exception:
            self.logger.exception(
                'IpScannerDriver, getting existing networks.')
            raise
Example #3
0
    def ensure_client(self, **kwargs):
        '''Ensure that object has a client object

        Client may be passed during __init__ as a kwarg, but call this before
        doing any client work to ensure

        :param kwargs: These will be passed to get_api_client.
        :type kwargs: dict
        '''
        if self.client:
            return
        else:
            self.client = get_api_client(**kwargs)
Example #4
0
    def ensure_client(self, **kwargs):
        '''Ensure that object has a client object

        Client may be passed during __init__ as a kwarg, but call this before
        doing any client work to ensure

        :param kwargs: These will be passed to get_api_client.
        :type kwargs: dict
        '''
        if self.client:
            return
        else:
            self.client = get_api_client(**kwargs)
Example #5
0
    def __init__(self, click_ctx=None):
        '''
        '''

        if click_ctx is None:
            raise Exception('Please pass click context to driver init')

        c = get_api_client()

        self.click_ctx = click_ctx
        self.site_id = click_ctx.obj['SITE_ID']
        self.client = c.sites(self.site_id)
        logger = logging.getLogger(__name__)
        self.logger = logger

        self.require_extra_attrs()
Example #6
0
    def __init__(self, click_ctx=None):
        '''
        '''

        if click_ctx is None:
            raise Exception('Please pass click context to driver init')

        c = get_api_client()

        self.click_ctx = click_ctx
        self.site_id = click_ctx.obj['SITE_ID']
        self.client = c.sites(self.site_id)
        logger = logging.getLogger(__name__)
        self.logger = logger

        self.require_extra_attrs()
Example #7
0
def reset(ctx, debug=False):
    """Resets the database. All sites and their related data are wiped out."""
    global logger
    logger = helpers.get_logger(debug)

    client = get_api_client()

    for site in client.sites.get():
        logger.info("Deleting site: {}".format(site))
        _reset_resources(client, site['id'])
        _reset_resources(client, site['id'])
        _reset_resources(client, site['id'])
        try:
            client.sites(site['id']).delete()
        except HttpClientError as e:
            logger.error(e)
            logger.error(e.response.json())
            sys.exit(-1)
Example #8
0
def create(ctx, name, description="", debug=False):
    """Create a site."""
    global logger
    logger = helpers.get_logger(debug)

    client = get_api_client()
    site = client.sites.get(name=name)

    site_data = {
        'name': name,
        'description': description,
    }

    if not site:
        site = client.sites.post(site_data)
    else:
        site = client.sites(site[0]['id']).put(site_data)

    logger.info(site)
    return site
Example #9
0
def loopbacks(ctx, site, filename="data/services.yml", debug=False):
    """Read service definition for loopbacks service and add data to the backend."""
    global logger
    logger = helpers.get_logger(debug)

    client = get_api_client()
    with open(filename, 'r') as f:
        service = yaml.load(f.read())['loopbacks']
        logger.debug(service)

    site_id = helpers.get_site_id(client, site)

    network = _create_networks(client, service['network_ranges'], 'loopback',
                               site_id)['loopbacks']

    ifaces = []
    for device in client.sites(site_id).devices.get():
        ifaces.append(_create_interface(client, site_id, device['id'], 'lo0', 'loopbacks',
                                        'loopback', 'loopback', network))
    return ifaces
Example #10
0
def ipfabric(ctx, site, filename="data/services.yml", debug=False):
    """Read service definition for ipfabric service and add data to the backend."""
    global logger
    logger = helpers.get_logger(debug)

    client = get_api_client()
    with open(filename, 'r') as f:
        service = yaml.load(f.read())['ipfabric']
        logger.debug(service)

    site_id = helpers.get_site_id(client, site)

    supernet = _create_networks(client, service['network_ranges'], 'ipfabric',
                                site_id)['fabric_links']

    for link in service['definition']['links']:
        network = None
        logger.debug("Processing {}".format(link))
        left_id = helpers.get_host_id(client, site_id, link['left_device'])
        right_id = helpers.get_host_id(client, site_id, link['right_device'])
        logger.debug("Found hosts {} and {}".format(left_id, right_id))

        left_interface = _create_interface(client, site_id, left_id, link['left_iface'],
                                           'fabric_links', link['right_device'],
                                           link['right_iface'])
        right_interface = _create_interface(client, site_id, right_id, link['right_iface'],
                                            'fabric_links', link['left_device'],
                                            link['left_iface'])
        if not left_interface['networks']:
            network = _create_subnet(client, site_id, supernet, 127)
            left_interface = _create_interface(client, site_id, left_id, link['left_iface'],
                                               'fabric_links', link['right_device'],
                                               link['right_iface'], network)
        if not right_interface['networks']:
            network = client.sites(site_id).networks(left_interface['networks'][0]).get()
            right_interface = _create_interface(client, site_id, right_id, link['right_iface'],
                                                'fabric_links', link['left_device'],
                                                link['left_iface'], network)

    return network
Example #11
0
def add_atribbutes(ctx, site, filename="data/attributes.yml", debug=False):
    """Add attributes to a site."""
    global logger
    logger = helpers.get_logger(debug)

    client = get_api_client()
    with open(filename, 'r') as f:
        attributes = yaml.load(f.read())['attributes']

    site_id = helpers.get_site_id(client, site)

    result = {}
    for resource, attrs in attributes.items():
        for attr_name, attr_options in attrs.items():
            attribute = client.sites(site_id).attributes.get(resource=resource,
                                                             name=attr_name)

            options = {
                'name': attr_name,
                'resource_name': resource,
            }
            options.update(attr_options)
            try:
                if not attribute:
                    logger.info("Creating attribute: {}".format(options))
                    attribute = client.sites(site_id).attributes.post(options)
                else:
                    logger.info("Updating attribute: {}".format(attribute))
                    attribute = client.sites(site_id).attributes(
                        attribute[0]['id']).put(options)
            except HttpClientError as e:
                logger.error(e)
                logger.error(e.response.json())
                sys.exit(-1)

            result[attr_name] = attribute

    return result
Example #12
0
    def __init__(self):
        self.config = dict()
        config_env = os.environ.get('NSOT_INVENTORY_CONFIG')
        if config_env:
            try:
                config_file = os.path.abspath(config_env)
            except IOError:  # If file non-existent, use default config
                self._config_default()
            except Exception as e:
                sys.exit('%s\n' % e)

            with open(config_file) as f:
                try:
                    self.config.update(yaml.safe_load(f))
                except TypeError:  # If empty file, use default config
                    warning('Empty config file')
                    self._config_default()
                except Exception as e:
                    sys.exit('%s\n' % e)
        else:  # Use defaults if env var missing
            self._config_default()
        self.groups = self.config.keys()
        self.client = get_api_client()
        self._meta = {'hostvars': dict()}
Example #13
0
    def __init__(self):
        self.config = dict()
        config_env = os.environ.get('NSOT_INVENTORY_CONFIG', './nsot.yaml')
        if os.path.isfile(config_env):
            try:
                config_file = os.path.abspath(config_env)
            except IOError:  # If file non-existent, use default config
                self._config_default()
            except Exception as e:
                sys.exit('%s\n' % e)

            with open(config_file) as f:
                try:
                    self.config.update(yaml.safe_load(f))
                except TypeError:  # If empty file, use default config
                    warning('Empty config file')
                    self._config_default()
                except Exception as e:
                    sys.exit('%s\n' % e)
        else:  # Use defaults if env var missing
            self._config_default()
        self.groups = self.config.keys()
        self.client = get_api_client()
        self._meta = {'hostvars': dict()}
Example #14
0
def add_devices(ctx, site, filename="data/devices.yml", debug=False):
    """Add devices to a site."""
    global logger
    logger = helpers.get_logger(debug)

    client = get_api_client()
    with open(filename, 'r') as f:
        devices = yaml.load(f.read())['devices']

    site_id = helpers.get_site_id(client, site)

    result = {}
    for host, data in devices.items():
        device = client.sites(site_id).devices.get(hostname=host)
        data = {'attributes': data, 'hostname': host}
        if not device:
            logger.info("Creating device: {}".format(data))
            device = client.sites(site_id).devices.post(data)
        else:
            logger.info("Updating device: {}".format(device))
            device = client.sites(site_id).devices(device[0]['id']).put(data)
        result[host] = device

    return result
Example #15
0
class NSoTClient:
    """Manage NSoT API client"""

    resource = get_api_client()
    # iter_sites_attributes = resource.attributes.get()
    _sites = {site["name"]: int(site["id"]) for site in resource.sites.get()}

    _sites_id_cache = {}
    _endpoints_cache = {}
    _default_site = None

    _resource_cache = {}

    @classmethod
    def get_resource(cls, name):
        if name not in RESOURCES:
            raise ValueError("Invalud resource: %s" % name)
        if cls._resource_cache.get(name) is None:
            cls._resource_cache[name] = {
                r["id"]: r
                for r in getattr(cls.resource, name).get()
            }
        return cls._resource_cache[name]

    @classmethod
    def check_sitename(cls, site_name):
        return bool(cls._sites.get(site_name))

    @classmethod
    def default_site(cls):
        """Return the default site ID and name if defined in pynsotrc file as
        default_site"""

        if cls._default_site is None:
            _site_id = cls.resource.default_site

            if _site_id is None:
                raise ValueError("No 'default_site' defined in pynsotrc")

            site_id = int(_site_id)
            site_name = [
                k for k, v in cls._sites.items() if v == int(site_id)
            ][0]
            cls._default_site = site_id, site_name

        return cls._default_site

    @classmethod
    def get_sitename(cls, site_id):
        "Return the site name of the given site ID"
        int_siteid = int(site_id)

        if cls._sites_id_cache.get(int_siteid) is None:

            site_name = None
            for name, _id in cls._sites.items():
                if _id == site_id:
                    site_name = name
                    break

            if site_name is None:
                raise ValueError("Site ID '%s' does not exists" % site_id)

            cls._sites_id_cache[int_siteid] = site_name

        return cls._sites_id_cache[int_siteid]

    @classmethod
    def get_siteid(cls, site_name):
        site_id = cls._sites.get(site_name)
        if site_id is None:
            raise ValueError("Site '%s' does not exists" % site_name)
        return site_id

    @classmethod
    def get_endpoint(cls, resource_name, site_name):
        "Return the endpoint API of the given resource and site name"

        # if site_name is None:
        #     site_id, site_name = cls.default_site()
        # else:
        #     site_id = cls._sites.get(site_name)
        #     if site_id is None:
        #         raise ValueError("Site '%s' does not exists" % site_name)

        key = resource_name, site_name
        if cls._endpoints_cache.get(key) is None:
            site_id = cls.get_site_id(site_name)
            cls._endpoints_cache[key] = getattr(cls.resource.sites(site_id),
                                                resource_name)
        return cls._endpoints_cache[key]

    @classmethod
    def create_site(cls, site_name, description=None):
        "POST a new site in NSoT server"
        if description is None:
            description = ""

        payload = {"name": site_name, "description": description}
        result = cls.resource.sites.post(payload)
        cls._sites[site_name] = int(result["id"])
Example #16
0
def client(config):
    """Create and return an admin client."""
    api = get_api_client(extra_args=config, use_dotfile=False)
    api.config = config
    return api
Example #17
0
def client(config):
    """Create and return an admin client."""
    api = get_api_client(extra_args=config, use_dotfile=False)
    api.config = config
    return api
    result = {}

    for host, data in devices.items():
        device = api.devices.get(site=site['id'], hostname=host)
        data = {'attributes': data, 'hostname': host}
        if not device:
            device = api.sites(site['id']).devices.post(data)
        else:
            device = api.sites(site['id']).devices(device[0]['id']).put(data)
        result[host] = device
        logger.debug("device: {}".format(device))
    return result


if __name__ == "__main__":
    with open('service.yaml', 'r') as f:
        data = yaml.load(f.read())

    _configure_logging(logger, debug=False)

    global api
    api = get_api_client()

    site = create_site(data['site'])
    attributes = create_attributes(data['attributes'], site)
    devices = create_devices(data['devices'], site)

    services = {}
    for svc, svc_data in data['services'].items():
        services[svc] = create_service(svc, svc_data, site, devices)