コード例 #1
0
ファイル: device.py プロジェクト: aruba/pyaoscx
    def get(self):
        '''
        Perform a GET call to retrieve device attributes
        After data from response, Device class attributes
        are generated using create_attrs()

        '''
        logging.info("Retrieving the switch attributes and capabilities")
        attributes = [
            'software_version',
            'software_images',
            'software_info',
            'platform_name',
            'hostname',
            'boot_time',
            'mgmt_intf_status',
            'aruba_central',
            'capabilities',
            'capacities',
            'admin_password_set',
            'other_config',
            'domain_name'
        ]

        attributes_list = ','.join(attributes)
        uri = "{}system?attributes={}&depth={}".format(
            self.session.base_url, attributes_list,
            self.session.api_version.default_depth)

        try:
            response = self.session.s.get(
                uri, verify=False, proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        # Load into json format
        data = json.loads(response.text)

        # Create class attributes using util.create_attrs
        utils.create_attrs(self, data)

        # Set device as materialized
        self.materialized = True
コード例 #2
0
ファイル: device.py プロジェクト: aruba/pyaoscx
    def get_subsystems(self):
        '''
         Perform GET call to retrieve subsystem attributes and create a dictionary containing them
        '''
        # Log
        logging.info("Retrieving the switch subsystem attributes and capabilities")
        
        # Attribute list
        attributes = [
            'product_info',
            'power_supplies',
            'interfaces',
            'fans',
            'resource_utilization'
        ]

        # Format attribute list by joining every element with a comma
        attributes_list = ','.join(attributes)

        # Build URI
        uri = "{}system/subsystems?attributes={}&depth={}".format(
            self.session.base_url, attributes_list,
            self.session.api_version.default_subsystem_facts_depth)

        try:
            # Try to perform a GET call and retrieve the data
            response = self.session.s.get(
                uri, verify=False, proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        # Load into json format
        data = json.loads(response.text)
        data_subsystems = {'subsystems' : data}

        # Create class attributes using util.create_attrs
        utils.create_attrs(self, data_subsystems)
コード例 #3
0
ファイル: acl.py プロジェクト: aruba/pyaoscx
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for an ACL table entry and fill
        the object with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch ACLs")

        depth = self.session.api_version.default_depth\
            if depth is None else depth
        selector = self.session.api_version.default_selector\
            if selector is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}".format(selectors))

        payload = {"depth": depth, "selector": selector}

        uri = "{base_url}{class_uri}/{id1}{separator}{id2}".format(
            base_url=self.session.base_url,
            class_uri=ACL.base_uri,
            id1=self.name,
            separator=self.session.api_version.compound_index_separator,
            id2=self.list_type)
        try:
            response = self.session.s.get(uri,
                                          verify=False,
                                          params=payload,
                                          proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)

        # Remove fields because they are not needed for the PUT request
        if 'name' in data:
            data.pop('name')
        if 'list_type' in data:
            data.pop('list_type')
        # Delete unwanted data
        if 'cfg_aces' in data:
            data.pop('cfg_aces')

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if the ACL is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs',
                                   ['name', 'list_type'])

        # Set original attributes
        self.__original_attributes = data

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True

        # Clean ACL Entries settings
        if self.cfg_aces == []:
            # Set ACL Entries if any
            # Adds ACL Entries to parent ACL already
            from pyaoscx.acl_entry import AclEntry
            AclEntry.get_all(self.session, self)
        return True
コード例 #4
0
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a OSPF Area table entry and
        fill the object with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch OSPF Areas")

        depth = self.session.api_version.default_depth if depth is None \
            else depth
        selector = self.session.api_version.default_selector if selector is \
            None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}".format(selectors))

        payload = {"depth": depth, "selector": selector}

        uri = "{base_url}{class_uri}/{id}".format(
            base_url=self.session.base_url,
            class_uri=self.base_uri,
            id=self.area_id)

        try:
            response = self.session.s.get(uri,
                                          verify=False,
                                          params=payload,
                                          proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)

        # Delete unwanted data
        if 'ospf_interfaces' in data:
            data.pop('ospf_interfaces')

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if the OSPF Area is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs', ['area_id'])

        # Set original attributes
        self.__original_attributes = data

        # Remove ID
        if 'area_id' in self.__original_attributes:
            self.__original_attributes.pop('area_id')

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True

        # Clean areas
        if self.ospf_interfaces == []:
            # Set Areas if any
            # Adds OSPF Interface to parent OSPF Area already
            OspfInterface.get_all(self.session, self)

        return True
コード例 #5
0
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a VRF table entry and fill the
        class with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information
            to return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch VRF")

        depth = self.session.api_version.default_depth if depth is \
            None else depth
        selector = self.session.api_version.default_selector if selector\
            is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}".format(selectors))

        payload = {"depth": depth, "selector": selector}

        uri = "{base_url}{class_uri}/{name}".format(
            base_url=self.session.base_url,
            class_uri=Vrf.base_uri,
            name=self.name)

        try:
            response = self.session.s.get(uri,
                                          verify=False,
                                          params=payload,
                                          proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)
        # Delete unwanted data
        if 'ospf_routers' in data:
            data.pop('ospf_routers')
            data.pop('bgp_routers')
        if 'static_routes' in data:
            data.pop("static_routes")

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if the VRF is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs', [
                'name', 'type', 'bgp_routers', 'ospf_routers',
                'vrf_address_families', 'static_routes'
            ])

        # Set original attributes
        self.__original_attributes = data
        # Remove ID
        if 'name' in self.__original_attributes:
            self.__original_attributes.pop('name')
        # Remove type
        if 'type' in self.__original_attributes:
            self.__original_attributes.pop('type')
        # Remove bgp_routers
        if 'bgp_routers' in self.__original_attributes:
            self.__original_attributes.pop('bgp_routers')
        # Remove ospf_routers
        if 'ospf_routers' in self.__original_attributes:
            self.__original_attributes.pop('ospf_routers')
        # Remove static_routes
        if 'static_routes' in self.__original_attributes:
            self.__original_attributes.pop('static_routes')
        # Remove vrf_address_families
        if 'vrf_address_families' in self.__original_attributes:
            self.__original_attributes.pop('vrf_address_families')

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True

        # Clean BGP Router settings
        if self.bgp_routers == []:
            # Set BGP Routers if any
            # Adds bgp_bouters to parent Vrf object
            BgpRouter.get_all(self.session, self)

        # Clean Address Families settings
        if self.address_families == []:
            # Set Address Families if any
            # Adds address_families to parent Vrf object
            VrfAddressFamily.get_all(self.session, self)

        # Clean OSPF Routers settings
        if self.ospf_routers == []:
            # Set OSPF Routers if any
            # Adds ospf_routers to parent Vrf object
            OspfRouter.get_all(self.session, self)

        # Clean Static Routess settings
        if self.static_routes == []:
            # Set Static Route if any
            # Adds static_routes to parent Vrf object
            StaticRoute.get_all(self.session, self)
        return True
コード例 #6
0
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a OSPF Router table entry and
        fill the object with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch OSPF Router information")

        depth = self.session.api_version.default_depth \
            if depth is None else depth
        selector = self.session.api_version.default_selector \
            if selector is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}".format(selectors))

        payload = {"depth": depth, "selector": selector}

        uri = "{base_url}{class_uri}/{instance_tag}".format(
            base_url=self.session.base_url,
            class_uri=self.base_uri,
            instance_tag=self.instance_tag)

        try:
            response = self.session.s.get(uri,
                                          verify=False,
                                          params=payload,
                                          proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)
        # Delete unwanted data
        if 'areas' in data:
            data.pop('areas')

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if the OSPF Router is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs',
                                   ['instance_tag'])

        # Set original attributes
        self.__original_attributes = data

        # Remove ID
        if 'instance_tag' in self.__original_attributes:
            self.__original_attributes.pop('instance_tag')

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True

        # Set a list of passive_interfaces as an attribute
        if hasattr(self, 'passive_interfaces') and \
                self.passive_interfaces is not None:
            interfaces_list = []
            # Get all URI elements in the form of a list
            uri_list = self.session.api_version.get_uri_from_data(
                self.passive_interfaces)

            for uri in uri_list:
                # Create an Interface object
                name, interface = Interface.from_uri(self.session, uri)

                # Materialize interface
                interface.get()

                # Add interface to list
                interfaces_list.append(interface)

            # Set list as Interfaces
            self.passive_interfaces = interfaces_list

        # Clean OSPF Area settings
        if self.areas == []:
            # Set Areas if any
            # Adds Area to parent OspfRouter
            OspfArea.get_all(self.session, self)
        return True
コード例 #7
0
ファイル: aggregate_address.py プロジェクト: aruba/pyaoscx
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a Aggregate Address table entry and fill
        the object with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch Aggregate Addresses")

        depth = self.session.api_version.default_depth\
            if depth is None else depth
        selector = self.session.api_version.default_selector\
            if selector is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}".format(selectors))

        payload = {"depth": depth, "selector": selector}

        uri = "{base_url}{class_uri}/{id1}{separator}{id2}".format(
            base_url=self.session.base_url,
            class_uri=self.base_uri,
            id1=self.address_family,
            separator=self.session.api_version.compound_index_separator,
            id2=self.reference_ip_prefix)

        try:
            response = self.session.s.get(uri,
                                          verify=False,
                                          params=payload,
                                          proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if the Aggregate Address is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs',
                                   ['address-family'])

        # Set original attributes
        self.__original_attributes = data
        # Remove ID
        if 'address-family' in self.__original_attributes:
            self.__original_attributes.pop('address-family')

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True
        return True
コード例 #8
0
ファイル: vsx.py プロジェクト: aruba/pyaoscx
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a VSX table entry and fill the
        class with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch VSX configuration")

        depth = self.session.api_version.default_depth \
            if depth is None else depth
        selector = self.session.api_version.default_selector \
            if selector is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception("ERROR: Selector should be one of %s" % selectors)

        payload = {"depth": depth, "selector": selector}

        uri = "{base_url}{class_uri}".format(base_url=self.session.base_url,
                                             class_uri=Vsx.base_uri)

        try:
            response = self.session.s.get(uri,
                                          verify=False,
                                          params=payload,
                                          proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if VSX is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs')

        # Set original attributes
        self.__original_attributes = data

        # If the VSX has a isl_port inside the switch and a new one is not
        # being added
        if hasattr(self, 'isl_port') and self.isl_port is not None:
            isl_port_response = self.isl_port
            interface_cls = self.session.api_version.get_module(
                self.session, 'Interface', '')
            # Set port as a Interface Object
            self.isl_port = interface_cls.from_response(
                self.session, isl_port_response)
            self.isl_port.get()
        # If the VSX has a keepalive_vrf inside the switch and a new one is
        # not being added
        if hasattr(self, 'keepalive_vrf') and self.keepalive_vrf is not None:

            # Set keepalive VRF as a Vrf object
            self.keepalive_vrf = Vrf.from_response(self.session,
                                                   self.keepalive_vrf)
            self.keepalive_vrf.get()

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True
        return True
コード例 #9
0
ファイル: static_nexthop.py プロジェクト: aruba/pyaoscx
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a Static Nexthop table
            entry and fill the object with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch static_nexthop routers")

        depth = self.session.api_version.default_depth\
            if depth is None else depth

        selector = self.session.api_version.default_selector\
            if selector is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}".format(selectors))

        payload = {"depth": depth, "selector": selector}

        uri = "{base_url}{class_uri}/{id}".format(
            base_url=self.session.base_url,
            class_uri=self.base_uri,
            id=self.id)
        try:
            response = self.session.s.get(uri,
                                          verify=False,
                                          params=payload,
                                          proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if the Static Nexthop is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs', ['id'])

        # Set original attributes
        self.__original_attributes = data
        # Remove ID
        if 'id' in self.__original_attributes:
            self.__original_attributes.pop('id')

        # If the Static Route has a port inside the switch
        if 'port' in data and \
                self.port is not None:
            port_response = self.port
            interface_cls = self.session.api_version.get_module(
                self.session, 'Interface', '')
            # Set port as a Interface Object
            self.port = interface_cls.from_response(self.session,
                                                    port_response)
            # Materialize port
            self.port.get()

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True

        return True
コード例 #10
0
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a VLAN table entry and fill
        the object with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving the switch VLANs")

        depth = self.session.api_version.default_depth if depth is None \
            else depth
        selector = self.session.api_version.default_selector if selector \
            is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}".format(selectors))

        payload = {
            "depth": depth,
            "selector": selector
        }

        uri = "{base_url}{class_uri}/{id}".format(
            base_url=self.session.base_url,
            class_uri=Vlan.base_uri,
            id=self.id
        )

        try:
            response = self.session.s.get(
                uri, verify=False, params=payload,
                proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(
                response.text, response.status_code, "GET VLAN")

        data = json.loads(response.text)

        # Add dictionary as attributes for the object
        utils.create_attrs(self, data)

        # Determines if the VLAN is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Set self.config_attrs and delete ID from it
            utils.set_config_attrs(self, data, 'config_attrs', ['id'])

        # Set original attributes
        self.__original_attributes = data
        # Remove ID
        if 'id' in self.__original_attributes:
            self.__original_attributes.pop('id')

        # Set all ACLs
        from pyaoscx.acl import ACL
        if hasattr(self, 'aclmac_in_cfg') and self.aclmac_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclmac_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclmac_in_cfg = acl

        if hasattr(self, 'aclv4_in_cfg') and self.aclv4_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv4_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv4_in_cfg = acl

        if hasattr(self, 'aclv6_in_cfg') and self.aclv6_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv6_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv6_in_cfg = acl

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True
        return True
コード例 #11
0
ファイル: interface.py プロジェクト: aruba/pyaoscx
    def get(self, depth=None, selector=None):
        '''
        Perform a GET call to retrieve data for a Port table entry, a Interface
        table entry and fill the object with the incoming attributes

        :param depth: Integer deciding how many levels into the API JSON that
            references will be returned.
        :param selector: Alphanumeric option to select specific information to
            return.  The options are 'configuration', 'status', or 'statistics.
        :return: Returns True if there is not an exception raised
        '''
        logging.info("Retrieving Port")

        depth = self.session.api_version.default_depth \
            if depth is None else depth
        selector = self.session.api_version.default_selector \
            if selector is None else selector

        if not self.session.api_version.valid_depth(depth):
            depths = self.session.api_version.valid_depths
            raise Exception("ERROR: Depth should be {}".format(depths))

        if selector not in self.session.api_version.valid_selectors:
            selectors = ' '.join(self.session.api_version.valid_selectors)
            raise Exception(
                "ERROR: Selector should be one of {}" .format(selectors))

        payload = {
            "depth": depth,
            "selector": selector
        }

        uri_ports = "{base_url}{class_uri}/{name}".format(
            base_url=self.session.base_url,
            class_uri=Interface.base_uri_ports,
            name=self.percents_name
        )

        # Bring Ports information
        try:
            response_ports = self.session.s.get(
                uri_ports, verify=False, params=payload,
                proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response_ports, "GET"):
            raise GenericOperationError(
                response_ports.text,
                response_ports.status_code)

        data_port = json.loads(response_ports.text)
        # Adding ACL attributes to data_port
        # to then be added as attributes to the object
        acl_names = [
            'aclv6_in_cfg',
            'aclv4_in_cfg',
            'aclmac_in_cfg',
            'aclv4_out_cfg']

        for acl_attr in acl_names:
            data_port[acl_attr] = None

        # Delete ip6 addresses from incoming data
        data_port.pop('ip6_addresses')
        # Add Port dictionary as attributes for the object
        utils.create_attrs(self, data_port)

        # Determines if the module is configurable
        if selector in self.session.api_version.configurable_selectors:
            # Get list of keys and create a list with the given keys
            utils.set_config_attrs(
                self, data_port, 'config_attrs',
                ['name', 'origin', 'other_config', 'ip6_addresses'])
        # Set original attributes
        self.__original_attributes_port = data_port
        # Delete unwanted attributes
        if 'name' in self.__original_attributes_port:
            self.__original_attributes_port.pop('name')
        if 'ip6_addresses' in self.__original_attributes_port:
            self.__original_attributes_port.pop('ip6_addresses')
        if 'origin' in self.__original_attributes_port:
            self.__original_attributes_port.pop('origin')
        if 'other_config' in self.__original_attributes_port:
            self.__original_attributes_port.pop('other_config')

        # Check if port is a LAG
        # If not, makes get request to system/interfaces
        if self.type != 'lag':
            uri_interfaces = "{base_url}{class_uri}/{name}".format(
                base_url=self.session.base_url,
                class_uri=Interface.base_uri_interface,
                name=self.percents_name
            )
            # Bring Interface information
            try:
                response_ints = self.session.s.get(
                    uri_interfaces, verify=False,
                    params=payload, proxies=self.session.proxy)

            except Exception as e:
                raise ResponseError('GET', e)

            if not utils._response_ok(response_ints, "GET"):
                raise GenericOperationError(response_ints.text,
                                            response_ints.status_code)

            data_int = json.loads(response_ints.text)
            # Add Interface dictionary as attributes for the object
            utils.create_attrs(self, data_int)

            # Determines if the module is configurable
            if selector in self.session.api_version.configurable_selectors:
                # Get list of keys and create a list with the given keys
                utils.set_config_attrs(
                    self, data_int, 'config_attrs_int', ['name', 'origin'])
            # Set original attributes
            self.__original_attributes_int = data_int
            # Delete unwanted attributes
            if 'name' in self.__original_attributes_int:
                self.__original_attributes_int.pop('name')
            if 'origin' in self.__original_attributes_int:
                self.__original_attributes_int.pop('origin')

        # Set a list of interfaces as an attribute
        if hasattr(self, 'interfaces') and self.interfaces is not None:
            interfaces_list = []
            # Get all URI elements in the form of a list
            uri_list = self.session.api_version.get_uri_from_data(
                self.interfaces)
            for uri in uri_list:
                # Create an Interface object
                name, interface = Interface.from_uri(self.session, uri)

                # Check for circular reference
                # No need to get() if it's circular; it is already
                # materialized. Just set flag
                if name == self.name:
                    interface.materialized = True
                else:
                    # Materialize interface
                    interface.get()

                # Add interface to list
                interfaces_list.append(interface)

            # Set list as Interfaces
            self.interfaces = interfaces_list
            # Set list of interfaces
            self.__prev_interfaces = list(self.interfaces)

        # Set VRF
        if hasattr(self, 'vrf') and self.vrf is not None:
            # Set keepalive VRF as a Vrf object
            vrf_obj = Vrf.from_response(self.session, self.vrf)
            self.vrf = vrf_obj
            # Materialized VRF
            self.vrf.get()

        # Set VLAN
        if hasattr(self, 'vlan_tag') and self.vlan_tag is not None:
            # Set VLAN as a Vlan Object
            vlan_obj = Vlan.from_response(self.session, self.vlan_tag)
            self.vlan_tag = vlan_obj
            # Materialized VLAN
            self.vlan_tag.get()

        # vlan_trunks
        # Set a list of vlans as an attribute
        if hasattr(self, 'vlan_trunks') and self.vlan_trunks is not None:
            vlan_trunks = []
            # Get all URI elements in the form of a list
            uri_list = self.session.api_version.get_uri_from_data(
                self.vlan_trunks)

            for uri in uri_list:
                # Create a Vlan object
                vlan_id, vlan = Vlan.from_uri(self.session, uri)
                # Materialize VLAN
                vlan.get()
                # Add VLAN to dictionary
                vlan_trunks.append(vlan)
            # Set list as VLANs
            self.vlan_trunks = vlan_trunks

        # Set all ACLs
        from pyaoscx.acl import ACL
        if hasattr(self, 'aclmac_in_cfg') and self.aclmac_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclmac_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclmac_in_cfg = acl

        if hasattr(self, 'aclmac_out_cfg') and self.aclmac_out_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclmac_out_cfg)
            # Materialize Acl object
            acl.get()
            self.aclmac_out_cfg = acl

        if hasattr(self, 'aclv4_in_cfg') and self.aclv4_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv4_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv4_in_cfg = acl

        if hasattr(self, 'aclv4_out_cfg') and self.aclv4_out_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv4_out_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv4_out_cfg = acl

        if hasattr(
                self,
                'aclv4_routed_in_cfg') and self.aclv4_routed_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv4_routed_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv4_routed_in_cfg = acl

        if hasattr(
                self,
                'aclv4_routed_out_cfg') and self.aclv4_routed_out_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv4_routed_out_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv4_routed_out_cfg = acl

        if hasattr(self, 'aclv6_in_cfg') and self.aclv6_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv6_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv6_in_cfg = acl

        if hasattr(self, 'aclv6_out_cfg') and self.aclv6_out_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv6_out_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv6_out_cfg = acl

        if hasattr(
                self,
                'aclv6_routed_in_cfg') and self.aclv6_routed_in_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv6_routed_in_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv6_routed_in_cfg = acl

        if hasattr(
                self,
                'aclv6_routed_out_cfg') and self.aclv6_routed_out_cfg is not None:
            # Create Acl object
            acl = ACL.from_response(self.session, self.aclv6_routed_out_cfg)
            # Materialize Acl object
            acl.get()
            self.aclv6_routed_out_cfg = acl

        # Set IPv6 addresses if any
        if self.ip6_addresses == []:
            # Set IPv6 addresses if any
            # Loads IPv6 already into the Interface
            Ipv6.get_all(self.session, self)

        # Sets object as materialized
        # Information is loaded from the Device
        self.materialized = True

        return True
コード例 #12
0
ファイル: configuration.py プロジェクト: aruba/pyaoscx
    def get(self):
        '''
        Perform a GET call to retrieve system attributes

        '''
        logging.info("Retrieving the switch attributes and capabilities")
        depth = self.session.api_version.default_depth
        uri = "{base_url}{class_uri}?depth={depth}".format(
            base_url=self.session.base_url,
            class_uri=Configuration.base_uri,
            depth=depth
        )

        try:
            response = self.session.s.get(
                uri, verify=False, proxies=self.session.proxy)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        data = json.loads(response.text)

        # Create class attributes using util.create_attrs
        utils.create_attrs(self, data)

        # Second GET request to obtain just the variables that are writable
        selector = self.session.api_version.default_selector
        payload = {
            "depth": depth,
            "selector": selector
        }
        uri = "{base_url}{class_uri}".format(
            base_url=self.session.base_url,
            class_uri=Configuration.base_uri,
            depth=self.session.api_version.default_depth
        )
        try:
            response = self.session.s.get(
                uri, verify=False,
                proxies=self.session.proxy,
                params=payload)

        except Exception as e:
            raise ResponseError('GET', e)

        if not utils._response_ok(response, "GET"):
            raise GenericOperationError(response.text, response.status_code)

        # Configurable data
        config_data = json.loads(response.text)

        # Set self.config_attrs and delete ID from it
        utils.set_config_attrs(self, config_data, 'config_attrs')

        # Set original attributes
        self.__original_attributes = config_data

        # Set device as materialized
        self.materialized = True