Example #1
0
    def identifier(self, stack_identifier):
        """Return a unique identifier for the event."""

        res_id = identifier.ResourceIdentifier(
            resource_name=self.resource_name, **stack_identifier)

        return identifier.EventIdentifier(event_id=str(self.uuid), **res_id)
Example #2
0
    def identifier(self):
        '''Return a unique identifier for the event'''
        if self.id is None:
            return None

        return identifier.EventIdentifier(event_id=str(self.id),
                                          **self.resource.identifier())
    def _test_format_event(self, event_id):
        event = self._dummy_event(event_id)

        event_keys = set((
            rpc_api.EVENT_ID,
            rpc_api.EVENT_STACK_ID,
            rpc_api.EVENT_STACK_NAME,
            rpc_api.EVENT_TIMESTAMP,
            rpc_api.EVENT_RES_NAME,
            rpc_api.EVENT_RES_PHYSICAL_ID,
            rpc_api.EVENT_RES_ACTION,
            rpc_api.EVENT_RES_STATUS,
            rpc_api.EVENT_RES_STATUS_DATA,
            rpc_api.EVENT_RES_TYPE,
            rpc_api.EVENT_RES_PROPERTIES))

        formatted = api.format_event(event)
        self.assertEqual(event_keys, set(six.iterkeys(formatted)))

        event_id_formatted = formatted[rpc_api.EVENT_ID]
        event_identifier = identifier.EventIdentifier(
            event_id_formatted['tenant'],
            event_id_formatted['stack_name'],
            event_id_formatted['stack_id'],
            event_id_formatted['path'])
        self.assertEqual(event_id, event_identifier.event_id)
Example #4
0
    def transform(key, value):
        if not include_key(key):
            return

        if key == rpc_api.EVENT_ID:
            identity = identifier.EventIdentifier(**value)
            yield ('id', identity.event_id)
            yield ('links', [util.make_link(req, identity),
                             util.make_link(req, identity.resource(),
                                            'resource'),
                             util.make_link(req, identity.stack(),
                                            'stack')])
        elif key in (rpc_api.EVENT_STACK_ID, rpc_api.EVENT_STACK_NAME,
                     rpc_api.EVENT_RES_ACTION):
            return
        elif (key == rpc_api.EVENT_RES_STATUS and
              rpc_api.EVENT_RES_ACTION in event):
            # To avoid breaking API compatibility, we join RES_ACTION
            # and RES_STATUS, so the API format doesn't expose the
            # internal split of state into action/status
            yield (key, '_'.join((event[rpc_api.EVENT_RES_ACTION], value)))
        elif (key == rpc_api.RES_NAME):
            yield ('logical_resource_id', value)
            yield (key, value)

        else:
            yield (key, value)
Example #5
0
    def identifier(self):
        """Return a unique identifier for the event."""
        if self.uuid is None:
            return None

        res_id = identifier.ResourceIdentifier(
            resource_name=self.resource_name, **self.stack.identifier())

        return identifier.EventIdentifier(event_id=str(self.uuid), **res_id)
Example #6
0
    def identifier(self):
        '''Return a unique identifier for the event.'''
        if self.id is None:
            return None

        res_id = identifier.ResourceIdentifier(
            resource_name=self.logical_resource_id, **self.stack.identifier())

        return identifier.EventIdentifier(event_id=str(self.id), **res_id)
Example #7
0
 def _id_format(resp):
     """
     Format the StackId field in the response as an ARN, and process other
     IDs into the correct format.
     """
     if 'StackId' in resp:
         identity = identifier.HeatIdentifier(**resp['StackId'])
         resp['StackId'] = identity.arn()
     if 'EventId' in resp:
         identity = identifier.EventIdentifier(**resp['EventId'])
         resp['EventId'] = identity.event_id
     return resp
Example #8
0
    def test_index_resource_nonexist(self, mock_enforce):
        self._mock_enforce_setup(mock_enforce, 'index', True)
        event_id = '42'
        res_name = 'WikiDatabase'
        stack_identity = identifier.HeatIdentifier(self.tenant,
                                                   'wordpress', '6')
        res_identity = identifier.ResourceIdentifier(resource_name=res_name,
                                                     **stack_identity)
        ev_identity = identifier.EventIdentifier(event_id=event_id,
                                                 **res_identity)

        req = self._get(stack_identity._tenant_path() +
                        '/resources/' + res_name + '/events')

        kwargs = {'stack_identity': stack_identity,
                  'limit': None, 'sort_keys': None, 'marker': None,
                  'sort_dir': None, 'filters': None}

        engine_resp = [
            {
                u'stack_name': u'wordpress',
                u'event_time': u'2012-07-23T13:05:39Z',
                u'stack_identity': dict(stack_identity),
                u'resource_name': 'SomeOtherResource',
                u'resource_status_reason': u'state changed',
                u'event_identity': dict(ev_identity),
                u'resource_action': u'CREATE',
                u'resource_status': u'IN_PROGRESS',
                u'physical_resource_id': None,
                u'resource_properties': {u'UserData': u'blah'},
                u'resource_type': u'AWS::EC2::Instance',
            }
        ]
        self.m.StubOutWithMock(rpc_client.EngineClient, 'call')
        rpc_client.EngineClient.call(
            req.context,
            ('list_events', kwargs)
        ).AndReturn(engine_resp)
        self.m.ReplayAll()

        self.assertRaises(webob.exc.HTTPNotFound,
                          self.controller.index,
                          req, tenant_id=self.tenant,
                          stack_name=stack_identity.stack_name,
                          stack_id=stack_identity.stack_id,
                          resource_name=res_name)
        self.m.VerifyAll()
Example #9
0
    def transform(key, value):
        if not include_key(key):
            return

        if key == engine_api.EVENT_ID:
            identity = identifier.EventIdentifier(**value)
            yield ('id', identity.event_id)
            yield ('links', [
                util.make_link(req, identity),
                util.make_link(req, identity.resource(), 'resource'),
                util.make_link(req, identity.stack(), 'stack')
            ])
        elif (key == engine_api.EVENT_STACK_ID
              or key == engine_api.EVENT_STACK_NAME):
            return
        else:
            yield (key, value)
Example #10
0
    def test_index_multiple_resource_names(self, mock_call, mock_enforce):
        self._mock_enforce_setup(mock_enforce, 'index', True)
        res_name = 'resource3'
        event_id = '42'
        params = {
            'resource_name': ['resource1', 'resource2']
        }
        stack_identity = identifier.HeatIdentifier(self.tenant,
                                                   'wibble', '6')
        res_identity = identifier.ResourceIdentifier(resource_name=res_name,
                                                     **stack_identity)
        ev_identity = identifier.EventIdentifier(event_id=event_id,
                                                 **res_identity)
        req = self._get(stack_identity._tenant_path() + '/events',
                        params=params)

        mock_call.return_value = [
            {
                u'stack_name': u'wordpress',
                u'event_time': u'2012-07-23T13:05:39Z',
                u'stack_identity': dict(stack_identity),
                u'resource_name': res_name,
                u'resource_status_reason': u'state changed',
                u'event_identity': dict(ev_identity),
                u'resource_action': u'CREATE',
                u'resource_status': u'IN_PROGRESS',
                u'physical_resource_id': None,
                u'resource_properties': {u'UserData': u'blah'},
                u'resource_type': u'AWS::EC2::Instance',
            }
        ]

        self.controller.index(req, tenant_id=self.tenant,
                              stack_name=stack_identity.stack_name,
                              stack_id=stack_identity.stack_id,
                              resource_name=res_name)

        rpc_call_args, _ = mock_call.call_args
        engine_args = rpc_call_args[1][1]
        self.assertEqual(6, len(engine_args))
        self.assertIn('filters', engine_args)
        self.assertIn('resource_name', engine_args['filters'])
        self.assertEqual(res_name, engine_args['filters']['resource_name'])
        self.assertNotIn('resource1', engine_args['filters']['resource_name'])
        self.assertNotIn('resource2', engine_args['filters']['resource_name'])
Example #11
0
 def test_event_resource(self):
     si = identifier.HeatIdentifier('t', 's', 'i')
     pi = identifier.ResourceIdentifier(resource_name='r', **si)
     ei = identifier.EventIdentifier(event_id='e', **pi)
     self.assertEqual(ei.resource(), pi)
Example #12
0
 def test_event_init_from_dict(self):
     hi = identifier.HeatIdentifier('t', 's', 'i', '/resources/p/events/42')
     ei = identifier.EventIdentifier(**hi)
     self.assertEqual(ei, hi)
Example #13
0
 def _test_event_init(self, event_id):
     si = identifier.HeatIdentifier('t', 's', 'i')
     pi = identifier.ResourceIdentifier(resource_name='p', **si)
     ei = identifier.EventIdentifier(event_id=event_id, **pi)
     self.assertEqual(ei.path, '/resources/p/events/{0}'.format(event_id))
Example #14
0
 def test_event_init(self):
     si = identifier.HeatIdentifier('t', 's', 'i')
     pi = identifier.ResourceIdentifier(resource_name='p', **si)
     ei = identifier.EventIdentifier(event_id='e', **pi)
     self.assertEqual(ei.path, '/resources/p/events/e')
Example #15
0
    def _test_resource_index(self, event_id, mock_enforce):
        self._mock_enforce_setup(mock_enforce, 'index', True)
        res_name = 'WikiDatabase'
        stack_identity = identifier.HeatIdentifier(self.tenant, 'wordpress',
                                                   '6')
        res_identity = identifier.ResourceIdentifier(resource_name=res_name,
                                                     **stack_identity)
        ev_identity = identifier.EventIdentifier(event_id=event_id,
                                                 **res_identity)

        req = self._get(stack_identity._tenant_path() + '/resources/' +
                        res_name + '/events')

        kwargs = {
            'stack_identity': stack_identity,
            'limit': None,
            'sort_keys': None,
            'marker': None,
            'sort_dir': None,
            'filters': {
                'resource_name': res_name
            }
        }

        engine_resp = [{
            u'stack_name': u'wordpress',
            u'event_time': u'2012-07-23T13:05:39Z',
            u'stack_identity': dict(stack_identity),
            u'resource_name': res_name,
            u'resource_status_reason': u'state changed',
            u'event_identity': dict(ev_identity),
            u'resource_action': u'CREATE',
            u'resource_status': u'IN_PROGRESS',
            u'physical_resource_id': None,
            u'resource_properties': {
                u'UserData': u'blah'
            },
            u'resource_type': u'AWS::EC2::Instance',
        }]
        self.m.StubOutWithMock(rpc_client.EngineClient, 'call')
        rpc_client.EngineClient.call(
            req.context, ('list_events', kwargs)).AndReturn(engine_resp)
        self.m.ReplayAll()

        result = self.controller.index(req,
                                       tenant_id=self.tenant,
                                       stack_name=stack_identity.stack_name,
                                       stack_id=stack_identity.stack_id,
                                       resource_name=res_name)

        expected = {
            'events': [{
                'id':
                event_id,
                'links': [
                    {
                        'href': self._url(ev_identity),
                        'rel': 'self'
                    },
                    {
                        'href': self._url(res_identity),
                        'rel': 'resource'
                    },
                    {
                        'href': self._url(stack_identity),
                        'rel': 'stack'
                    },
                ],
                u'resource_name':
                res_name,
                u'logical_resource_id':
                res_name,
                u'resource_status_reason':
                u'state changed',
                u'event_time':
                u'2012-07-23T13:05:39Z',
                u'resource_status':
                u'CREATE_IN_PROGRESS',
                u'physical_resource_id':
                None,
            }]
        }

        self.assertEqual(expected, result)
        self.m.VerifyAll()
Example #16
0
    def _test_resource_index(self, event_id, mock_enforce, nested_depth=None):
        self._mock_enforce_setup(mock_enforce, 'index', True)
        res_name = 'WikiDatabase'
        params = {}
        if nested_depth:
            params['nested_depth'] = nested_depth
        stack_identity = identifier.HeatIdentifier(self.tenant, 'wordpress',
                                                   '6')
        res_identity = identifier.ResourceIdentifier(resource_name=res_name,
                                                     **stack_identity)
        ev_identity = identifier.EventIdentifier(event_id=event_id,
                                                 **res_identity)

        req = self._get(stack_identity._tenant_path() + '/resources/' +
                        res_name + '/events',
                        params=params)

        kwargs = {
            'stack_identity': stack_identity,
            'nested_depth': nested_depth,
            'limit': None,
            'sort_keys': None,
            'marker': None,
            'sort_dir': None,
            'filters': {
                'resource_name': res_name
            }
        }

        engine_resp = [{
            u'stack_name': u'wordpress',
            u'event_time': u'2012-07-23T13:05:39Z',
            u'stack_identity': dict(stack_identity),
            u'resource_name': res_name,
            u'resource_status_reason': u'state changed',
            u'event_identity': dict(ev_identity),
            u'resource_action': u'CREATE',
            u'resource_status': u'IN_PROGRESS',
            u'physical_resource_id': None,
            u'resource_type': u'AWS::EC2::Instance',
        }]
        if nested_depth:
            engine_resp[0]['root_stack_id'] = dict(stack_identity)

        mock_call = self.patchobject(rpc_client.EngineClient,
                                     'call',
                                     return_value=engine_resp)

        result = self.controller.index(req,
                                       tenant_id=self.tenant,
                                       stack_name=stack_identity.stack_name,
                                       stack_id=stack_identity.stack_id,
                                       resource_name=res_name)

        expected = {
            'events': [{
                'id':
                event_id,
                'links': [
                    {
                        'href': self._url(ev_identity),
                        'rel': 'self'
                    },
                    {
                        'href': self._url(res_identity),
                        'rel': 'resource'
                    },
                    {
                        'href': self._url(stack_identity),
                        'rel': 'stack'
                    },
                ],
                u'resource_name':
                res_name,
                u'logical_resource_id':
                res_name,
                u'resource_status_reason':
                u'state changed',
                u'event_time':
                u'2012-07-23T13:05:39Z',
                u'resource_status':
                u'CREATE_IN_PROGRESS',
                u'physical_resource_id':
                None,
            }]
        }
        if nested_depth:
            expected['events'][0]['links'].append({
                'href':
                self._url(stack_identity),
                'rel':
                'root_stack'
            })

        self.assertEqual(expected, result)

        mock_call.assert_called_once_with(req.context, ('list_events', kwargs),
                                          version='1.31')
Example #17
0
 def test_resource_name(self):
     ei = identifier.EventIdentifier('t', 's', 'i', '/resources/p', 'e')
     self.assertEqual(ei.resource_name, 'p')
Example #18
0
 def event_match(ev):
     identity = identifier.EventIdentifier(**ev[rpc_api.EVENT_ID])
     return (ev[rpc_api.EVENT_RES_NAME] == resource_name and
             identity.event_id == event_id)
Example #19
0
 def _test_event_id(self, event_id):
     ei = identifier.EventIdentifier('t', 's', 'i', '/resources/p',
                                     event_id)
     self.assertEqual(ei.event_id, event_id)
Example #20
0
    def _test_show(self, event_id, mock_enforce):
        self._mock_enforce_setup(mock_enforce, 'show', True)
        res_name = 'WikiDatabase'
        stack_identity = identifier.HeatIdentifier(self.tenant, 'wordpress',
                                                   '6')
        res_identity = identifier.ResourceIdentifier(resource_name=res_name,
                                                     **stack_identity)
        ev_identity = identifier.EventIdentifier(event_id=event_id,
                                                 **res_identity)

        req = self._get(stack_identity._tenant_path() + '/resources/' +
                        res_name + '/events/' + event_id)

        kwargs = {
            'stack_identity': stack_identity,
            'limit': None,
            'sort_keys': None,
            'marker': None,
            'sort_dir': None,
            'nested_depth': None,
            'filters': {
                'resource_name': res_name,
                'uuid': event_id
            }
        }

        engine_resp = [{
            u'stack_name': u'wordpress',
            u'event_time': u'2012-07-23T13:06:00Z',
            u'stack_identity': dict(stack_identity),
            u'resource_name': res_name,
            u'resource_status_reason': u'state changed',
            u'event_identity': dict(ev_identity),
            u'resource_action': u'CREATE',
            u'resource_status': u'COMPLETE',
            u'physical_resource_id': u'a3455d8c-9f88-404d-a85b-5315293e67de',
            u'resource_properties': {
                u'UserData': u'blah'
            },
            u'resource_type': u'AWS::EC2::Instance',
        }]
        mock_call = self.patchobject(rpc_client.EngineClient,
                                     'call',
                                     return_value=engine_resp)

        result = self.controller.show(req,
                                      tenant_id=self.tenant,
                                      stack_name=stack_identity.stack_name,
                                      stack_id=stack_identity.stack_id,
                                      resource_name=res_name,
                                      event_id=event_id)

        expected = {
            'event': {
                'id':
                event_id,
                'links': [
                    {
                        'href': self._url(ev_identity),
                        'rel': 'self'
                    },
                    {
                        'href': self._url(res_identity),
                        'rel': 'resource'
                    },
                    {
                        'href': self._url(stack_identity),
                        'rel': 'stack'
                    },
                ],
                u'resource_name':
                res_name,
                u'logical_resource_id':
                res_name,
                u'resource_status_reason':
                u'state changed',
                u'event_time':
                u'2012-07-23T13:06:00Z',
                u'resource_status':
                u'CREATE_COMPLETE',
                u'physical_resource_id':
                u'a3455d8c-9f88-404d-a85b-5315293e67de',
                u'resource_type':
                u'AWS::EC2::Instance',
                u'resource_properties': {
                    u'UserData': u'blah'
                },
            }
        }

        self.assertEqual(expected, result)

        mock_call.assert_called_once_with(req.context, ('list_events', kwargs),
                                          version='1.31')