Create API to update hosts info 03/39703/7
authorLinda Wang <wangwulin@huawei.com>
Mon, 21 Aug 2017 03:24:28 +0000 (03:24 +0000)
committerLinda Wang <wangwulin@huawei.com>
Fri, 25 Aug 2017 03:27:24 +0000 (03:27 +0000)
API: /api/v1/functest/envs/action
METHOD: POST
PARAMS:
{
    "action": "update_hosts",
    "args": {
            "identity.ac.dz.com": "8.20.11.22",
            "image.ac.dz.com": "8.20.11.22"
    }
}

JIRA: FUNCTEST-856

Change-Id: Ifaf064110ade6e39189dd14b38c921d9e1c3568d
Signed-off-by: Linda Wang <wangwulin@huawei.com>
functest/api/resources/v1/envs.py
functest/api/urls.py
requirements.txt
upper-constraints.txt

index 9c45519..fb76fa6 100644 (file)
 Resources to handle environment related requests
 """
 
+import IPy
 from flask import jsonify
 
 from functest.api.base import ApiResource
-from functest.cli.commands.cli_env import Env
 from functest.api.common import api_utils
+from functest.cli.commands.cli_env import Env
 import functest.utils.functest_utils as ft_utils
 
 
@@ -38,3 +39,41 @@ class V1Envs(ApiResource):
             return api_utils.result_handler(status=1, data=str(err))
         return api_utils.result_handler(
             status=0, data="Prepare env successfully")
+
+    def update_hosts(self, hosts_info):  # pylint: disable=no-self-use
+        """ Update hosts info """
+
+        if not isinstance(hosts_info, dict):
+            return api_utils.result_handler(
+                status=1, data='Error, args should be a dict')
+
+        for key, value in hosts_info.items():
+            if key:
+                try:
+                    IPy.IP(value)
+                except Exception:  # pylint: disable=broad-except
+                    return api_utils.result_handler(
+                        status=1, data='The IP %s is invalid' % value)
+            else:
+                return api_utils.result_handler(
+                    status=1, data='Domain name is absent')
+
+        try:
+            functest_flag = "# SUT hosts info for Functest"
+            hosts_list = ('\n{} {} {}'.format(ip, host_name, functest_flag)
+                          for host_name, ip in hosts_info.items())
+
+            with open("/etc/hosts", 'r') as file_hosts:
+                origin_lines = [line for line in file_hosts
+                                if functest_flag not in line]
+
+            with open("/etc/hosts", 'w') as file_hosts:
+                file_hosts.writelines(origin_lines)
+                file_hosts.write(functest_flag)
+                file_hosts.writelines(hosts_list)
+        except Exception:  # pylint: disable=broad-except
+            return api_utils.result_handler(
+                status=1, data='Error when updating hosts info')
+        else:
+            return api_utils.result_handler(
+                status=0, data='Update hosts info successfully')
index f7bcae3..59bf7d5 100644 (file)
@@ -25,8 +25,9 @@ URLPATTERNS = [
     # GET /api/v1/functest/envs => GET environment
     Url('/api/v1/functest/envs', 'v1_envs'),
 
-    # POST /api/v1/functest/envs/action , {"action":"prepare"}
-    # => Prepare environment
+    # POST /api/v1/functest/envs/action
+    # {"action":"prepare"} => Prepare environment
+    # {"action":"update_hosts", "args": {}} => Update hosts info
     Url('/api/v1/functest/envs/action', 'v1_envs'),
 
     # GET /api/v1/functest/openstack/credentials => GET credentials
index e1d34a3..ba5d91c 100644 (file)
@@ -23,6 +23,7 @@ openbaton-cli
 cloudify_rest_client
 Flask!=0.11,<1.0,>=0.10 # BSD
 Flask-RESTful>=0.3.5 # BSD
+IPy
 mock>=2.0 # BSD
 iniparse==0.4
 PrettyTable<0.8,>=0.7.1 # BSD
index 74d363c..fae9427 100644 (file)
@@ -14,3 +14,4 @@ robotframework-httplibrary===0.4.2
 robotframework-requests===0.4.7
 robotframework-sshlibrary===2.1.3;python_version=='2.7'
 rally===0.9.1
+IPy===0.83