Esempio n. 1
0
    def test_list_projects_v2(self):
        self.use_keystone_v2()
        project_one = self._get_project_data(v3=False)
        project_two = self._get_project_data(v3=False)
        project_list = [project_one, project_two]

        first_response = {'tenants': [project_one.json_response['tenant']]}
        second_response = {'tenants': [p.json_response['tenant']
                                       for p in project_list]}

        mock_uri = self.get_mock_url(
            service_type='identity', interface='admin', resource='tenants')

        self.register_uris([
            dict(method='GET', uri=mock_uri, status_code=200,
                 json=first_response),
            dict(method='GET', uri=mock_uri, status_code=200,
                 json=second_response)])

        self.assertEqual(
            self.cloud._normalize_projects(
                meta.obj_list_to_munch(first_response['tenants'])),
            self.cloud.list_projects())
        self.assertEqual(
            self.cloud._normalize_projects(
                meta.obj_list_to_munch(first_response['tenants'])),
            self.cloud.list_projects())
        # invalidate the list_projects cache
        self.cloud.list_projects.invalidate(self.cloud)
        # ensure the new values are now retrieved
        self.assertEqual(
            self.cloud._normalize_projects(
                meta.obj_list_to_munch(second_response['tenants'])),
            self.cloud.list_projects())
        self.assert_calls()
Esempio n. 2
0
    def test_list_projects_v2(self):
        self.use_keystone_v2()
        project_one = self._get_project_data(v3=False)
        project_two = self._get_project_data(v3=False)
        project_list = [project_one, project_two]

        first_response = {'tenants': [project_one.json_response['tenant']]}
        second_response = {'tenants': [p.json_response['tenant']
                                       for p in project_list]}

        mock_uri = self.get_mock_url(
            service_type='identity', interface='admin', resource='tenants')

        self.register_uris([
            dict(method='GET', uri=mock_uri, status_code=200,
                 json=first_response),
            dict(method='GET', uri=mock_uri, status_code=200,
                 json=second_response)])

        self.assertEqual(
            self.cloud._normalize_projects(
                meta.obj_list_to_munch(first_response['tenants'])),
            self.cloud.list_projects())
        self.assertEqual(
            self.cloud._normalize_projects(
                meta.obj_list_to_munch(first_response['tenants'])),
            self.cloud.list_projects())
        # invalidate the list_projects cache
        self.cloud.list_projects.invalidate(self.cloud)
        # ensure the new values are now retrieved
        self.assertEqual(
            self.cloud._normalize_projects(
                meta.obj_list_to_munch(second_response['tenants'])),
            self.cloud.list_projects())
        self.assert_calls()
Esempio n. 3
0
    def _munch_response(self, response, result_key=None, error_message=None):
        exc.raise_from_response(response, error_message=error_message)

        if not response.content:
            # This doens't have any content
            return self._log_request_id(response)

        # Some REST calls do not return json content. Don't decode it.
        if 'application/json' not in response.headers.get('Content-Type'):
            return self._log_request_id(response)

        try:
            result_json = response.json()
        except Exception:
            return self._log_request_id(response)

        # Note(rods): this is just a temporary step needed until we
        # don't update all the other REST API calls
        if isinstance(result_json, dict):
            for key in ['volumes', 'volume', 'volumeAttachment', 'backups',
                        'volume_types', 'volume_type_access', 'snapshots',
                        'network', 'networks', 'subnet', 'subnets',
                        'router', 'routers', 'floatingip', 'floatingips',
                        'floating_ip', 'floating_ips', 'port', 'ports']:
                if key in result_json.keys():
                    self._log_request_id(response)
                    return result_json

        if isinstance(result_json, list):
            self._log_request_id(response)
            return meta.obj_list_to_munch(result_json)

        result = None
        if isinstance(result_json, dict):
            # Wrap the keys() call in list() because in python3 keys returns
            # a "dict_keys" iterator-like object rather than a list
            json_keys = list(result_json.keys())
            if len(json_keys) > 1 and result_key:
                result = result_json[result_key]
            elif len(json_keys) == 1:
                result = result_json[json_keys[0]]
        if result is None:
            # Passthrough the whole body - sometimes (hi glance) things
            # come through without a top-level container. Also, sometimes
            # you need to deal with pagination
            result = result_json

        self._log_request_id(response, result)

        if isinstance(result, list):
            return meta.obj_list_to_munch(result)
        elif isinstance(result, dict):
            return meta.obj_to_munch(result)
        return result
Esempio n. 4
0
def normalize_roles(roles):
    """Normalize Identity roles."""
    ret = [dict(
        id=role.get('id'),
        name=role.get('name'),
    ) for role in roles]
    return meta.obj_list_to_munch(ret)
Esempio n. 5
0
def normalize_roles(roles):
    """Normalize Identity roles."""
    ret = [
        dict(
            id=role.get('id'),
            name=role.get('name'),
        ) for role in roles
    ]
    return meta.obj_list_to_munch(ret)
Esempio n. 6
0
def normalize_domains(domains):
    ret = [
        dict(
            id=domain.get('id'),
            name=domain.get('name'),
            description=domain.get('description'),
            enabled=domain.get('enabled'),
        ) for domain in domains
    ]
    return meta.obj_list_to_munch(ret)
Esempio n. 7
0
def normalize_domains(domains):
    ret = [
        dict(
            id=domain.get('id'),
            name=domain.get('name'),
            description=domain.get('description'),
            enabled=domain.get('enabled'),
        ) for domain in domains
    ]
    return meta.obj_list_to_munch(ret)
Esempio n. 8
0
    def test_list_nics(self, mock_client):
        port1 = fakes.FakeMachinePort(1, "aa:bb:cc:dd", "node1")
        port2 = fakes.FakeMachinePort(2, "dd:cc:bb:aa", "node2")
        port_list = [port1, port2]
        port_dict_list = meta.obj_list_to_munch(port_list)

        mock_client.port.list.return_value = port_list
        nics = self.op_cloud.list_nics()

        self.assertTrue(mock_client.port.list.called)
        self.assertEqual(port_dict_list, nics)
Esempio n. 9
0
def normalize_groups(domains):
    """Normalize Identity groups."""
    ret = [
        dict(
            id=domain.get('id'),
            name=domain.get('name'),
            description=domain.get('description'),
            domain_id=domain.get('domain_id'),
        ) for domain in domains
    ]
    return meta.obj_list_to_munch(ret)
Esempio n. 10
0
def normalize_groups(domains):
    """Normalize Identity groups."""
    ret = [
        dict(
            id=domain.get('id'),
            name=domain.get('name'),
            description=domain.get('description'),
            domain_id=domain.get('domain_id'),
        ) for domain in domains
    ]
    return meta.obj_list_to_munch(ret)
Esempio n. 11
0
    def test_list_nics(self, mock_client):
        port1 = fakes.FakeMachinePort(1, "aa:bb:cc:dd", "node1")
        port2 = fakes.FakeMachinePort(2, "dd:cc:bb:aa", "node2")
        port_list = [port1, port2]
        port_dict_list = meta.obj_list_to_munch(port_list)

        mock_client.port.list.return_value = port_list
        nics = self.op_cloud.list_nics()

        self.assertTrue(mock_client.port.list.called)
        self.assertEqual(port_dict_list, nics)
Esempio n. 12
0
    def test_obj_list_to_munch(self):
        """Test conversion of a list of objects to a list of dictonaries"""
        class obj0(object):
            value = 0

        class obj1(object):
            value = 1

        list = [obj0, obj1]
        new_list = meta.obj_list_to_munch(list)
        self.assertEqual(new_list[0]['value'], 0)
        self.assertEqual(new_list[1]['value'], 1)
Esempio n. 13
0
    def test_obj_list_to_munch(self):
        """Test conversion of a list of objects to a list of dictonaries"""
        class obj0(object):
            value = 0

        class obj1(object):
            value = 1

        list = [obj0, obj1]
        new_list = meta.obj_list_to_munch(list)
        self.assertEqual(new_list[0]['value'], 0)
        self.assertEqual(new_list[1]['value'], 1)
Esempio n. 14
0
    def wait(self, raw=False):
        super(RequestTask, self).wait()

        if raw:
            # Do NOT convert the result.
            return self._result

        if _is_listlike(self._result):
            return meta.obj_list_to_munch(
                self._result, request_id=self._request_id)
        elif _is_objlike(self._result):
            return meta.obj_to_munch(self._result, request_id=self._request_id)
        return self._result
Esempio n. 15
0
    def wait(self, raw=False):
        super(RequestTask, self).wait()

        if raw:
            # Do NOT convert the result.
            return self._result

        if _is_listlike(self._result):
            return meta.obj_list_to_munch(self._result,
                                          request_id=self._request_id)
        elif _is_objlike(self._result):
            return meta.obj_to_munch(self._result, request_id=self._request_id)
        return self._result
Esempio n. 16
0
def normalize_users(users):
    ret = [
        dict(id=user.get('id'),
             email=user.get('email'),
             name=user.get('name'),
             username=user.get('username'),
             default_project_id=user.get('default_project_id',
                                         user.get('tenantId')),
             domain_id=user.get('domain_id'),
             enabled=user.get('enabled'),
             description=user.get('description')) for user in users
    ]
    return meta.obj_list_to_munch(ret)
Esempio n. 17
0
def normalize_users(users):
    ret = [
        dict(
            id=user.get('id'),
            email=user.get('email'),
            name=user.get('name'),
            username=user.get('username'),
            default_project_id=user.get('default_project_id',
                                        user.get('tenantId')),
            domain_id=user.get('domain_id'),
            enabled=user.get('enabled'),
            description=user.get('description')
        ) for user in users
    ]
    return meta.obj_list_to_munch(ret)
Esempio n. 18
0
 def test_list_volume_backups(self):
     backup = {'id': '6ff16bdf-44d5-4bf9-b0f3-687549c76414',
               'status': 'available'}
     search_opts = {'status': 'available'}
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['backups', 'detail'],
                  qs_elements=['='.join(i) for i in search_opts.items()]),
              json={"backups": [backup]})])
     result = self.cloud.list_volume_backups(True, search_opts)
     self.assertEqual(len(result), 1)
     self.assertEqual(
         meta.obj_list_to_munch([backup]),
         result)
     self.assert_calls()
Esempio n. 19
0
 def test_search_stacks(self):
     fake_stacks = [
         self.stack,
         fakes.make_fake_stack(self.getUniqueString('id'),
                               self.getUniqueString('name'))
     ]
     self.register_uris([
         dict(method='GET',
              uri='{endpoint}/stacks'.format(
                  endpoint=fakes.ORCHESTRATION_ENDPOINT),
              json={"stacks": fake_stacks}),
     ])
     stacks = self.cloud.search_stacks()
     self.assertEqual(
         self.cloud._normalize_stacks(meta.obj_list_to_munch(fake_stacks)),
         stacks)
     self.assert_calls()
Esempio n. 20
0
 def test_search_volume_backups(self):
     name = 'Volume1'
     vol1 = {'name': name, 'availability_zone': 'az1'}
     vol2 = {'name': name, 'availability_zone': 'az1'}
     vol3 = {'name': 'Volume2', 'availability_zone': 'az2'}
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['backups', 'detail']),
              json={"backups": [vol1, vol2, vol3]})])
     result = self.cloud.search_volume_backups(
         name, {'availability_zone': 'az1'})
     self.assertEqual(len(result), 2)
     self.assertEqual(
         meta.obj_list_to_munch([vol1, vol2]),
         result)
     self.assert_calls()
Esempio n. 21
0
 def test_search_stacks(self):
     fake_stacks = [
         self.stack,
         fakes.make_fake_stack(
             self.getUniqueString('id'),
             self.getUniqueString('name'))
     ]
     self.register_uris([
         dict(method='GET',
              uri='{endpoint}/stacks'.format(
                  endpoint=fakes.ORCHESTRATION_ENDPOINT),
              json={"stacks": fake_stacks}),
     ])
     stacks = self.cloud.search_stacks()
     self.assertEqual(
         self.cloud._normalize_stacks(meta.obj_list_to_munch(fake_stacks)),
         stacks)
     self.assert_calls()
Esempio n. 22
0
def normalize_keystone_services(services):
    """Normalize the structure of keystone services

    In keystone v2, there is a field called "service_type". In v3, it's
    "type". Just make the returned dict have both.

    :param list services: A list of keystone service dicts

    :returns: A list of normalized dicts.
    """
    ret = []
    for service in services:
        service_type = service.get('type', service.get('service_type'))
        new_service = {
            'id': service['id'],
            'name': service['name'],
            'description': service.get('description', None),
            'type': service_type,
            'service_type': service_type,
            'enabled': service['enabled']
        }
        ret.append(new_service)
    return meta.obj_list_to_munch(ret)
Esempio n. 23
0
def normalize_keystone_services(services):
    """Normalize the structure of keystone services

    In keystone v2, there is a field called "service_type". In v3, it's
    "type". Just make the returned dict have both.

    :param list services: A list of keystone service dicts

    :returns: A list of normalized dicts.
    """
    ret = []
    for service in services:
        service_type = service.get('type', service.get('service_type'))
        new_service = {
            'id': service['id'],
            'name': service['name'],
            'description': service.get('description', None),
            'type': service_type,
            'service_type': service_type,
            'enabled': service['enabled']
        }
        ret.append(new_service)
    return meta.obj_list_to_munch(ret)
Esempio n. 24
0
    def _munch_response(self, response, result_key=None, error_message=None):
        exc.raise_from_response(response, error_message=error_message)

        if not response.content:
            # This doens't have any content
            return self._log_request_id(response)

        # Some REST calls do not return json content. Don't decode it.
        if 'application/json' not in response.headers.get('Content-Type'):
            return self._log_request_id(response)

        try:
            result_json = response.json()
        except Exception:
            return self._log_request_id(response)

        # Note(rods): this is just a temporary step needed until we
        # don't update all the other REST API calls
        if isinstance(result_json, dict):
            for key in ['volumes', 'volume', 'volumeAttachment', 'backups',
                        'volume_types', 'volume_type_access', 'snapshots',
                        'network', 'networks', 'subnet', 'subnets',
                        'router', 'routers', 'floatingip', 'floatingips',
                        'floating_ip', 'floating_ips', 'port', 'ports',
                        'rule_types', 'policy', 'policies',
                        'stack', 'stacks', 'zones', 'events',
                        'security_group', 'security_groups',
                        'security_group_rule', 'security_group_rules',
                        'users', 'user', 'projects', 'tenants',
                        'project', 'tenant', 'servers', 'server',
                        'flavor', 'flavors', 'baymodels', 'aggregate',
                        'aggregates', 'availabilityZoneInfo',
                        'flavor_access', 'output', 'server_groups', 'domain',
                        'domains']:
                if key in result_json.keys():
                    self._log_request_id(response)
                    return result_json

        if isinstance(result_json, list):
            self._log_request_id(response)
            return meta.obj_list_to_munch(result_json)

        result = None
        if isinstance(result_json, dict):
            # Wrap the keys() call in list() because in python3 keys returns
            # a "dict_keys" iterator-like object rather than a list
            json_keys = list(result_json.keys())
            if len(json_keys) > 1 and result_key:
                result = result_json[result_key]
            elif len(json_keys) == 1:
                result = result_json[json_keys[0]]
        if result is None:
            # Passthrough the whole body - sometimes (hi glance) things
            # come through without a top-level container. Also, sometimes
            # you need to deal with pagination
            result = result_json

        self._log_request_id(response, result)

        if isinstance(result, list):
            return meta.obj_list_to_munch(result)
        elif isinstance(result, dict):
            return meta.obj_to_munch(result)
        return result