def _project_read(self, proj_id=None, fq_name=None): if proj_id: proj_id = self._project_id_neutron_to_vnc(proj_id) try: return self._vnc_api.project_read(id=proj_id, fq_name=fq_name) except vnc_exc.NoIdError: raise neutron_exc.NotFound(proj_id)
def request(self, method, url, token=None, args=None, headers=None, accepted_codes=[200, 201, 202, 204]): params = {} if args: # extract filter and fields if 'filters' in args: params = args.pop('filters') if 'fields' in args: params['fields'] = args.pop('fields') args = jsonutils.dumps(args) if not headers: headers = { 'Content-type': 'application/json', 'X-Auth-Token': token } headers['User-Agent'] = OCTAVIA_PROXY_CLIENT url = '{}/{}'.format(self.base_url, str(url)) LOG.debug("url = %s", url) LOG.debug("args = %s", args) LOG.debug("params = %s", str(params)) r = requests.request(method, url, data=args, params=params, headers=headers) LOG.debug("Octavia Response Code: {0}".format(r.status_code)) LOG.debug("Octavia Response Body: {0}".format(r.content)) LOG.debug("Octavia Response Headers: {0}".format(r.headers)) if r.status_code in accepted_codes: if method != 'DELETE': return r.json() elif r.status_code == 413: e = lib_exc.OverQuota() e.msg = str(r.content) raise e elif r.status_code == 409: e = lib_exc.Conflict() e.msg = str(r.content) raise e elif r.status_code == 401: e = lib_exc.NotAuthorized() e.msg = str(r.content) raise e elif r.status_code == 404: e = lib_exc.NotFound() e.msg = str(r.content) raise e elif r.status_code == 400: e = lib_exc.BadRequest(resource="", msg="") e.msg = str(r.content) raise e else: raise loadbalancerv2.DriverError(msg=str(r.content))
def _get_resource(self, resource, context, id, fields=None, sub_resource=None, resource_id=None): # not sure how to test that or if we even support sorting/filtering? args = {} if fields: if 'tenant_id' in fields: fields.remove('tenant_id') if 'project_id' not in fields: fields.append('project_id') args['fields'] = fields resource_ = resource if not sub_resource else sub_resource LOG.debug("-get_resource context-tenant-id %s" % context.tenant_id) res = self.get( '{}/{}'.format(self._path(resource, sub_resource, resource_id), id), context.auth_token, args) response = res[resource_] if ('provisioning_status' in response) and (response['provisioning_status'] == 'DELETED'): raise lib_exc.NotFound() # neutron is looking for tenant_id in the response for RBAC if 'project_id' in response: response['tenant_id'] = response['project_id'] else: response['tenant_id'] = context.tenant_id return response
def validate_host_groups(self, resource_id, host_group_names): session = self._session resource = vim_util.get_moref(resource_id, 'ResourcePool') cluster = session.invoke_api(vim_util, "get_object_property", self._session.vim, resource, "owner") client_factory = session.vim.client.factory config_spec = client_factory.create('ns0:ClusterConfigSpecEx') cluster_config = session.invoke_api(vim_util, "get_object_property", self._session.vim, cluster, "configurationEx") groups = [] if hasattr(cluster_config, 'group'): groups = cluster_config.group for host_group_name in host_group_names: found = False for group in groups: if host_group_name == group.name: found = True break if not found: LOG.error("%s does not exist", host_group_name) raise exceptions.NotFound() update_cluster = False num_host_groups = len(host_group_names) rules = [] if hasattr(cluster_config, 'rule'): rules = cluster_config.rule # Ensure that the VM groups are created for index in range(num_host_groups): entry_id = index + 1 vmGroup = None for group in groups: if 'neutron-group-%s' % entry_id == group.name: vmGroup = group break if vmGroup is None: groupSpec = self._create_vm_group_spec( client_factory, 'neutron-group-%s' % entry_id, [], vmGroup) config_spec.groupSpec.append(groupSpec) update_cluster = True config_rule = None # Create the config rule if it does not exist for rule in rules: if 'neutron-rule-%s' % entry_id == rule.name: config_rule = rule break if config_rule is None and index < num_host_groups: ruleSpec = self._create_cluster_rules_spec( client_factory, 'neutron-rule-%s' % entry_id, 'neutron-group-%s' % entry_id, host_group_names[index - 1]) config_spec.rulesSpec.append(ruleSpec) update_cluster = True if update_cluster: try: self._reconfigure_cluster(session, cluster, config_spec) except Exception as e: LOG.error('Unable to update cluster for host groups %s', e)
def test__check_requirements_fail_on_missing_pools(self): with mock.patch.object( self.mixin, '_get_default_external_network'),\ mock.patch.object( self.mixin, '_get_supported_subnetpools') as g: g.side_effect = n_exc.NotFound() self.assertRaises(exceptions.AutoAllocationFailure, self.mixin._check_requirements, self.ctx, 'foo_tenant')
def delete_dummy(self, context, id): try: svc_type_id = self.dummys[id]['service_type'] del self.dummys[id] self.svctype_mgr.decrease_service_type_refcount( context, svc_type_id) except KeyError: raise exceptions.NotFound()
def test__get_latest_resource_none(self, plugin_mock): plugin_mock.return_value.get_network.side_effect = nexc.NotFound() l2 = mech_driver_v2.L2_RESOURCES full_sync.ALL_RESOURCES[plugin_constants.CORE] = l2 mock_row = self._mock_row(odl_const.ODL_NETWORK) self.assertRaises(nexc.NotFound, recovery._get_latest_resource, self.db_context.session, mock_row)
def test_neutron_nonfound_to_webob_exception(self): # this endpoint raises a Neutron notfound exception. make sure it gets # translated into a 404 error with mock.patch( 'neutron.pecan_wsgi.controllers.resource.' 'CollectionsController.get', side_effect=n_exc.NotFound()): response = self.app.get('/v2.0/ports.json', expect_errors=True) self.assertEqual(response.status_int, 404)
def lsn_for_network_get(cluster, network_id): filters = {"tag": network_id, "tag_scope": "n_network_id"} results = nsxlib.do_request(HTTP_GET, nsxlib._build_uri_path(LSERVICESNODE_RESOURCE, fields="uuid", filters=filters), cluster=cluster)['results'] if not results: raise exception.NotFound() elif len(results) == 1: return results[0]['uuid']
def _lsn_port_get(cluster, lsn_id, filters): results = nsxlib.do_request(HTTP_GET, nsxlib._build_uri_path( LSERVICESNODEPORT_RESOURCE, parent_resource_id=lsn_id, fields="uuid", filters=filters), cluster=cluster)['results'] if not results: raise exception.NotFound() elif len(results) == 1: return results[0]['uuid']
def _get_supported_subnetpools(self, context): """Return the default subnet pools available.""" default_subnet_pools = [ self.core_plugin.get_default_subnetpool(context, ver) for ver in (4, 6) ] available_pools = [s for s in default_subnet_pools if s] if not available_pools: LOG.error("No default pools available") raise n_exc.NotFound() return available_pools
def test__cleanup_handles_failures(self): retry_then_notfound = ( [db_exc.RetryRequest(ValueError())] + [n_exc.NotFound()] * 10 ) self.mixin._l3_plugin.remove_router_interface.side_effect = ( retry_then_notfound) self.mixin._l3_plugin.delete_router.side_effect = ( retry_then_notfound) self.mixin._core_plugin.delete_network.side_effect = ( retry_then_notfound) self.mixin._cleanup(self.ctx, network_id=44, router_id=45, subnets=[{'id': 46}])
def do_request(*args, **kwargs): """Issue a request to the cluster specified in kwargs. :param args: a list of positional arguments. :param kwargs: a list of keyworkds arguments. :returns: the result of the operation loaded into a python object or None. """ cluster = kwargs["cluster"] try: res = cluster.api_client.request(*args) if res: return jsonutils.loads(res) except api_exc.ResourceNotFound: raise exception.NotFound() except api_exc.ReadOnlyMode: raise nsx_exc.MaintenanceInProgress()
def test_service_cluster_not_found(self): self.mock_request.side_effect = exceptions.NotFound() expected = lsnlib.service_cluster_exists(None, 'foo_uuid') self.assertFalse(expected)
def get_resource(_type, is_top, resource_id): for resource in RES_MAP[_type][is_top]: if resource['id'] == resource_id: return copy.deepcopy(resource) raise q_exceptions.NotFound()
def update_resource(_type, is_top, resource_id, body): for resource in RES_MAP[_type][is_top]: if resource['id'] == resource_id: resource.update(body) return copy.deepcopy(resource) raise q_exceptions.NotFound()
def get_resource(_type, is_top, resource_id): for resource in get_resource_list(_type, is_top): if resource['id'] == resource_id: return copy.deepcopy(resource) raise q_exceptions.NotFound()
def get_dummy(self, context, id, fields): try: return self.dummys[id] except KeyError: raise exceptions.NotFound()
def test__get_latest_resource_none(self, plugin_mock): plugin_mock.return_value.get_network.side_effect = nexc.NotFound() mock_row = self._mock_row(odl_const.ODL_NETWORK) self.assertRaises( nexc.NotFound, recovery._get_latest_resource, mock_row)
def raise_not_found(self): raise n_exc.NotFound()