2 """Main script to import json data for any data category from Mammoth.""" 9 from ody_migr_utils
import generate_hash, get_valid_json
10 from ody_migr_config
import (ENV_MIGR_SECRET_KEY,
12 DATA_OPT_SWITCH_ACCOUNTS,
18 LOGGER = logging.getLogger(__name__)
22 """Retrieve response from Mammoth.""" 38 err_msg =
"{} server must only be used for {} operation.".format(
39 self.
server, DATA_OPT_GETCULIST)
41 raise SystemExit(err_msg)
54 "endpoint_script",
"hcuadm/mOdysseyMigrExp.prg")
57 if (_data_category == DATA_OPT_MEMDATA
or 58 _data_category == DATA_OPT_CREATE_HIST
or 59 _data_category == DATA_OPT_SWITCH_ACCOUNTS):
61 assert "params" in kwargs
62 self.
payload = kwargs.get(
"params")
65 elif _data_category == DATA_OPT_GET_HIST:
66 assert "stream" in kwargs
72 """Generate a Mammoth endpoint url""" 74 os.environ.get(ENV_MIGR_SECRET_KEY))
75 server_dest =
"https://{}.homecu.net".format(self.
server)
77 resource_dest = (
"{}?" 84 resource_dest = (
"{}?" 91 return os.path.join(server_dest, resource_dest)
94 """Validate response data returned from Mammoth. 102 resp_error = DEFAULT_ERROR_STR
107 "Content-Type"].strip() ==
"application/json":
108 resp_error = self.
response.json()[
"error"]
109 if resp_error != DEFAULT_ERROR_STR:
110 if "Process is already started" in resp_error:
112 if "ERROR:" in resp_error:
113 resp_error = resp_error.split(
"ERROR:")[1].strip()
120 "Content-Type"].split(
";")[0].strip() ==
"text/plain":
121 resp_error = self.
response.content.decode()
122 if "Process is already started" in resp_error:
124 if "ERROR:" in resp_error:
125 resp_error = resp_error.split(
"ERROR:")[1].strip()
133 if resp_error != DEFAULT_ERROR_STR:
134 if "Process is already started" in resp_error:
136 if "ERROR:" in resp_error:
137 resp_error = resp_error.split(
"ERROR:")[1].strip()
143 LOGGER.error(
"Mammoth endpoint status: {}".format(resp_error))
145 "Migration Request with `cu = {} and " 146 "data_category = {}` returned with error. Error: {}" 152 resp_error =
"NORMAL!" if resp_error == DEFAULT_ERROR_STR \
154 LOGGER.info(
"Mammoth endpoint status: {}".format(resp_error))
157 """Establish connection with Mammoth and obtain response object""" 162 with requests.Session()
as s:
177 status_code = http_resp.status_code
178 if int(status_code) >= 300:
179 http_resp.raise_for_status()
181 LOGGER.info(
"HTTP response url: {}".format(http_resp.url))
182 if int(status_code) == 200:
189 except (requests.exceptions.HTTPError,
190 requests.exceptions.ConnectionError)
as err:
191 LOGGER.error(
"Mammoth endpoint status: {}".format(err))
192 raise SystemExit(err)
195 """Connect to mammoth endpoint and fetch response json data""" 201 return get_valid_json(self.
response)
def _validate_response(self)
def _initiate_http_request(self)