class IpamApiRequest:
    """IPAM API Call Definition."""
    # pylint: disable = R0903
    # This class is just meant to house the api construct.
    def __init__(self):
        self._env_cls = EnvironmentValues()
        self._log_cls = LoggingValues()
        self._ipam_url = self._env_cls.payload_url()
        self._ipam_username = self._env_cls.payload_username()
        self._ipam_password = self._env_cls.payload_password()
        logging.getLogger("urllib3").setLevel(logging.WARNING)
        logging.basicConfig(filename=self._log_cls.log_filename(),
                            level=logging.INFO,
                            format=self._log_cls.log_format())
        self._logger = logging.getLogger(__name__)

    def ipam_api_request(self, _):
        """
        IPAM API Call.

        Args:
            self._item: The missing puzzle piece needed for the api call.  The
            return value of Class IpamCallTypes.
        Returns:
            The requests content decoded to 'utf-8' then returns as a python
            object after going through a json.loads process.
        """
        try_call_countdown = 3
        net_call = None
        self._logger.info('Calling IPAM for: %s', _)
        for countdown in range(try_call_countdown):
            try:
                net_call = requests.get(self._ipam_url +
                                        _,
                                        auth=(self._ipam_username,
                                              self._ipam_password),
                                        verify=False)
                break
            except requests.exceptions.ConnectionError as nerrt:
                if countdown < try_call_countdown - 1:
                    self._logger.warning('Failed %s lookup. Round %s of 3.',
                                         _,
                                         countdown)
                    time.sleep(5)
                    continue
                else:
                    self._logger.warning(
                        'Timeout Error for container view: %s, %s, %s',
                        _,
                        countdown,
                        nerrt)
                    return []
        return net_call
예제 #2
0
class TestProjEnvVariables(unittest.TestCase):
    def setUp(self):
        self.env_cls = EnvironmentValues()

    def test_type_of_env_cls_payload_url(self):
        self.assertIsInstance(self.env_cls.payload_url(), str)

    def test_type_of_env_cls_payload_username(self):
        self.assertIsInstance(self.env_cls.payload_username(), str)

    def test_type_of_env_cls_payload_password(self):
        self.assertIsInstance(self.env_cls.payload_password(), str)

    def test_type_of_env_cls_header_row(self):
        self.assertIsInstance(self.env_cls.header_row(), list)
예제 #3
0
def main(ext_att=False, net_view=False, get_networks=True):
    log_cls = LoggingValues()
    logging.basicConfig(filename=log_cls.log_filename(),
                        level=logging.DEBUG,
                        filemode='a',
                        format=log_cls.log_format())

    logger = logging.getLogger(__file__.split("/")[-1:][0])

    logger.info('=========================')
    logger.info('Main Script Initializing.')
    logger.info('=========================')

    logger.info('Loading Project Environment Variables.')
    env_cls = EnvironmentValues()
    dir_cls = DirectoryValues()
    write_cls = Writer()
    reader_cls = Reader()
    logger.info('Project Environment Variables Loaded.')

    logger.info('===================')
    logger.info('Compiling DDI Data.')
    logger.info('===================')
    call_types = DdiCallTypes()
    filenames = DdiCallFilenames()
    ext_call_setup = IpamApiRequestGet()

    def extensible_attribute_call():
        logger.info('Pulling current Extensible Attribute data.')
        ext_attr_data = ext_call_setup.ddi_call(
            call_types.extensibles_attribute())
        # print(ext_attr_data)
        # print(type(ext_attr_data))
        write_cls.write_to_pkl(dir_cls.raw_dir(),
                               filenames.extensibles_attribute_filename(),
                               ext_attr_data)
        logger.info('Extensible Attr data written to .pkl file in Raw Dir.')

    if ext_att:
        extensible_attribute_call()
    else:
        logger.info('Extensible Attributes data was not set to True. Skipping')

    def network_view_call():
        logger.info('Pulling current Network View Data.')
        network_view_data = ext_call_setup.ddi_call(call_types.network_views())
        # print(network_view_data)
        # print(type(network_view_data))
        write_cls.write_to_pkl(dir_cls.raw_dir(),
                               filenames.network_views_filename(),
                               network_view_data)
        logger.info('Network View data written to .pkl file in raw Dir.')

    if net_view:
        network_view_call()
    else:
        logger.info('Network View data was not set to True. Skipping.')

    def get_network():
        logger.info('Pulling current IPAM Data.')
        network_views = reader_cls.read_from_pkl(
            dir_cls.raw_dir(), filenames.network_views_filename())
        network_data = []
        start = time.perf_counter()
        for _ref in network_views:
            # if _ref == network_views[1]:
            #     break
            network_data += ext_call_setup.ddi_call(
                call_types.networks(_ref['name']))
        end = time.perf_counter()
        logging.info("Downloaded {} Networks in {} seconds".format(
            len(network_data), end - start))
        write_cls.write_to_pkl(dir_cls.raw_dir(),
                               filenames.networks_filename(), network_data)
        logger.info('Network Data written to .pkl file in raw Dir.')

    if get_networks:
        get_network()
    else:
        logger.info('Network data was not set to True.  Skipping.')
    exit()
    print(env_cls.payload_password())
    print(env_cls.header_row())
    print(type(dir_cls.reports_dir()))
    print(dir_cls.raw_dir())
    print(dir_cls.interim_dir())
    print(dir_cls.reports_dir())
    print(type(dir_cls.processed_dir()))