Merge "Fill in remaining Secrets API and tests"
diff --git a/barbican_tempest_plugin/plugin.py b/barbican_tempest_plugin/plugin.py
index 293a17d..7eee973 100644
--- a/barbican_tempest_plugin/plugin.py
+++ b/barbican_tempest_plugin/plugin.py
@@ -41,6 +41,10 @@
             'name': 'secret_v1',
             'service_version': 'secret.v1',
             'module_path': 'barbican_tempest_plugin.services.key_manager.json',
-            'client_names': ['SecretClient', 'ContainerClient'],
+            'client_names': [
+                'ConsumerClient',
+                'ContainerClient',
+                'SecretClient'
+            ],
         }
         return [v1_params]
diff --git a/barbican_tempest_plugin/services/key_manager/json/__init__.py b/barbican_tempest_plugin/services/key_manager/json/__init__.py
index 81fa776..0e56400 100644
--- a/barbican_tempest_plugin/services/key_manager/json/__init__.py
+++ b/barbican_tempest_plugin/services/key_manager/json/__init__.py
@@ -12,9 +12,11 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
+from barbican_tempest_plugin.services.key_manager.json.consumer_client \
+    import ConsumerClient
 from barbican_tempest_plugin.services.key_manager.json.container_client \
     import ContainerClient
-from barbican_tempest_plugin.services.key_manager.json.secret_client import \
-    SecretClient
+from barbican_tempest_plugin.services.key_manager.json.secret_client \
+    import SecretClient
 
-__all__ = ['SecretClient', 'ContainerClient']
+__all__ = ['ConsumerClient', 'ContainerClient', 'SecretClient']
diff --git a/barbican_tempest_plugin/services/key_manager/json/consumer_client.py b/barbican_tempest_plugin/services/key_manager/json/consumer_client.py
new file mode 100644
index 0000000..e76400a
--- /dev/null
+++ b/barbican_tempest_plugin/services/key_manager/json/consumer_client.py
@@ -0,0 +1,49 @@
+# Copyright (c) 2016 Johns Hopkins University Applied Physics Laboratory
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+
+import json
+
+from six.moves.urllib import parse as urllib
+
+from tempest import config
+from tempest.lib.common import rest_client
+
+CONF = config.CONF
+
+
+class ConsumerClient(rest_client.RestClient):
+
+    def list_consumers_in_container(self, container_id, **kwargs):
+        uri = "/v1/containers/%s/consumers" % container_id
+        if kwargs:
+            uri += "?%s" % urllib.urlencode(kwargs)
+
+        response, body = self.get(uri)
+        self.expected_success(200, response.status)
+        return json.loads(body)
+
+    def add_consumer_to_container(self, container_id, **kwargs):
+        uri = "/v1/containers/%s/consumers" % container_id
+
+        response, body = self.post(uri, json.dumps(kwargs))
+        self.expected_success(200, response.status)
+        return json.loads(body)
+
+    def delete_consumer_from_container(self, container_id, **kwargs):
+        uri = "/v1/containers/%s/consumers" % container_id
+
+        response, body = self.delete(uri, body=json.dumps(kwargs))
+        self.expected_success(200, response.status)
+        return json.loads(body)
diff --git a/barbican_tempest_plugin/tests/api/base.py b/barbican_tempest_plugin/tests/api/base.py
index 6b37bc5..c5a7597 100644
--- a/barbican_tempest_plugin/tests/api/base.py
+++ b/barbican_tempest_plugin/tests/api/base.py
@@ -59,10 +59,13 @@
     def setup_clients(cls):
         super(BaseKeyManagerTest, cls).setup_clients()
         os = getattr(cls, 'os_%s' % cls.credentials[0])
-        cls.secret_client = os.secret_v1.SecretClient(service='key-manager')
+        cls.consumer_client = os.secret_v1.ConsumerClient(
+            service='key-manager'
+        )
         cls.container_client = os.secret_v1.ContainerClient(
             service='key-manager'
         )
+        cls.secret_client = os.secret_v1.SecretClient(service='key-manager')
 
     @classmethod
     def resource_setup(cls):
diff --git a/barbican_tempest_plugin/tests/api/test_consumers.py b/barbican_tempest_plugin/tests/api/test_consumers.py
new file mode 100644
index 0000000..bd11cc8
--- /dev/null
+++ b/barbican_tempest_plugin/tests/api/test_consumers.py
@@ -0,0 +1,96 @@
+# Copyright (c) 2016 Johns Hopkins University Applied Physics Laboratory
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from barbican_tempest_plugin.tests.api import base
+
+
+class ConsumersTest(base.BaseKeyManagerTest):
+    """Containers API tests."""
+
+    def test_add_delete_consumers_in_container(self):
+        # Create a container to test against
+        body = self.create_container(
+            type="generic",
+            name="consumer-container"
+        )
+        container_id = base._get_uuid(body.get('container_ref'))
+
+        # Confirm that the container has no consumers
+        body = self.consumer_client.list_consumers_in_container(container_id)
+        self.assertEqual(0, body.get('total'), body)
+        self.assertEmpty(body.get('consumers'), body)
+
+        # Add some consumers to the container
+        body = self.consumer_client.add_consumer_to_container(
+            container_id,
+            name="consumer1",
+            URL="url1"
+        )
+        self.assertEqual(
+            container_id,
+            base._get_uuid(body.get('container_ref')),
+            body
+        )
+        self.assertEqual(1, len(body.get('consumers')), body)
+        body = self.consumer_client.add_consumer_to_container(
+            container_id,
+            name="consumer2",
+            URL="url2"
+        )
+        self.assertEqual(
+            container_id,
+            base._get_uuid(body.get('container_ref')),
+            body
+        )
+        self.assertEqual(2, len(body.get('consumers')), body)
+
+        # Confirm that the consumers are in the container
+        body = self.consumer_client.list_consumers_in_container(container_id)
+        self.assertEqual(2, body.get('total'), body)
+        self.assertEqual(2, len(body.get('consumers')), body)
+        for consumer in body.get('consumers'):
+            self.assertIn(consumer.get('name'), ("consumer1", "consumer2"))
+            self.assertIn(consumer.get('URL'), ("url1", "url2"))
+
+        # Remove the consumers from the container
+        body = self.consumer_client.delete_consumer_from_container(
+            container_id,
+            name="consumer1",
+            URL="url1"
+        )
+        self.assertEqual(
+            container_id,
+            base._get_uuid(body.get('container_ref')),
+            body
+        )
+        self.assertEqual(1, len(body.get('consumers')), body)
+        body = self.consumer_client.delete_consumer_from_container(
+            container_id,
+            name="consumer2",
+            URL="url2"
+        )
+        self.assertEqual(
+            container_id,
+            base._get_uuid(body.get('container_ref')),
+            body
+        )
+        self.assertEqual(0, len(body.get('consumers')), body)
+
+        # Confirm that the container has no consumers
+        body = self.consumer_client.list_consumers_in_container(container_id)
+        self.assertEqual(0, body.get('total'), body)
+        self.assertEqual(0, len(body.get('consumers')), body)
+
+        # Clean up the container
+        self.delete_container(container_id)