Example #1
0
def get_network_from_name(name, compute_networks_client):
    """Get a full network dict from just a network name

    :param str name: the name of the network to use
    :param NetworksClientJSON compute_networks_client: The network client
        object to use for making the network lists api request
    :return: The full dictionary for the network in question, unless the
        network for the supplied name can not be found. In which case a dict
        with just the name will be returned.
    :rtype: dict
    """
    caller = misc_utils.find_test_caller()
    if not name:
        network = {'name': name}
    else:
        try:
            resp = compute_networks_client.list_networks(name=name)
            if isinstance(resp, list):
                networks = resp
            elif isinstance(resp, dict):
                networks = resp['networks']
            else:
                raise lib_exc.NotFound()
            if len(networks) > 0:
                network = networks[0]
            else:
                msg = "Network with name: %s not found" % name
                if caller:
                    LOG.warn('(%s) %s' % (caller, msg))
                else:
                    LOG.warn(msg)
                raise lib_exc.NotFound()
            # To be consistent with network isolation, add name is only
            # label is available
            name = network.get('name', network.get('label'))
            if name:
                network['name'] = name
            else:
                raise lib_exc.NotFound()
        except lib_exc.NotFound:
            # In case of nova network, if the fixed_network_name is not
            # owned by the tenant, and the network client is not an admin
            # one, list_networks will not find it
            msg = ('Unable to find network %s. '
                   'Starting instance without specifying a network.' %
                   name)
            if caller:
                LOG.info('(%s) %s' % (caller, msg))
            else:
                LOG.info(msg)
            network = {'name': name}
    return network
    def _image_get_state(cls, image_id):
        resp, data = cls.client.DescribeImages(ImageIds=[image_id])
        if resp.status_code == 200:
            if not data['Images']:
                raise exceptions.NotFound()
            return data['Images'][0]['State']

        if resp.status_code == 400:
            error = data['Error']
            if error['Code'] == 'InvalidAMIID.NotFound':
                raise exceptions.NotFound()

        raise EC2ResponceException(resp, data)
    def _volume_attachment_get_state(cls, volume_id):
        resp, data = cls.client.DescribeVolumes(VolumeIds=[volume_id])
        if resp.status_code == 200:
            volume = data['Volumes'][0]
            if 'Attachments' in volume and len(volume['Attachments']) > 0:
                return volume['Attachments'][0]['State']
            raise exceptions.NotFound()

        if resp.status_code == 400:
            error = data['Error']
            if error['Code'] == 'InvalidVolume.NotFound':
                raise exceptions.NotFound()

        raise EC2ResponceException(resp, data)
    def _instance_get_state(cls, instance_id):
        resp, data = cls.client.DescribeInstances(InstanceIds=[instance_id])
        if resp.status_code == 200:
            state = data['Reservations'][0]['Instances'][0]['State']['Name']
            if state != 'terminated':
                return state
            raise exceptions.NotFound()

        if resp.status_code == 400:
            error = data['Error']
            if error['Code'] == 'InvalidInstanceID.NotFound':
                raise exceptions.NotFound()

        raise EC2ResponceException(resp, data)
Example #5
0
 def test_is_resource_deleted_true(self):
     self.useFixture(
         mockpatch.Patch(
             'tempest_lib.services.compute.floating_ips_client.'
             'FloatingIPsClient.show_floating_ip',
             side_effect=lib_exc.NotFound()))
     self.assertTrue(self.client.is_resource_deleted('fake-id'))
Example #6
0
 def _get_role_id(self, role_name):
     try:
         roles = self.client.list_roles()
         return next(r['id'] for r in roles if r['name'] == role_name)
     except StopIteration:
         msg = "Role name '%s' is not found" % role_name
         raise lib_exc.NotFound(msg)
Example #7
0
    def get_image_id(self, image_name):
        if uuidutils.is_uuid_like(image_name):
            return image_name
        for image in self.nova_client.images.list():
            if image.name == image_name:
                return image.id

        raise exc.NotFound(image_name)
Example #8
0
 def get_network_id(self, network_name):
     if uuidutils.is_uuid_like(network_name):
         return network_name
     networks = self.neutron_client.list_networks(name=network_name)
     networks = networks['networks']
     if len(networks) < 1:
         raise exc.NotFound(network_name)
     return networks[0]['id']
Example #9
0
 def wrapped_func(self, *args, **kwargs):
     try:
         return f(self, *args, **kwargs)
     except tempest_lib_exc.CommandFailed as e:
         if re.search('No (\w+) with a name or ID', e.stderr):
             # Raise appropriate 'NotFound' error
             raise tempest_lib_exc.NotFound()
         raise
Example #10
0
    def get_flavor_id(self, flavor_name):
        if uuidutils.is_uuid_like(flavor_name) or flavor_name.isdigit():
            return flavor_name
        for flavor in self.nova_client.flavors.list():
            if flavor.name == flavor_name:
                return flavor.id

        raise exc.NotFound(flavor_name)
 def list_security_group_rules(self, security_group_id):
     """List all rules for a security group."""
     resp, body = self.get('os-security-groups')
     body = json.loads(body)
     self.validate_response(schema.list_security_groups, resp, body)
     for sg in body['security_groups']:
         if sg['id'] == security_group_id:
             return service_client.ResponseBodyList(resp, sg['rules'])
     raise lib_exc.NotFound('No such Security Group')
Example #12
0
    def get_share_type(self, share_type, microversion=None):
        """Get share type.

        :param share_type: str -- Name or ID of share type
        """
        share_types = self.list_share_types(True, microversion=microversion)
        for st in share_types:
            if share_type in (st['ID'], st['Name']):
                return st
        raise tempest_lib_exc.NotFound()
Example #13
0
    def test_create_user_missing_tenant(self):
        self.fake_client.identity.get_tenant_by_name.side_effect = \
            lib_exc.NotFound("tenant is not found")
        self.useFixture(mockpatch.PatchObject(javelin, "keystone_admin",
                                              return_value=self.fake_client))

        javelin.create_users([self.fake_object])

        mocked_function = self.fake_client.identity.create_user
        self.assertFalse(mocked_function.called)
Example #14
0
 def _assign_user_role(self, tenant, user, role_name):
     role = None
     try:
         roles = self._list_roles()
         role = next(r for r in roles if r['name'] == role_name)
     except StopIteration:
         msg = 'No "%s" role found' % role_name
         raise lib_exc.NotFound(msg)
     self.identity_admin_client.assign_user_role(tenant['id'], user['id'],
                                                 role['id'])
    def _subnet_get_state(cls, subnet_id):
        resp, data = cls.client.DescribeSubnets(SubnetIds=[subnet_id])
        if resp.status_code == 200:
            return data['Subnets'][0]['State']

        if resp.status_code == 400:
            error = data['Error']
            if error['Code'] == 'InvalidSubnetID.NotFound':
                raise exceptions.NotFound()

        raise EC2ResponceException(resp, data)
    def _snapshot_get_state(cls, volume_id):
        resp, data = cls.client.DescribeSnapshots(SnapshotIds=[volume_id])
        if resp.status_code == 200:
            return data['Snapshots'][0]['State']

        if resp.status_code == 400:
            error = data['Error']
            if error['Code'] == 'InvalidSnapshot.NotFound':
                raise exceptions.NotFound()

        raise EC2ResponceException(resp, data)
    def _vpc_get_state(cls, vpc_id):
        resp, data = cls.client.DescribeVpcs(VpcIds=[vpc_id])
        if resp.status_code == 200:
            return data['Vpcs'][0]['State']

        if resp.status_code == 400:
            error = data['Error']
            if error['Code'] == 'InvalidVpcID.NotFound':
                raise exceptions.NotFound()

        raise EC2ResponceException(resp, data)
Example #18
0
 def assign_user_role(self, user, project, role_name):
     role = self._check_role_exists(role_name)
     if not role:
         msg = 'No "%s" role found' % role_name
         raise lib_exc.NotFound(msg)
     try:
         self.roles_client.assign_user_role(project['id'], user['id'],
                                            role['id'])
     except lib_exc.Conflict:
         LOG.debug("Role %s already assigned on project %s for user %s" %
                   (role['id'], project['id'], user['id']))
    def _network_interface_get_state(cls, ni_id):
        resp, data = cls.client.DescribeNetworkInterfaces(
            NetworkInterfaceIds=[ni_id])
        if resp.status_code == 200:
            return data['NetworkInterfaces'][0]['Status']

        if resp.status_code == 400:
            error = data['Error']
            if error['Code'] == 'InvalidNetworkInterfaceID.NotFound':
                raise exceptions.NotFound()

        raise EC2ResponceException(resp, data)
Example #20
0
 def assign_user_role(self, user, project, role_name):
     try:
         roles = self._list_roles()
         role = next(r for r in roles if r['name'] == role_name)
     except StopIteration:
         msg = 'No "%s" role found' % role_name
         raise lib_exc.NotFound(msg)
     try:
         self.identity_client.assign_user_role(project['id'], user['id'],
                                               role['id'])
     except lib_exc.Conflict:
         LOG.debug("Role %s already assigned on project %s for user %s" %
                   (role['id'], project['id'], user['id']))
Example #21
0
 def _assign_user_role(self, tenant, user, role_name):
     role = None
     try:
         roles = self._list_roles()
         role = next(r for r in roles if r['name'] == role_name)
     except StopIteration:
         msg = 'No "%s" role found' % role_name
         raise lib_exc.NotFound(msg)
     try:
         self.identity_admin_client.assign_user_role(
             tenant['id'], user['id'], role['id'])
     except lib_exc.Conflict:
         LOG.warning('Trying to add %s for user %s in tenant %s but they '
                     ' were already granted that role' %
                     (role_name, user['name'], tenant['name']))
Example #22
0
def get_tenant_network(creds_provider, compute_networks_client):
    """Get a network usable by the primary tenant

    :param creds_provider: instance of credential provider
    :param compute_networks_client: compute network client. We want to have the
           compute network client so we can have use a common approach for both
           neutron and nova-network cases. If this is not an admin network
           client, set_network_kwargs might fail in case fixed_network_name
           is the network to be used, and it's not visible to the tenant
    :return a dict with 'id' and 'name' of the network
    """
    fixed_network_name = CONF.compute.fixed_network_name
    network = None
    # NOTE(andreaf) get_primary_network will always be available once
    # bp test-accounts-continued is implemented
    if (CONF.auth.allow_tenant_isolation
            and (CONF.service_available.neutron
                 and not CONF.service_available.ironic)):
        network = creds_provider.get_primary_network()
    else:
        if fixed_network_name:
            try:
                resp = compute_networks_client.list_networks(
                    name=fixed_network_name)
                if isinstance(resp, list):
                    networks = resp
                elif isinstance(resp, dict):
                    networks = resp['networks']
                else:
                    raise lib_exc.NotFound()
                if len(networks) > 0:
                    network = networks[0]
                else:
                    msg = "Configured fixed_network_name not found"
                    raise exceptions.InvalidConfiguration(msg)
                # To be consistent with network isolation, add name is only
                # label is available
                network['name'] = network.get('name', network.get('label'))
            except lib_exc.NotFound:
                # In case of nova network, if the fixed_network_name is not
                # owned by the tenant, and the network client is not an admin
                # one, list_networks will not find it
                LOG.info('Unable to find network %s. '
                         'Starting instance without specifying a network.' %
                         fixed_network_name)
                network = {'name': fixed_network_name}
    LOG.info('Found network %s available for tenant' % network)
    return network
Example #23
0
    def _keystone_aws_get(self):
        # FIXME(andreaf) Move EC2 credentials to AuthProvider
        import keystoneclient.v2_0.client

        keystone = keystoneclient.v2_0.client.Client(**self.ks_cred)
        ec2_cred_list = keystone.ec2.list(keystone.auth_user_id)
        ec2_cred = None
        for cred in ec2_cred_list:
            if cred.tenant_id == keystone.auth_tenant_id:
                ec2_cred = cred
                break
        else:
            ec2_cred = keystone.ec2.create(keystone.auth_user_id,
                                           keystone.auth_tenant_id)
        if not all((ec2_cred, ec2_cred.access, ec2_cred.secret)):
            raise lib_exc.NotFound("Unable to get access and secret keys")
        return ec2_cred
Example #24
0
    def test_create_users(self):
        self.fake_client.identity.get_tenant_by_name.return_value = \
            self.fake_object['tenant']
        self.fake_client.identity.get_user_by_username.side_effect = \
            lib_exc.NotFound("user is not found")
        self.useFixture(mockpatch.PatchObject(javelin, "keystone_admin",
                                              return_value=self.fake_client))

        javelin.create_users([self.fake_object])

        fake_tenant_id = self.fake_object['tenant']['id']
        fake_email = "%s@%s" % (self.fake_object['user'], fake_tenant_id)
        mocked_function = self.fake_client.identity.create_user
        mocked_function.assert_called_once_with(self.fake_object['name'],
                                                self.fake_object['password'],
                                                fake_tenant_id,
                                                fake_email,
                                                enabled=True)
Example #25
0
 def get_aws_credentials(self, identity_client):
     """
     Obtain existing, or create new AWS credentials
     :param identity_client: identity client with embedded credentials
     :return: EC2 credentials
     """
     ec2_cred_list = identity_client.list_user_ec2_credentials(
         identity_client.user_id)
     for cred in ec2_cred_list:
         if cred['tenant_id'] == identity_client.tenant_id:
             ec2_cred = cred
             break
     else:
         ec2_cred = identity_client.create_user_ec2_credentials(
             identity_client.user_id, identity_client.tenant_id)
     if not all((ec2_cred, ec2_cred['access'], ec2_cred['secret'])):
         raise lib_exc.NotFound("Unable to get access and secret keys")
     else:
         ec2_cred_aws = {}
         ec2_cred_aws['aws_access_key_id'] = ec2_cred['access']
         ec2_cred_aws['aws_secret_access_key'] = ec2_cred['secret']
     return ec2_cred_aws
Example #26
0
 def test_list_share_servers_with_host_filter(self):
     # Get list of share servers and remember 'host' name
     servers = self.shares_client.list_share_servers()
     # Remember name of server that was used by this test suite
     # to be sure it will be still existing.
     host = ""
     for server in servers:
         if server["share_network_name"] in self.sn_name_and_id:
             if not server["host"]:
                 msg = ("Server '%s' has wrong value for host - "
                        "'%s'.") % (server["id"], server["host"])
                 raise lib_exc.InvalidContentType(message=msg)
             host = server["host"]
             break
     if not host:
         msg = ("Appropriate server was not found. Its share_network_data"
                ": '%s'. List of servers: '%s'.") % (self.sn_name_and_id,
                                                     str(servers))
         raise lib_exc.NotFound(message=msg)
     search_opts = {"host": host}
     servers = self.shares_client.list_share_servers(search_opts)
     self.assertTrue(len(servers) > 0)
     for server in servers:
         self.assertEqual(server["host"], host)
Example #27
0
    def _error_checker(self, method, url,
                       headers, body, resp, resp_body):

        # NOTE(mtreinish): Check for httplib response from glance_http. The
        # object can't be used here because importing httplib breaks httplib2.
        # If another object from a class not imported were passed here as
        # resp this could possibly fail
        if str(type(resp)) == "<type 'instance'>":
            ctype = resp.getheader('content-type')
        else:
            try:
                ctype = resp['content-type']
            # NOTE(mtreinish): Keystone delete user responses doesn't have a
            # content-type header. (They don't have a body) So just pretend it
            # is set.
            except KeyError:
                ctype = 'application/json'

        # It is not an error response
        if resp.status < 400:
            return

        JSON_ENC = ['application/json', 'application/json; charset=utf-8']
        # NOTE(mtreinish): This is for compatibility with Glance and swift
        # APIs. These are the return content types that Glance api v1
        # (and occasionally swift) are using.
        TXT_ENC = ['text/plain', 'text/html', 'text/html; charset=utf-8',
                   'text/plain; charset=utf-8']

        if ctype.lower() in JSON_ENC:
            parse_resp = True
        elif ctype.lower() in TXT_ENC:
            parse_resp = False
        else:
            raise exceptions.UnexpectedContentType(str(resp.status),
                                                   resp=resp)

        if resp.status == 401:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Unauthorized(resp_body, resp=resp)

        if resp.status == 403:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Forbidden(resp_body, resp=resp)

        if resp.status == 404:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.NotFound(resp_body, resp=resp)

        if resp.status == 400:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.BadRequest(resp_body, resp=resp)

        if resp.status == 410:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Gone(resp_body, resp=resp)

        if resp.status == 409:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Conflict(resp_body, resp=resp)

        if resp.status == 413:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            if self.is_absolute_limit(resp, resp_body):
                raise exceptions.OverLimit(resp_body, resp=resp)
            else:
                raise exceptions.RateLimitExceeded(resp_body, resp=resp)

        if resp.status == 415:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.InvalidContentType(resp_body, resp=resp)

        if resp.status == 422:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.UnprocessableEntity(resp_body, resp=resp)

        if resp.status in (500, 501):
            message = resp_body
            if parse_resp:
                try:
                    resp_body = self._parse_resp(resp_body)
                except ValueError:
                    # If response body is a non-json string message.
                    # Use resp_body as is and raise InvalidResponseBody
                    # exception.
                    raise exceptions.InvalidHTTPResponseBody(message)
                else:
                    if isinstance(resp_body, dict):
                        # I'm seeing both computeFault
                        # and cloudServersFault come back.
                        # Will file a bug to fix, but leave as is for now.
                        if 'cloudServersFault' in resp_body:
                            message = resp_body['cloudServersFault']['message']
                        elif 'computeFault' in resp_body:
                            message = resp_body['computeFault']['message']
                        elif 'error' in resp_body:
                            message = resp_body['error']['message']
                        elif 'message' in resp_body:
                            message = resp_body['message']
                    else:
                        message = resp_body

            if resp.status == 501:
                raise exceptions.NotImplemented(resp_body, resp=resp,
                                                message=message)
            else:
                raise exceptions.ServerFault(resp_body, resp=resp,
                                             message=message)

        if resp.status >= 400:
            raise exceptions.UnexpectedResponseCode(str(resp.status),
                                                    resp=resp)
Example #28
0
 def get_user_by_username(self, tenant_id, username):
     users = self.list_users_for_tenant(tenant_id)
     for user in users:
         if user['name'] == username:
             return user
     raise lib_exc.NotFound('No such user')
Example #29
0
 def get_tenant_by_name(self, tenant_name):
     tenants = self.list_tenants()
     for tenant in tenants:
         if tenant['name'] == tenant_name:
             return tenant
     raise lib_exc.NotFound('No such tenant')
Example #30
0
    def validate_ovsdb(self, seg_id, port, network_id_1, tunnel_key):
        # Check Logical_Switch
        objConnection = ovsdb_connections.OVSDBConnection(OVSDB_IP, OVSDB_PORT)
        resp = objConnection.get_response(OVSDB_IP, OVSDB_PORT,
                                          "Logical_Switch")
        resp_dec = json.loads(resp)
        count = resp.count('_uuid')
        try:
            self.assertIn(str(network_id_1), resp)
        except Exception:
            raise lib_exc.NotFound("Network not found in Logical Switch table")
        row = objConnection.find_row(network_id_1, count, resp_dec)
        try:
            self.assertIn(str(tunnel_key), row)
        except Exception:
            raise lib_exc.NotFound(
                "Tunnel key not found in Logical Switch table")
        objConnection.stop("true")

        # Check Physical_Port
        objConnection = ovsdb_connections.OVSDBConnection(OVSDB_IP, OVSDB_PORT)
        resp = objConnection.get_response(OVSDB_IP, OVSDB_PORT,
                                          "Physical_Port")
        count = resp.count('_uuid')
        try:
            self.assertIn(str(seg_id[0]), resp)
        except Exception:
            raise lib_exc.NotFound(
                "Segmentation ID not found in Physical Port table")
        objConnection.stop("true")

        # Check Physical_Locator
        objConnection = ovsdb_connections.OVSDBConnection(OVSDB_IP, OVSDB_PORT)
        resp = objConnection.get_response(OVSDB_IP, OVSDB_PORT,
                                          "Physical_Locator")
        count = resp.count('_uuid')
        port_str = str(port)
        count_port = port_str.count('fixed_ips')
        net_node_host = []
        compute_node_host = []
        # Extracting unique Network node host name and Compute host name
        for i in range(count_port):
            net_id = port['ports'][i]['network_id']
            device_owner = port['ports'][i]['device_owner']
            if net_id == network_id_1 and device_owner == 'network:dhcp':
                if port['ports'][i]['binding:host_id'] not in net_node_host:
                    net_node_host.append(port['ports'][i]['binding:host_id'])
            if port['ports'][i]['device_owner'] == 'compute:None':
                if port['ports'][i]['binding:host_id'] not in net_node_host:
                    compute_node_host.append(
                        port['ports'][i]['binding:host_id'])
        ip_SW = CONF.network.l2gw_switch_ip
        host_and_ip = CONF.network.hosts
        list_ = host_and_ip.split(', ')
        host_ip_dict = {}
        for i in list_:
            sub_list = i.split(':')
            host = sub_list[0]
            ip = sub_list[1]
            host_ip_dict.update({host: ip})
        for net_node in net_node_host:
            ip_NN = host_ip_dict[net_node]
            try:
                self.assertIn(ip_NN, resp)
            except Exception:
                raise lib_exc.NotFound(
                    "Network Node IP not found in Physical Locator table")
        for compute_node in compute_node_host:
            ip_CN = host_ip_dict[compute_node]
            try:
                self.assertIn(ip_CN, resp)
            except Exception:
                raise lib_exc.NotFound(
                    "Compute Node IP not found in Physical Locator table")
        try:
            self.assertIn(ip_SW, resp)
        except Exception:
            raise lib_exc.NotFound(
                "Switch IP not found in Physical Locator table")
        objConnection.stop("true")

        # Check Ucast_macs_Remote
        objConnection = ovsdb_connections.OVSDBConnection(OVSDB_IP, OVSDB_PORT)
        resp = objConnection.get_response(OVSDB_IP, OVSDB_PORT,
                                          "Ucast_Macs_Remote")
        ip_mac_dict = {}
        count_uuid = resp.count('_uuid')
        resp_dec = json.loads(resp)
        for i in range(count_port):
            mac = port['ports'][i]['mac_address']
            ip = port['ports'][i]['fixed_ips'][0]['ip_address']
            ip_mac_dict.update({mac: ip})
        try:
            for key, value in ip_mac_dict.iteritems():
                row = objConnection.find_row(key, count_uuid, resp_dec)
                self.assertIn(value, row)
        except Exception:
            raise lib_exc.NotFound(
                "MAC & its port not found in UCast MAC Remote table")
        objConnection.stop("true")