diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index faed19e..d0c2897 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -19,7 +19,7 @@ jobs: run: | python -m pip install --upgrade pip pip install testtools - pip install requests + pip install urllib3 pip install pytest pip install pytest-coverage if [ -f requirements.txt ]; then pip install -r requirements.txt; fi diff --git a/PyPowerFlex/base_client.py b/PyPowerFlex/base_client.py index f289959..2c44d82 100644 --- a/PyPowerFlex/base_client.py +++ b/PyPowerFlex/base_client.py @@ -14,14 +14,16 @@ # under the License. import logging +import json -import requests -from requests.packages.urllib3.exceptions import InsecureRequestWarning +import urllib3 +from urllib3.util import make_headers from PyPowerFlex import exceptions from PyPowerFlex import utils +from PyPowerFlex.constants import HTTPStatusConstants -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) LOG = logging.getLogger(__name__) @@ -85,7 +87,17 @@ def send_request(self, method, url, params=None, **url_params): if method in [self.PUT, self.POST]: request_params['data'] = utils.prepare_params(params) - response = requests.request(method, request_url, **request_params) + if self.verify_certificate: + http = urllib3.PoolManager(cert_reqs = "CERT_REQUIRED") + else: + http = urllib3.PoolManager(cert_reqs = "CERT_NONE") + response = http.request( + method, + request_url, + body=request_params.get('data'), + headers=request_params['headers'], + timeout=request_params['timeout'] + ) self.logout(version) return response @@ -110,17 +122,19 @@ def send_mdm_cluster_post_request(self, url, params=None, **url_params): response = None version = self.login() request_url = self.base_url + url.format(**url_params) - r = requests.post(request_url, - auth=( - self.configuration.username, - self.token.get() - ), - headers=self.headers, - data=utils.prepare_params(params), - verify=self.verify_certificate, - timeout=self.configuration.timeout) - - if r.content != b'': + if self.verify_certificate: + http = urllib3.PoolManager(cert_reqs = "CERT_REQUIRED") + else: + http = urllib3.PoolManager(cert_reqs = "CERT_NONE") + auth_header = make_headers(basic_auth=f'{self.configuration.username}:{self.token.get()}') + r = http.request( + "POST", + request_url, + body=utils.prepare_params(params), + headers={**auth_header, **self.headers}, + timeout=self.configuration.timeout + ) + if r.data != b'': response = r.json() self.logout(version) return r, response @@ -145,13 +159,19 @@ def logout(self, version): def get_api_version(self): request_url = self.base_url + '/version' self._login() - r = requests.get(request_url, - auth=( - self.configuration.username, - self.token.get()), - verify=self.verify_certificate, - timeout=self.configuration.timeout) - response = r.json() + auth_headers = make_headers(basic_auth= f'{self.configuration.username}:{self.token.get()}') + if self.verify_certificate: + http = urllib3.PoolManager(cert_reqs = "CERT_REQUIRED") + else: + http = urllib3.PoolManager(cert_reqs = "CERT_NONE") + r = http.request( + "GET", + request_url, + headers={**self.headers, **auth_headers}, + timeout=self.configuration.timeout + ) + if r.data != b'': + response = r.json() return response # API Login method for 4.0 and above. @@ -160,11 +180,19 @@ def _appliance_login(self): payload = {"username": "%s" % self.configuration.username, "password": "%s" % self.configuration.password } - r = requests.post(request_url, headers=self.headers, json=payload, - verify=self.verify_certificate, - timeout=self.configuration.timeout - ) - if r.status_code != requests.codes.ok: + if self.verify_certificate: + http = urllib3.PoolManager(cert_reqs = "CERT_REQUIRED") + else: + http = urllib3.PoolManager(cert_reqs = "CERT_NONE") + payload_json = json.dumps(payload).encode('utf-8') + r = http.request( + 'POST', + request_url, + headers=self.headers, + body=payload_json, + timeout=self.configuration.timeout + ) + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailQuerying('token') LOG.error(exc.message) raise exc @@ -177,12 +205,21 @@ def _appliance_login(self): def _appliance_logout(self): request_url = self.auth_url + '/logout' data = {'refresh_token': '{0}'.format(self.__refresh_token)} - r = requests.post(request_url, headers=self.get_auth_headers(), json=data, - verify=self.verify_certificate, - timeout=self.configuration.timeout - ) + + if self.verify_certificate: + http = urllib3.PoolManager(cert_reqs = "CERT_REQUIRED") + else: + http = urllib3.PoolManager(cert_reqs = "CERT_NONE") + payload_json = json.dumps(data).encode('utf-8') + r = http.request( + 'POST', + request_url, + headers=self.get_auth_headers(), + body=payload_json, + timeout=self.configuration.timeout + ) - if r.status_code != requests.codes.no_content: + if r.status != HTTPStatusConstants.NO_CONTENT: exc = exceptions.PowerFlexFailQuerying('token') LOG.error(exc.message) raise exc @@ -192,17 +229,24 @@ def _appliance_logout(self): def _login(self): request_url = self.base_url + '/login' try: - r = requests.get(request_url, - auth=( - self.configuration.username, - self.configuration.password - ), - verify=self.verify_certificate, - timeout=self.configuration.timeout) - r.raise_for_status() + if self.verify_certificate: + http = urllib3.PoolManager(cert_reqs = "CERT_REQUIRED") + else: + http = urllib3.PoolManager(cert_reqs = "CERT_NONE") + auth_header = make_headers(basic_auth=f'{self.configuration.username}:{self.configuration.password}') + r = http.request( + "GET", + request_url, + headers={**auth_header, **self.headers}, + timeout=self.configuration.timeout + ) + if r.status >= 400: + exc = exceptions.PowerFlexFailQuerying('token') + LOG.error(exc.message) + raise exc token = r.json() self.token.set(token) - except requests.exceptions.RequestException as e: + except urllib3.exceptions.RequestError as e: error_msg = f'Login failed with error:{e.response.content}' if e.response else f'Login failed with error:{str(e)}' LOG.error(error_msg) raise Exception(error_msg) @@ -212,14 +256,18 @@ def _logout(self): if token: request_url = self.base_url + '/logout' - r = requests.get(request_url, - auth=( - self.configuration.username, - token - ), - verify=self.verify_certificate, - timeout=self.configuration.timeout) - if r.status_code != requests.codes.ok: + if self.verify_certificate: + http = urllib3.PoolManager(cert_reqs = "CERT_REQUIRED") + else: + http = urllib3.PoolManager(cert_reqs = "CERT_NONE") + auth_header = make_headers(basic_auth=f'{self.configuration.username}:{token}') + r = http.request( + "GET", + request_url, + headers={**auth_header, **self.headers}, + timeout=self.configuration.timeout + ) + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailQuerying('token') LOG.error(exc.message) raise exc @@ -251,7 +299,7 @@ def _create_entity(self, params=None): entity=self.entity, params=params ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailCreating(self.entity, response) LOG.error(exc.message) raise exc @@ -267,7 +315,7 @@ def _delete_entity(self, entity_id, params=None): entity=self.entity, entity_id=entity_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailDeleting(self.entity, entity_id, response) LOG.error(exc.message) @@ -279,7 +327,7 @@ def _rename_entity(self, action, entity_id, params=None): entity=self.entity, entity_id=entity_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailRenaming(self.entity, entity_id, response) LOG.error(exc.message) @@ -299,7 +347,7 @@ def get(self, entity_id=None, filter_fields=None, fields=None): raise exceptions.InvalidInput(msg) r, response = self.send_get_request(url, **url_params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailQuerying(self.entity, entity_id, response) LOG.error(exc.message) @@ -320,7 +368,7 @@ def get_related(self, entity_id, related, filter_fields=None, r, response = self.send_get_request(self.base_relationship_url, **url_params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( 'Failed to query related {related} entities for PowerFlex ' '{entity} with id {_id}.' @@ -348,7 +396,7 @@ def _perform_entity_operation_based_on_action(self, entity_id, action, entity_id=entity_id, params=params, **url_params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailEntityOperation(self.entity, entity_id, action, response) LOG.error(exc.message) @@ -359,7 +407,7 @@ def _query_selected_statistics(self, action, params=None): action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailQuerying(self.entity, response=response, entity_id=params["ids"] diff --git a/PyPowerFlex/constants.py b/PyPowerFlex/constants.py index 98535bf..9b5b002 100644 --- a/PyPowerFlex/constants.py +++ b/PyPowerFlex/constants.py @@ -106,3 +106,7 @@ class SnapshotPolicyConstants: "autoSnapshotVolIds","expiredButLockedSnapshotsIds","numOfAutoSnapshots", "numOfExpiredButLockedSnapshots","numOfSrcVols","srcVolIds" ] + +class HTTPStatusConstants: + OK = 200 + NO_CONTENT = 204 diff --git a/PyPowerFlex/objects/deployment.py b/PyPowerFlex/objects/deployment.py index a737f8c..fe77959 100644 --- a/PyPowerFlex/objects/deployment.py +++ b/PyPowerFlex/objects/deployment.py @@ -14,10 +14,12 @@ # under the License. import logging -import requests + +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions from PyPowerFlex import utils + LOG = logging.getLogger(__name__) @@ -45,7 +47,7 @@ def get(self, filters=None, full=None, include_devices=None, include_template=No includeTemplate=include_template ) r, response = self.send_get_request(utils.build_uri_with_params(self.deployment_url, **params)) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to retrieve deployments. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) @@ -58,7 +60,7 @@ def get_by_id(self, deployment_id): :return: A dictionary containing the retrieved Deployment. """ r, response = self.send_get_request(f'{self.deployment_url}/{deployment_id}') - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to retrieve deployment by id {deployment_id}. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) @@ -75,7 +77,7 @@ def validate(self, rg_data): PowerFlexClientException: If the deployment fails. """ r, response = self.send_post_request(f'{self.deployment_url}/validate', rg_data) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to validate the deployment. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) @@ -93,7 +95,7 @@ def create(self, rg_data): PowerFlexClientException: If the deployment fails. """ r, response = self.send_post_request(self.deployment_url, rg_data) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to create a new deployment. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) @@ -114,7 +116,7 @@ def edit(self, deployment_id, rg_data): request_url = f'{self.deployment_url}/{deployment_id}' r, response = self.send_put_request(request_url, rg_data) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to edit the deployment. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) @@ -134,7 +136,7 @@ def delete(self, deployment_id): request_url = f'{self.deployment_url}/{deployment_id}' response = self.send_delete_request(request_url) - if response.status_code != requests.codes.no_content: + if response.status != HTTPStatusConstants.NO_CONTENT: msg = (f'Failed to delete deployment. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) diff --git a/PyPowerFlex/objects/device.py b/PyPowerFlex/objects/device.py index 7b03e12..c7065e8 100644 --- a/PyPowerFlex/objects/device.py +++ b/PyPowerFlex/objects/device.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -141,7 +141,7 @@ def set_media_type(self, entity=self.entity, entity_id=device_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set media type for PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=device_id, diff --git a/PyPowerFlex/objects/fault_set.py b/PyPowerFlex/objects/fault_set.py index c6666f9..8027fb5 100644 --- a/PyPowerFlex/objects/fault_set.py +++ b/PyPowerFlex/objects/fault_set.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -38,7 +38,7 @@ def clear(self, fault_set_id): action=action, entity=self.entity, entity_id=fault_set_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to clear PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=fault_set_id, diff --git a/PyPowerFlex/objects/firmware_repository.py b/PyPowerFlex/objects/firmware_repository.py index d8428cc..7adbbce 100644 --- a/PyPowerFlex/objects/firmware_repository.py +++ b/PyPowerFlex/objects/firmware_repository.py @@ -14,7 +14,7 @@ # under the License. import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions from PyPowerFlex import utils @@ -44,7 +44,7 @@ def get(self, filters=None, limit=None, offset=None, sort=None, related=False, b components=components ) r, response = self.send_get_request(utils.build_uri_with_params(self.firmware_repository_url, **params)) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to retrieve firmware repository. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) diff --git a/PyPowerFlex/objects/managed_device.py b/PyPowerFlex/objects/managed_device.py index 1e41c23..a390d21 100644 --- a/PyPowerFlex/objects/managed_device.py +++ b/PyPowerFlex/objects/managed_device.py @@ -14,7 +14,7 @@ # under the License. import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions from PyPowerFlex import utils @@ -37,7 +37,7 @@ def get(self, filters=None, limit=None, offset=None, sort=None): sort=sort ) r, response = self.send_get_request(utils.build_uri_with_params(self.managed_device_url, **params)) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to retrieve managed devices. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) diff --git a/PyPowerFlex/objects/protection_domain.py b/PyPowerFlex/objects/protection_domain.py index 9f8cb44..c8f37b2 100644 --- a/PyPowerFlex/objects/protection_domain.py +++ b/PyPowerFlex/objects/protection_domain.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -54,7 +54,7 @@ def activate(self, protection_domain_id, force=False): entity=self.entity, entity_id=protection_domain_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to activate PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=protection_domain_id, @@ -136,7 +136,7 @@ def inactivate(self, protection_domain_id, force=False): entity=self.entity, entity_id=protection_domain_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to inactivate PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=protection_domain_id, @@ -190,7 +190,7 @@ def network_limits(self, protection_domain_id, rebuild_limit=None, entity_id=protection_domain_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to update the network limits of PowerFlex {entity}' ' with id {_id}. Error: {response}' .format(entity=self.entity, _id=protection_domain_id, @@ -217,7 +217,7 @@ def set_rfcache_enabled(self, protection_domain_id, enable_rfcache=None): action=action, entity=self.entity, entity_id=protection_domain_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to enable/disable RFcache in PowerFlex {entity} ' ' with id {_id}. Error: {response}' .format(entity=self.entity, _id=protection_domain_id, @@ -253,7 +253,7 @@ def rfcache_parameters(self, protection_domain_id, page_size=None, entity_id=protection_domain_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set RFcache parameters in PowerFlex {entity} ' ' with id {_id}. Error: {response}' .format(entity=self.entity, _id=protection_domain_id, diff --git a/PyPowerFlex/objects/replication_consistency_group.py b/PyPowerFlex/objects/replication_consistency_group.py index 2510557..6e464b0 100644 --- a/PyPowerFlex/objects/replication_consistency_group.py +++ b/PyPowerFlex/objects/replication_consistency_group.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -40,7 +40,7 @@ def create_snapshot(self, action=action, entity=self.entity, entity_id=rcg_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to create a snapshot of PowerFlex {entity} ' 'with id {_id} . Error: {response}'.format(entity=self.entity, _id=rcg_id, @@ -307,7 +307,7 @@ def get_all_statistics(self, api_version_less_than_3_6): entity=self.entity, action="querySelectedStatistics", params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to list replication consistencty group statistics for PowerFlex. ' 'Error: {response}'.format(response=response)) LOG.error(msg) diff --git a/PyPowerFlex/objects/replication_pair.py b/PyPowerFlex/objects/replication_pair.py index c1755f3..4f18395 100644 --- a/PyPowerFlex/objects/replication_pair.py +++ b/PyPowerFlex/objects/replication_pair.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -94,7 +94,7 @@ def get_all_statistics(self): r, response = self.send_post_request(self.list_statistics_url, entity=self.entity, action="querySelectedStatistics") - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to list statistics for all ReplicationPair objects. ' 'Error: {response}'.format(response=response)) LOG.error(msg) diff --git a/PyPowerFlex/objects/sdc.py b/PyPowerFlex/objects/sdc.py index 518ce0c..3fc8e65 100644 --- a/PyPowerFlex/objects/sdc.py +++ b/PyPowerFlex/objects/sdc.py @@ -14,7 +14,7 @@ # under the License. import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions diff --git a/PyPowerFlex/objects/sds.py b/PyPowerFlex/objects/sds.py index aa80acf..f97853a 100644 --- a/PyPowerFlex/objects/sds.py +++ b/PyPowerFlex/objects/sds.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -153,7 +153,7 @@ def add_ip(self, sds_id, sds_ip): entity=self.entity, entity_id=sds_id, params=sds_ip) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to add IP for PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) @@ -284,7 +284,7 @@ def remove_ip(self, sds_id, ip): entity=self.entity, entity_id=sds_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to remove IP from PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) @@ -318,7 +318,7 @@ def set_ip_role(self, sds_id, ip, role, force=None): entity=self.entity, entity_id=sds_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set ip role for PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) @@ -346,7 +346,7 @@ def set_port(self, sds_id, sds_port): entity=self.entity, entity_id=sds_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set port for PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) @@ -371,7 +371,7 @@ def set_rfcache_enabled(self, sds_id, rfcache_enabled): action=action, entity=self.entity, entity_id=sds_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to enable/disable Rfcache for PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) @@ -399,7 +399,7 @@ def set_rmcache_enabled(self, sds_id, rmcache_enabled): entity=self.entity, entity_id=sds_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to enable/disable Rmcache for PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) @@ -427,7 +427,7 @@ def set_rmcache_size(self, sds_id, rmcache_size): entity=self.entity, entity_id=sds_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set Rmcache size for PowerFlex {entity} ' 'with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) @@ -455,7 +455,7 @@ def set_performance_parameters(self, sds_id, performance_profile): entity=self.entity, entity_id=sds_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set performance parameters for PowerFlex ' '{entity} with id {_id}. Error: {response}' .format(entity=self.entity, _id=sds_id, response=response)) diff --git a/PyPowerFlex/objects/sdt.py b/PyPowerFlex/objects/sdt.py index 689244a..dd677dd 100644 --- a/PyPowerFlex/objects/sdt.py +++ b/PyPowerFlex/objects/sdt.py @@ -14,7 +14,7 @@ # under the License. import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions from PyPowerFlex import utils @@ -118,7 +118,7 @@ def add_ip(self, sdt_id, ip, role): entity_id=sdt_id, params=params, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to add IP for PowerFlex {entity} " "with id {_id}. Error: {response}".format( @@ -149,7 +149,7 @@ def remove_ip(self, sdt_id, ip): entity_id=sdt_id, params=params, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to remove IP from PowerFlex {entity} " "with id {_id}. Error: {response}".format( @@ -185,7 +185,7 @@ def set_ip_role(self, sdt_id, ip, role): entity_id=sdt_id, params=params, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to set ip role for PowerFlex {entity} " "with id {_id}. Error: {response}".format( @@ -216,7 +216,7 @@ def set_storage_port(self, sdt_id, storage_port): entity_id=sdt_id, params=params, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to set storage port for PowerFlex {entity} " "with id {_id}. Error: {response}".format( @@ -247,7 +247,7 @@ def set_nvme_port(self, sdt_id, nvme_port): entity_id=sdt_id, params=params, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to set nvme port for PowerFlex {entity} " "with id {_id}. Error: {response}".format( @@ -278,7 +278,7 @@ def set_discovery_port(self, sdt_id, discovery_port): entity_id=sdt_id, params=params, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to set discovery port for PowerFlex {entity} " "with id {_id}. Error: {response}".format( @@ -306,7 +306,7 @@ def enter_maintenance_mode(self, sdt_id): entity_id=sdt_id, params=None, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to enter maintenance mode for PowerFlex {entity} " "with id {_id}. Error: {response}".format( @@ -334,7 +334,7 @@ def exit_maintenance_mode(self, sdt_id): entity_id=sdt_id, params=None, ) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ( "Failed to exit maintenance mode for PowerFlex {entity} " "with id {_id}. Error: {response}".format( diff --git a/PyPowerFlex/objects/service_template.py b/PyPowerFlex/objects/service_template.py index 66d138c..0dbaf77 100644 --- a/PyPowerFlex/objects/service_template.py +++ b/PyPowerFlex/objects/service_template.py @@ -14,7 +14,7 @@ # under the License. import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions from PyPowerFlex import utils @@ -41,7 +41,7 @@ def get(self, filters=None, full=None, limit=None, offset=None, sort=None, inclu includeAttachments=include_attachments ) r, response = self.send_get_request(utils.build_uri_with_params(self.service_template_url, **params)) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to retrieve service templates. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) @@ -58,7 +58,7 @@ def get_by_id(self, service_template_id, for_deployment=False): if for_deployment: url += '?forDeployment=true' r, response = self.send_get_request(url) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to retrieve service template by id {service_template_id}. Error: {response}') LOG.error(msg) raise exceptions.PowerFlexClientException(msg) diff --git a/PyPowerFlex/objects/snapshot_policy.py b/PyPowerFlex/objects/snapshot_policy.py index ab7fe95..d8b7b20 100644 --- a/PyPowerFlex/objects/snapshot_policy.py +++ b/PyPowerFlex/objects/snapshot_policy.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -51,7 +51,7 @@ def add_source_volume(self, snapshot_policy_id, volume_id): entity=self.entity, entity_id=snapshot_policy_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to add source volume to PowerFlex {entity} ' 'with id {_id}. ' 'Error: {response}'.format(entity=self.entity, @@ -120,7 +120,7 @@ def modify(self, entity=self.entity, entity_id=snapshot_policy_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to modify PowerFlex {entity} ' 'with id {_id}. ' 'Error: {response}'.format(entity=self.entity, @@ -144,7 +144,7 @@ def pause(self, snapshot_policy_id): action=action, entity=self.entity, entity_id=snapshot_policy_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to pause PowerFlex {entity} with id {_id}.' ' Error: {response}'.format(entity=self.entity, _id=snapshot_policy_id, @@ -183,7 +183,7 @@ def remove_source_volume(self, entity=self.entity, entity_id=snapshot_policy_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to remove source volume from PowerFlex {entity} ' 'with id {_id}. ' 'Error: {response}'.format(entity=self.entity, @@ -223,7 +223,7 @@ def resume(self, snapshot_policy_id): action=action, entity=self.entity, entity_id=snapshot_policy_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to resume PowerFlex {entity} with id {_id}. ' 'Error: {response}'.format(entity=self.entity, _id=snapshot_policy_id, diff --git a/PyPowerFlex/objects/storage_pool.py b/PyPowerFlex/objects/storage_pool.py index 2992030..8f1bf58 100644 --- a/PyPowerFlex/objects/storage_pool.py +++ b/PyPowerFlex/objects/storage_pool.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -232,7 +232,7 @@ def set_checksum_enabled(self, storage_pool_id, checksum_enabled): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to enable/disable checksum for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -259,7 +259,7 @@ def set_compression_method(self, storage_pool_id, compression_method): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set compression method for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -302,7 +302,7 @@ def set_external_acceleration_type( entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set external acceleration type for PowerFlex ' f'{self.entity} with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -335,7 +335,7 @@ def set_media_type(self, entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set media type for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -362,7 +362,7 @@ def set_rebalance_enabled(self, storage_pool_id, rebalance_enabled): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to enable/disable rebalance for PowerFlex {self.entity}' ' with id {storage_pool_id}. Error: {response}' ) @@ -390,7 +390,7 @@ def set_rebuild_enabled(self, storage_pool_id, rebuild_enabled): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to enable/disable rebuild for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -417,7 +417,7 @@ def set_spare_percentage(self, storage_pool_id, spare_percentage): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set spare percentage for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -441,7 +441,7 @@ def set_use_rfcache(self, storage_pool_id, use_rfcache): action=action, entity=self.entity, entity_id=storage_pool_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set Rfcache usage for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -468,7 +468,7 @@ def set_use_rmcache(self, storage_pool_id, use_rmcache): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set Rmcache usage for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -496,7 +496,7 @@ def set_zero_padding_policy(self, storage_pool_id, zero_padding_enabled): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set Zero Padding policy for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -523,7 +523,7 @@ def set_rep_cap_max_ratio(self, storage_pool_id, rep_cap_max_ratio): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set the replication journal capacity ratio for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -553,7 +553,7 @@ def set_cap_alert_thresholds(self, storage_pool_id, cap_alert_high_threshold, ca entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set the capacity alert thresholds for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -584,7 +584,7 @@ def set_protected_maintenance_mode_io_priority_policy(self, storage_pool_id, pol entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set the protected maintenance mode IO priority policy for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -616,7 +616,7 @@ def set_vtree_migration_io_priority_policy(self, storage_pool_id, policy, concur entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set VTree migration I/O priority policy for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -647,7 +647,7 @@ def rebalance_io_priority_policy(self, storage_pool_id, policy, concurrent_ios_p entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set the rebalance I/O priority policy for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -674,7 +674,7 @@ def set_rmcache_write_handling_mode(self, storage_pool_id, rmcache_write_handlin entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set the RM cache write handling mode for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -701,7 +701,7 @@ def set_rebuild_rebalance_parallelism_limit(self, storage_pool_id, no_of_paralle entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set rebuild rebalance parallelism limit for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -733,7 +733,7 @@ def set_persistent_checksum(self, storage_pool_id, enable, validate, builder_lim entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to set the persistent checksum for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -762,7 +762,7 @@ def modify_persistent_checksum(self, storage_pool_id, validate, builder_limit): entity=self.entity, entity_id=storage_pool_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to modify the persistent checksum for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) @@ -787,7 +787,7 @@ def set_fragmentation_enabled(self, storage_pool_id, enable_fragmentation): action=action, entity=self.entity, entity_id=storage_pool_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = (f'Failed to enable/disable fragmentation for PowerFlex {self.entity} ' f'with id {storage_pool_id}. Error: {response}') LOG.error(msg) diff --git a/PyPowerFlex/objects/system.py b/PyPowerFlex/objects/system.py index 1e18d64..e467fe6 100644 --- a/PyPowerFlex/objects/system.py +++ b/PyPowerFlex/objects/system.py @@ -16,7 +16,7 @@ import logging import re -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -67,7 +67,7 @@ def api_version(self, cached=True): if not self.__api_version or not cached: r, response = self.send_get_request(url) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: exc = exceptions.PowerFlexFailQuerying('API version') LOG.error(exc.message) raise exc @@ -75,7 +75,7 @@ def api_version(self, cached=True): if not pattern.match(response): msg = ( 'Failed to query PowerFlex API version. Invalid version ' - 'format: {response}.'.format(response=r.text) + 'format: {response}.'.format(response=r.data.decode()) ) LOG.error(msg) raise exceptions.PowerFlexClientException(msg) @@ -103,7 +103,7 @@ def remove_cg_snapshots(self, system_id, cg_id, allow_ext_managed=None): entity=self.entity, entity_id=system_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to remove consistency group snapshots from ' 'PowerFlex {entity} with id {_id}. ' 'Error: {response}'.format(entity=self.entity, @@ -144,7 +144,7 @@ def snapshot_volumes(self, entity=self.entity, entity_id=system_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to snapshot volumes on PowerFlex {entity} ' 'with id {_id}.' ' Error: {response}'.format(entity=self.entity, @@ -199,7 +199,7 @@ def add_standby_mdm(self, mdm_ips, role, management_ips=None, port=None, action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to add standBy MDM on PowerFlex {entity}. ' 'Error: {response}'.format(entity=self.entity, response=response)) @@ -224,7 +224,7 @@ def remove_standby_mdm(self, mdm_id): action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok and response is not None: + if r.status != HTTPStatusConstants.OK and response is not None: msg = ('Failed to remove standBy MDM from PowerFlex {entity}. ' 'Error: {response}.'.format(entity=self.entity, response=response)) @@ -242,7 +242,7 @@ def get_mdm_cluster_details(self): r, response = self.send_post_request(self.query_mdm_cluster_url, entity=self.entity) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to get MDM cluster details on PowerFlex {entity}. ' 'Error: {response}'.format(entity=self.entity, response=response)) @@ -259,7 +259,7 @@ def get_gateway_configuration_details(self): """ r, response = self.send_get_request('/Configuration') - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to get gateway configuration details on PowerFlex {entity}. ' 'Error: {response}'.format(entity=self.entity, response=response)) @@ -286,7 +286,7 @@ def change_mdm_ownership(self, mdm_id): action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to change ownership on PowerFlex {entity}. ' 'Error: {response}'.format(entity=self.entity, response=response)) @@ -311,7 +311,7 @@ def set_cluster_mdm_performance_profile(self, performance_profile): action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set performance profile of MDMs on PowerFlex ' '{entity}. Error: {response}'.format(entity=self.entity, response=response)) @@ -341,7 +341,7 @@ def rename_mdm(self, mdm_id, mdm_new_name): action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to rename the MDM on PowerFlex {entity}. Error: ' '{response}'.format(entity=self.entity, response=response)) LOG.error(msg) @@ -380,7 +380,7 @@ def modify_virtual_ip_interface(self, mdm_id, virtual_ip_interfaces=None, action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to modify virtual IP interface on PowerFlex ' '{entity}. Error: {response}'.format(entity=self.entity, response=response)) @@ -423,7 +423,7 @@ def switch_cluster_mode(self, cluster_mode, add_secondary=None, action=action, entity=self.entity, params=params) - if r.status_code != requests.codes.ok and response is not None: + if r.status != HTTPStatusConstants.OK and response is not None: msg = ('Failed to switch MDM cluster mode PowerFlex {entity}. ' 'Error: {response}.'.format(entity=self.entity, response=response)) diff --git a/PyPowerFlex/objects/utility.py b/PyPowerFlex/objects/utility.py index dfe04bd..c63f9e6 100644 --- a/PyPowerFlex/objects/utility.py +++ b/PyPowerFlex/objects/utility.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -54,7 +54,7 @@ def get_statistics_for_all_storagepools(self, ids=None, properties=None): entity='StoragePool', action=action, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to list storage pool statistics for PowerFlex. ' 'Error: {response}'.format(response=response)) LOG.error(msg) @@ -83,7 +83,7 @@ def get_statistics_for_all_volumes(self, ids=None, properties=None): entity='Volume', action=action, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to list volume statistics for PowerFlex. ' 'Error: {response}'.format(response=response)) LOG.error(msg) @@ -112,7 +112,7 @@ def get_statistics_for_all_snapshot_policies(self, ids=None, properties=None): entity='SnapshotPolicy', action=action, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to list snapshot policy statistics for PowerFlex. ' 'Error: {response}'.format(response=response)) LOG.error(msg) diff --git a/PyPowerFlex/objects/volume.py b/PyPowerFlex/objects/volume.py index d7e45b1..06302a7 100644 --- a/PyPowerFlex/objects/volume.py +++ b/PyPowerFlex/objects/volume.py @@ -15,7 +15,7 @@ import logging -import requests +from PyPowerFlex.constants import HTTPStatusConstants from PyPowerFlex import base_client from PyPowerFlex import exceptions @@ -88,7 +88,7 @@ def add_mapped_sdc(self, entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to map PowerFlex {entity} with id {_id} ' 'to SDC. Error: {response}'.format(entity=self.entity, _id=volume_id, @@ -167,7 +167,7 @@ def extend(self, volume_id, size_in_gb, allow_ext_managed=None): entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to extend PowerFlex {entity} with id {_id}. ' 'Error: {response}'.format(entity=self.entity, _id=volume_id, @@ -202,7 +202,7 @@ def lock_auto_snapshot(self, volume_id): action=action, entity=self.entity, entity_id=volume_id) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to lock AutoSnapshot for PowerFlex {entity} ' 'with id {_id}. ' 'Error: {response}'.format(entity=self.entity, @@ -253,7 +253,7 @@ def remove_mapped_sdc(self, entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to unmap PowerFlex {entity} with id {_id} from ' 'SDC. Error: {response}'.format(entity=self.entity, _id=volume_id, @@ -300,7 +300,7 @@ def unlock_auto_snapshot(self, volume_id, remove_auto_snapshot=None): entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to unlock AutoSnapshot for PowerFlex {entity} ' 'with id {_id}. Error: ' '{response}'.format(entity=self.entity, _id=volume_id, @@ -338,7 +338,7 @@ def set_mapped_sdc_limits(self, volume_id, sdc_id, bandwidth_limit=None, entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to update the SDC limits of PowerFlex' ' {entity} with id {_id}. ' 'Error: {response}'.format(entity=self.entity, @@ -373,7 +373,7 @@ def set_compression_method(self, volume_id, compression_method): entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to update the compression method of PowerFlex' ' {entity} with id' ' {_id}. Error: {response}'.format(entity=self.entity, @@ -409,7 +409,7 @@ def set_use_rmcache(self, volume_id, use_rmcache): entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to update the use_rmcache of PowerFlex' ' {entity} with id {_id}. ' 'Error: {response}'.format(entity=self.entity, @@ -446,7 +446,7 @@ def set_access_mode_for_sdc(self, volume_id, sdc_id, access_mode): entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set the access mode for the SDC {sdc_id}' ' mapped to PowerFlex {entity} with id {_id}. Error:' ' {response}'.format(entity=self.entity, _id=volume_id, @@ -479,7 +479,7 @@ def set_retention_period(self, snap_id, retention_period): entity=self.entity, entity_id=snap_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to set the retention period for PowerFlex' ' {entity} with id {_id}.' ' Error: {response}'.format(entity=self.entity, @@ -513,7 +513,7 @@ def set_volume_access_mode_limit(self, volume_id, access_mode_limit): entity=self.entity, entity_id=volume_id, params=params) - if r.status_code != requests.codes.ok: + if r.status != HTTPStatusConstants.OK: msg = ('Failed to update the Volume Access Mode Limit of ' 'PowerFlex {entity} with id {_id}. Error: {response}' .format(entity=self.entity, _id=volume_id, diff --git a/requirements.txt b/requirements.txt index 7651129..cd90918 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -requests +urllib3 packaging - diff --git a/setup.py b/setup.py index b9d3303..9fc5549 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,6 @@ author_email='ansible.team@dell.com', install_requires=[ 'packaging>=20.4', - 'requests>=2.23.0', ], license_files = ('LICENSE',), classifiers=['License :: OSI Approved :: Apache Software License'], diff --git a/tests/__init__.py b/tests/__init__.py index c6e1df7..c123dd7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -20,32 +20,39 @@ from unittest import mock from unittest import TestCase -import requests +import urllib3 import PyPowerFlex from PyPowerFlex import utils -class MockResponse(requests.Response): +class MockResponse(urllib3.BaseHTTPResponse): """Mock HTTP Response. Defines http replies from mocked calls to do_request(). """ - def __init__(self, content, status_code=200): - super(MockResponse, self).__init__() + def __init__(self, content, status=200): + super(MockResponse, self).__init__( + status=status, + version=None, + version_string=None, + decode_content=False, + reason=None, + request_url=None + ) self._content = content self.request = mock.MagicMock() - self.status_code = status_code + self.status = status def json(self, **kwargs): return self._content @property - def text(self): + def data(self) -> bytes: if not isinstance(self._content, bytes): - return json.dumps(self._content) - return super(MockResponse, self).text + return json.dumps(self._content).encode() + return self._content class PyPowerFlexTestCase(TestCase): @@ -102,13 +109,10 @@ def setUp(self): self.username, self.password, log_level=logging.DEBUG) - requests.request = self.get_mock_response - self.get_mock = self.mock_object(requests, - 'get', + urllib3.PoolManager.request = self.get_mock_response + self.request_mock = self.mock_object(urllib3.PoolManager, + 'request', side_effect=self.get_mock_response) - self.post_mock = self.mock_object(requests, - 'post', - side_effect=self.get_mock_response) utils.is_version_3 = mock.MagicMock(return_value=True) def mock_object(self, obj, attr_name, *args, **kwargs): diff --git a/tests/test_system.py b/tests/test_system.py index 2c99c40..bc6200c 100644 --- a/tests/test_system.py +++ b/tests/test_system.py @@ -81,7 +81,7 @@ def setUp(self): def test_system_api_version(self): self.client.system.api_version() - self.assertEqual(4, self.get_mock.call_count) + self.assertEqual(5, self.request_mock.call_count) def test_system_api_version_bad_status(self): with self.http_response_mode(self.RESPONSE_MODE.BadStatus): @@ -99,7 +99,7 @@ def test_system_api_version_cached(self): self.client.system.api_version() self.client.system.api_version() self.client.system.api_version() - self.assertEqual(4, self.get_mock.call_count) + self.assertEqual(5, self.request_mock.call_count) def test_system_remove_cg_snapshots(self): self.client.system.remove_cg_snapshots(self.fake_system_id,