def list(cls, request, associated=None, maintenance=None): """Return a list of Nodes :param request: request object :type request: django.http.HttpRequest :param associated: should we also retrieve all Nodes, only those associated with an Instance, or only those not associated with an Instance? :type associated: bool :param maintenance: should we also retrieve all Nodes, only those in maintenance mode, or those which are not in maintenance mode? :type maintenance: bool :return: list of Nodes, or an empty list if there are none :rtype: list of tuskar_ui.api.node.Node """ nodes = ironicclient(request).node.list(associated=associated, maintenance=maintenance) if associated is None or associated: servers = nova.server_list(request)[0] servers_dict = utils.list_to_dict(servers) nodes_with_instance = [] for n in nodes: server = servers_dict.get(n.instance_uuid, None) nodes_with_instance.append(cls(n, instance=server, request=request)) return [cls.get(request, node.uuid) for node in nodes_with_instance] return [cls.get(request, node.uuid) for node in nodes]
def resources(self, with_joins=True): """Return a list of all Resources associated with the Stack :param with_joins: should we also retrieve objects associated with each retrieved Resource? :type with_joins: bool :return: list of all Resources or an empty list if there are none :rtype: list of tuskar_ui.api.heat.Resource """ resources = [ r for r in TEST_DATA.heatclient_resources.list() if r.stack_id == self.id ] if not with_joins: return [ Resource(r, request=self._request, stack=self) for r in resources ] nodes_dict = utils.list_to_dict(node.Node.list(self._request, associated=True), key_attribute='instance_uuid') joined_resources = [] for r in resources: joined_resources.append( Resource(r, node=nodes_dict.get(r.physical_resource_id, None), request=self._request, stack=self)) # TODO(lsmola) I want just resources with nova instance # this could be probably filtered a better way, investigate return [r for r in joined_resources if r.node is not None]
def resources(self, with_joins=True): """Return a list of all Resources associated with the Stack :param with_joins: should we also retrieve objects associated with each retrieved Resource? :type with_joins: bool :return: list of all Resources or an empty list if there are none :rtype: list of tuskar_ui.api.heat.Resource """ resources = [r for r in TEST_DATA.heatclient_resources.list() if r.stack_id == self.id] if not with_joins: return [Resource(r, request=self._request, stack=self) for r in resources] nodes_dict = utils.list_to_dict(node.Node.list(self._request, associated=True), key_attribute='instance_uuid') joined_resources = [] for r in resources: joined_resources.append( Resource(r, node=nodes_dict.get(r.physical_resource_id, None), request=self._request, stack=self)) # TODO(lsmola) I want just resources with nova instance # this could be probably filtered a better way, investigate return [r for r in joined_resources if r.node is not None]
def list(cls, request, associated=None): nodes = NodeClient(request).node_class.list( request, associated=associated) if associated is None or associated: servers = TEST_DATA.novaclient_servers.list() servers_dict = utils.list_to_dict(servers) nodes_with_instance = [] for n in nodes: server = servers_dict.get(n.instance_uuid, None) nodes_with_instance.append(cls(n, instance=server, request=request)) return nodes_with_instance else: return [cls(node, request=request) for node in nodes]
def list(cls, request, associated=None, maintenance=None): if NodeClient.ironic_enabled(request): nodes = NodeClient(request).node_class.list( request, associated=associated, maintenance=maintenance) else: nodes = NodeClient(request).node_class.list( request, associated=associated) if associated is None or associated: servers = nova.server_list(request)[0] servers_dict = utils.list_to_dict(servers) nodes_with_instance = [] for n in nodes: server = servers_dict.get(n.instance_uuid, None) nodes_with_instance.append(cls(n, instance=server, request=request)) return nodes_with_instance else: return [cls(node, request=request) for node in nodes]
def resources(self, with_joins=True, role=None): """Return list of OS::Nova::Server Resources Return list of OS::Nova::Server Resources associated with the Stack and which are associated with a Role :param with_joins: should we also retrieve objects associated with each retrieved Resource? :type with_joins: bool :return: list of all Resources or an empty list if there are none :rtype: list of tuskar_ui.api.heat.Resource """ if role: roles = [role] else: roles = self.plan.role_list resource_dicts = [] # A provider resource is deployed as a nested stack, so we have to # drill down and retrieve those that match a tuskar role for role in roles: resource_group_name = role.provider_resource_group_name try: resource_group = heat.resource_get(self._request, self.id, resource_group_name) group_resources = heat.resources_list( self._request, resource_group.physical_resource_id) for group_resource in group_resources: if not group_resource.physical_resource_id: # Skip groups who has no physical resource. continue nova_resources = heat.resources_list( self._request, group_resource.physical_resource_id) resource_dicts.extend([{ "resource": resource, "role": role } for resource in nova_resources]) except heatclient.exc.HTTPNotFound: pass if not with_joins: return [ Resource(rd['resource'], request=self._request, stack=self, role=rd['role']) for rd in resource_dicts ] nodes_dict = utils.list_to_dict(node.Node.list(self._request, associated=True), key_attribute='instance_uuid') joined_resources = [] for rd in resource_dicts: resource = rd['resource'] joined_resources.append( Resource(resource, node=nodes_dict.get(resource.physical_resource_id, None), request=self._request, stack=self, role=rd['role'])) # TODO(lsmola) I want just resources with nova instance # this could be probably filtered a better way, investigate return [r for r in joined_resources if r.node is not None]
def resources(self, with_joins=True, role=None): """Return list of OS::Nova::Server Resources Return list of OS::Nova::Server Resources associated with the Stack and which are associated with a Role :param with_joins: should we also retrieve objects associated with each retrieved Resource? :type with_joins: bool :return: list of all Resources or an empty list if there are none :rtype: list of tuskar_ui.api.heat.Resource """ if role: roles = [role] else: roles = self.plan.role_list resource_dicts = [] # A provider resource is deployed as a nested stack, so we have to # drill down and retrieve those that match a tuskar role for role in roles: resource_group_name = role.provider_resource_group_name try: resource_group = heat.resource_get(self._request, self.id, resource_group_name) group_resources = heat.resources_list( self._request, resource_group.physical_resource_id) for group_resource in group_resources: if not group_resource.physical_resource_id: # Skip groups who has no physical resource. continue nova_resources = heat.resources_list( self._request, group_resource.physical_resource_id) resource_dicts.extend([{"resource": resource, "role": role} for resource in nova_resources]) except heatclient.exc.HTTPNotFound: pass if not with_joins: return [Resource(rd['resource'], request=self._request, stack=self, role=rd['role']) for rd in resource_dicts] nodes_dict = utils.list_to_dict(node.Node.list(self._request, associated=True), key_attribute='instance_uuid') joined_resources = [] for rd in resource_dicts: resource = rd['resource'] joined_resources.append( Resource(resource, node=nodes_dict.get(resource.physical_resource_id, None), request=self._request, stack=self, role=rd['role'])) # TODO(lsmola) I want just resources with nova instance # this could be probably filtered a better way, investigate return [r for r in joined_resources if r.node is not None]
def test_list_to_dict(self): Item = collections.namedtuple('Item', 'id') ret = utils.list_to_dict([Item('foo'), Item('bar'), Item('bar')]) self.assertEqual(ret, {'foo': Item('foo'), 'bar': Item('bar')})
def test_list_to_dict(self): Item = collections.namedtuple('Item', 'id') ret = utils.list_to_dict([Item('foo'), Item('bar'), Item('bar')]) self.assertEqual(ret, {'foo': Item('foo'), 'bar': Item('bar')})