def test_timeout(self): URL = "/".join((TestFunctionRequests.URL_TEST_HTTP_STATUS_CODES, "200?sleep=30000")) rc = RequestsCommon(None, None) with self.assertRaises(IntegrationError): resp = rc.execute_call("get", URL, None, resp_type='text', timeout=10)
def test_resp_types(self): IPIFY = TestFunctionRequests.URL_TEST_DATA_RESULTS rc = RequestsCommon(None, None) # J S O N json_result = rc.execute_call("get", "{}?format=json".format(IPIFY), None) try: json.dumps(json_result) except (TypeError, OverflowError): self.fail("result is not json") # T E X T text_result = rc.execute_call("get", "{}?format=text".format(IPIFY), None, resp_type='text') self.assertIsNotNone(text_result) # B Y T E S bytes_result = rc.execute_call("get", "{}?format=text".format(IPIFY), None, resp_type='bytes') self.assertIsNotNone(bytes_result) self.assertTrue(isinstance(bytes_result, bytes))
def test_basicauth(self): URL = "/".join((TestFunctionRequests.URL_TEST_HTTP_VERBS, "basic-auth")) basicauth = ("postman", "password") rc = RequestsCommon(None, None) resp = rc.execute_call("get", URL, None, basicauth=basicauth) self.assertTrue(resp.get("authenticated"))
def test_statuscode(self): URL = TestFunctionRequests.URL_TEST_HTTP_STATUS_CODES rc = RequestsCommon(None, None) resp = rc.execute_call("get", "/".join((URL, "200")), None, resp_type='text') with self.assertRaises(IntegrationError): resp = rc.execute_call("get", "/".join((URL, "300")), None, resp_type='text')
def test_v2_resp_type(self): IPIFY = TestFunctionRequests.URL_TEST_DATA_RESULTS rc = RequestsCommon(None, None) # R E S P O N S E Object response = rc.execute_call_v2("get", "{}?format=json".format(IPIFY)) self.assertTrue(isinstance(response, requests.models.Response))
def test_statuscode(self): URL = "/".join((TestFunctionRequests.URL_TEST_HTTP_STATUS_CODES, "300")) def callback(resp): if resp.status_code != 300: raise ValueError(resp.status_code) rc = RequestsCommon(None, None) resp = rc.execute_call("get", URL, None, resp_type='text', callback=callback)
def test_headers(self): # G E T with headers headers = { "Content-type": "application/json; charset=UTF-8", "my-sample-header": "my header" } URL = "/".join((TestFunctionRequests.URL_TEST_HTTP_VERBS, "headers")) rc = RequestsCommon() json_result = rc.execute_call("get", URL, None, headers=headers) self.assertEqual(json_result['headers'].get("my-sample-header"), "my header")
def test_verbs(self): URL = TestFunctionRequests.URL_TEST_HTTP_VERBS headers = { "Content-type": "application/json; charset=UTF-8" } payload = { 'title': 'foo', 'body': 'bar', 'userId': 1 } rc = RequestsCommon(None, None) # P O S T # test data argument resp = rc.execute_call("post", "/".join((URL, "post")), payload, headers=headers, log=TestFunctionRequests.LOG) print (resp) self.assertEqual(resp['data'], "body=bar&userId=1&title=foo") # test json argument resp = rc.execute_call("post", "/".join((URL, "post")), payload, log=TestFunctionRequests.LOG) print (resp) self.assertEqual(resp['json'].get("body"), "bar") # G E T resp = rc.execute_call("get", "/".join((URL, "get")), payload, log=TestFunctionRequests.LOG) self.assertTrue(resp['args'].get("userId")) self.assertEqual(resp['args'].get("userId"), '1') # P U T resp = rc.execute_call("put", "/".join((URL, "put")), payload, headers=headers, log=TestFunctionRequests.LOG) TestFunctionRequests.LOG.info(resp) self.assertTrue(resp['args'].get("title")) self.assertEqual(resp['args'].get("title"), 'foo') # P A T C H patch = { 'title': 'patch' } resp = rc.execute_call("patch", "/".join((URL, "patch")), patch, headers=headers, log=TestFunctionRequests.LOG) print ("resp {}".format(resp)) self.assertTrue(resp['args'].get("title")) self.assertEqual(resp['args'].get("title"), 'patch') # D E L E T E DEL_URL = "/".join((URL, "delete")) resp = rc.execute_call("delete", DEL_URL, None, log=TestFunctionRequests.LOG) self.assertEqual(resp.get("url"), DEL_URL) # bad verb with self.assertRaises(IntegrationError): resp = rc.execute_call("bad", URL, None, log=TestFunctionRequests.LOG)
def __init__(self, opts, location, vsys=None): pan_config = opts.get("fn_pa_panorama") pan_config["location"] = location # validate config fields validate_fields(["panorama_host", "api_key", "location"], pan_config) self.__key = pan_config["api_key"] self.__location = pan_config["location"] self.__vsys = vsys self.__output_format = "json" self.verify = str_to_bool(pan_config.get("cert", "True")) self.host = pan_config["panorama_host"] self.rc = RequestsCommon(opts, pan_config) self.query_parameters = self.__build_query_parameters()
def test_proxy(self): rc = RequestsCommon() proxy_url = TestFunctionRequests.URL_TEST_PROXY proxy_result = rc.execute_call("get", proxy_url, None) proxies = { 'http': proxy_result['curl'] if proxy_result['protocol'] == 'http' else None, 'https': proxy_result['curl'] if proxy_result['protocol'] == 'https' else None } URL = "?".join((TestFunctionRequests.URL_TEST_DATA_RESULTS, "format=json")) # J S O N json_result = rc.execute_call("get", URL, None, proxies=proxies) self.assertTrue(json_result.get("ip")) integrations = { "integrations": { 'http_proxy': proxy_result['curl'] if proxy_result['protocol'] == 'http' else None, 'https_proxy': proxy_result['curl'] if proxy_result['protocol'] == 'https' else None } } rc = RequestsCommon(opts=integrations) json_result = rc.execute_call("get", URL, None) self.assertTrue(json_result.get("ip"))
def test_resilient_common_proxies(self): rc = RequestsCommon() self.assertIsNone(rc.get_proxies()) integrations = { } rc = RequestsCommon(opts=integrations) self.assertIsNone(rc.get_proxies()) integrations = { "integrations": { } } rc = RequestsCommon(integrations, None) self.assertIsNone(rc.get_proxies()) integrations = { "integrations": { "https_proxy": "abc" } } rc = RequestsCommon(function_opts=None, opts=integrations) proxies = rc.get_proxies() self.assertEqual("abc", proxies['https']) self.assertIsNone(proxies['http']) integrations = { "integrations": { "https_proxy": "abc", 'http_proxy': 'def' } } rc = RequestsCommon(integrations) proxies = rc.get_proxies() self.assertEqual("abc", proxies['https']) self.assertEqual("def", proxies['http'])
def test_proxy_override(self): rc = RequestsCommon(None, None) proxies = rc.get_proxies() self.assertIsNone(proxies) # test only integration proxies integrations_xyz = { "integrations": { 'http_proxy': "http://xyz.com", 'https_proxy': "https://xyz.com" } } function_proxy_none = { 'http_proxy': None, 'https_proxy': None } rc = RequestsCommon(integrations_xyz) proxies = rc.get_proxies() self.assertEqual(proxies['http'], "http://xyz.com") self.assertEqual(proxies['https'], "https://xyz.com") rc = RequestsCommon(integrations_xyz, function_proxy_none) proxies = rc.get_proxies() self.assertEqual(proxies['http'], "http://xyz.com") self.assertEqual(proxies['https'], "https://xyz.com") # test only function proxies integrations_none = { "integrations": { 'http_proxy': None, 'https_proxy': None } } function_proxy_abc = { 'http_proxy': "http://abc.com", 'https_proxy': "https://abc.com" } rc = RequestsCommon(function_opts=function_proxy_abc) proxies = rc.get_proxies() self.assertEqual(proxies['http'], "http://abc.com") self.assertEqual(proxies['https'], "https://abc.com") rc = RequestsCommon(integrations_none, function_proxy_abc) proxies = rc.get_proxies() self.assertEqual(proxies['http'], "http://abc.com") self.assertEqual(proxies['https'], "https://abc.com") # test integration and function proxies (override) rc = RequestsCommon(integrations_xyz, function_proxy_abc) proxies = rc.get_proxies() self.assertEqual(proxies['http'], "http://abc.com") self.assertEqual(proxies['https'], "https://abc.com")
def test_verbs_v2(self): URL = TestFunctionRequests.URL_TEST_HTTP_VERBS headers = { "Content-type": "application/json; charset=UTF-8" } payload = { 'title': 'foo', 'body': 'bar', 'userId': 1 } rc = RequestsCommon(None, None) # P O S T # test json argument without headers resp = rc.execute_call_v2("post", "/".join((URL, "post")), json=payload) print (resp.json()) self.assertEqual(resp.json()["json"].get("body"), "bar") # test json argument with headers resp = rc.execute_call_v2("post", "/".join((URL, "post")), json=payload, headers=headers) print (resp.json()) self.assertEqual(resp.json()['json'].get("body"), "bar") # test data argument headers_data = { "Content-type": "application/x-www-form-urlencoded" } resp = rc.execute_call_v2("post", "/".join((URL, "post")), data=payload, headers=headers_data) print (resp.json()) self.assertEqual(resp.json()['json'].get("body"), "bar") # G E T resp = rc.execute_call_v2("get", "/".join((URL, "get")), params=payload) self.assertTrue(resp.json()['args'].get("userId")) self.assertEqual(resp.json()['args'].get("userId"), '1') # P U T # With params resp = rc.execute_call_v2("put", "/".join((URL, "put")), params=payload, headers=headers) TestFunctionRequests.LOG.info(resp) self.assertTrue(resp.json()['args'].get("title")) self.assertEqual(resp.json()['args'].get("title"), 'foo') # With json body resp = rc.execute_call_v2("put", "/".join((URL, "put")), json=payload, headers=headers) TestFunctionRequests.LOG.info(resp) self.assertTrue(resp.json()['json'].get("title")) self.assertEqual(resp.json()['json'].get("title"), 'foo') # P A T C H patch = { 'title': 'patch' } # With params resp = rc.execute_call_v2("patch", "/".join((URL, "patch")), params=patch, headers=headers) print ("resp {}".format(resp.json())) self.assertTrue(resp.json()['args'].get("title")) self.assertEqual(resp.json()['args'].get("title"), 'patch') # With json body resp = rc.execute_call_v2("patch", "/".join((URL, "patch")), json=patch, headers=headers) print ("resp {}".format(resp.json())) self.assertTrue(resp.json()['json'].get("title")) self.assertEqual(resp.json()['json'].get("title"), 'patch') # D E L E T E DEL_URL = "/".join((URL, "delete")) resp = rc.execute_call_v2("delete", DEL_URL) self.assertEqual(resp.json().get("url"), DEL_URL) # bad verb with self.assertRaises(IntegrationError): resp = rc.execute_call_v2("bad", URL)
class PanoramaClient: """ Object to handle the communication and authentication between the integration and Panorama """ def __init__(self, opts, location, vsys=None): pan_config = opts.get("fn_pa_panorama") pan_config["location"] = location # validate config fields validate_fields(["panorama_host", "api_key", "location"], pan_config) self.__key = pan_config["api_key"] self.__location = pan_config["location"] self.__url_path = "/".join( [URI_PATH, pan_config.get("api_version", DEFAULT_API_VERSION)]) self.__vsys = vsys self.__output_format = "json" self.verify = str_to_bool(pan_config.get("cert", "True")) self.host = pan_config["panorama_host"] self.rc = RequestsCommon(opts, pan_config) self.query_parameters = self.__build_query_parameters() def __build_query_parameters(self): query_params = dict() query_params["key"] = self.__key query_params["location"] = self.__location query_params["output-format"] = self.__output_format if self.__location == "vsys" or self.__location == "panorama-pushed": query_params["vsys"] = self.__vsys return query_params def __get(self, resource_uri, parameters): """Generic GET""" uri = self.__build_url(resource_uri) response = self.rc.execute_call_v2("GET", uri, params=parameters, verify=self.verify) log.debug("Response: %s", response) return response.json() def __post(self, resource_uri, params, payload): """Generic POST""" uri = self.__build_url(resource_uri) response = self.rc.execute_call_v2("POST", uri, params=params, json=json.loads(payload), verify=self.verify) log.debug("Status code: %s, Response: %s", response.status_code, response.content) return response.json() def __put(self, resource_uri, params, payload): """Generic PUT""" uri = self.__build_url(resource_uri) response = self.rc.execute_call_v2("PUT", uri, params=params, json=json.loads(payload), verify=self.verify) log.debug("Status code: %s, Response: %s", response.status_code, response.content) return response.json() def __build_url(self, resource_uri): "build url for api calls" return u"/".join([self.host, self.__url_path, resource_uri]) def get_addresses(self): """Get list of addresses""" return self.__get("Objects/Addresses", self.query_parameters) def get_address_groups(self, name_param=None): """Get list of address groups""" params = self.query_parameters if name_param: params["name"] = name_param return self.__get("Objects/AddressGroups", params) def edit_address_groups(self, name_param, payload): """Edits an address group, adds/removes addresses""" params = self.query_parameters params["name"] = name_param return self.__put("Objects/AddressGroups", params, payload) def add_address(self, name_param, payload): """Creates a new address""" params = self.query_parameters params["name"] = name_param return self.__post("Objects/Addresses", params, payload) def get_users_in_a_group(self, xpath): """Gets list of users in a group, uses custom POST method due to this being a SOAP based call. Returns XML string """ params = { "type": "config", "action": "get", "key": self.__key, "xpath": xpath } uri = "{}/api/".format(self.host) response = self.rc.execute_call_v2("POST", uri, params=params, verify=self.verify) response.raise_for_status() return response.text def edit_users_in_a_group(self, xpath, xml_object): """Edits list of users in a group, uses custom POST method due to this being a SOAP based call. Returns XML string """ params = { "type": "config", "action": "edit", "key": self.__key, "xpath": xpath, "element": xml_object } uri = "{}/api/?".format(self.host) response = self.rc.execute_call_v2("POST", uri, params=params, verify=self.verify) response.raise_for_status() return response.text
def test_timeout(self): integrations = {"integrations": {}} rc = RequestsCommon(integrations, None) self.assertIsEqual(rc.get_timeout(), 30) integrations_timeout = {"integrations": {"timeout": "35"}} rc = RequestsCommon(integrations_timeout, None) self.assertIsEqual(rc.get_timeout(), 35) # test timeout integrations_twenty = {"integrations": {"timeout": "20"}} rc = RequestsCommon(integrations_twenty, None) url = "/".join(TestFunctionRequests.URL_TEST_HTTP_VERBS, "delay", "25") with self.assertRaises(IntegrationError): rc.execute_call_v2("get", url) integrations_fourty = {"integrations": {"timeout": "40"}} rc = RequestsCommon(integrations_fourty, None) url = "/".join(TestFunctionRequests.URL_TEST_HTTP_VERBS, "delay", "35") rc.execute_call_v2("get", url)