-
-
-
- // Accepts a map of strings and
- // a) validates if all parameters are appropriate (else, throws an exception)
- // and b) if correct returns a JSON object with appropriate key-value
- // pairs to send to the server.
- public JSONObject ReqMessage(Map <String, String> params) throws APPCException, NumberFormatException, JSONException{
-
- // Mandatory parameters, that must be in the supplied information to the Ansible Adapter
- // 1. URL to connect to
- // 2. credentials for URL (assume username password for now)
- // 3. Playbook name
- String[] mandatoryTestParams = {"AgentUrl", "PlaybookName", "User", "Password"};
-
- // Optional testService parameters that may be provided in the request
- String[] optionalTestParams = {"EnvParameters", "NodeList", "LocalParameters", "Timeout", "Version", "FileParameters", "Action"};
-
- JSONObject JsonPayload = new JSONObject();
- String payload = "";
- JSONObject paramsJson;
-
-
- // Verify all the mandatory parameters are there
- for (String key: mandatoryTestParams){
- if (! params.containsKey(key)){
- throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key));
- }
- payload = params.get(key);
- if (Strings.isNullOrEmpty(payload)){
- throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key % value is Null or Emtpy", key));
- }
-
- JsonPayload.put(key, payload);
- }
-
- // Iterate through optional parameters
- // If null or empty omit it
- for (String key : optionalTestParams){
- if (params.containsKey(key)){
- payload = params.get(key);
- if(!Strings.isNullOrEmpty(payload)){
-
- // different cases require different treatment
- switch (key){
- case "Timeout":
- int Timeout = Integer.parseInt(payload);
- if (Timeout < 0){
- throw new NumberFormatException(" : specified negative integer for timeout = " + payload);
- }
- JsonPayload.put(key, payload);
- break;
-
- case "Version":
- JsonPayload.put(key, payload);
- break;
-
- case "LocalParameters":
- paramsJson = new JSONObject(payload);
- JsonPayload.put(key, paramsJson);
- break;
-
- case "EnvParameters":
- paramsJson = new JSONObject(payload);
- JsonPayload.put(key, paramsJson);
- break;
-
- case "NodeList":
- JSONArray paramsArray = new JSONArray(payload);
- JsonPayload.put(key, paramsArray);
- break;
-
- case "FileParameters":
- // Files may have strings with newlines. Escape them as appropriate
- String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r");
- JSONObject fileParams = new JSONObject(formattedPayload);
- JsonPayload.put(key, fileParams);
- break;
-
- }
- }
- }
- }
-
-
- // Generate a unique uuid for the test
- String ReqId = UUID.randomUUID().toString();
- JsonPayload.put("Id", ReqId);
-
- return JsonPayload;
-
+ private static final String STATUS_MESSAGE_KEY = "StatusMessage";
+ private static final String STATUS_CODE_KEY = "StatusCode";
+
+ private static final String PLAYBOOK_NAME_KEY = "PlaybookName";
+ private static final String AGENT_URL_KEY = "AgentUrl";
+ private static final String PASS_KEY = "Password";
+ private static final String USER_KEY = "User";
+ private static final String ID_KEY = "Id";
+
+ private static final String LOCAL_PARAMETERS_OPT_KEY = "LocalParameters";
+ private static final String FILE_PARAMETERS_OPT_KEY = "FileParameters";
+ private static final String ENV_PARAMETERS_OPT_KEY = "EnvParameters";
+ private static final String NODE_LIST_OPT_KEY = "NodeList";
+ private static final String TIMEOUT_OPT_KEY = "Timeout";
+ private static final String VERSION_OPT_KEY = "Version";
+ private static final String ACTION_OPT_KEY = "Action";
+
+ /**
+ * Accepts a map of strings and
+ * a) validates if all parameters are appropriate (else, throws an exception) and
+ * b) if correct returns a JSON object with appropriate key-value pairs to send to the server.
+ *
+ * Mandatory parameters, that must be in the supplied information to the Ansible Adapter
+ * 1. URL to connect to
+ * 2. credentials for URL (assume username password for now)
+ * 3. Playbook name
+ *
+ */
+ public JSONObject reqMessage(Map<String, String> params) throws APPCException {
+ final String[] mandatoryTestParams = { AGENT_URL_KEY, PLAYBOOK_NAME_KEY, USER_KEY, PASS_KEY };
+ final String[] optionalTestParams = { ENV_PARAMETERS_OPT_KEY, NODE_LIST_OPT_KEY, LOCAL_PARAMETERS_OPT_KEY,
+ TIMEOUT_OPT_KEY, VERSION_OPT_KEY, FILE_PARAMETERS_OPT_KEY, ACTION_OPT_KEY };
+
+ JSONObject jsonPayload = new JSONObject();
+
+ for (String key : mandatoryTestParams) {
+ throwIfMissingMandatoryParam(params, key);
+ jsonPayload.put(key, params.get(key));
+ }
+
+ parseOptionalParams(params, optionalTestParams, jsonPayload);
+
+ // Generate a unique uuid for the test
+ String reqId = UUID.randomUUID().toString();
+ jsonPayload.put(ID_KEY, reqId);
+
+ return jsonPayload;