def test_format_stack_resource(self): res = self.stack['generic1'] resource_keys = set(( rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_METADATA, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) resource_details_keys = resource_keys.union(set( (rpc_api.RES_DESCRIPTION, rpc_api.RES_METADATA))) formatted = api.format_stack_resource(res, True) self.assertEqual(resource_details_keys, set(formatted.keys())) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(formatted.keys()))
def test_format_stack_resource_with_props(self, mock_format_props): mock_format_props.return_value = 'formatted_res_props' res = self.stack['generic1'] formatted = api.format_stack_resource(res, True, with_props=True) formatted_props = formatted[rpc_api.RES_SCHEMA_PROPERTIES] self.assertEqual('formatted_res_props', formatted_props)
def list_stack_resources(self, cnxt, stack_identity): s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) return [api.format_stack_resource(resource, detail=False) for resource in stack.values()]
def test_format_stack_resource_with_attributes(self, mock_format_attrs): mock_format_attrs.return_value = 'formatted_resource_attrs' res = self.stack['generic1'] formatted = api.format_stack_resource(res, True, with_attr=['a', 'b']) formatted_attrs = formatted[rpc_api.RES_SCHEMA_ATTRIBUTES] self.assertEqual('formatted_resource_attrs', formatted_attrs)
def describe_stack_resources(self, context, stack_identity, physical_resource_id, logical_resource_id): if stack_identity is not None: s = self._get_stack(context, stack_identity) else: rs = db_api.resource_get_by_physical_resource_id(context, physical_resource_id) if not rs: msg = "The specified PhysicalResourceId doesn't exist" raise AttributeError(msg) s = rs.stack if not s: raise AttributeError("The specified stack doesn't exist") stack = parser.Stack.load(context, s.id) if logical_resource_id is not None: name_match = lambda r: r.name == logical_resource_id else: name_match = lambda r: True return [api.format_stack_resource(resource) for resource in stack if resource.id is not None and name_match(resource)]
def test_format_stack_resource_with_parent_stack(self): res = self.stack['generic1'] res.stack.parent_resource = mock.Mock() res.stack.parent_resource.name = 'foobar' formatted = api.format_stack_resource(res, False) self.assertEqual('foobar', formatted[rpc_api.RES_PARENT_RESOURCE])
def list_stack_resources(self, cnxt, stack_identity, nested_depth=0): s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) depth = min(nested_depth, cfg.CONF.max_nested_stack_depth) return [api.format_stack_resource(resource, detail=False) for resource in stack.iter_resources(depth)]
def list_stack_resources(self, context, stack_identity): s = self._get_stack(context, stack_identity) stack = parser.Stack.load(context, s.id) return [api.format_stack_resource(resource) for resource in stack if resource.id is not None]
def list_stack_resources(self, cnxt, stack_identity): s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) return [api.format_stack_resource(resource, detail=False) for resource in stack]
def describe_stack_resources(self, context, stack_identity, physical_resource_id, logical_resource_id): if stack_identity is not None: s = self._get_stack(context, stack_identity) else: rs = db_api.resource_get_by_physical_resource_id( context, physical_resource_id) if not rs: msg = "The specified PhysicalResourceId doesn't exist" raise AttributeError(msg) s = rs.stack if not s: raise AttributeError("The specified stack doesn't exist") stack = parser.Stack.load(context, s.id) if logical_resource_id is not None: name_match = lambda r: r.name == logical_resource_id else: name_match = lambda r: True return [ api.format_stack_resource(resource) for resource in stack if resource.id is not None and name_match(resource) ]
def test_format_stack_resource_with_nested_stack(self): res = self.stack['generic1'] nested_id = {'foo': 'bar'} res.nested = mock.Mock() res.nested.return_value.identifier.return_value = nested_id formatted = api.format_stack_resource(res, False) self.assertEqual(nested_id, formatted[rpc_api.RES_NESTED_STACK_ID])
def test_format_stack_resource_with_nested_stack(self): res = self.stack['generic4'] nested_id = {'foo': 'bar'} res.nested = mock.Mock() res.nested.return_value.identifier.return_value = nested_id formatted = api.format_stack_resource(res, False) self.assertEqual(nested_id, formatted[rpc_api.RES_NESTED_STACK_ID])
def test_format_stack_resource_has_been_deleted(self): # assume the stack and resource have been deleted, # to test the resource's action inherit from stack self.stack.state_set(self.stack.DELETE, self.stack.COMPLETE, 'test_delete') res = self.stack['generic1'] formatted = api.format_stack_resource(res, False) self.assertEqual(res.DELETE, formatted[rpc_api.RES_ACTION])
def describe_stack_resources(self, cnxt, stack_identity, resource_name): s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) return [api.format_stack_resource(resource) for name, resource in stack.iteritems() if resource_name is None or name == resource_name]
def list_stack_resources(self, context, stack_identity): s = self._get_stack(context, stack_identity) stack = parser.Stack.load(context, stack=s) return [ api.format_stack_resource(resource, detail=False) for resource in stack if resource.id is not None ]
def test_format_stack_resource(self): res = self.stack['generic1'] resource_keys = set( (rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_METADATA, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) resource_details_keys = resource_keys.union( set((rpc_api.RES_DESCRIPTION, rpc_api.RES_METADATA))) formatted = api.format_stack_resource(res, True) self.assertEqual(resource_details_keys, set(formatted.keys())) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(formatted.keys()))
def test_format_stack_resource_has_been_rollback(self): # Rollback a stack, the resources perhaps have not been # created yet or have been deleted when rollback. # To test the resource's action inherit from stack self.stack.state_set(self.stack.ROLLBACK, self.stack.COMPLETE, 'test_rollback') res = self.stack['generic1'] formatted = api.format_stack_resource(res, False) self.assertEqual(res.ROLLBACK, formatted[rpc_api.RES_ACTION])
def test_format_stack_resource_with_nested_stack_empty(self): res = self.stack['generic1'] nested_id = {'foo': 'bar'} res.nested = mock.MagicMock() res.nested.return_value.identifier.return_value = nested_id res.nested.return_value.__len__.return_value = 0 formatted = api.format_stack_resource(res, False) res.nested.return_value.identifier.assert_called_once_with() self.assertEqual(nested_id, formatted[rpc_api.RES_NESTED_STACK_ID])
def test_format_stack_resource_with_nested_stack_empty(self): res = self.stack['generic4'] nested_id = {'foo': 'bar'} res.nested = mock.MagicMock() res.nested.return_value.identifier.return_value = nested_id res.nested.return_value.__len__.return_value = 0 formatted = api.format_stack_resource(res, False) res.nested.return_value.identifier.assert_called_once_with() self.assertEqual(nested_id, formatted[rpc_api.RES_NESTED_STACK_ID])
def describe_stack_resources(self, cnxt, stack_identity, resource_name): s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) if resource_name is not None: name_match = lambda r: r.name == resource_name else: name_match = lambda r: True return [api.format_stack_resource(resource) for resource in stack if name_match(resource)]
def list_stack_resources(self, context, stack_name): auth.authenticate(context) s = db_api.stack_get_by_name(context, stack_name) if not s: raise AttributeError('Unknown stack name') stack = parser.Stack.load(context, s.id) return [api.format_stack_resource(resource) for resource in stack if resource.id is not None]
def describe_stack_resource(self, context, stack_identity, resource_name): s = self._get_stack(context, stack_identity) stack = parser.Stack.load(context, s.id) if resource_name not in stack: raise AttributeError('Unknown resource name') resource = stack[resource_name] if resource.id is None: raise AttributeError('Resource not created') return api.format_stack_resource(stack[resource_name])
def describe_stack_resource(self, context, stack_identity, resource_name): s = self._get_stack(context, stack_identity) stack = parser.Stack.load(context, stack=s) if resource_name not in stack: raise AttributeError('Unknown resource name') resource = stack[resource_name] if resource.id is None: raise AttributeError('Resource not created') return api.format_stack_resource(stack[resource_name])
def describe_stack_resource(self, context, stack_identity, resource_name): s = self._get_stack(context, stack_identity) stack = parser.Stack.load(context, stack=s) if resource_name not in stack: raise exception.ResourceNotFound(resource_name=resource_name, stack_name=stack.name) resource = stack[resource_name] if resource.id is None: raise exception.ResourceNotAvailable(resource_name=resource_name) return api.format_stack_resource(stack[resource_name])
def describe_stack_resources(self, cnxt, stack_identity, resource_name): s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) if resource_name is not None: name_match = lambda r: r.name == resource_name else: name_match = lambda r: True return [api.format_stack_resource(resource) for resource in stack if resource.id is not None and name_match(resource)]
def test_format_stack_resource_with_nested_stack_none(self): res = self.stack['generic1'] res.nested = mock.Mock() res.nested.return_value = None resource_keys = set( (rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(formatted.keys()))
def test_format_stack_resource_with_nested_stack_none(self): res = self.stack['generic1'] res.nested = mock.Mock() res.nested.return_value = None resource_keys = set( (rpc_api.RES_CREATION_TIME, rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(six.iterkeys(formatted)))
def test_format_stack_resource_with_nested_stack_not_found(self): res = self.stack['generic1'] res.nested = mock.Mock() res.nested.side_effect = exception.NotFound() resource_keys = set( (rpc_api.RES_CREATION_TIME, rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(six.iterkeys(formatted)))
def test_format_stack_resource(self): self.stack.created_time = datetime(2015, 8, 3, 17, 5, 1) self.stack.updated_time = datetime(2015, 8, 3, 17, 6, 2) res = self.stack['generic1'] resource_keys = set(( rpc_api.RES_CREATION_TIME, rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY, )) resource_details_keys = resource_keys.union( set(( rpc_api.RES_DESCRIPTION, rpc_api.RES_METADATA, rpc_api.RES_SCHEMA_ATTRIBUTES, ))) formatted = api.format_stack_resource(res, True) self.assertEqual(resource_details_keys, set(six.iterkeys(formatted))) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(six.iterkeys(formatted))) self.assertEqual(self.stack.created_time.isoformat(), formatted[rpc_api.RES_CREATION_TIME]) self.assertEqual(self.stack.updated_time.isoformat(), formatted[rpc_api.RES_UPDATED_TIME]) self.assertEqual(res.INIT, formatted[rpc_api.RES_ACTION])
def test_format_stack_resource(self): self.stack.created_time = datetime(2015, 8, 3, 17, 5, 1) self.stack.updated_time = datetime(2015, 8, 3, 17, 6, 2) res = self.stack['generic1'] resource_keys = set(( rpc_api.RES_CREATION_TIME, rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY, )) resource_details_keys = resource_keys.union(set(( rpc_api.RES_DESCRIPTION, rpc_api.RES_METADATA, rpc_api.RES_SCHEMA_ATTRIBUTES, ))) formatted = api.format_stack_resource(res, True) self.assertEqual(resource_details_keys, set(six.iterkeys(formatted))) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(six.iterkeys(formatted))) self.assertEqual(self.stack.created_time.isoformat(), formatted[rpc_api.RES_CREATION_TIME]) self.assertEqual(self.stack.updated_time.isoformat(), formatted[rpc_api.RES_UPDATED_TIME]) self.assertEqual(res.INIT, formatted[rpc_api.RES_ACTION])
def describe_stack_resource(self, context, stack_name, resource_name): auth.authenticate(context) s = db_api.stack_get_by_name(context, stack_name) if not s: raise AttributeError('Unknown stack name') stack = parser.Stack.load(context, s.id) if resource_name not in stack: raise AttributeError('Unknown resource name') resource = stack[resource_name] if resource.id is None: raise AttributeError('Resource not created') return api.format_stack_resource(stack[resource_name])
def test_format_stack_resource_with_nested_stack_not_found(self): res = self.stack['generic4'] self.patchobject(parser.Stack, 'load', side_effect=exception.NotFound()) resource_keys = set( (rpc_api.RES_CREATION_TIME, rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) formatted = api.format_stack_resource(res, False) # 'nested_stack_id' is not in formatted self.assertEqual(resource_keys, set(six.iterkeys(formatted)))
def describe_stack_resource(self, cnxt, stack_identity, resource_name): s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) if cfg.CONF.heat_stack_user_role in cnxt.roles: if not self._authorize_stack_user(cnxt, stack, resource_name): logger.warning("Access denied to resource %s" % resource_name) raise exception.Forbidden() if resource_name not in stack: raise exception.ResourceNotFound(resource_name=resource_name, stack_name=stack.name) resource = stack[resource_name] if resource.id is None: raise exception.ResourceNotAvailable(resource_name=resource_name) return api.format_stack_resource(stack[resource_name])
def test_format_stack_resource_with_nested_stack_none(self): res = self.stack['generic4'] resource_keys = set(( rpc_api.RES_CREATION_TIME, rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) formatted = api.format_stack_resource(res, False) self.assertEqual(resource_keys, set(formatted.keys()))
def test_format_stack_resource_with_nested_stack_not_found(self): res = self.stack['generic4'] self.patchobject(parser.Stack, 'load', side_effect=exception.NotFound()) resource_keys = set(( rpc_api.RES_CREATION_TIME, rpc_api.RES_UPDATED_TIME, rpc_api.RES_NAME, rpc_api.RES_PHYSICAL_ID, rpc_api.RES_ACTION, rpc_api.RES_STATUS, rpc_api.RES_STATUS_DATA, rpc_api.RES_TYPE, rpc_api.RES_ID, rpc_api.RES_STACK_ID, rpc_api.RES_STACK_NAME, rpc_api.RES_REQUIRED_BY)) formatted = api.format_stack_resource(res, False) # 'nested_stack_id' is not in formatted self.assertEqual(resource_keys, set(six.iterkeys(formatted)))
def test_format_stack_resource_no_attrs(self): res = self.stack['generic1'] formatted = api.format_stack_resource(res, True, with_attr=False) self.assertNotIn(rpc_api.RES_ATTRIBUTES, formatted) self.assertIn(rpc_api.RES_METADATA, formatted)
def test_format_stack_resource_required_by(self): res1 = api.format_stack_resource(self.stack['generic1']) res2 = api.format_stack_resource(self.stack['generic2']) self.assertEqual(['generic2'], res1['required_by']) self.assertEqual([], res2['required_by'])
def test_format_stack_resource_required_by(self): res1 = api.format_stack_resource(self.stack["generic1"]) res2 = api.format_stack_resource(self.stack["generic2"]) self.assertEqual(["generic2"], res1["required_by"]) self.assertEqual([], res2["required_by"])