From e75777b29903ab6441041214228f0910e8177092 Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Tue, 30 Sep 2014 18:01:57 +0200 Subject: [PATCH] Update usage of Barbican library Update Barbican resources to use the new API of the client library. Change-Id: Ia634b325cd251ef70eb5eb133a801a3f3510ec67 Closes-Bug: 1374128 --- .../heat_barbican/resources/order.py | 7 ++-- .../heat_barbican/resources/secret.py | 11 ++++--- .../heat_barbican/tests/test_order.py | 33 ++++++++++++------- .../heat_barbican/tests/test_secret.py | 23 +++++++++---- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/contrib/heat_barbican/heat_barbican/resources/order.py b/contrib/heat_barbican/heat_barbican/resources/order.py index 2629065f5..ffa02c7e7 100644 --- a/contrib/heat_barbican/heat_barbican/resources/order.py +++ b/contrib/heat_barbican/heat_barbican/resources/order.py @@ -108,12 +108,13 @@ class Order(resource.Resource): def handle_create(self): info = dict(self.properties) - order_ref = self.barbican().orders.create(**info) + order = self.barbican().orders.Order(**info) + order_ref = order.submit() self.resource_id_set(order_ref) return order_ref def check_create_complete(self, order_href): - order = self.barbican().orders.get(order_href) + order = self.barbican().orders.Order(order_href) if order.status == 'ERROR': reason = order.error_reason @@ -138,7 +139,7 @@ class Order(resource.Resource): raise def _resolve_attribute(self, name): - order = self.barbican().orders.get(self.resource_id) + order = self.barbican().orders.Order(self.resource_id) return getattr(order, name) diff --git a/contrib/heat_barbican/heat_barbican/resources/secret.py b/contrib/heat_barbican/heat_barbican/resources/secret.py index a73506f28..16e068719 100644 --- a/contrib/heat_barbican/heat_barbican/resources/secret.py +++ b/contrib/heat_barbican/heat_barbican/resources/secret.py @@ -125,7 +125,8 @@ class Secret(resource.Resource): def handle_create(self): info = dict(self.properties) - secret_ref = self.barbican().secrets.store(**info) + secret = self.barbican().secrets.Secret(**info) + secret_ref = secret.store() self.resource_id_set(secret_ref) return secret_ref @@ -143,11 +144,11 @@ class Secret(resource.Resource): raise def _resolve_attribute(self, name): - if name == self.DECRYPTED_PAYLOAD: - return self.barbican().secrets.decrypt( - self.resource_id) + secret = self.barbican().secrets.Secret(self.resource_id) + + if name == self.DECRYPTED_PAYLOAD: + return secret.payload - secret = self.barbican().secrets.get(self.resource_id) if name == self.STATUS: return secret.status diff --git a/contrib/heat_barbican/heat_barbican/tests/test_order.py b/contrib/heat_barbican/heat_barbican/tests/test_order.py index 6eb458e31..03c53e2fe 100644 --- a/contrib/heat_barbican/heat_barbican/tests/test_order.py +++ b/contrib/heat_barbican/heat_barbican/tests/test_order.py @@ -39,6 +39,15 @@ resources: ''' +class FakeOrder(object): + + def __init__(self, name): + self.name = name + + def submit(self): + return self.name + + class TestOrder(HeatTestCase): def setUp(self): @@ -68,7 +77,7 @@ class TestOrder(HeatTestCase): def _create_resource(self, name, snippet, stack): res = order.Order(name, snippet, stack) res.check_create_complete = mock.Mock(return_value=True) - self.barbican.orders.create.return_value = name + self.barbican.orders.Order.return_value = FakeOrder(name) scheduler.TaskRunner(res.create)() return res @@ -76,7 +85,7 @@ class TestOrder(HeatTestCase): res = self._create_resource('foo', self.res_template, self.stack) expected_state = (res.CREATE, res.COMPLETE) self.assertEqual(expected_state, res.state) - args = self.barbican.orders.create.call_args[1] + args = self.barbican.orders.Order.call_args[1] self.assertEqual('foobar-order', args['name']) self.assertEqual('aes', args['algorithm']) self.assertEqual('cbc', args['mode']) @@ -88,24 +97,24 @@ class TestOrder(HeatTestCase): mock_order.order_ref = 'test-order-ref' mock_order.secret_ref = 'test-secret-ref' - self.barbican.orders.get.return_value = mock_order res = self._create_resource('foo', self.res_template, self.stack) + self.barbican.orders.Order.return_value = mock_order self.assertEqual('test-order-ref', res.FnGetAtt('order_ref')) self.assertEqual('test-secret-ref', res.FnGetAtt('secret_ref')) def test_attributes_handle_exceptions(self): mock_order = mock.Mock() - self.barbican.orders.get.return_value = mock_order res = self._create_resource('foo', self.res_template, self.stack) + self.barbican.orders.Order.return_value = mock_order self.barbican.barbican_client.HTTPClientError = Exception - self.barbican.orders.get.side_effect = Exception('boom') + self.barbican.orders.Order.side_effect = Exception('boom') self.assertRaises(self.barbican.barbican_client.HTTPClientError, res.FnGetAtt, 'order_ref') def test_create_order_sets_resource_id(self): - self.barbican.orders.create.return_value = 'foo' + self.barbican.orders.Order.return_value = FakeOrder('foo') res = self._create_resource('foo', self.res_template, self.stack) self.assertEqual('foo', res.resource_id) @@ -113,7 +122,7 @@ class TestOrder(HeatTestCase): def test_create_order_defaults_to_octet_stream(self): res = self._create_resource('foo', self.res_template, self.stack) - args = self.barbican.orders.create.call_args[1] + args = self.barbican.orders.Order.call_args[1] self.assertEqual('application/octet-stream', args[res.PAYLOAD_CONTENT_TYPE]) @@ -124,7 +133,7 @@ class TestOrder(HeatTestCase): self.props) res = self._create_resource(defn.name, defn, self.stack) - args = self.barbican.orders.create.call_args[1] + args = self.barbican.orders.Order.call_args[1] self.assertEqual(content_type, args[res.PAYLOAD_CONTENT_TYPE]) def test_create_order_other_content_types_now_allowed(self): @@ -137,7 +146,7 @@ class TestOrder(HeatTestCase): scheduler.TaskRunner(res.create)) def test_delete_order(self): - self.barbican.orders.create.return_value = 'foo' + self.barbican.orders.Order.return_value = 'foo' res = self._create_resource('foo', self.res_template, self.stack) self.assertEqual('foo', res.resource_id) @@ -167,16 +176,16 @@ class TestOrder(HeatTestCase): res = order.Order('foo', self.res_template, self.stack) mock_active = mock.Mock(status='ACTIVE') - self.barbican.orders.get.return_value = mock_active + self.barbican.orders.Order.return_value = mock_active self.assertTrue(res.check_create_complete('foo')) mock_not_active = mock.Mock(status='PENDING') - self.barbican.orders.get.return_value = mock_not_active + self.barbican.orders.Order.return_value = mock_not_active self.assertFalse(res.check_create_complete('foo')) mock_not_active = mock.Mock(status='ERROR', error_reason='foo', error_status_code=500) - self.barbican.orders.get.return_value = mock_not_active + self.barbican.orders.Order.return_value = mock_not_active exc = self.assertRaises(exception.Error, res.check_create_complete, 'foo') self.assertIn('foo', six.text_type(exc)) diff --git a/contrib/heat_barbican/heat_barbican/tests/test_secret.py b/contrib/heat_barbican/heat_barbican/tests/test_secret.py index 5868aac58..bb3871334 100644 --- a/contrib/heat_barbican/heat_barbican/tests/test_secret.py +++ b/contrib/heat_barbican/heat_barbican/tests/test_secret.py @@ -36,6 +36,15 @@ resources: ''' +class FakeSecret(object): + + def __init__(self, name): + self.name = name + + def store(self): + return self.name + + class TestSecret(HeatTestCase): def setUp(self): @@ -64,28 +73,28 @@ class TestSecret(HeatTestCase): def _create_resource(self, name, snippet, stack): res = secret.Secret(name, snippet, stack) - self.barbican.secrets.store.return_value = name + '_id' + self.barbican.secrets.Secret.return_value = FakeSecret(name + '_id') scheduler.TaskRunner(res.create)() return res def test_create_secret(self): expected_state = (self.res.CREATE, self.res.COMPLETE) self.assertEqual(expected_state, self.res.state) - args = self.barbican.secrets.store.call_args[1] + args = self.barbican.secrets.Secret.call_args[1] self.assertEqual('foobar-secret', args['name']) def test_attributes(self): mock_secret = mock.Mock() mock_secret.status = 'test-status' - self.barbican.secrets.get.return_value = mock_secret - self.barbican.secrets.decrypt.return_value = 'foo' + self.barbican.secrets.Secret.return_value = mock_secret + mock_secret.payload = 'foo' self.assertEqual('test-status', self.res.FnGetAtt('status')) self.assertEqual('foo', self.res.FnGetAtt('decrypted_payload')) def test_attributes_handles_exceptions(self): self.barbican.barbican_client.HTTPClientError = Exception - self.barbican.secrets.get.side_effect = Exception('boom') + self.barbican.secrets.Secret.side_effect = Exception('boom') self.assertRaises(self.barbican.barbican_client.HTTPClientError, self.res.FnGetAtt, 'order_ref') @@ -104,7 +113,7 @@ class TestSecret(HeatTestCase): props) res = self._create_resource(defn.name, defn, self.stack) - args = self.barbican.secrets.store.call_args[1] + args = self.barbican.secrets.Secret.call_args[1] self.assertEqual('foobar', args[res.PAYLOAD]) self.assertEqual(content_type, args[res.PAYLOAD_CONTENT_TYPE]) @@ -120,7 +129,7 @@ class TestSecret(HeatTestCase): props) res = self._create_resource(defn.name, defn, self.stack) - args = self.barbican.secrets.store.call_args[1] + args = self.barbican.secrets.Secret.call_args[1] self.assertEqual('foobar', args[res.PAYLOAD]) self.assertEqual(content_type, args[res.PAYLOAD_CONTENT_TYPE])