def convert_with_links(hardware_types, detail=False): """Convert drivers and hardware types to an API-serializable object. :param hardware_types: dict mapping hardware type names to conductor hostnames. :param detail: boolean, whether to include detailed info, such as the 'type' field and default/enabled interfaces fields. :returns: an API-serializable driver collection object. """ collection = DriverList() collection.drivers = [] # NOTE(jroll) we return hardware types in all API versions, # but restrict type/default/enabled fields to 1.30. # This is checked in Driver.convert_with_links(), however also # checking here can save us a DB query. if api_utils.allow_dynamic_drivers() and detail: iface_info = api.request.dbapi.list_hardware_type_interfaces( list(hardware_types)) else: iface_info = [] for htname in hardware_types: interface_info = [ i for i in iface_info if i['hardware_type'] == htname ] collection.drivers.append( Driver.convert_with_links(htname, list(hardware_types[htname]), detail=detail, interface_info=interface_info)) return collection
def convert_with_links(drivers, hardware_types, detail=False): """Convert drivers and hardware types to an API-serializable object. :param drivers: dict mapping driver names to conductor hostnames. :param hardware_types: dict mapping hardware type names to conductor hostnames. :param detail: boolean, whether to include detailed info, such as the 'type' field and default/enabled interfaces fields. :returns: an API-serializable driver collection object. """ collection = DriverList() collection.drivers = [ Driver.convert_with_links(dname, list(drivers[dname]), 'classic', detail=detail) for dname in drivers] # NOTE(jroll) we return hardware types in all API versions, # but restrict type/default/enabled fields to 1.30. # This is checked in Driver.convert_with_links(), however also # checking here can save us a DB query. if api_utils.allow_dynamic_drivers() and detail: iface_info = pecan.request.dbapi.list_hardware_type_interfaces( list(hardware_types)) else: iface_info = [] for htname in hardware_types: interface_info = [i for i in iface_info if i['hardware_type'] == htname] collection.drivers.append( Driver.convert_with_links(htname, list(hardware_types[htname]), 'dynamic', detail=detail, interface_info=interface_info)) return collection
def convert_with_links(name, hosts, detail=False, interface_info=None): """Convert driver/hardware type info to an API-serializable object. :param name: name of a hardware type. :param hosts: list of conductor hostnames driver is active on. :param detail: boolean, whether to include detailed info, such as the 'type' field and default/enabled interfaces fields. :param interface_info: optional list of dicts of hardware interface info. :returns: API-serializable driver object. """ driver = Driver() driver.name = name driver.hosts = hosts driver.links = [ link.Link.make_link('self', api.request.public_url, 'drivers', name), link.Link.make_link('bookmark', api.request.public_url, 'drivers', name, bookmark=True) ] if api_utils.allow_links_node_states_and_driver_properties(): driver.properties = [ link.Link.make_link('self', api.request.public_url, 'drivers', name + "/properties"), link.Link.make_link('bookmark', api.request.public_url, 'drivers', name + "/properties", bookmark=True) ] if api_utils.allow_dynamic_drivers(): # NOTE(dtantsur): only dynamic drivers (based on hardware types) # are supported starting with the Rocky release. driver.type = 'dynamic' if detail: if interface_info is None: # TODO(jroll) objectify this interface_info = ( api.request.dbapi.list_hardware_type_interfaces([name ])) for iface_type in driver_base.ALL_INTERFACES: default = None enabled = set() for iface in interface_info: if iface['interface_type'] == iface_type: iface_name = iface['interface_name'] enabled.add(iface_name) # NOTE(jroll) this assumes the default is the same # on all conductors if iface['default']: default = iface_name default_key = 'default_%s_interface' % iface_type enabled_key = 'enabled_%s_interfaces' % iface_type setattr(driver, default_key, default) setattr(driver, enabled_key, list(enabled)) hide_fields_in_newer_versions(driver) return driver
def test_allow_dynamic_drivers(self, mock_request): mock_request.version.minor = 30 self.assertTrue(utils.allow_dynamic_drivers()) mock_request.version.minor = 29 self.assertFalse(utils.allow_dynamic_drivers())
def convert_with_links(name, hosts, driver_type, detail=False, interface_info=None): """Convert driver/hardware type info to an API-serializable object. :param name: name of driver or hardware type. :param hosts: list of conductor hostnames driver is active on. :param driver_type: 'classic' for classic drivers, 'dynamic' for hardware types. :param detail: boolean, whether to include detailed info, such as the 'type' field and default/enabled interfaces fields. :param interface_info: optional list of dicts of hardware interface info. :returns: API-serializable driver object. """ driver = Driver() driver.name = name driver.hosts = hosts driver.links = [ link.Link.make_link('self', pecan.request.public_url, 'drivers', name), link.Link.make_link('bookmark', pecan.request.public_url, 'drivers', name, bookmark=True) ] if api_utils.allow_links_node_states_and_driver_properties(): driver.properties = [ link.Link.make_link('self', pecan.request.public_url, 'drivers', name + "/properties"), link.Link.make_link('bookmark', pecan.request.public_url, 'drivers', name + "/properties", bookmark=True) ] if api_utils.allow_dynamic_drivers(): driver.type = driver_type if driver_type == 'dynamic' and detail: if interface_info is None: # TODO(jroll) objectify this interface_info = (pecan.request.dbapi .list_hardware_type_interfaces([name])) for iface_type in driver_base.ALL_INTERFACES: default = None enabled = set() for iface in interface_info: if iface['interface_type'] == iface_type: iface_name = iface['interface_name'] enabled.add(iface_name) # NOTE(jroll) this assumes the default is the same # on all conductors if iface['default']: default = iface_name default_key = 'default_%s_interface' % iface_type enabled_key = 'enabled_%s_interfaces' % iface_type setattr(driver, default_key, default) setattr(driver, enabled_key, list(enabled)) elif detail: for iface_type in driver_base.ALL_INTERFACES: # always return None for classic drivers setattr(driver, 'default_%s_interface' % iface_type, None) setattr(driver, 'enabled_%s_interfaces' % iface_type, None) hide_fields_in_newer_versions(driver) return driver