11import json
2-
32from cloudshell .api .cloudshell_api import CloudShellAPISession
43
54
5+ # OPTIONAL SCRIPT PARAMETERS, IF PRESENT WILL OVERRIDE THE DEFAULT READ-ONLY VALUES
6+ REPO_URL_PARAM = "REPO_URL"
7+ REPO_USER_PARAM = "REPO_USER"
8+ REPO_PASSWORD_PARAM = "REPO_PASSWORD"
9+ CONNECTION_METHOD_PARAM = "CONNECTION_METHOD"
10+
11+
612class AnsibleConfiguration (object ):
713 def __init__ (self , playbook_repo = None , hosts_conf = None , additional_cmd_args = None , timeout_minutes = None ):
814 """
@@ -16,6 +22,9 @@ def __init__(self, playbook_repo=None, hosts_conf=None, additional_cmd_args=None
1622 self .hosts_conf = hosts_conf or []
1723 self .additional_cmd_args = additional_cmd_args
1824
25+ def get_pretty_json (self ):
26+ return json .dumps (self , default = lambda o : getattr (o , '__dict__' , str (o )), indent = 4 )
27+
1928
2029class PlaybookRepository (object ):
2130 def __init__ (self ):
@@ -36,6 +45,29 @@ def __init__(self):
3645 self .parameters = {}
3746
3847
48+ def over_ride_defaults (ansi_conf , params_dict ):
49+ """
50+ go over custom params and over-ride values
51+ :param AnsibleConfiguration ansi_conf:
52+ :param dict params_dict:
53+ :return same config:
54+ :rtype AnsibleConfiguration
55+ """
56+ if params_dict .get (REPO_URL_PARAM ):
57+ ansi_conf .playbook_repo .url = params_dict [REPO_URL_PARAM ]
58+
59+ if params_dict .get (REPO_USER_PARAM ):
60+ ansi_conf .playbook_repo .username = params_dict [REPO_USER_PARAM ]
61+
62+ if params_dict .get (REPO_PASSWORD_PARAM ):
63+ ansi_conf .playbook_repo .password = params_dict [REPO_PASSWORD_PARAM ]
64+
65+ if params_dict .get (CONNECTION_METHOD_PARAM ):
66+ ansi_conf .hosts_conf [0 ].connection_method = params_dict [CONNECTION_METHOD_PARAM ].lower ()
67+
68+ return ansi_conf
69+
70+
3971class AnsibleConfigurationParser (object ):
4072
4173 def __init__ (self , api ):
@@ -72,7 +104,9 @@ def json_to_object(self, json_str):
72104 host_conf .access_key = self ._get_access_key (json_host )
73105 host_conf .groups = json_host .get ('groups' )
74106 if json_host .get ('parameters' ):
75- host_conf .parameters = dict ((i ['name' ], i ['value' ]) for i in json_host ['parameters' ])
107+ all_params_dict = dict ((i ['name' ], i ['value' ]) for i in json_host ['parameters' ])
108+ host_conf .parameters = all_params_dict
109+ ansi_conf = over_ride_defaults (ansi_conf , all_params_dict )
76110 ansi_conf .hosts_conf .append (host_conf )
77111
78112 return ansi_conf
@@ -94,7 +128,7 @@ def _get_access_key(self, json_host):
94128 @staticmethod
95129 def _validate (json_obj ):
96130 """
97- :type json_obj: json
131+ :type json_obj: dict
98132 :rtype bool
99133 """
100134 basic_msg = 'Failed to parse ansible configuration input json: '
0 commit comments