Odyssey
Public Member Functions | Public Attributes | Private Member Functions | List of all members
ody_migr_mmth_endpoint.MammothMigration Class Reference

Public Member Functions

def __init__ (self, _cu, _data_category, _server, _username, _password, **kwargs)
 
def run (self)
 
def __str__ (self)
 

Public Attributes

 cu
 
 data_category
 
 server
 
 username
 
 password
 
 payload
 
 getstream
 
 histstatus
 
 endpoint_script
 
 response
 

Private Member Functions

def _dest_url (self)
 
def _validate_response (self)
 
def _initiate_http_request (self)
 

Detailed Description

Retrieve response from Mammoth.

Definition at line 21 of file ody_migr_mmth_endpoint.py.

Member Function Documentation

◆ _dest_url()

def ody_migr_mmth_endpoint.MammothMigration._dest_url (   self)
private
Generate a Mammoth endpoint url

Definition at line 71 of file ody_migr_mmth_endpoint.py.

71  def _dest_url(self):
72  """Generate a Mammoth endpoint url"""
73  hash = generate_hash(self.data_category, self.cu,
74  os.environ.get(ENV_MIGR_SECRET_KEY))
75  server_dest = "https://{}.homecu.net".format(self.server)
76  if self.data_category == DATA_OPT_GETCULIST:
77  resource_dest = ("{}?"
78  "passphrase={}&"
79  "action={}").format(self.endpoint_script,
80  hash,
81  self.data_category)
82 
83  else:
84  resource_dest = ("{}?"
85  "cu={}&"
86  "passphrase={}&"
87  "action={}").format(self.endpoint_script,
88  self.cu,
89  hash,
90  self.data_category)
91  return os.path.join(server_dest, resource_dest)
92 

◆ _initiate_http_request()

def ody_migr_mmth_endpoint.MammothMigration._initiate_http_request (   self)
private
Establish connection with Mammoth and obtain response object

Definition at line 156 of file ody_migr_mmth_endpoint.py.

156  def _initiate_http_request(self):
157  """Establish connection with Mammoth and obtain response object"""
158 
159  try:
160  # we run http connection within the requests context manager
161  # in order to avoid possible resource leaks
162  with requests.Session() as s:
163  url = self._dest_url()
164  if self.payload is None:
165  http_resp = s.get(
166  url,
167  auth=(self.username, self.password),
168  stream=self.getstream
169  )
170  else:
171  http_resp = s.post(
172  url,
173  auth=(self.username, self.password),
174  data=self.payload
175  )
176 
177  status_code = http_resp.status_code
178  if int(status_code) >= 300:
179  http_resp.raise_for_status()
180 
181  LOGGER.info("HTTP response url: {}".format(http_resp.url))
182  if int(status_code) == 200:
183  if self.data_category == DATA_OPT_GET_HIST:
184  self.response = http_resp
185 
186  else:
187  self.response = http_resp.json()
188 
189  except (requests.exceptions.HTTPError,
190  requests.exceptions.ConnectionError) as err:
191  LOGGER.error("Mammoth endpoint status: {}".format(err))
192  raise SystemExit(err)
193 

◆ _validate_response()

def ody_migr_mmth_endpoint.MammothMigration._validate_response (   self)
private
Validate response data returned from Mammoth.

Raises:
     SystemExit on error

Definition at line 93 of file ody_migr_mmth_endpoint.py.

93  def _validate_response(self):
94  """Validate response data returned from Mammoth.
95 
96  Raises:
97  SystemExit on error
98  """
99 
100  # at this point, response dict must have already been populated
101  error_flag = False
102  resp_error = DEFAULT_ERROR_STR
103 
104  if self.data_category == DATA_OPT_GET_HIST:
105  # check for error in application/json response
106  if dict(self.response.headers)[
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:
111  # do not consider this case as error
112  if "ERROR:" in resp_error:
113  resp_error = resp_error.split("ERROR:")[1].strip()
114  self.histstatus = resp_error
115  else:
116  error_flag = True
117 
118  # check for error in text/plain response
119  if dict(self.response.headers)[
120  "Content-Type"].split(";")[0].strip() == "text/plain":
121  resp_error = self.response.content.decode()
122  if "Process is already started" in resp_error:
123  # do not consider this case as error
124  if "ERROR:" in resp_error:
125  resp_error = resp_error.split("ERROR:")[1].strip()
126  self.histstatus = resp_error
127  else:
128  error_flag = True
129 
130  else:
131  assert self.response != {}
132  resp_error = self.response["error"]
133  if resp_error != DEFAULT_ERROR_STR:
134  if "Process is already started" in resp_error:
135  # do not consider this case as error
136  if "ERROR:" in resp_error:
137  resp_error = resp_error.split("ERROR:")[1].strip()
138  self.histstatus = resp_error
139  else:
140  error_flag = True
141 
142  if error_flag:
143  LOGGER.error("Mammoth endpoint status: {}".format(resp_error))
144  raise SystemExit(
145  "Migration Request with `cu = {} and "
146  "data_category = {}` returned with error. Error: {}"
147  .format(
148  self.cu,
149  self.data_category,
150  resp_error))
151  else:
152  resp_error = "NORMAL!" if resp_error == DEFAULT_ERROR_STR \
153  else resp_error
154  LOGGER.info("Mammoth endpoint status: {}".format(resp_error))
155 

◆ run()

def ody_migr_mmth_endpoint.MammothMigration.run (   self)
Connect to mammoth endpoint and fetch response json data

Definition at line 194 of file ody_migr_mmth_endpoint.py.

194  def run(self):
195  """Connect to mammoth endpoint and fetch response json data"""
196  self._initiate_http_request()
197  self._validate_response()
198  return self.response
199 

The documentation for this class was generated from the following file: