aboutsummaryrefslogtreecommitdiffstats
path: root/functest
diff options
context:
space:
mode:
authorMorgan Richomme <morgan.richomme@orange.com>2017-08-25 12:14:36 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-08-25 12:14:36 +0000
commit6e1245a7d19d62810fbe5ee1aab248a85fa2bc15 (patch)
treecb10db703316cf974eafdd0f6596b86ae1af279f /functest
parent20f03e5678f7eb2280d8a0c0ec37b18a00a92e3b (diff)
parent8252374c2f165feea0f407b51deeb31930fb092e (diff)
Merge "Create API to update hosts info"
Diffstat (limited to 'functest')
-rw-r--r--functest/api/resources/v1/envs.py41
-rw-r--r--functest/api/urls.py5
2 files changed, 43 insertions, 3 deletions
diff --git a/functest/api/resources/v1/envs.py b/functest/api/resources/v1/envs.py
index 9c455198..fb76fa63 100644
--- a/functest/api/resources/v1/envs.py
+++ b/functest/api/resources/v1/envs.py
@@ -10,11 +10,12 @@
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')
diff --git a/functest/api/urls.py b/functest/api/urls.py
index f7bcae38..59bf7d51 100644
--- a/functest/api/urls.py
+++ b/functest/api/urls.py
@@ -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