Add API to get environments 33/37333/2
authorchenjiankun <chenjiankun1@huawei.com>
Thu, 13 Jul 2017 06:14:43 +0000 (06:14 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Thu, 13 Jul 2017 07:12:47 +0000 (07:12 +0000)
JIRA: YARDSTICK-716

API: /api/v2/yardstick/environments
METHOD: GET

Change-Id: I46b7fb2b143fe76b6a0edbf1ecc8281187b85918
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
api/database/v2/handlers.py
api/resources/v2/environments.py
api/urls.py
tests/unit/common/test_utils.py
yardstick/common/utils.py

index eb73281..095ad72 100644 (file)
@@ -24,6 +24,9 @@ class V2EnvironmentHandler(object):
         db_session.commit()
         return environment
 
+    def list_all(self):
+        return V2Environment.query.all()
+
     def get_by_uuid(self, uuid):
         environment = V2Environment.query.filter_by(uuid=uuid).first()
         if not environment:
index dc6eac7..4b27c45 100644 (file)
@@ -1,9 +1,12 @@
 import uuid
 import logging
 
+from oslo_serialization import jsonutils
+
 from api import ApiResource
 from api.database.v2.handlers import V2EnvironmentHandler
 from yardstick.common.utils import result_handler
+from yardstick.common.utils import change_obj_to_dict
 from yardstick.common import constants as consts
 
 LOG = logging.getLogger(__name__)
@@ -12,6 +15,20 @@ LOG.setLevel(logging.DEBUG)
 
 class V2Environments(ApiResource):
 
+    def get(self):
+        environment_handler = V2EnvironmentHandler()
+        environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]
+
+        for e in environments:
+            container_info = e['container_id']
+            e['container_id'] = jsonutils.loads(container_info) if container_info else {}
+
+        data = {
+            'environments': environments
+        }
+
+        return result_handler(consts.API_SUCCESS, data)
+
     def post(self):
         return self._dispatch_post()
 
@@ -24,6 +41,7 @@ class V2Environments(ApiResource):
         env_id = str(uuid.uuid4())
 
         environment_handler = V2EnvironmentHandler()
+
         env_init_data = {
             'name': name,
             'uuid': env_id
index 0ffba89..0c63fb0 100644 (file)
@@ -22,5 +22,6 @@ urlpatterns = [
     Url('/yardstick/env/action', 'v1_env'),
 
     # api v2
+    Url('/api/v2/yardstick/environments', 'v2_environments'),
     Url('/api/v2/yardstick/environments/action', 'v2_environments'),
 ]
index 7f260cf..664b38b 100644 (file)
@@ -163,6 +163,19 @@ class TranslateToStrTestCase(unittest.TestCase):
         self.assertEqual(result, output_str)
 
 
+class ChangeObjToDictTestCase(unittest.TestCase):
+
+    def test_change_obj_to_dict(self):
+        class A(object):
+            def __init__(self):
+                self.name = 'yardstick'
+
+        obj = A()
+        obj_r = utils.change_obj_to_dict(obj)
+        obj_s = {'name': 'yardstick'}
+        self.assertEqual(obj_r, obj_s)
+
+
 def main():
     unittest.main()
 
index 1faba4d..0c0bac9 100644 (file)
@@ -232,3 +232,13 @@ def result_handler(status, data):
         'result': data
     }
     return jsonify(result)
+
+
+def change_obj_to_dict(obj):
+    dic = {}
+    for k, v in vars(obj).items():
+        try:
+            vars(v)
+        except TypeError:
+            dic.update({k: v})
+    return dic