Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
160 changes: 104 additions & 56 deletions PyPowerFlex/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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}.'
Expand Down Expand Up @@ -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)
Expand All @@ -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"]
Expand Down
4 changes: 4 additions & 0 deletions PyPowerFlex/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ class SnapshotPolicyConstants:
"autoSnapshotVolIds","expiredButLockedSnapshotsIds","numOfAutoSnapshots",
"numOfExpiredButLockedSnapshots","numOfSrcVols","srcVolIds"
]

class HTTPStatusConstants:
OK = 200
NO_CONTENT = 204
16 changes: 9 additions & 7 deletions PyPowerFlex/objects/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions PyPowerFlex/objects/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import logging

import requests
from PyPowerFlex.constants import HTTPStatusConstants

from PyPowerFlex import base_client
from PyPowerFlex import exceptions
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions PyPowerFlex/objects/fault_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import logging

import requests
from PyPowerFlex.constants import HTTPStatusConstants

from PyPowerFlex import base_client
from PyPowerFlex import exceptions
Expand All @@ -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,
Expand Down
Loading
Loading