diff options
-rw-r--r-- | pycadf/audit/api.py | 22 | ||||
-rw-r--r-- | pycadf/cadftype.py | 3 | ||||
-rw-r--r-- | pycadf/tests/audit/test_api.py | 55 |
3 files changed, 40 insertions, 40 deletions
diff --git a/pycadf/audit/api.py b/pycadf/audit/api.py index 71e11c2..7d1dfe7 100644 --- a/pycadf/audit/api.py +++ b/pycadf/audit/api.py @@ -24,6 +24,7 @@ import urlparse from pycadf import cadftaxonomy as taxonomy from pycadf import cadftype from pycadf import eventfactory as factory +from pycadf import identifier from pycadf import reason from pycadf import reporterstep from pycadf import resource @@ -153,7 +154,7 @@ class OpenStackAuditApi(object): return action - def gen_event(self, req, correlation_id): + def create_event(self, req, correlation_id): action = self._get_action(req) catalog = ast.literal_eval(req.environ['HTTP_X_SERVICE_CATALOG']) for endpoint in catalog: @@ -199,27 +200,30 @@ class OpenStackAuditApi(object): correlation_id)) return event - def append_audit_event(self, msg, req, correlation_id): + def append_audit_event(self, req): + correlation_id = identifier.generate_uuid() setattr(req, 'CADF_EVENT_CORRELATION_ID', correlation_id) - event = self.gen_event(req, correlation_id) + event = self.create_event(req, correlation_id) event.add_reporterstep( reporterstep.Reporterstep( role=cadftype.REPORTER_ROLE_OBSERVER, reporter='target')) - msg['cadf_event'] = event + setattr(req, 'cadf_model', event) + req.environ['cadf_event'] = event.as_dict() - def mod_audit_event(self, msg, response, correlation_id): + def mod_audit_event(self, req, response): if response.status_int >= 200 and response.status_int < 400: result = taxonomy.OUTCOME_SUCCESS else: result = taxonomy.OUTCOME_FAILURE - if 'cadf_event' in msg: - msg['cadf_event'].outcome = result - msg['cadf_event'].reason = \ + if req.cadf_model: + req.cadf_model.outcome = result + req.cadf_model.reason = \ reason.Reason(reasonType='HTTP', reasonCode=str(response.status_int)) - msg['cadf_event'].add_reporterstep( + req.cadf_model.add_reporterstep( reporterstep.Reporterstep( role=cadftype.REPORTER_ROLE_MODIFIER, reporter='target', reporterTime=timestamp.get_utc_now())) + req.environ['cadf_event'] = req.cadf_model.as_dict() diff --git a/pycadf/cadftype.py b/pycadf/cadftype.py index 1c0209c..604b178 100644 --- a/pycadf/cadftype.py +++ b/pycadf/cadftype.py @@ -65,7 +65,8 @@ class ValidatorDescriptor(object): if self.func(value): instance.__dict__[self.name] = value else: - raise ValueError('Value failed validation: %s' % self.func) + raise ValueError('%s failed validation: %s' % + (self.name, self.func)) else: instance.__dict__[self.name] = value else: diff --git a/pycadf/tests/audit/test_api.py b/pycadf/tests/audit/test_api.py index 0bf7bd2..2cc112e 100644 --- a/pycadf/tests/audit/test_api.py +++ b/pycadf/tests/audit/test_api.py @@ -19,7 +19,6 @@ import uuid import webob from pycadf.audit import api -from pycadf import identifier from pycadf.tests import base @@ -56,14 +55,13 @@ class TestAuditApi(base.TestCase): def api_request(self, method, url): self.ENV_HEADERS['REQUEST_METHOD'] = method req = webob.Request.blank(url, environ=self.ENV_HEADERS) - msg = {} - self.audit_api.append_audit_event(msg, req, - identifier.generate_uuid()) - return msg + self.audit_api.append_audit_event(req) + self.assertTrue(req.CADF_EVENT_CORRELATION_ID) + return req def test_get_list(self): - msg = self.api_request('GET', 'http://host:8774/v2/public/servers') - payload = msg['cadf_event'].as_dict() + req = self.api_request('GET', 'http://host:8774/v2/public/servers') + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'list') self.assertEqual(payload['typeURI'], 'http://schemas.dmtf.org/cloud/audit/1.0/event') @@ -90,17 +88,17 @@ class TestAuditApi(base.TestCase): self.assertEqual(payload['reporterchain'][0]['reporter'], 'target') def test_get_read(self): - msg = self.api_request('GET', + req = self.api_request('GET', 'http://host:8774/v2/public/servers/' + str(uuid.uuid4())) - payload = msg['cadf_event'].as_dict() + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'read') self.assertEqual(payload['outcome'], 'pending') def test_get_unknown_endpoint(self): - msg = self.api_request('GET', + req = self.api_request('GET', 'http://unknown:8774/v2/public/servers/') - payload = msg['cadf_event'].as_dict() + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'list') self.assertEqual(payload['outcome'], 'pending') self.assertEqual(payload['target']['name'], 'unknown') @@ -108,34 +106,34 @@ class TestAuditApi(base.TestCase): self.assertEqual(payload['target']['typeURI'], 'unknown') def test_put(self): - msg = self.api_request('PUT', 'http://host:8774/v2/public/servers') - payload = msg['cadf_event'].as_dict() + req = self.api_request('PUT', 'http://host:8774/v2/public/servers') + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'update') self.assertEqual(payload['outcome'], 'pending') def test_delete(self): - msg = self.api_request('DELETE', 'http://host:8774/v2/public/servers') - payload = msg['cadf_event'].as_dict() + req = self.api_request('DELETE', 'http://host:8774/v2/public/servers') + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'delete') self.assertEqual(payload['outcome'], 'pending') def test_head(self): - msg = self.api_request('HEAD', 'http://host:8774/v2/public/servers') - payload = msg['cadf_event'].as_dict() + req = self.api_request('HEAD', 'http://host:8774/v2/public/servers') + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'read') self.assertEqual(payload['outcome'], 'pending') def test_post_update(self): - msg = self.api_request('POST', + req = self.api_request('POST', 'http://host:8774/v2/public/servers/' + str(uuid.uuid4())) - payload = msg['cadf_event'].as_dict() + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'update') self.assertEqual(payload['outcome'], 'pending') def test_post_create(self): - msg = self.api_request('POST', 'http://host:8774/v2/public/servers') - payload = msg['cadf_event'].as_dict() + req = self.api_request('POST', 'http://host:8774/v2/public/servers') + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'create') self.assertEqual(payload['outcome'], 'pending') @@ -145,19 +143,16 @@ class TestAuditApi(base.TestCase): environ=self.ENV_HEADERS) req.body = '{"createImage" : {"name" : "new-image","metadata": ' \ '{"ImageType": "Gold","ImageVersion": "2.0"}}}' - msg = {} - self.audit_api.append_audit_event(msg, req, - identifier.generate_uuid()) - payload = msg['cadf_event'].as_dict() + self.audit_api.append_audit_event(req) + payload = req.environ['cadf_event'] self.assertEqual(payload['action'], 'create') self.assertEqual(payload['outcome'], 'pending') def test_response_mod_msg(self): - msg = self.api_request('GET', 'http://host:8774/v2/public/servers') - payload = msg['cadf_event'].as_dict() - self.audit_api.mod_audit_event(msg, webob.Response(), - identifier.generate_uuid()) - payload2 = msg['cadf_event'].as_dict() + req = self.api_request('GET', 'http://host:8774/v2/public/servers') + payload = req.environ['cadf_event'] + self.audit_api.mod_audit_event(req, webob.Response()) + payload2 = req.environ['cadf_event'] self.assertEqual(payload['id'], payload2['id']) self.assertEqual(payload['tags'], payload2['tags']) self.assertEqual(payload2['outcome'], 'success') |