def test_plugin_prefix_with_parent_resource(self): controller = self.DummySvcPlugin() parent = dict(member_name="tenant", collection_name="tenants") member = {'custom_member_action': "GET"} collections = {'collection_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, parent, path_prefix="/dummy_svc", member_actions=member, collection_actions=collections) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) index_response = test_app.get("/dummy_svc/tenants/1/tweedles") self.assertEqual(200, index_response.status_int) response = test_app.get("/dummy_svc/tenants/1/" "tweedles/1/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['member_action'], "value") response = test_app.get("/dummy_svc/tenants/2/" "tweedles/collection_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")
def test_plugin_prefix_with_parent_resource(self): controller = self.DummySvcPlugin() parent = dict(member_name="tenant", collection_name="tenants") member = {'custom_member_action': "GET"} collections = {'collection_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, parent, path_prefix="/dummy_svc", member_actions=member, collection_actions=collections) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) index_response = test_app.get("/dummy_svc/tenants/1/tweedles") self.assertEqual(200, index_response.status_int) response = test_app.get("/dummy_svc/tenants/1/" "tweedles/1/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual( jsonutils.loads(response.body)['member_action'], "value") response = test_app.get("/dummy_svc/tenants/2/" "tweedles/collection_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")
def _get_handler(resource): if resource == GET_200[2]: data = json.loads('[{"name":"a"},{"name":"b"}]') return 200, '', '', data if resource in GET_200: return 200, '', '', '' else: data = json.loads('{"complete":"True", "success": "True"}') return 202, '', '', data
def test_resource_extension_for_get_custom_collection_action(self): controller = self.ResourceExtensionController() collections = {'custom_collection_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, collection_actions=collections) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) LOG.debug(jsonutils.loads(response.body)) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")
def _post_handler(resource, binary): if re.search(r'/api/workflow/.+/action/.+', resource): data = json.loads('{"uri":"some_uri"}') return 202, '', '', data elif re.search(r'/api/service\?name=.+', resource): data = json.loads('{"links":{"actions":{"provision":"someuri"}}}') return 201, '', '', data elif binary: return 201, '', '', '' else: return 202, '', '', ''
def _post_handler(resource, binary): if re.search(r'/api/workflow/.+/action/.+', resource): data = jsonutils.loads('{"uri":"some_uri"}') return 202, '', '', data elif re.search(r'/api/service\?name=.+', resource): data = jsonutils.loads('{"links":{"actions":{"provision":"someuri"}}}') return 201, '', '', data elif binary: return 201, '', '', '' else: return 202, '', '', ''
def _fetch_template_and_params(self, context, sc_instance, sc_spec, sc_node): stack_template = sc_node.get('config') # TODO(magesh):Raise an exception ?? if not stack_template: LOG.error(_("Service Config is not defined for the service" " chain Node")) return stack_template = jsonutils.loads(stack_template) config_param_values = sc_instance.get('config_param_values', {}) stack_params = {} # config_param_values has the parameters for all Nodes. Only apply # the ones relevant for this Node if config_param_values: config_param_values = jsonutils.loads(config_param_values) config_param_names = sc_spec.get('config_param_names', []) if config_param_names: config_param_names = ast.literal_eval(config_param_names) # This service chain driver knows how to fill in two parameter values # for the template at present. # 1)Subnet -> Provider PTG subnet is used # 2)PoolMemberIPs -> List of IP Addresses of all PTs in Provider PTG # TODO(magesh):Process on the basis of ResourceType rather than Name # eg: Type: OS::Neutron::PoolMember # Variable number of pool members is not handled yet. We may have to # dynamically modify the template json to achieve that member_ips = [] provider_ptg_id = sc_instance.get("provider_ptg_id") # If we have the key "PoolMemberIP*" in template input parameters, # fetch the list of IPs of all PTs in the PTG for key in config_param_names or []: if "PoolMemberIP" in key: member_ips = self._get_member_ips(context, provider_ptg_id) break member_count = 0 for key in config_param_names or []: if "PoolMemberIP" in key: value = (member_ips[member_count] if len(member_ips) > member_count else '0') member_count = member_count + 1 config_param_values[key] = value elif key == "Subnet": value = self._get_ptg_subnet(context, provider_ptg_id) config_param_values[key] = value node_params = (stack_template.get('Parameters') or stack_template.get('parameters')) if node_params: for parameter in config_param_values.keys(): if parameter in node_params.keys(): stack_params[parameter] = config_param_values[parameter] return (stack_template, stack_params)
def _get_handler(resource): if resource == GET_200[2]: if rest_call_function_mock.TEMPLATES_MISSING: data = json.loads("[]") else: data = json.loads('[{"name":"openstack_l2_l3"},{"name":"openstack_l4"}]') return 200, "", "", data if resource in GET_200: return 200, "", "", "" else: data = json.loads('{"complete":"True", "success": "True"}') return 202, "", "", data
def _get_handler(resource): if resource == GET_200[2]: if rest_call_function_mock.TEMPLATES_MISSING: data = json.loads('[]') else: data = json.loads( '[{"name":"openstack_l2_l3"},{"name":"openstack_l4"}]') return 200, '', '', data if resource in GET_200: return 200, '', '', '' else: data = json.loads('{"complete":"True", "success": "True"}') return 202, '', '', data
def test_sync_multi_chunk(self): # The fake NSX API client cannot be used for this test ctx = context.get_admin_context() # Generate 4 networks, 1 port per network, and 4 routers with self._populate_data(ctx, net_size=4, port_size=1, router_size=4): fake_lswitches = json.loads( self.fc.handle_get('/ws.v1/lswitch'))['results'] fake_lrouters = json.loads( self.fc.handle_get('/ws.v1/lrouter'))['results'] fake_lswitchports = json.loads( self.fc.handle_get('/ws.v1/lswitch/*/lport'))['results'] return_values = [ # Chunk 0 - lswitches (fake_lswitches, None, 4), # Chunk 0 - lrouters (fake_lrouters[:2], 'xxx', 4), # Chunk 0 - lports (size only) ([], 'start', 4), # Chunk 1 - lrouters (2 more) (lswitches are skipped) (fake_lrouters[2:], None, None), # Chunk 1 - lports (fake_lswitchports, None, 4) ] def fake_fetch_data(*args, **kwargs): return return_values.pop(0) # 2 Chunks, with 6 resources each. # 1st chunk lswitches and lrouters # 2nd chunk lrouters and lports # Mock _fetch_data with mock.patch.object(self._plugin._synchronizer, '_fetch_data', side_effect=fake_fetch_data): sp = sync.SyncParameters(6) def do_chunk(chunk_idx, ls_cursor, lr_cursor, lp_cursor): self._plugin._synchronizer._synchronize_state(sp) self.assertEqual(chunk_idx, sp.current_chunk) self.assertEqual(ls_cursor, sp.ls_cursor) self.assertEqual(lr_cursor, sp.lr_cursor) self.assertEqual(lp_cursor, sp.lp_cursor) # check 1st chunk do_chunk(1, None, 'xxx', 'start') # check 2nd chunk do_chunk(0, None, None, None) # Chunk size should have stayed the same self.assertEqual(sp.chunk_size, 6)
def test_sync_multi_chunk(self): # The fake NVP API client cannot be used for this test ctx = context.get_admin_context() # Generate 4 networks, 1 port per network, and 4 routers with self._populate_data(ctx, net_size=4, port_size=1, router_size=4): fake_lswitches = json.loads( self.fc.handle_get('/ws.v1/lswitch'))['results'] fake_lrouters = json.loads( self.fc.handle_get('/ws.v1/lrouter'))['results'] fake_lswitchports = json.loads( self.fc.handle_get('/ws.v1/lswitch/*/lport'))['results'] return_values = [ # Chunk 0 - lswitches (fake_lswitches, None, 4), # Chunk 0 - lrouters (fake_lrouters[:2], 'xxx', 4), # Chunk 0 - lports (size only) ([], 'start', 4), # Chunk 1 - lrouters (2 more) (lswitches are skipped) (fake_lrouters[2:], None, None), # Chunk 1 - lports (fake_lswitchports, None, 4)] def fake_fetch_data(*args, **kwargs): return return_values.pop(0) # 2 Chunks, with 6 resources each. # 1st chunk lswitches and lrouters # 2nd chunk lrouters and lports # Mock _fetch_data with mock.patch.object( self._plugin._synchronizer, '_fetch_data', side_effect=fake_fetch_data): sp = sync.SyncParameters(6) def do_chunk(chunk_idx, ls_cursor, lr_cursor, lp_cursor): self._plugin._synchronizer._synchronize_state(sp) self.assertEqual(chunk_idx, sp.current_chunk) self.assertEqual(ls_cursor, sp.ls_cursor) self.assertEqual(lr_cursor, sp.lr_cursor) self.assertEqual(lp_cursor, sp.lp_cursor) # check 1st chunk do_chunk(1, None, 'xxx', 'start') # check 2nd chunk do_chunk(0, None, None, None) # Chunk size should have stayed the same self.assertEqual(sp.chunk_size, 6)
def _goose_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:googoose'] = req.GET.get('chewing') res.body = jsonutils.dumps(data) return res
def get_vif_port_set(self): port_names = self.get_port_name_list() edge_ports = set() args = ["--format=json", "--", "--columns=name,external_ids,ofport", "list", "Interface"] result = self.run_vsctl(args, check_error=True) if not result: return edge_ports for row in jsonutils.loads(result)["data"]: name = row[0] if name not in port_names: continue external_ids = dict(row[1][1]) # Do not consider VIFs which aren't yet ready # This can happen when ofport values are either [] or ["set", []] # We will therefore consider only integer values for ofport ofport = row[2] try: int_ofport = int(ofport) except (ValueError, TypeError): LOG.warn(_("Found not yet ready openvswitch port: %s"), row) else: if int_ofport > 0: if "iface-id" in external_ids and "attached-mac" in external_ids: edge_ports.add(external_ids["iface-id"]) elif "xs-vif-uuid" in external_ids and "attached-mac" in external_ids: # if this is a xenserver and iface-id is not # automatically synced to OVS from XAPI, we grab it # from XAPI directly iface_id = self.get_xapi_iface_id(external_ids["xs-vif-uuid"]) edge_ports.add(iface_id) else: LOG.warn(_("Found failed openvswitch port: %s"), row) return edge_ports
def _bands_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:big_bands'] = 'Pig Bands!' res.body = jsonutils.dumps(data) return res
def _handler(self, client_sock, client_addr): """Handle incoming lease relay stream connection. This method will only read the first 1024 bytes and then close the connection. The limit exists to limit the impact of misbehaving clients. """ try: msg = client_sock.recv(1024) data = jsonutils.loads(msg) client_sock.close() network_id = data['network_id'] if not uuidutils.is_uuid_like(network_id): raise ValueError( _("Network ID %s is not a valid UUID") % network_id) ip_address = str(netaddr.IPAddress(data['ip_address'])) lease_remaining = int(data['lease_remaining']) self.callback(network_id, ip_address, lease_remaining) except ValueError as e: LOG.warn(_('Unable to parse lease relay msg to dict.')) LOG.warn(_('Exception value: %s'), e) LOG.warn(_('Message representation: %s'), repr(msg)) except Exception as e: LOG.exception(_('Unable update lease. Exception'))
def _show(self, resource_type, response_file, uuid1, uuid2=None, relations=None): target_uuid = uuid2 or uuid1 if resource_type.endswith('attachment'): resource_type = resource_type[:resource_type.index('attachment')] with open("%s/%s" % (self.fake_files_path, response_file)) as f: response_template = f.read() res_dict = getattr(self, '_fake_%s_dict' % resource_type) for item in res_dict.itervalues(): if 'tags' in item: item['tags_json'] = jsonutils.dumps(item['tags']) # replace sec prof rules with their json dump def jsonify_rules(rule_key): if rule_key in item: rules_json = jsonutils.dumps(item[rule_key]) item['%s_json' % rule_key] = rules_json jsonify_rules('logical_port_egress_rules') jsonify_rules('logical_port_ingress_rules') items = [jsonutils.loads(response_template % res_dict[res_uuid]) for res_uuid in res_dict if res_uuid == target_uuid] if items: return jsonutils.dumps(items[0]) raise api_exc.ResourceNotFound()
def _build_item(resource): item = jsonutils.loads(response_template % resource) if relations: for relation in relations: self._build_relation(resource, item, resource_type, relation) return item
def _add_lswitch_lport(self, body, ls_uuid): fake_lport = jsonutils.loads(body) new_uuid = uuidutils.generate_uuid() fake_lport['uuid'] = new_uuid # put the tenant_id and the ls_uuid in the main dict # for simplyfying templating fake_lport['ls_uuid'] = ls_uuid fake_lport['tenant_id'] = self._get_tag(fake_lport, 'os_tid') fake_lport['neutron_port_id'] = self._get_tag(fake_lport, 'q_port_id') fake_lport['neutron_device_id'] = self._get_tag(fake_lport, 'vm_id') fake_lport['att_type'] = "NoAttachment" fake_lport['att_info_json'] = '' self._fake_lswitch_lport_dict[fake_lport['uuid']] = fake_lport fake_lswitch = self._fake_lswitch_dict[ls_uuid] fake_lswitch['lport_count'] += 1 fake_lport_status = fake_lport.copy() fake_lport_status['ls_tenant_id'] = fake_lswitch['tenant_id'] fake_lport_status['ls_uuid'] = fake_lswitch['uuid'] fake_lport_status['ls_name'] = fake_lswitch['display_name'] fake_lport_status['ls_zone_uuid'] = fake_lswitch['zone_uuid'] # set status value fake_lport['status'] = 'true' self._fake_lswitch_lportstatus_dict[new_uuid] = fake_lport_status return fake_lport
def get_qos_by_port(self, port_id): args = ['--format=json', '--', 'find', 'qos', 'external_ids:port-id="%s"' % port_id] result = self.run_vsctl(args) if not result: return json_result = jsonutils.loads(result) try: # Retrieve the indexes of the columns we're looking for headings = json_result['headings'] qos_idx = headings.index('_uuid') ext_ids_idx = headings.index('external_ids') other_idx = headings.index('other_config') queues_idx = headings.index('queues') type_idx = headings.index('type') # If data attribute is missing or empty the line below will raise # an exeception which will be captured in this block. # We won't deal with the possibility of ovs-vsctl return multiple # rows since the interface identifier is unique data = json_result['data'][0] qos_id = data[qos_idx][1] ext_id_dict = dict((item[0], item[1]) for item in data[ext_ids_idx][1]) port_id = ext_id_dict['port-id'] other_dict = dict((item[0], item[1]) for item in data[other_idx][1]) queues_dict = dict((item[0], item[1]) for item in data[queues_idx][1]) type = data[type_idx] return PortQos(port_id, qos_id, other_dict, queues_dict, type) except Exception as e: LOG.warn(_("Unable to parse qos details. Exception: %s"), e) return
def _get_keystone_token_v2(self): kcfg = cfg.CONF.keystone_authtoken auth_body = { "auth": { "passwordCredentials": { "username": kcfg.admin_user, "password": kcfg.admin_password, "tenantName": kcfg.admin_tenant_name } } } keystone_url = "%s://%s:%s%s" % ( cfg.CONF.keystone_authtoken.auth_protocol, cfg.CONF.keystone_authtoken.auth_host, cfg.CONF.keystone_authtoken.auth_port, "/v2.0/tokens" ) response = self._query_keystone_server(keystone_url, auth_body) if response.status_code == requests.codes.ok: authn_content = json.loads(response.text) self._authn_token = authn_content['access']['token']['id'] return response else: raise RuntimeError('Authentication Failure')
def get_vif_port_by_id(self, port_id): args = ['--format=json', '--', '--columns=external_ids,name,ofport', 'find', 'Interface', 'external_ids:iface-id="%s"' % port_id] result = self.run_vsctl(args) if not result: return json_result = jsonutils.loads(result) try: # Retrieve the indexes of the columns we're looking for headings = json_result['headings'] ext_ids_idx = headings.index('external_ids') name_idx = headings.index('name') ofport_idx = headings.index('ofport') # If data attribute is missing or empty the line below will raise # an exeception which will be captured in this block. # We won't deal with the possibility of ovs-vsctl return multiple # rows since the interface identifier is unique data = json_result['data'][0] port_name = data[name_idx] ofport = data[ofport_idx] # ofport must be integer otherwise return None if not isinstance(ofport, int) or ofport == -1: LOG.warn(_("ofport: %(ofport)s for VIF: %(vif)s is not a" "positive integer"), {'ofport': ofport, 'vif': port_id}) return # Find VIF's mac address in external ids ext_id_dict = dict((item[0], item[1]) for item in data[ext_ids_idx][1]) vif_mac = ext_id_dict['attached-mac'] return VifPort(port_name, ofport, port_id, vif_mac, self) except Exception as e: LOG.warn(_("Unable to parse interface details. Exception: %s"), e) return
def get_token(self): authn_token = None if self.ks_sess: authn_token = self.ks_sess.get_token() else: if self._ksinsecure: response = requests.post( self._keystone_url, data=self._authn_body, headers={'Content-type': 'application/json'}, verify=False) elif not self._ksinsecure and self._use_ks_certs: response = requests.post( self._keystone_url, data=self._authn_body, headers={'Content-type': 'application/json'}, verify=self._kscertbundle) else: response = requests.post( self._keystone_url, data=self._authn_body, headers={'Content-type': 'application/json'}) if (response.status_code == requests.codes.ok): authn_content = json.loads(response.text) authn_token = authn_content['access']['token']['id'] return authn_token
def _add_lswitch_lport(self, body, ls_uuid): fake_lport = json.loads(body) new_uuid = uuidutils.generate_uuid() fake_lport['uuid'] = new_uuid # put the tenant_id and the ls_uuid in the main dict # for simplyfying templating fake_lport['ls_uuid'] = ls_uuid fake_lport['tenant_id'] = self._get_tag(fake_lport, 'os_tid') fake_lport['neutron_port_id'] = self._get_tag(fake_lport, 'q_port_id') fake_lport['neutron_device_id'] = self._get_tag(fake_lport, 'vm_id') fake_lport['att_type'] = "NoAttachment" fake_lport['att_info_json'] = '' self._fake_lswitch_lport_dict[fake_lport['uuid']] = fake_lport fake_lswitch = self._fake_lswitch_dict[ls_uuid] fake_lswitch['lport_count'] += 1 fake_lport_status = fake_lport.copy() fake_lport_status['ls_tenant_id'] = fake_lswitch['tenant_id'] fake_lport_status['ls_uuid'] = fake_lswitch['uuid'] fake_lport_status['ls_name'] = fake_lswitch['display_name'] fake_lport_status['ls_zone_uuid'] = fake_lswitch['zone_uuid'] # set status value fake_lport['status'] = 'true' self._fake_lswitch_lportstatus_dict[new_uuid] = fake_lport_status return fake_lport
def _build_item(resource): item = json.loads(response_template % resource) if relations: for relation in relations: self._build_relation(resource, item, resource_type, relation) return item
def get_port_tag_dict(self): """Get a dict of port names and associated vlan tags. e.g. the returned dict is of the following form:: {u'int-br-eth2': [], u'patch-tun': [], u'qr-76d9e6b6-21': 1, u'tapce5318ff-78': 1, u'tape1400310-e6': 1} The TAG ID is only available in the "Port" table and is not available in the "Interface" table queried by the get_vif_port_set() method. """ port_names = self.get_port_name_list() #['ha-0e10bc14-99', 'int-br-ex', 'patch-tun', 'qr-77bec540-b3', 'qr-8f9a5cdd-3d', 'qvoee622161-cf', 'tap6f3f6216-ef', 'tap9bf56d57-97'] args = ['--format=json', '--', '--columns=name,tag', 'list', 'Port'] result = self.run_vsctl(args, check_error=True) #'{"data":[["qr-8f9a5cdd-3d",1],["ha-0e10bc14-99",3],["br-ex",["set",[]]],["tap6f3f6216-ef",2],["patch-tun",["set",[]]],["phy-br-ex",["set",[]]],["tap9bf56d57-97",1],["patch-int",["set",[]]],["int-br-ex",["set",[]]],["qr-77bec540-b3",2],["br-tun",["set",[]]],["br-int",["set",[]]],["qvoee622161-cf",2]],"headings":["name","tag"]}\n' port_tag_dict = {} if not result: return port_tag_dict for name, tag in jsonutils.loads(result)['data']: if name not in port_names: continue # 'tag' can be [u'set', []] or an integer if isinstance(tag, list): tag = tag[1] port_tag_dict[name] = tag return port_tag_dict
def _handler(self, client_sock, client_addr): """Handle incoming lease relay stream connection. This method will only read the first 1024 bytes and then close the connection. The limit exists to limit the impact of misbehaving clients. """ try: msg = client_sock.recv(1024) data = jsonutils.loads(msg) client_sock.close() network_id = data['network_id'] if not uuidutils.is_uuid_like(network_id): raise ValueError(_("Network ID %s is not a valid UUID") % network_id) ip_address = str(netaddr.IPAddress(data['ip_address'])) lease_remaining = int(data['lease_remaining']) self.callback(network_id, ip_address, lease_remaining) except ValueError as e: LOG.warn(_('Unable to parse lease relay msg to dict.')) LOG.warn(_('Exception value: %s'), e) LOG.warn(_('Message representation: %s'), repr(msg)) except Exception as e: LOG.exception(_('Unable update lease. Exception'))
def _show(self, resource_type, response_file, uuid1, uuid2=None, relations=None): target_uuid = uuid2 or uuid1 if resource_type.endswith('attachment'): resource_type = resource_type[:resource_type.index('attachment')] with open("%s/%s" % (self.fake_files_path, response_file)) as f: response_template = f.read() res_dict = getattr(self, '_fake_%s_dict' % resource_type) for item in res_dict.itervalues(): if 'tags' in item: item['tags_json'] = json.dumps(item['tags']) # replace sec prof rules with their json dump def jsonify_rules(rule_key): if rule_key in item: rules_json = json.dumps(item[rule_key]) item['%s_json' % rule_key] = rules_json jsonify_rules('logical_port_egress_rules') jsonify_rules('logical_port_ingress_rules') items = [ json.loads(response_template % res_dict[res_uuid]) for res_uuid in res_dict if res_uuid == target_uuid ] if items: return json.dumps(items[0]) raise api_exc.ResourceNotFound()
def _request_api_server(self, url, data=None, headers=None): # Attempt to post to Api-Server if self._apiinsecure: response = requests.post(url, data=data, headers=headers,verify=False) elif not self._apiinsecure and self._use_api_certs: response = requests.post(url, data=data, headers=headers,verify=self._apicertbundle) else: response = requests.post(url, data=data, headers=headers) if (response.status_code == requests.codes.unauthorized): # Get token from keystone and save it for next request if self._ksinsecure: response = requests.post(self._keystone_url, data=self._authn_body, headers={'Content-type': 'application/json'},verify=False) elif not self._ksinsecure and self._use_ks_certs: response = requests.post(self._keystone_url, data=self._authn_body, headers={'Content-type': 'application/json'},verify=self._kscertbundle) else: response = requests.post(self._keystone_url, data=self._authn_body, headers={'Content-type': 'application/json'}) if (response.status_code == requests.codes.ok): # plan is to re-issue original request with new token auth_headers = headers or {} authn_content = json.loads(response.text) self._authn_token = authn_content['access']['token']['id'] auth_headers['X-AUTH-TOKEN'] = self._authn_token response = self._request_api_server(url, data, auth_headers) else: raise RuntimeError('Authentication Failure') return response
def api_providers(self): """Parse api_providers from response. Returns: api_providers in [(host, port, is_ssl), ...] format """ def _provider_from_listen_addr(addr): # (pssl|ptcp):<ip>:<port> => (host, port, is_ssl) parts = addr.split(':') return (parts[1], int(parts[2]), parts[0] == 'pssl') try: if self.successful(): ret = [] body = json.loads(self.value.body) for node in body.get('results', []): for role in node.get('roles', []): if role.get('role') == 'api_provider': addr = role.get('listen_addr') if addr: ret.append(_provider_from_listen_addr(addr)) return ret except Exception as e: LOG.warn(_("[%(rid)d] Failed to parse API provider: %(e)s"), {'rid': self._rid(), 'e': e}) # intentionally fall through return None
def test_resource_extension_with_custom_member_action_and_attr_map(self): controller = self.ResourceExtensionController() member = {'custom_member_action': "GET"} params = { 'tweedles': { 'id': { 'allow_post': False, 'allow_put': False, 'validate': { 'type:uuid': None }, 'is_visible': True }, 'name': { 'allow_post': True, 'allow_put': True, 'validate': { 'type:string': None }, 'default': '', 'is_visible': True }, } } res_ext = extensions.ResourceExtension('tweedles', controller, member_actions=member, attr_map=params) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/tweedles/some_id/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual( jsonutils.loads(response.body)['member_action'], "value")
def get_port_tag_dict(self): """Get a dict of port names and associated vlan tags. e.g. the returned dict is of the following form:: {u'int-ebr-p798de6': [], u'qve-76d9e6b6-21': 1, u'tapce5318ff-78': 1, u'tape1400310-e6': 1} The TAG ID is only available in the "Port" table and is not available in the "Interface" table queried by the get_vif_port_set() method. """ port_names = self.get_port_name_list() args = ['--format=json', '--', '--columns=name,tag', 'list', 'Port'] result = self.run_vsctl(args, check_error=True) port_tag_dict = {} if not result: return port_tag_dict for name, tag in jsonutils.loads(result)['data']: if name not in port_names: continue # 'tag' can be [u'set', []] or an integer if isinstance(tag, list): tag = tag[1] port_tag_dict[name] = tag return port_tag_dict
def api_providers(self): """Parse api_providers from response. Returns: api_providers in [(host, port, is_ssl), ...] format """ def _provider_from_listen_addr(addr): # (pssl|ptcp):<ip>:<port> => (host, port, is_ssl) parts = addr.split(':') return (parts[1], int(parts[2]), parts[0] == 'pssl') try: if self.successful(): ret = [] body = jsonutils.loads(self.value.body) for node in body.get('results', []): for role in node.get('roles', []): if role.get('role') == 'api_provider': addr = role.get('listen_addr') if addr: ret.append(_provider_from_listen_addr(addr)) return ret except Exception as e: LOG.warn(_("[%(rid)d] Failed to parse API provider: %(e)s"), { 'rid': self._rid(), 'e': e }) # intentionally fall through return None
def get_port_tag_dict(self): """Get a dict of port names and associated vlan tags. e.g. the returned dict is of the following form:: {u'int-br-eth2': [], u'patch-tun': [], u'qr-76d9e6b6-21': 1, u'tapce5318ff-78': 1, u'tape1400310-e6': 1} The TAG ID is only available in the "Port" table and is not available in the "Interface" table queried by the get_vif_port_set() method. """ port_names = self.get_port_name_list() args = ['--format=json', '--', '--columns=name,tag', 'list', 'Port'] result = self.run_vsctl(args, check_error=True) port_tag_dict = {} if not result: return port_tag_dict for name, tag in jsonutils.loads(result)['data']: if name not in port_names: continue # 'tag' can be [u'set', []] or an integer if isinstance(tag, list): tag = tag[1] port_tag_dict[name] = tag return port_tag_dict
def app(environ, start_response): uri = environ['PATH_INFO'] method = environ['REQUEST_METHOD'] headers = [('Content-type', 'text/json')] content_len_str = environ['CONTENT_LENGTH'] content_len = 0 request_data = None if content_len_str: content_len = int(content_len_str) request_data = environ.get('wsgi.input').read(content_len) if request_data: try: request_data = jsonutils.loads(request_data) except Exception: # OK for it not to be json! Ignore it pass if self.debug: print('\n') if self.debug_env: print('environ:') for (key, value) in sorted(environ.iteritems()): print(' %16s : %s' % (key, value)) print('%s %s' % (method, uri)) if request_data: print('%s' % jsonutils.dumps( request_data, sort_keys=True, indent=4)) status, body = self.request_handler(method, uri, None) body_data = None if body: try: body_data = jsonutils.loads(body) except Exception: # OK for it not to be json! Ignore it pass start_response(status, headers) if self.debug: if self.debug_env: print('%s: %s' % ('Response', jsonutils.dumps( body_data, sort_keys=True, indent=4))) return body
def app(environ, start_response): uri = environ['PATH_INFO'] method = environ['REQUEST_METHOD'] headers = [('Content-type', 'text/json')] content_len_str = environ['CONTENT_LENGTH'] content_len = 0 request_data = None if content_len_str: content_len = int(content_len_str) request_data = environ.get('wsgi.input').read(content_len) if request_data: try: request_data = jsonutils.loads(request_data) except Exception: # OK for it not to be json! Ignore it pass if self.debug: print('\n') if self.debug_env: print('environ:') for (key, value) in sorted(environ.iteritems()): print(' %16s : %s' % (key, value)) print('%s %s' % (method, uri)) if request_data: print('%s' % jsonutils.dumps( request_data, sort_keys=True, indent=4)) status, body = self.request_handler(method, uri, None) body_data = None if body: try: body_data = jsonutils.loads(body) except Exception: # OK for it not to be json! Ignore it pass start_response(status, headers) if self.debug: if self.debug_env: print( '%s: %s' % ('Response', jsonutils.dumps(body_data, sort_keys=True, indent=4))) return body
def test_get_resources(self): app = _setup_extensions_test_app() response = app.get("/dummy_resources/1?chewing=newblue") response_data = jsonutils.loads(response.body) self.assertEqual('newblue', response_data['FOXNSOX:googoose']) self.assertEqual("Pig Bands!", response_data['FOXNSOX:big_bands'])
def _from_json(self, datastring): try: return jsonutils.loads( datastring ) #/usr/lib/python2.7/site-packages/neutron/openstack/common/jsonutils.py(171)loads() except ValueError: msg = _("Cannot understand JSON") raise exception.MalformedRequestBody(reason=msg)
def get_configuration_dict(self, agent_db): try: conf = jsonutils.loads(agent_db.configurations) except Exception: msg = _("Configuration for agent %(agent_type)s on host %(host)s" " is invalid.") LOG.warn(msg, {"agent_type": agent_db.agent_type, "host": agent_db.host}) conf = {} return conf
def request(self, *args, **kwargs): request_data = jsonutils.loads(kwargs["data"]) context_dict = request_data["context"] context = neutron_context.Context.from_dict(context_dict) resource_type = context_dict["type"] operation = context_dict["operation"] data = request_data["data"] resource = None if data.get("resource"): body = data["resource"] if resource_type not in ["security_group_rule", "router", "floatingip"]: for key, value in body.items(): if value is None: body[key] = attr.ATTR_NOT_SPECIFIED resource = {resource_type: body} obj = {} code = webob.exc.HTTPOk.code try: if operation == "READ": func = getattr(self, "get_%s" % resource_type) obj = func(context, data["id"]) if operation == "READALL": func = getattr(self, "get_%ss" % resource_type) obj = func(context, filters=data.get("filters")) if operation == "READCOUNT": func = getattr(self, "get_%ss_count" % resource_type) count = func(context, filters=data.get("filters")) obj = {"count": count} if operation == "CREATE": func = getattr(self, "create_%s" % resource_type) obj = func(context, resource) if operation == "UPDATE": func = getattr(self, "update_%s" % resource_type) obj = func(context, data["id"], resource) if operation == "DELETE": func = getattr(self, "delete_%s" % resource_type) obj = func(context, data["id"]) if operation == "ADDINTERFACE": obj = self.add_router_interface(context, data["id"], data["resource"]) if operation == "DELINTERFACE": obj = self.remove_router_interface(context, data["id"], data["resource"]) except (exc.NeutronException, netaddr.AddrFormatError) as error: for fault in api_base.FAULT_MAP: if isinstance(error, fault): mapped_exc = api_base.FAULT_MAP[fault] code = mapped_exc.code obj = {"type": error.__class__.__name__, "message": error.msg, "detail": ""} if data.get("id"): obj["id"] = data.get("id") response = mock.MagicMock() response.status_code = code def return_obj(): return obj response.json = return_obj return response
def clear_interface_qos_ingress(self, port_name): args = [ '--format=json', '--', '--columns=qos', 'list', 'port', port_name ] result = self.run_vsctl(args) if not result: return json_result = jsonutils.loads(result) try: qos = json_result['data'][0][0][ 1] #when no qos,json_result['data'][0][0][1]=[] except Exception as e: LOG.warn( _("qosdebug: Unable to parse qos details of port : %s. Exception: %s" ), port_name, e) return if not qos: #qos=[], there is no qos in the port return args = ['--format=json', '--', '--columns=queues', 'list', 'qos', qos] result = self.run_vsctl(args) # if not result: #when qos=[],result=None # return json_result = jsonutils.loads(result) try: queue = json_result['data'][0][0][1][0][1][1] except Exception as e: LOG.warn( _("qosdebug: Unable to parse queue details of qos : %s. Exception: %s" ), qos, e) return #clear qos of port args = ["clear", "port", port_name, "qos"] self.run_vsctl(args) #delete qos args = ['destroy', 'qos', qos] self.run_vsctl(args) if queue: #delete queue args = ['destroy', 'queue', queue] self.run_vsctl(args)
def to_hosting_device(device_dict, down_cb): return { 'id': device_dict['id'], 'management_ip_addresses': jsonutils.loads( device_dict['mgmt_url']), 'boot_wait': cfg.CONF.monitor.boot_wait, 'down_cb': down_cb, 'device': device_dict, }
def to_hosting_device(device_dict, down_cb): return { 'id': device_dict['id'], 'management_ip_addresses': jsonutils.loads(device_dict['mgmt_url']), 'boot_wait': cfg.CONF.monitor.boot_wait, 'down_cb': down_cb, 'device': device_dict, }
def _fetch_template_and_params(self, context, sc_instance, sc_spec, sc_node): stack_template = sc_node.get('config') if not stack_template: return stack_template = jsonutils.loads(stack_template) config_param_values = sc_instance.get('config_param_values', {}) stack_params = {} # config_param_values has the parameters for all Nodes. Only apply # the ones relevant for this Node if config_param_values: config_param_values = jsonutils.loads(config_param_values) config_param_names = sc_spec.get('config_param_names', []) if config_param_names: config_param_names = ast.literal_eval(config_param_names) # TODO(magesh):Process on the basis of ResourceType rather than Name provider_ptg_id = sc_instance.get("provider_ptg_id") node_params = (stack_template.get('Parameters') or stack_template.get('parameters')) if not node_params: return (stack_template, stack_params) for key in list(set(config_param_names) & set(node_params.keys())): if key == "PoolMemberIPs": value = self._get_member_ips(context, provider_ptg_id) # TODO(Magesh):Return one value for now value = value[0] if value else "" config_param_values[key] = value elif key == "pool_member_port": value = self._get_member_ports(context, provider_ptg_id) # TODO(Magesh):Return one value for now value = value[0] if value else "" config_param_values[key] = value elif key == "Subnet": value = self._get_ptg_subnet(context, provider_ptg_id) config_param_values[key] = value elif key == "vip_port": value = self._create_lb_service_port(context, provider_ptg_id) config_param_values[key] = value for parameter in list(set(config_param_values.keys()) & set(node_params.keys())): if parameter in node_params.keys(): stack_params[parameter] = config_param_values[parameter] return (stack_template, stack_params)
def _fetch_template_and_params(self, context, sc_instance, sc_spec, sc_node): stack_template = sc_node.get('config') if not stack_template: return stack_template = jsonutils.loads(stack_template) config_param_values = sc_instance.get('config_param_values', {}) stack_params = {} # config_param_values has the parameters for all Nodes. Only apply # the ones relevant for this Node if config_param_values: config_param_values = jsonutils.loads(config_param_values) config_param_names = sc_spec.get('config_param_names', []) if config_param_names: config_param_names = ast.literal_eval(config_param_names) # TODO(magesh):Process on the basis of ResourceType rather than Name provider_ptg_id = sc_instance.get("provider_ptg_id") node_params = (stack_template.get('Parameters') or stack_template.get('parameters')) if not node_params: return (stack_template, stack_params) for key in list(set(config_param_names) & set(node_params.keys())): if key == "PoolMemberIPs": value = self._get_member_ips(context, provider_ptg_id) # TODO(Magesh):Return one value for now value = value[0] if value else "" config_param_values[key] = value elif key == "pool_member_port": value = self._get_member_ports(context, provider_ptg_id) # TODO(Magesh):Return one value for now value = value[0] if value else "" config_param_values[key] = value elif key == "Subnet": value = self._get_ptg_subnet(context, provider_ptg_id) config_param_values[key] = value elif key == "vip_port": value = self._create_lb_service_port(context, provider_ptg_id) config_param_values[key] = value for parameter in list( set(config_param_values.keys()) & set(node_params.keys())): if parameter in node_params.keys(): stack_params[parameter] = config_param_values[parameter] return (stack_template, stack_params)
def get_vif_port_set(self, vswitch_prefix): ebr_int_port_names = self.get_port_name_list() tap_port_names = [] for port_name in ebr_int_port_names: if port_name[0:3] == 'qve': tap_port_name = port_name.replace('qve', 'tap') tap_port_names.append(tap_port_name) edge_ports = set() args = [ '--format=json', '--', '--columns=name,external_ids,ofport', 'list', 'Interface' ] result = self.run_vsctl(args, check_error=True) if not result: return edge_ports for row in jsonutils.loads(result)['data']: external_ids = dict(row[1][1]) # Do not consider VIFs which aren't yet ready # This can happen when ofport values are either [] or ["set", []] # We will therefore consider only integer values for ofport ofport = row[2] port_name = row[0] if port_name not in tap_port_names: continue try: int_ofport = int(ofport) except (ValueError, TypeError): LOG.warn(_("Found not yet ready EVS switch port: %s"), row) else: if int_ofport > 0: if ("iface-id" in external_ids and "attached-mac" in external_ids): switch = get_bridge_for_iface(self.root_helper, port_name) if switch == self.br_name or switch.startswith( vswitch_prefix): edge_ports.add(external_ids['iface-id']) elif ("xs-vif-uuid" in external_ids and "attached-mac" in external_ids): # if this is a xenserver and iface-id is not # automatically synced to OVS from XAPI, we grab it # from XAPI directly iface_id = self.get_xapi_iface_id( external_ids["xs-vif-uuid"]) switch = get_bridge_for_iface(self.root_helper, port_name) if switch == self.br_name or switch.startswith( vswitch_prefix): edge_ports.add(iface_id) else: LOG.warn(_("Found failed evs switch port: %s"), row) return edge_ports
def _get_profile(self, binding): if binding.profile: try: return jsonutils.loads(binding.profile) except Exception: LOG.error(_("Serialized profile DB value '%(value)s' for " "port %(port)s is invalid"), {'value': binding.profile, 'port': binding.port_id}) return {}
def _get_vif_details(self, binding): if binding.vif_details: try: return jsonutils.loads(binding.vif_details) except Exception: LOG.error(_("Serialized vif_details DB value '%(value)s' " "for port %(port)s is invalid"), {'value': binding.vif_details, 'port': binding.port_id}) return {}
def _add_lrouter_nat(self, body, lr_uuid): fake_nat = json.loads(body) new_uuid = uuidutils.generate_uuid() fake_nat['uuid'] = new_uuid fake_nat['lr_uuid'] = lr_uuid self._fake_lrouter_nat_dict[fake_nat['uuid']] = fake_nat if 'match' in fake_nat: match_json = json.dumps(fake_nat['match']) fake_nat['match_json'] = match_json return fake_nat
def get_dvr_configuration_dict(cls, agent_db): try: conf = jsonutils.loads(agent_db.configurations) except Exception: msg = _('Configuration for agent %(agent_type)s on host %(host)s' ' is invalid.') LOG.warn(msg, {'agent_type': agent_db.agent_type, 'host': agent_db.host}) conf = {} return conf
def _request_backend(self, context, data_dict, obj_name, action): context_dict = self._encode_context(context, action, obj_name) data = json.dumps({'context': context_dict, 'data': data_dict}) url_path = "%s/%s" % (self.PLUGIN_URL_PREFIX, obj_name) response = self._relay_request(url_path, data=data) if response.content: return response.status_code, json.loads(response.content) else: return response.status_code, response.content
def test_resource_ext_with_custom_member_action_gets_plugin_prefix(self): controller = self.DummySvcPlugin() member = {'custom_member_action': "GET"} collections = {'collection_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, path_prefix="/dummy_svc", member_actions=member, collection_actions=collections) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/dummy_svc/tweedles/1/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual( jsonutils.loads(response.body)['member_action'], "value") response = test_app.get("/dummy_svc/tweedles/collection_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")