Exemplo n.º 1
0
    def resolve_ext_resource(self, resource, name_or_id):
        """Returns the id and validate neutron ext resource."""

        path = self._resolve_resource_path(resource)

        try:
            record = self.client().show_ext(path + '/%s', name_or_id)
            return record.get(resource).get('id')
        except exceptions.NotFound:
            res_plural = resource + 's'
            result = self.client().list_ext(collection=res_plural,
                                            path=path,
                                            retrieve_all=True)
            resources = result.get(res_plural)
            matched = []
            for res in resources:
                if res.get('name') == name_or_id:
                    matched.append(res.get('id'))
            if len(matched) > 1:
                raise exceptions.NeutronClientNoUniqueMatch(resource=resource,
                                                            name=name_or_id)
            elif len(matched) == 0:
                not_found_message = (_("Unable to find %(resource)s with name "
                                       "or id '%(name_or_id)s'") % {
                                           'resource': resource,
                                           'name_or_id': name_or_id
                                       })
                raise exceptions.NotFound(message=not_found_message)
            else:
                return matched[0]
Exemplo n.º 2
0
def _find_resourceid_by_name(client,
                             resource,
                             name,
                             project_id=None,
                             cmd_resource=None,
                             parent_id=None):
    if not cmd_resource:
        cmd_resource = resource
    cmd_resource_plural = _get_resource_plural(cmd_resource, client)
    resource_plural = _get_resource_plural(resource, client)
    obj_lister = getattr(client, "list_%s" % cmd_resource_plural)
    params = {'name': name, 'fields': 'id'}
    if project_id:
        params['tenant_id'] = project_id
    if parent_id:
        data = obj_lister(parent_id, **params)
    else:
        data = obj_lister(**params)
    collection = resource_plural
    info = data[collection]
    if len(info) > 1:
        raise exceptions.NeutronClientNoUniqueMatch(resource=resource,
                                                    name=name)
    elif len(info) == 0:
        not_found_message = (_("Unable to find %(resource)s with name "
                               "'%(name)s'") % {
                                   'resource': resource,
                                   'name': name
                               })
        # 404 is used to simulate server side behavior
        raise exceptions.NeutronClientException(message=not_found_message,
                                                status_code=404)
    else:
        return info[0]['id']
Exemplo n.º 3
0
def find_sfc_resource(client, resource, name_or_id):
    """Returns the id and validate sfc resource."""
    path = _resolve_resource_path(resource)

    try:
        record = client.show_ext(path + '/%s', name_or_id)
        return record.get(resource).get('id')
    except exceptions.NotFound:
        res_plural = resource + 's'
        record = client.list_ext(collection=res_plural,
                                 path=path,
                                 retrieve_all=True)
        record1 = record.get(res_plural)
        rec_chk = []
        for rec in record1:
            if rec.get('name') == name_or_id:
                rec_chk.append(rec.get('id'))
        if len(rec_chk) > 1:
            raise exceptions.NeutronClientNoUniqueMatch(resource=resource,
                                                        name=name_or_id)
        elif len(rec_chk) == 0:
            not_found_message = (_("Unable to find %(resource)s with name "
                                   "or id '%(name_or_id)s'") % {
                                       'resource': resource,
                                       'name_or_id': name_or_id
                                   })
            raise exceptions.NotFound(message=not_found_message)
        else:
            return rec_chk[0]
Exemplo n.º 4
0
    def test_get_fixed_network_id_with_duplicated_name(self,
                                                       mock_neutron_v20_find):
        ex = n_exception.NeutronClientNoUniqueMatch(resource='subnet',
                                                    name='duplicated-name')

        self.assert_raises_from_get_fixed_network_id(
            mock_neutron_v20_find,
            ex,
            exception.InvalidSubnet,
        )
Exemplo n.º 5
0
    def _default_security_group(self, tenant_id, client=None):
        """Get default secgroup for given tenant_id.

        :returns: DeletableSecurityGroup -- default secgroup for given tenant
        """
        if client is None:
            client = self.network_client
        sgs = [
            sg for sg in client.list_security_groups().values()[0]
            if sg['tenant_id'] == tenant_id and sg['name'] == 'default'
        ]
        msg = "No default security group for tenant %s." % (tenant_id)
        self.assertTrue(len(sgs) > 0, msg)
        if len(sgs) > 1:
            msg = "Found %d default security groups" % len(sgs)
            raise exc.NeutronClientNoUniqueMatch(msg=msg)
        return net_common.DeletableSecurityGroup(client=client, **sgs[0])
Exemplo n.º 6
0
def _find_resourceid_by_name(client, resource, name):
    resource_plural = _get_resource_plural(resource, client)
    obj_lister = getattr(client, "list_%s" % resource_plural)
    data = obj_lister(name=name, fields='id')
    collection = resource_plural
    info = data[collection]
    if len(info) > 1:
        raise exceptions.NeutronClientNoUniqueMatch(resource=resource,
                                                    name=name)
    elif len(info) == 0:
        not_found_message = (_("Unable to find %(resource)s with name "
                               "'%(name)s'") %
                             {'resource': resource, 'name': name})
        # 404 is used to simulate server side behavior
        raise exceptions.NeutronClientException(
            message=not_found_message, status_code=404)
    else:
        return info[0]['id']
Exemplo n.º 7
0
 def get_network_by_name(self, network_name):
     if network_name is None:
         raise ValueError('network_name must be provided')
     neutron_client = self.__get_neutron_client()
     logger.debug('Retrieving network with name %s', network_name)
     result = neutron_client.list_networks()
     matches = []
     for network in result['networks']:
         if network['name'] == network_name:
             matches.append(network)
     if len(matches) > 1:
         raise neutronexceptions.NeutronClientNoUniqueMatch(
             resource='Network', name=network_name)
     elif len(matches) == 1:
         return matches[0]
     else:
         raise neutronexceptions.NotFound(
             message='Unable to find network with name \'{0}\''.format(
                 network_name))
 def find_resource(self, resource, name_or_id, project_id=None,
                   cmd_resource=None, parent_id=None, fields=None):
     if resource == 'security_group':
         # lookup first by unique id
         sg = self._fake_security_groups.get(name_or_id)
         if sg:
             return sg
         # lookup by name, raise an exception on duplicates
         res = None
         for sg in self._fake_security_groups.values():
             if sg['name'] == name_or_id:
                 if res:
                     raise n_exc.NeutronClientNoUniqueMatch(
                         resource=resource, name=name_or_id)
                 res = sg
         if res:
             return res
     raise n_exc.NotFound("Fake %s '%s' not found." %
                          (resource, name_or_id))
 def fake_find_resourceid_by_name_or_id(client,
                                        param,
                                        name,
                                        project_id=None):
     raise n_exc.NeutronClientNoUniqueMatch()