def test_list_resources(self, mokc_image_list): plugin = ImageProtectablePlugin(self._context) mokc_image_list.return_value = \ [ image_info(id='123', name='name123', owner='abcd'), image_info(id='456', name='name456', owner='efgh'), ] self.assertEqual(plugin.list_resources(self._context), [ resource.Resource( type=constants.IMAGE_RESOURCE_TYPE, id='123', name='name123'), resource.Resource( type=constants.IMAGE_RESOURCE_TYPE, id='456', name='name456') ])
def test_graph_serialize(self): resource_a = resource.Resource('server', 0, 'a', {'name': 'a'}) resource_b = resource.Resource('volume', 1, 'b', {'name': 'b'}) test_base = {resource_a: [resource_b], resource_b: []} test_graph = graph.build_graph(test_base.keys(), test_base.__getitem__) self.assertIn(graph.serialize_resource_graph(test_graph), [ '[{"0x1": ["server", 0, "a", {"name": "a"}], ' '"0x0": ["volume", 1, "b", {"name": "b"}]}, ' '[["0x1", ["0x0"]]]]', '[{"0x0": ["volume", 1, "b", {"name": "b"}], ' '"0x1": ["server", 0, "a", {"name": "a"}]}, ' '[["0x1", ["0x0"]]]]' ])
def _get_dependent_resources_by_server(self, context, parent_resource): def _is_attached_to(vol): if parent_resource.type == constants.SERVER_RESOURCE_TYPE: return any([ s.get('server_id') == parent_resource.id for s in vol.attachments ]) if parent_resource.type == constants.PROJECT_RESOURCE_TYPE: return getattr( vol, 'os-vol-tenant-attr:tenant_id') == parent_resource.id try: volumes = self._client(context).volumes.list(detailed=True) except Exception as e: LOG.exception("List all detailed volumes from cinder failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [ resource.Resource( type=self._SUPPORT_RESOURCE_TYPE, id=vol.id, name=vol.name, extra_info={'availability_zone': vol.availability_zone}) for vol in volumes if _is_attached_to(vol) ]
def list_resources(self, context, parameters=None): # TODO(yuvalbr) handle admin context for multiple projects? return [ resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=context.project_id, name=context.project_name) ]
def _get_dependent_resources_by_server(self, context, parent_resource): try: # get metadata about network from neutron net_client = self._neutron_client(context) network_infos = net_client.list_networks().get('networks') neutron_networks = {network["id"] for network in network_infos} # get interface info from server nova_networks = set() serverid = parent_resource.id nova_client = ClientFactory.create_client("nova", context) interface_list = nova_client.servers.interface_list(serverid) # check net_id in interface for iface in interface_list: net_id = iface.net_id if net_id not in nova_networks: nova_networks.add(net_id) # get the exsited networks valid_networks = nova_networks.intersection(neutron_networks) if valid_networks: return [ resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=self._get_network_id(), name="Network Topology") ] return [] except Exception as e: LOG.exception("List all interfaces from nova failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e))
def show_resource(self, context, resource_id, parameters=None): try: if not parameters: raise name = parameters.get("name", None) if ":" in name: pod_namespace, pod_name = name.split(":") else: pod_namespace = self.namespace pod_name = name pod = self._client(context).read_namespaced_pod( pod_name, pod_namespace) except Exception as e: LOG.exception("Show a summary pod from kubernetes failed.") raise exception.ProtectableResourceNotFound( id=resource_id, type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: if pod.status.phase in INVALID_POD_STATUS: raise exception.ProtectableResourceInvalidStatus( id=resource_id, type=self._SUPPORT_RESOURCE_TYPE, status=pod.status.phase) return resource.Resource( type=self._SUPPORT_RESOURCE_TYPE, id=uuid.uuid5(uuid.NAMESPACE_OID, "%s:%s" % (self.namespace, pod.metadata.name)), name="%s:%s" % (pod_namespace, pod.metadata.name), extra_info={'namespace': pod_namespace})
def test_show_resource(self, mock_image_get): image_info = namedtuple('image_info', field_names=['id', 'name']) plugin = ImageProtectablePlugin(self._context) mock_image_get.return_value = \ image_info(id='123', name='name123') self.assertEqual( plugin.show_resource(self._context, '123'), resource.Resource(type=constants.IMAGE_RESOURCE_TYPE, id='123', name='name123'))
def show_resource(self, context, resource_id, parameters=None): try: image = self._glance_client(context).images.get(resource_id) except Exception as e: LOG.exception(_LE("Show a image from glance failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=image.id, name=image.name)
def show_resource(self, context, resource_id, parameters=None): try: volume = self._client(context).volumes.get(resource_id) except Exception as e: LOG.exception(_LE("Show a summary volume " "from cinder failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=volume.id, name=volume.name)
def show_resource(self, context, resource_id, parameters=None): try: server = self._client(context).servers.get(resource_id) except Exception as e: LOG.exception(_LE("Show a server from nova failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=server.id, name=server.name)
def list_resources(self, context, parameters=None): try: servers = self._client(context).servers.list(detailed=False) except Exception as e: LOG.exception(_LE("List all servers from nova failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=server.id, name=server.name) for server in servers]
def list_resources(self, context, parameters=None): try: volumes = self._client(context).volumes.list(detailed=False) except Exception as e: LOG.exception(_LE("List all summary volumes " "from cinder failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=vol.id, name=vol.name) for vol in volumes]
def list_resources(self, context, parameters=None): try: shares = self._client(context).shares.list(detailed=True) except Exception as e: LOG.exception("List all summary shares from manila failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=share.id, name=share.name) for share in shares if share.status not in INVALID_SHARE_STATUS]
def list_resources(self, context, parameters=None): try: images = self._glance_client(context).images.list() except Exception as e: LOG.exception(_LE("List all images from glance failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [ resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=image.id, name=image.name) for image in images ]
def test_get_server_dependent_resources(self, mock_server_get, mock_image_get): vm = server_info(id='server1', type=constants.SERVER_RESOURCE_TYPE, name='nameserver1', image=dict(id='123', name='name123')) image = image_info(id='123', name='name123', owner='abcd') plugin = ImageProtectablePlugin(self._context) mock_server_get.return_value = vm mock_image_get.return_value = image self.assertEqual(plugin.get_dependent_resources(self._context, vm), [ resource.Resource( type=constants.IMAGE_RESOURCE_TYPE, id='123', name='name123') ])
def list_resources(self, context, parameters=None): try: instances = self._client(context).instances.list() except Exception as e: LOG.exception("List all database instances from trove failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [ resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=instance.id, name=instance.name) for instance in instances if instance.status not in INVALID_INSTANCE_STATUS ]
def _get_dependent_resources_by_project(self, context, parent_resource): try: images = self._glance_client(context).images.list() except Exception as e: LOG.exception(_LE("List all images from glance failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [ resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=image.id, name=image.name) for image in images if image.owner == parent_resource.id ]
def test_get_project_dependent_resources(self, mock_image_list): project = project_info(id='abcd', type=constants.PROJECT_RESOURCE_TYPE, name='nameabcd') plugin = ImageProtectablePlugin(self._context) mock_image_list.return_value = [ image_info('123', 'abcd', 'nameabcd', 'active'), image_info('456', 'efgh', 'nameefgh', 'active'), ] self.assertEqual( plugin.get_dependent_resources(self._context, project), [ resource.Resource(type=constants.IMAGE_RESOURCE_TYPE, name='nameabcd', id='123') ])
def get_dependent_resources(self, context, parent_resource): try: shares = self._client(context).shares.list() except Exception as e: LOG.exception("List all shares from manila failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=share.id, name=share.name) for share in shares if share.project_id == parent_resource.id and share.status not in INVALID_SHARE_STATUS]
def show_resource(self, context, resource_id, parameters=None): try: image = self._glance_client(context).images.get(resource_id) except Exception as e: LOG.exception("Show a image from glance failed.") raise exception.ProtectableResourceNotFound( id=resource_id, type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: if image.status in INVALID_IMAGE_STATUS: raise exception.ProtectableResourceInvalidStatus( id=image.id, type=self._SUPPORT_RESOURCE_TYPE, status=image.status) return resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=image.id, name=image.name)
def show_resource(self, context, resource_id, parameters=None): try: share = self._client(context).shares.get(resource_id) except Exception as e: LOG.exception("Show a summary share from manila failed.") raise exception.ProtectableResourceNotFound( id=resource_id, type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: if share.status in INVALID_SHARE_STATUS: raise exception.ProtectableResourceInvalidStatus( id=resource_id, type=self._SUPPORT_RESOURCE_TYPE, status=share.status) return resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=share.id, name=share.name)
def list_resources(self, context, parameters=None): try: volumes = self._client(context).volumes.list(detailed=True) except Exception as e: LOG.exception("List all summary volumes from cinder failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [ resource.Resource( type=self._SUPPORT_RESOURCE_TYPE, id=vol.id, name=vol.name, extra_info={'availability_zone': vol.availability_zone}) for vol in volumes if vol.status not in INVALID_VOLUME_STATUS ]
def list_resources(self, context, parameters=None): try: netclient = self._neutron_client(context) networks = netclient.list_networks( project_id=context.project_id).get('networks') except Exception as e: LOG.exception("List all summary networks from neutron failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: if networks: return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=self._get_network_id(), name="Network Topology")] return []
def _get_dependent_resources_by_pod(self, context, parent_resource): try: name = parent_resource.name pod_namespace, pod_name = name.split(":") pod = self._k8s_client(context).read_namespaced_pod( pod_name, pod_namespace) if not pod.spec.volumes: return [] mounted_vol_list = [] for volume in pod.spec.volumes: volume_pvc = volume.persistent_volume_claim volume_cinder = volume.cinder if volume_pvc: pvc_name = volume_pvc.claim_name pvc = self._k8s_client( context).read_namespaced_persistent_volume_claim( pvc_name, pod_namespace) pv_name = pvc.spec.volume_name if pv_name: pv = self._k8s_client(context).read_persistent_volume( pv_name) if pv.spec.cinder: mounted_vol_list.append(pv.spec.cinder.volume_id) elif volume_cinder: mounted_vol_list.append(volume_cinder.volume_id) except Exception as e: LOG.exception("Get mounted volumes from kubernetes " "pod failed.") raise exception.ProtectableResourceNotFound( id=parent_resource.id, type=parent_resource.type, reason=six.text_type(e)) try: volumes = self._client(context).volumes.list(detailed=True) except Exception as e: LOG.exception("List all detailed volumes from cinder failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [ resource.Resource( type=self._SUPPORT_RESOURCE_TYPE, id=vol.id, name=vol.name, extra_info={'availability_zone': vol.availability_zone}) for vol in volumes if (vol.id in mounted_vol_list) ]
def list_resources(self, context, parameters=None): try: pods = self._client(context).list_namespaced_pod(self.namespace) except Exception as e: LOG.exception("List all summary pods from kubernetes failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: return [ resource.Resource( type=self._SUPPORT_RESOURCE_TYPE, id=uuid.uuid5( uuid.NAMESPACE_OID, "%s:%s" % (self.namespace, pod.metadata.name)), name="%s:%s" % (self.namespace, pod.metadata.name), extra_info={'namespace': self.namespace}) for pod in pods.items if pod.status.phase not in INVALID_POD_STATUS ]
def _get_dependent_resources_by_project(self, context, parent_resource): try: tid = parent_resource.id netclient = self._neutron_client(context) networks = netclient.list_networks(tenant_id=tid).get('networks') if networks: return [ resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=self._get_network_id(), name="Network Topology") ] else: return [] except Exception as e: LOG.exception("List all summary networks from neutron failed.") raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e))
def test_list_resources(self, mock_client_list_networks): plugin = NetworkProtectablePlugin(self._context) fake_network_info = {'networks': [ {u'status': u'ACTIVE', u'description': u'', u'tenant_id': u'abcd', u'name': u'private'}, {u'status': u'ACTIVE', u'description': u'', u'name': u'ext_net', u'tenant_id': u'abcd'} ]} mock_client_list_networks.return_value = fake_network_info self.assertEqual(plugin.list_resources(self._context), [resource.Resource (type=constants.NETWORK_RESOURCE_TYPE, id='abcd', name="Network Topology")])
def show_resource(self, context, resource_id, parameters=None): try: volume = self._client(context).volumes.get(resource_id) except Exception as e: LOG.exception("Show a summary volume from cinder failed.") raise exception.ProtectableResourceNotFound( id=resource_id, type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) else: if volume.status in INVALID_VOLUME_STATUS: raise exception.ProtectableResourceInvalidStatus( id=resource_id, type=self._SUPPORT_RESOURCE_TYPE, status=volume.status) return resource.Resource( type=self._SUPPORT_RESOURCE_TYPE, id=volume.id, name=volume.name, extra_info={'availability_zone': volume.availability_zone})
def test_get_server_dependent_resources(self, mock_generate_session, mock_server_get, mock_image_get): vm = server_info(id='server1', type=constants.SERVER_RESOURCE_TYPE, name='nameserver1', image=dict(id='123', name='name123')) image = image_info(id='123', name='name123', owner='abcd', status='active') plugin = ImageProtectablePlugin(self._context) mock_generate_session.return_value = keystone_session.Session( auth=None) mock_server_get.return_value = vm mock_image_get.return_value = image self.assertEqual(plugin.get_dependent_resources(self._context, vm), [ resource.Resource(type=constants.IMAGE_RESOURCE_TYPE, id='123', name='name123', extra_info={'server_id': 'server1'}) ])
def _get_dependent_resources_by_server(self, context, parent_resource): try: server = self._nova_client(context).servers.get(parent_resource.id) except Exception as e: LOG.exception(_LE("List all server from nova failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) if not server.image: return [] try: image = self._glance_client(context).images.get(server.image['id']) except Exception as e: LOG.exception(_LE("Getting image from glance failed.")) raise exception.ListProtectableResourceFailed( type=self._SUPPORT_RESOURCE_TYPE, reason=six.text_type(e)) return [ resource.Resource(type=self._SUPPORT_RESOURCE_TYPE, id=server.image['id'], name=image.name) ]