Esempio n. 1
0
    def describe_stack_resources(self, context, stack_name,
                                 physical_resource_id, logical_resource_id):
        auth.authenticate(context)

        if stack_name is not None:
            s = db_api.stack_get_by_name(context, stack_name)
        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)]
Esempio n. 2
0
    def event_create(self, context, event):

        auth.authenticate(context)

        stack_name = event['stack']
        resource_name = event['resource']
        stack = db_api.stack_get_by_name(context, stack_name)
        resource = db_api.resource_get_by_name_and_stack(context,
                                                         resource_name,
                                                         stack.id)
        if not resource:
            return ['Unknown resource', None]
        new_event = {
            'name': event['message'],
            'resource_status_reason': event['reason'],
            'StackId': stack.id,
            'LogicalResourceId': resource.name,
            'PhysicalResourceId': None,
            'ResourceType': event['resource_type'],
            'ResourceProperties': {},
        }
        try:
            result = db_api.event_create(context, new_event)
            new_event['id'] = result.id
            return [None, new_event]
        except Exception as ex:
            logger.warn('db error %s' % str(ex))
            msg = 'Error creating event'
            return [msg, None]
Esempio n. 3
0
    def setUp(self):
        self.m = mox.Mox()
        self.ctx = create_context(self.m, self.username, self.tenant)
        auth.authenticate(self.ctx).AndReturn(True)
        setup_mocks(self.m, self.stack)
        self.m.ReplayAll()

        self.man = manager.EngineManager()
Esempio n. 4
0
    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]
Esempio n. 5
0
 def get_template(self, context, stack_name, params):
     """
     Get the template.
     arg1 -> RPC context.
     arg2 -> Name of the stack you want to see.
     arg3 -> Dict of http request parameters passed in from API side.
     """
     auth.authenticate(context)
     s = db_api.stack_get_by_name(context, stack_name)
     if s:
         return s.raw_template.template
     return None
Esempio n. 6
0
    def test_validate_volumeattach_invalid(self):
        t = json.loads(test_template_volumeattach % 'sda')
        self.m.StubOutWithMock(auth, 'authenticate')
        auth.authenticate(None).AndReturn(True)
        stack = parser.Stack(None, 'test_stack', parser.Template(t))

        self.m.StubOutWithMock(db_api, 'resource_get_by_name_and_stack')
        db_api.resource_get_by_name_and_stack(None, 'test_resource_name',
                                              stack).AndReturn(None)

        self.m.ReplayAll()
        volumeattach = stack.resources['MountPoint']
        self.assertTrue(volumeattach.validate())
Esempio n. 7
0
    def test_validate_findinmap_invalid(self):
        t = json.loads(test_template_findinmap_invalid)
        t['Parameters']['KeyName']['Value'] = 'test'
        params = {}
        self.m.StubOutWithMock(auth, 'authenticate')
        auth.authenticate(None).AndReturn(True)

        self.m.StubOutWithMock(instances.Instance, 'nova')
        instances.Instance.nova().AndReturn(self.fc)
        self.m.ReplayAll()

        manager = managers.EngineManager()
        res = dict(manager.
            validate_template(None, t, params)['ValidateTemplateResult'])
        self.assertNotEqual(res['Description'], 'Successfully validated')
Esempio n. 8
0
    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])
Esempio n. 9
0
    def test_validate_ref_valid(self):
        t = json.loads(test_template_ref % 'WikiDatabase')
        t['Parameters']['KeyName']['Value'] = 'test'
        params = {}
        self.m.StubOutWithMock(auth, 'authenticate')
        auth.authenticate(None).AndReturn(True)

        self.m.StubOutWithMock(instances.Instance, 'nova')
        instances.Instance.nova().AndReturn(self.fc)
        self.m.ReplayAll()

        manager = managers.EngineManager()
        res = dict(manager.
            validate_template(None, t, params)['ValidateTemplateResult'])
        print 'res %s' % res
        self.assertEqual(res['Description'], 'test.')
Esempio n. 10
0
    def nova(self, service_type='compute'):
        if service_type in self._nova:
            return self._nova[service_type]

        self._nova[service_type] = auth.authenticate(self.context,
                                                     service_type=service_type,
                                                     service_name=None)
        return self._nova[service_type]
Esempio n. 11
0
    def nova(self, service_type='compute'):
        if service_type in self._nova:
            return self._nova[service_type]

        self._nova[service_type] = auth.authenticate(self.context,
                                                     service_type=service_type,
                                                     service_name=None)
        return self._nova[service_type]
Esempio n. 12
0
    def delete_stack(self, context, stack_name, params):
        """
        The delete_stack method deletes a given stack.
        arg1 -> RPC context.
        arg2 -> Name of the stack you want to delete.
        arg3 -> Params passed from API.
        """

        auth.authenticate(context)

        st = db_api.stack_get_by_name(context, stack_name)
        if not st:
            raise AttributeError('Unknown stack name')

        logger.info('deleting stack %s' % stack_name)

        stack = parser.Stack.load(context, st.id)
        greenpool.spawn_n(stack.delete)
        return None
Esempio n. 13
0
    def list_events(self, context, stack_name, params):
        """
        The list_events method lists all events associated with a given stack.
        arg1 -> RPC context.
        arg2 -> Name of the stack you want to get events for.
        arg3 -> Params passed from API.
        """

        auth.authenticate(context)

        if stack_name is not None:
            st = db_api.stack_get_by_name(context, stack_name)
            if not st:
                raise AttributeError('Unknown stack name')

            events = db_api.event_get_all_by_stack(context, st.id)
        else:
            events = db_api.event_get_all_by_tenant(context)

        return {'events': [api.format_event(e) for e in events]}
Esempio n. 14
0
    def validate_template(self, context, template, params):
        """
        The validate_template method uses the stack parser to check
        the validity of a template.

        arg1 -> RPC context.
        arg3 -> Template of stack you want to create.
        arg4 -> Params passed from API.
        """
        auth.authenticate(context)

        logger.info('validate_template')
        if template is None:
            msg = _("No Template provided.")
            return webob.exc.HTTPBadRequest(explanation=msg)

        tmpl = parser.Template(template)
        resources = template.get('Resources', [])

        if not resources:
            return {'Error': 'At least one Resources member must be defined.'}

        for res in resources.values():
            if not res.get('Type'):
                return {'Error':
                        'Every Resources object must contain a Type member.'}

        parameters = []
        for param_key, param in template.get('Parameters', {}).items():
            parameters.append({
                'NoEcho': param.get('NoEcho', 'false'),
                'ParameterKey': param_key,
                'Description': param.get('Description', '')
            })

        result = {
            'Description': template.get('Description', ''),
            'Parameters': parameters,
        }
        return {'ValidateTemplateResult': result}
Esempio n. 15
0
    def update_stack(self, context, stack_name, template, params, args):
        """
        The update_stack method updates an existing stack based on the
        provided template and parameters.
        Note that at this stage the template has already been fetched from the
        heat-api process if using a template-url.
        arg1 -> RPC context.
        arg2 -> Name of the stack you want to create.
        arg3 -> Template of stack you want to create.
        arg4 -> Stack Input Params
        arg4 -> Request parameters/args passed from API
        """
        logger.info('template is %s' % template)

        auth.authenticate(context)

        # Get the database representation of the existing stack
        db_stack = db_api.stack_get_by_name(None, stack_name)
        if not db_stack:
            raise AttributeError('No stack exists with that name')

        current_stack = parser.Stack.load(context, db_stack.id)

        # Now parse the template and any parameters for the updated
        # stack definition.
        tmpl = parser.Template(template)
        template_params = parser.Parameters(stack_name, tmpl, params)
        common_params = api.extract_args(args)

        updated_stack = parser.Stack(context, stack_name, tmpl,
                                     template_params, **common_params)

        response = updated_stack.validate()
        if response['Description'] != 'Successfully validated':
            return response

        greenpool.spawn_n(current_stack.update, updated_stack)

        return {'StackName': current_stack.name, 'StackId': current_stack.id}
Esempio n. 16
0
    def show_stack(self, context, stack_name, params):
        """
        The show_stack method returns the attributes of one stack.
        arg1 -> RPC context.
        arg2 -> Name of the stack you want to see, or None to see all
        arg3 -> Dict of http request parameters passed in from API side.
        """
        auth.authenticate(context)

        if stack_name is not None:
            s = db_api.stack_get_by_name(context, stack_name)
            if s:
                stacks = [s]
            else:
                raise AttributeError('Unknown stack name')
        else:
            stacks = db_api.stack_get_by_tenant(context) or []

        def format_stack_detail(s):
            stack = parser.Stack.load(context, s.id)
            return api.format_stack(stack)

        return {'stacks': [format_stack_detail(s) for s in stacks]}
Esempio n. 17
0
    def create_stack(self, context, stack_name, template, params, args):
        """
        The create_stack method creates a new stack using the template
        provided.
        Note that at this stage the template has already been fetched from the
        heat-api process if using a template-url.
        arg1 -> RPC context.
        arg2 -> Name of the stack you want to create.
        arg3 -> Template of stack you want to create.
        arg4 -> Stack Input Params
        arg4 -> Request parameters/args passed from API
        """
        logger.info('template is %s' % template)

        auth.authenticate(context)

        if db_api.stack_get_by_name(None, stack_name):
            raise AttributeError('Stack already exists with that name')

        tmpl = parser.Template(template)

        # Extract the template parameters, and any common query parameters
        template_params = parser.Parameters(stack_name, tmpl, params)
        common_params = api.extract_args(args)

        stack = parser.Stack(context, stack_name, tmpl, template_params,
                             **common_params)

        response = stack.validate()
        if response['Description'] != 'Successfully validated':
            return response

        stack_id = stack.store()
        greenpool.spawn_n(stack.create)

        return {'StackName': stack.name, 'StackId': stack.id}