def find_cert_and_identity(self, name, cert_pem): nsx_style_pem = cert_pem certs = self.get_certs() cert_ids = [ cert['id'] for cert in certs if cert['pem_encoded'] == nsx_style_pem.decode('ascii') ] if not cert_ids: raise nsxlib_exc.ResourceNotFound( manager=self.client.nsx_api_managers, operation="find_certificate") identities = self.get_identities(name) # should be zero or one matching identities results = [ identity for identity in identities if identity['certificate_id'] in cert_ids ] if not results: raise nsxlib_exc.ResourceNotFound( manager=self.client.nsx_api_managers, operation="delete_identity") return results[0]['certificate_id'], results[0]['id']
def list(self, cluster_id, resource_type): if not resource_type: msg = "null resource type is not supported" raise exceptions.ResourceNotFound(details=msg) request_url = "%s?cluster_id=%s" % (self.get_path( self._get_path_for_resource(resource_type)), cluster_id) return self.client.url_get(request_url)
def delete(self, resource_type, resource_id): if not resource_type: msg = "null resource type is not supported" raise exceptions.ResourceNotFound(details=msg) request_url = "%s/%s" % (self.get_path( self._get_path_for_resource(resource_type)), resource_id) return self.client.url_delete(request_url)
def test_create_logical_router_intf_port_by_ls_id(self): logical_router_id = uuidutils.generate_uuid() display_name = 'dummy' tags = [] ls_id = uuidutils.generate_uuid() logical_switch_port_id = uuidutils.generate_uuid() address_groups = [] with mock.patch.object( self.nsxlib.router._router_port_client, "get_by_lswitch_id", side_effect=nsxlib_exc.ResourceNotFound()) as get_port,\ mock.patch.object(self.nsxlib.router._router_port_client, "create") as create_port: self.nsxlib.router.create_logical_router_intf_port_by_ls_id( logical_router_id, display_name, tags, ls_id, logical_switch_port_id, address_groups) get_port.assert_called_once_with(ls_id) create_port.assert_called_once_with( logical_router_id, display_name, tags, nsx_constants.LROUTERPORT_DOWNLINK, logical_switch_port_id, address_groups, urpf_mode=None, relay_service_uuid=None)
def get_tier1_link_port(self, logical_router_id): logical_router_ports = self.get_by_router_id(logical_router_id) for port in logical_router_ports: if port['resource_type'] == nsx_constants.LROUTERPORT_LINKONTIER1: return port raise exceptions.ResourceNotFound(manager=self.client.nsx_api_managers, operation="get router link port")
def update_pool_member(self, lb_pool_id, ip_address, port=None, display_name=None, weight=None, tenant=constants.POLICY_INFRA_TENANT): lb_pool_def = lb_defs.LBPoolDef(lb_pool_id=lb_pool_id, tenant=tenant) lb_pool = self.policy_api.get(lb_pool_def) lb_pool_members = lb_pool.get('members', []) member_to_update = [ x for x in lb_pool_members if (x.get('ip_address') == ip_address and x.get('port') == port) ] if member_to_update: member_to_update[0]['display_name'] = display_name member_to_update[0]['weight'] = weight self._update_helper(lb_pool_id, members=lb_pool_members, pool_data=lb_pool, tenant=tenant) else: ops = ('Updating member %(address)s:%(port)d failed, not found in ' 'pool %(pool)s', { 'address': ip_address, 'port': port, 'pool': lb_pool_id }) raise nsxlib_exc.ResourceNotFound(manager=lb_pool_def, operation=ops)
def check_manager_status_v2(client, manager_url): """MP healthcheck for Version 2.4 and above""" # Try to get the status silently and with no retries status = client.get('reverse-proxy/node/health', silent=True, with_retries=False) if (not status or not status.get('healthy', False)): msg = _("Manager is not in working state: %s") % status LOG.warning(msg) raise exceptions.ResourceNotFound( manager=manager_url, operation=msg)
def check_manager_status_v1(client, manager_url): """MP healthcheck for Version 2.3 and below""" # Try to get the cluster status silently and with no retries status = client.get('operational/application/status', silent=True, with_retries=False) if (not status or status.get('application_status') != 'WORKING'): msg = _("Manager is not in working state: %s") % status LOG.warning(msg) raise exceptions.ResourceNotFound( manager=manager_url, operation=msg)
def do_update(): object_url = self.resource + '/' + resource_id body = self.client.get(object_url) item_list = body.get(item_key) if item_list and item_id in item_list: item_list.remove(item_id) body[item_key] = item_list return self.client.update(object_url, body) else: ops = ('removing item %s from resource %s %s as it is not in ' 'the list', item_id, item_key, item_list) raise nsxlib_exc.ResourceNotFound( manager=self.client.nsx_api_managers, operation=ops)
def get_by_lswitch_id(self, logical_switch_id): resource = '?logical_switch_id=%s' % logical_switch_id router_ports = self.client.url_get(self.get_path(resource)) result_count = int(router_ports.get('result_count', "0")) if result_count >= 2: raise exceptions.ManagerError( details=_("Can't support more than one logical router ports " "on same logical switch %s ") % logical_switch_id) elif result_count == 1: return router_ports['results'][0] else: err_msg = (_("Logical router link port not found on logical " "switch %s") % logical_switch_id) raise exceptions.ResourceNotFound( manager=self.client.nsx_api_managers, operation=err_msg)
def _delete_resource_by_values(self, resource, skip_not_found=True, strict_mode=True, **kwargs): """Delete resource objects matching the values in kwargs If skip_not_found is True - do not raise an exception if no object was found. If strict_mode is True - warnings will be issued if 0 or >1 objects where deleted. """ resources_list = self.client.list(resource) matched_num = 0 for res in resources_list['results']: if utils.dict_match(kwargs, res): LOG.debug("Deleting %s from resource %s", res, resource) delete_resource = resource + "/" + str(res['id']) self.client.delete(delete_resource) matched_num = matched_num + 1 if matched_num == 0: if skip_not_found: if strict_mode: LOG.warning( "No resource in %(res)s matched for values: " "%(values)s", { 'res': resource, 'values': kwargs }) else: err_msg = (_("No resource in %(res)s matched for values: " "%(values)s") % { 'res': resource, 'values': kwargs }) raise exceptions.ResourceNotFound( manager=self.client.nsx_api_managers, operation=err_msg) elif matched_num > 1 and strict_mode: LOG.warning( "%(num)s resources in %(res)s matched for values: " "%(values)s", { 'num': matched_num, 'res': resource, 'values': kwargs })
def validate_connection(self, cluster_api, endpoint, conn): client = nsx_client.NSX3Client( conn, url_prefix=endpoint.provider.url, url_path_base=cluster_api.nsxlib_config.url_base, default_headers=conn.default_headers) keepalive_section = cluster_api.nsxlib_config.keepalive_section result = client.get(keepalive_section, silent=True) # If keeplive section returns a list, it is assumed to be non-empty if not result or result.get('result_count', 1) <= 0: msg = _("No %(section)s found " "for '%(url)s'") % { 'section': keepalive_section, 'url': endpoint.provider.url } LOG.warning(msg) raise exceptions.ResourceNotFound(manager=endpoint.provider.url, operation=msg)
def get_id_by_resource_and_tag(self, resource_type, scope, tag, alert_not_found=False, alert_multiple=False): """Search a resource type by 1 scope&tag. Return the id of the result only if it is single. """ query_tags = [{ 'scope': utils.escape_tag_data(scope), 'tag': utils.escape_tag_data(tag) }] query_result = self.search_by_tags(tags=query_tags, resource_type=resource_type) if not query_result['result_count']: if alert_not_found: msg = _("No %(type)s found for tag '%(scope)s:%(tag)s'") % { 'type': resource_type, 'scope': scope, 'tag': tag } LOG.warning(msg) raise exceptions.ResourceNotFound( manager=self.nsxlib_config.nsx_api_managers, operation=msg) elif query_result['result_count'] == 1: return query_result['results'][0]['id'] else: # multiple results if alert_multiple: msg = _("Multiple %(type)s found for tag '%(scope)s:" "%(tag)s'") % { 'type': resource_type, 'scope': scope, 'tag': tag } LOG.warning(msg) raise exceptions.ManagerError( manager=self.nsxlib_config.nsx_api_managers, operation=msg, details='')
def update_lb_rule(self, virtual_server_id, lb_rule_name, actions=None, match_conditions=None, match_strategy=None, phase=None, position=-1, tenant=constants.POLICY_INFRA_TENANT): lb_rule = lb_defs.LBRuleDef(actions, match_conditions, lb_rule_name, match_strategy, phase) lbvs_def = self.entry_def(virtual_server_id=virtual_server_id, tenant=tenant) body = self.policy_api.get(lbvs_def) lb_rules = body.get('rules', []) # Remove existing rule try: rule_index = next( lb_rules.index(r) for r in lb_rules if r.get('display_name') == lb_rule_name) except Exception: err_msg = (_("No resource in rules matched for values: " "%(values)s") % { 'values': lb_rule_name }) raise nsxlib_exc.ResourceNotFound(manager=self, operation=err_msg) if position < 0: position = rule_index del (lb_rules[rule_index]) # Insert new rule lb_rules = self._add_rule_in_position(body, lb_rule, position) return self._update_helper(virtual_server_id, rules=lb_rules, vs_data=body, tenant=tenant)
def _get_path_for_resource(self, resource_type): path = self.RESOURCES_PATH.get(resource_type) if not path: msg = "backend resource %s is not supported" % resource_type raise exceptions.ResourceNotFound(details=msg) return path
def create(self, resource_type, resource): if not resource_type: msg = "null resource type is not supported" raise exceptions.ResourceNotFound(details=msg) request_url = self.get_path(self._get_path_for_resource(resource_type)) return self.client.url_post(request_url, resource)