def test_axapi_ex_no_http_method(self): response = {'response': {'err': {'code': 1023410176, 'msg': 'ni'}}} with self.assertRaises(ae.NotFound) as context: resp.raise_axapi_ex(response, 'CREATE', Mock()) self.assertTrue('1023410176 ni' in str(context.exception))
def test_axapi_ex_match(self): response = {'response': {'err': {'code': 1023459339, 'msg': 'ni'}}} with self.assertRaises(ae.Exists) as context: resp.raise_axapi_ex(response, 'CREATE', '/axapi/v3/slb/server') self.assertTrue('1023459339 ni' in str(context.exception))
def test_axapi_ex_http_method(self): response = {'response': {'err': {'code': 1023410176, 'msg': 'ni'}}} self.assertIsNone(resp.raise_axapi_ex(response, 'DELETE', Mock()))
def test_axapi_ex_unknown_code(self): response = {'response': {'err': {'code': 1234, 'msg': 'ni'}}} with self.assertRaises(ae.ACOSException) as context: resp.raise_axapi_ex(response, Mock(), Mock()) self.assertTrue('1234 ni' in str(context.exception))
def test_axapi_ex_no_response(self): response = {'response': {}} with self.assertRaises(ae.ACOSException) as context: resp.raise_axapi_ex(response, Mock(), Mock())
def request(self, method, api_url, params={}, headers=None, file_name=None, file_content=None, axapi_args=None, **kwargs): ''' Preforms HTTP/HTTPS requests against the AXAPI. Args: method (string): POST/GET/PUT/DELETE api_url (string): API endpoint **kwargs: arbitrary keyword arguments Kwargs: params (dict): payload headers (dict): HTTP headers file_name (string): name of the file for ftp file_contents (object): file to be uploaded axapi_args (dict): extra axapi arguments Returns (dict): JSON response from the AXAPI ''' LOG.debug("axapi_http: full url = %s", self.url_base + api_url) LOG.debug("axapi_http: %s url = %s", method, api_url) # Update params with axapi_args for currently unsupported configuration of objects if axapi_args is not None: formatted_axapi_args = dict([(k.replace('_', '-'), v) for k, v in axapi_args.iteritems()]) params = self.merge_dicts(params, formatted_axapi_args) if (file_name is None and file_content is not None) or \ (file_name is not None and file_content is None): raise ValueError("file_name and file_content must both be " "populated if one is") hdrs = self.HEADERS.copy() if headers: hdrs.update(headers) if params: params_copy = params.copy() payload = json.dumps(params_copy) else: payload = None if file_name is not None: files = { 'file': (file_name, file_content, "application/octet-stream"), 'json': ('blob', payload, "application/json") } hdrs.pop("Content-type", None) hdrs.pop("Content-Type", None) last_e = None try: last_e = None if file_name is not None: z = requests.request(method, self.url_base + api_url, verify=False, files=files, headers=hdrs) else: z = requests.request(method, self.url_base + api_url, verify=False, data=payload, headers=hdrs) except (socket.error, requests.exceptions.ConnectionError) as e: raise e if z.status_code == 204: return None try: r = z.json() except ValueError as e: if z.status_code == 200: return {} else: raise e if 'response' in r and 'status' in r['response']: if r['response']['status'] == 'fail': acos_responses.raise_axapi_ex(r, method, api_url) if 'authorizationschema' in r: acos_responses.raise_axapi_auth_error( r, method, api_url, headers) return r
def request(self, method, api_url, params={}, headers=None, file_name=None, file_content=None, axapi_args=None, **kwargs): LOG.debug("axapi_http: full url = %s", self.url_base + api_url) LOG.debug("axapi_http: %s url = %s", method, api_url) # Update params with axapi_args for currently unsupported configuration of objects if axapi_args is not None: formatted_axapi_args = dict([(k.replace('_', '-'), v) for k, v in axapi_args.iteritems()]) params = self.merge_dicts(params, formatted_axapi_args) if (file_name is None and file_content is not None) or \ (file_name is not None and file_content is None): raise ValueError("file_name and file_content must both be " "populated if one is") hdrs = self.HEADERS.copy() if headers: hdrs.update(headers) if params: params_copy = params.copy() payload = json.dumps(params_copy) else: payload = None if file_name is not None: files = { 'file': (file_name, file_content, "application/octet-stream"), 'json': ('blob', payload, "application/json") } hdrs.pop("Content-type", None) hdrs.pop("Content-Type", None) last_e = None try: last_e = None if file_name is not None: z = requests.request(method, self.url_base + api_url, verify=False, files=files, headers=hdrs) else: z = requests.request(method, self.url_base + api_url, verify=False, data=payload, headers=hdrs) except (socket.error, requests.exceptions.ConnectionError) as e: raise e if z.status_code == 204: return None try: r = z.json() except ValueError as e: if z.status_code == 200: return {} else: raise e if 'response' in r and 'status' in r['response']: if r['response']['status'] == 'fail': acos_responses.raise_axapi_ex(r, method, api_url) if 'authorizationschema' in r: acos_responses.raise_axapi_auth_error(r, method, api_url, headers) return r