def get_device(config_id, device_name): """Get device from BAM.""" device = None response = get_api()._api_client.service.getEntityByName(config_id, device_name, "Device") if has_response(response): device = get_api().instantiate_entity(response) return device
def get_ip4_networks_data(hint): """ Get a list of networks that corresponds to the configuration and hint entered by the user :param hint: Network hint to search with :return: networks that correspond to the network hint """ # Use the default result template result = get_result_template() result['data']['autocomplete_field'] = [] # Retrieve the configuration try: configuration_id = request.form['configuration'] except PortalException as e: result['status'] = FAIL g.user.logger.warning('%s' % e, msg_type=g.user.logger.EXCEPTION) return result # Retrieve network based on user input try: networks = g.user.get_api()._api_client.service.getIP4NetworksByHint(configuration_id, 0, 5, 'hint=%s' % hint) except BAMException as e: result['status'] = FAIL g.user.logger.warning('%s' % e, msg_type=g.user.logger.EXCEPTION) result['message'] = 'Unable to retrieve network with error %s: ' % safe_str(e) return result if has_response(networks): for network in networks.item: network = g.user.get_api().instantiate_entity(network) network_string = network.get_property('CIDR') result['data']['autocomplete_field'].append({"value": network_string}) result['status'] = SUCCESS return result
def get_or_create_device_type(parent_id, name, device_type): """Gets device type or creates device type if it doesn't exist.""" result = get_api()._api_client.service.getEntityByName(parent_id, name, device_type) if has_response(result): device_type_object = get_api().instantiate_entity(result) elif parent_id == 0: device_type_object = get_api()._api_client.service.addDeviceType(name, '') device_type_object = get_api().get_entity_by_id(device_type_object) else: device_type_object = get_api()._api_client.service.addDeviceSubtype(parent_id, name, '') device_type_object = get_api().get_entity_by_id(device_type_object) return device_type_object
def get_zones_data(hint): """ Get a list of zone FQDNs that corresponds to the given hint. :return: FQDN of any zones that match the given hint as data in JSON, using result template format. """ # Declare variables result = get_result_template() result['data']['autocomplete_field'] = [] # Retrieve the configuration and view object try: configuration = g.user.get_api().get_configuration( config.default_configuration) view = configuration.get_view(config.default_view) except PortalException as e: result['status'] = FAIL g.user.logger.warning('%s' % e, msg_type=g.user.logger.EXCEPTION) return result # If null user input then set hint to empty to pre-populate the dropdown, otherwise search with user input if safe_str(hint) == 'null': properties = 'hint=|' else: properties = 'hint=' + hint # Retrieve zones zones = g.user.get_api()._api_client.service.getZonesByHint( view.get_id(), 0, 5, properties) # If valid zones are retrieved then extract the absolute name and append to result if has_response(zones): for zone in zones.item: result['data']['autocomplete_field'].append( properties_to_map(zone['properties'])['absoluteName']) result['status'] = SUCCESS return result