Blacken openstackclient.api

Black used with the '-l 79 -S' flags.

A future change will ignore this commit in git-blame history by adding a
'git-blame-ignore-revs' file.

Change-Id: I1df5bc4c35f02147fe5ac5b4073f0e01e7d51c2f
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2023-05-08 11:36:34 +01:00
parent 7d80f9e962
commit a6f81a736c
10 changed files with 113 additions and 162 deletions

View File

@ -30,12 +30,7 @@ class KeystoneSession(object):
"""
def __init__(
self,
session=None,
endpoint=None,
**kwargs
):
def __init__(self, session=None, endpoint=None, **kwargs):
"""Base object that contains some common API objects and methods
:param Session session:
@ -87,11 +82,7 @@ class BaseAPI(KeystoneSession):
"""Base API"""
def __init__(
self,
session=None,
service_type=None,
endpoint=None,
**kwargs
self, session=None, service_type=None, endpoint=None, **kwargs
):
"""Base object that contains some common API objects and methods
@ -110,13 +101,7 @@ class BaseAPI(KeystoneSession):
# The basic action methods all take a Session and return dict/lists
def create(
self,
url,
session=None,
method=None,
**params
):
def create(self, url, session=None, method=None, **params):
"""Create a new resource
:param string url:
@ -136,12 +121,7 @@ class BaseAPI(KeystoneSession):
except json.JSONDecodeError:
return ret
def delete(
self,
url,
session=None,
**params
):
def delete(self, url, session=None, **params):
"""Delete a resource
:param string url:
@ -152,14 +132,7 @@ class BaseAPI(KeystoneSession):
return self._request('DELETE', url, **params)
def list(
self,
path,
session=None,
body=None,
detailed=False,
**params
):
def list(self, path, session=None, body=None, detailed=False, **params):
"""Return a list of resources
GET ${ENDPOINT}/${PATH}?${PARAMS}
@ -255,9 +228,7 @@ class BaseAPI(KeystoneSession):
if len(data) > 1:
msg = _("Multiple %(resource)s exist with %(attr)s='%(value)s'")
raise exceptions.CommandError(
msg % {'resource': resource,
'attr': attr,
'value': value}
msg % {'resource': resource, 'attr': attr, 'value': value}
)
# Search by id
@ -267,16 +238,10 @@ class BaseAPI(KeystoneSession):
return data[0]
msg = _("No %(resource)s with a %(attr)s or ID of '%(value)s' found")
raise exceptions.CommandError(
msg % {'resource': resource,
'attr': attr,
'value': value}
msg % {'resource': resource, 'attr': attr, 'value': value}
)
def find_bulk(
self,
path,
**kwargs
):
def find_bulk(self, path, **kwargs):
"""Bulk load and filter locally
:param string path:
@ -302,11 +267,7 @@ class BaseAPI(KeystoneSession):
return ret
def find_one(
self,
path,
**kwargs
):
def find_one(self, path, **kwargs):
"""Find a resource by name or ID
:param string path:

View File

@ -22,6 +22,7 @@ from osc_lib.i18n import _
# TODO(dtroyer): Mingrate this to osc-lib
class InvalidValue(Exception):
"""An argument value is not valid: wrong type, out of range, etc"""
message = "Supplied value is not valid"
@ -291,11 +292,7 @@ class APIv2(api.BaseAPI):
return self.list(url)["hosts"]
def host_set(
self,
host=None,
status=None,
maintenance_mode=None,
**params
self, host=None, status=None, maintenance_mode=None, **params
):
"""Modify host properties
@ -576,7 +573,7 @@ class APIv2(api.BaseAPI):
value=security_group,
)
if security_group is not None:
for (k, v) in params.items():
for k, v in params.items():
# Only set a value if it is already present
if k in security_group:
security_group[k] = v

View File

@ -33,11 +33,7 @@ class APIv1(api.BaseAPI):
self.endpoint = self.endpoint + self._endpoint_suffix
def image_list(
self,
detailed=False,
public=False,
private=False,
**filter
self, detailed=False, public=False, private=False, **filter
):
"""Get available images

View File

@ -36,10 +36,7 @@ class APIv1(api.BaseAPI):
super(APIv1, self).__init__(**kwargs)
def container_create(
self,
container=None,
public=False,
storage_policy=None
self, container=None, public=False, storage_policy=None
):
"""Create a container
@ -62,7 +59,8 @@ class APIv1(api.BaseAPI):
headers['x-storage-policy'] = storage_policy
response = self.create(
urllib.parse.quote(container), method='PUT', headers=headers)
urllib.parse.quote(container), method='PUT', headers=headers
)
data = {
'account': self._find_account_id(),
@ -192,9 +190,7 @@ class APIv1(api.BaseAPI):
data = {
'account': self._find_account_id(),
'container': container,
'object_count': response.headers.get(
'x-container-object-count'
),
'object_count': response.headers.get('x-container-object-count'),
'bytes_used': response.headers.get('x-container-bytes-used'),
'storage_policy': response.headers.get('x-storage-policy'),
}
@ -208,8 +204,9 @@ class APIv1(api.BaseAPI):
if 'x-container-sync-key' in response.headers:
data['sync_key'] = response.headers.get('x-container-sync-key')
properties = self._get_properties(response.headers,
'x-container-meta-')
properties = self._get_properties(
response.headers, 'x-container-meta-'
)
if properties:
data['properties'] = properties
@ -228,8 +225,9 @@ class APIv1(api.BaseAPI):
properties to remove from the container
"""
headers = self._unset_properties(properties,
'X-Remove-Container-Meta-%s')
headers = self._unset_properties(
properties, 'X-Remove-Container-Meta-%s'
)
if headers:
self.create(urllib.parse.quote(container), headers=headers)
@ -259,8 +257,10 @@ class APIv1(api.BaseAPI):
# object's name in the container.
object_name_str = name if name else object
full_url = "%s/%s" % (urllib.parse.quote(container),
urllib.parse.quote(object_name_str))
full_url = "%s/%s" % (
urllib.parse.quote(container),
urllib.parse.quote(object_name_str),
)
with io.open(object, 'rb') as f:
response = self.create(
full_url,
@ -293,8 +293,10 @@ class APIv1(api.BaseAPI):
if container is None or object is None:
return
self.delete("%s/%s" % (urllib.parse.quote(container),
urllib.parse.quote(object)))
self.delete(
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object))
)
def object_list(
self,
@ -394,8 +396,8 @@ class APIv1(api.BaseAPI):
response = self._request(
'GET',
"%s/%s" % (urllib.parse.quote(container),
urllib.parse.quote(object)),
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
stream=True,
)
if response.status_code == 200:
@ -429,9 +431,11 @@ class APIv1(api.BaseAPI):
headers = self._set_properties(properties, 'X-Object-Meta-%s')
if headers:
self.create("%s/%s" % (urllib.parse.quote(container),
urllib.parse.quote(object)),
headers=headers)
self.create(
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
headers=headers,
)
def object_unset(
self,
@ -451,9 +455,11 @@ class APIv1(api.BaseAPI):
headers = self._unset_properties(properties, 'X-Remove-Object-Meta-%s')
if headers:
self.create("%s/%s" % (urllib.parse.quote(container),
urllib.parse.quote(object)),
headers=headers)
self.create(
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
headers=headers,
)
def object_show(
self,
@ -473,9 +479,11 @@ class APIv1(api.BaseAPI):
if container is None or object is None:
return {}
response = self._request('HEAD', "%s/%s" %
(urllib.parse.quote(container),
urllib.parse.quote(object)))
response = self._request(
'HEAD',
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
)
data = {
'account': self._find_account_id(),
@ -484,9 +492,7 @@ class APIv1(api.BaseAPI):
'content-type': response.headers.get('content-type'),
}
if 'content-length' in response.headers:
data['content-length'] = response.headers.get(
'content-length'
)
data['content-length'] = response.headers.get('content-length')
if 'last-modified' in response.headers:
data['last-modified'] = response.headers.get('last-modified')
if 'etag' in response.headers:
@ -549,8 +555,9 @@ class APIv1(api.BaseAPI):
properties to remove from the account
"""
headers = self._unset_properties(properties,
'X-Remove-Account-Meta-%s')
headers = self._unset_properties(
properties, 'X-Remove-Account-Meta-%s'
)
if headers:
self.create("", headers=headers)
@ -596,5 +603,5 @@ class APIv1(api.BaseAPI):
properties = {}
for k, v in headers.items():
if k.lower().startswith(header_tag):
properties[k[len(header_tag):]] = v
properties[k[len(header_tag) :]] = v
return properties

View File

@ -47,7 +47,6 @@ LIST_BODY = {
class TestSession(utils.TestCase):
BASE_URL = 'https://api.example.com:1234/vX'
def setUp(self):

View File

@ -20,7 +20,6 @@ from openstackclient.tests.unit.api import fakes as api_fakes
class TestKeystoneSession(api_fakes.TestSession):
def setUp(self):
super(TestKeystoneSession, self).setUp()
self.api = api.KeystoneSession(
@ -40,7 +39,6 @@ class TestKeystoneSession(api_fakes.TestSession):
class TestBaseAPI(api_fakes.TestSession):
def setUp(self):
super(TestBaseAPI, self).setUp()
self.api = api.BaseAPI(
@ -80,7 +78,6 @@ class TestBaseAPI(api_fakes.TestSession):
# find tests
def test_find_attr_by_id(self):
# All first requests (by name) will fail in this test
self.requests_mock.register_uri(
'GET',
@ -172,7 +169,6 @@ class TestBaseAPI(api_fakes.TestSession):
self.assertEqual(api_fakes.RESP_ITEM_1, ret)
def test_find_attr_path_resource(self):
# Test resource different than path
self.requests_mock.register_uri(
'GET',

View File

@ -26,7 +26,6 @@ FAKE_URL = 'http://gopher.com/v2'
class TestComputeAPIv2(utils.TestCase):
def setUp(self):
super(TestComputeAPIv2, self).setUp()
sess = session.Session()
@ -35,18 +34,17 @@ class TestComputeAPIv2(utils.TestCase):
class TestFloatingIP(TestComputeAPIv2):
FAKE_FLOATING_IP_RESP = {
'id': 1,
'ip': '203.0.113.11', # TEST-NET-3
'fixed_ip': '198.51.100.11', # TEST-NET-2
'ip': '203.0.113.11', # TEST-NET-3
'fixed_ip': '198.51.100.11', # TEST-NET-2
'pool': 'nova',
'instance_id': None,
}
FAKE_FLOATING_IP_RESP_2 = {
'id': 2,
'ip': '203.0.113.12', # TEST-NET-3
'fixed_ip': '198.51.100.12', # TEST-NET-2
'ip': '203.0.113.12', # TEST-NET-3
'fixed_ip': '198.51.100.12', # TEST-NET-2
'pool': 'nova',
'instance_id': None,
}
@ -213,7 +211,6 @@ class TestFloatingIP(TestComputeAPIv2):
class TestFloatingIPPool(TestComputeAPIv2):
LIST_FLOATING_IP_POOL_RESP = [
{"name": "tide"},
{"name": "press"},
@ -231,7 +228,6 @@ class TestFloatingIPPool(TestComputeAPIv2):
class TestHost(TestComputeAPIv2):
FAKE_HOST_RESP_1 = {
"zone": "internal",
"host_name": "myhost",
@ -340,7 +336,6 @@ class TestHost(TestComputeAPIv2):
class TestNetwork(TestComputeAPIv2):
FAKE_NETWORK_RESP = {
'id': '1',
'label': 'label1',
@ -491,20 +486,19 @@ class TestNetwork(TestComputeAPIv2):
class TestSecurityGroup(TestComputeAPIv2):
FAKE_SECURITY_GROUP_RESP = {
'id': '1',
'name': 'sg1',
'description': 'test security group',
'tenant_id': '0123456789',
'rules': []
'rules': [],
}
FAKE_SECURITY_GROUP_RESP_2 = {
'id': '2',
'name': 'sg2',
'description': 'another test security group',
'tenant_id': '0123456789',
'rules': []
'rules': [],
}
LIST_SECURITY_GROUP_RESP = [
FAKE_SECURITY_GROUP_RESP_2,
@ -656,8 +650,8 @@ class TestSecurityGroup(TestComputeAPIv2):
status_code=200,
)
ret = self.api.security_group_set(
security_group='1',
description='desc2')
security_group='1', description='desc2'
)
self.assertEqual(self.FAKE_SECURITY_GROUP_RESP, ret)
def test_security_group_set_options_name(self):
@ -679,13 +673,12 @@ class TestSecurityGroup(TestComputeAPIv2):
status_code=200,
)
ret = self.api.security_group_set(
security_group='sg2',
description='desc2')
security_group='sg2', description='desc2'
)
self.assertEqual(self.FAKE_SECURITY_GROUP_RESP_2, ret)
class TestSecurityGroupRule(TestComputeAPIv2):
FAKE_SECURITY_GROUP_RULE_RESP = {
'id': '1',
'name': 'sgr1',

View File

@ -25,7 +25,6 @@ FAKE_URL = 'http://gopher.dev10.com'
class TestImageAPIv1(utils.TestCase):
def setUp(self):
super(TestImageAPIv1, self).setUp()
@ -35,7 +34,6 @@ class TestImageAPIv1(utils.TestCase):
class TestImage(TestImageAPIv1):
PUB_PROT = {
'id': '1',
'name': 'pub1',

View File

@ -25,7 +25,6 @@ FAKE_URL = 'http://gopher.dev20.com'
class TestImageAPIv2(utils.TestCase):
def setUp(self):
super(TestImageAPIv2, self).setUp()
@ -35,7 +34,6 @@ class TestImageAPIv2(utils.TestCase):
class TestImage(TestImageAPIv2):
PUB_PROT = {
'id': '1',
'name': 'pub1',

View File

@ -30,10 +30,18 @@ FAKE_CONTAINER = 'rainbarrel'
FAKE_OBJECT = 'spigot'
LIST_CONTAINER_RESP = [
{"name": "qaz", "count": 0, "bytes": 0,
"last_modified": "2020-05-16T05:52:07.377550"},
{"name": "fred", "count": 0, "bytes": 0,
"last_modified": "2020-05-16T05:55:07.377550"},
{
"name": "qaz",
"count": 0,
"bytes": 0,
"last_modified": "2020-05-16T05:52:07.377550",
},
{
"name": "fred",
"count": 0,
"bytes": 0,
"last_modified": "2020-05-16T05:55:07.377550",
},
]
LIST_OBJECT_RESP = [
@ -43,7 +51,6 @@ LIST_OBJECT_RESP = [
class TestObjectAPIv1(utils.TestCase):
def setUp(self):
super(TestObjectAPIv1, self).setUp()
sess = session.Session()
@ -52,7 +59,6 @@ class TestObjectAPIv1(utils.TestCase):
class TestContainer(TestObjectAPIv1):
def setUp(self):
super(TestContainer, self).setUp()
@ -128,15 +134,17 @@ class TestContainer(TestObjectAPIv1):
)
self.requests_mock.register_uri(
'GET',
FAKE_URL +
'?marker=%s&limit=1&format=json' % LIST_CONTAINER_RESP[0]['name'],
FAKE_URL
+ '?marker=%s&limit=1&format=json'
% LIST_CONTAINER_RESP[0]['name'],
json=[LIST_CONTAINER_RESP[1]],
status_code=200,
)
self.requests_mock.register_uri(
'GET',
FAKE_URL +
'?marker=%s&limit=1&format=json' % LIST_CONTAINER_RESP[1]['name'],
FAKE_URL
+ '?marker=%s&limit=1&format=json'
% LIST_CONTAINER_RESP[1]['name'],
json=[],
status_code=200,
)
@ -151,7 +159,7 @@ class TestContainer(TestObjectAPIv1):
'X-Container-Meta-Owner': FAKE_ACCOUNT,
'x-container-object-count': '1',
'x-container-bytes-used': '577',
'x-storage-policy': 'o1--sr-r3'
'x-storage-policy': 'o1--sr-r3',
}
resp = {
'account': FAKE_ACCOUNT,
@ -172,7 +180,6 @@ class TestContainer(TestObjectAPIv1):
class TestObject(TestObjectAPIv1):
def setUp(self):
super(TestObject, self).setUp()
@ -209,7 +216,7 @@ class TestObject(TestObjectAPIv1):
def test_object_create(self):
self.base_object_create('111\n222\n333\n')
self.base_object_create(bytes([0x31, 0x00, 0x0d, 0x0a, 0x7f, 0xff]))
self.base_object_create(bytes([0x31, 0x00, 0x0D, 0x0A, 0x7F, 0xFF]))
def test_object_delete(self):
self.requests_mock.register_uri(
@ -274,35 +281,35 @@ class TestObject(TestObjectAPIv1):
)
self.assertEqual(LIST_CONTAINER_RESP, ret)
# def test_list_objects_full_listing(self):
# sess = self.app.client_manager.session
#
# def side_effect(*args, **kwargs):
# rv = sess.get().json.return_value
# sess.get().json.return_value = []
# sess.get().json.side_effect = None
# return rv
#
# resp = [{'name': 'is-name'}]
# sess.get().json.return_value = resp
# sess.get().json.side_effect = side_effect
#
# data = lib_object.list_objects(
# sess,
# fake_url,
# fake_container,
# full_listing=True,
# )
#
# # Check expected values
# sess.get.assert_called_with(
# fake_url + '/' + fake_container,
# params={
# 'format': 'json',
# 'marker': 'is-name',
# }
# )
# self.assertEqual(resp, data)
# def test_list_objects_full_listing(self):
# sess = self.app.client_manager.session
#
# def side_effect(*args, **kwargs):
# rv = sess.get().json.return_value
# sess.get().json.return_value = []
# sess.get().json.side_effect = None
# return rv
#
# resp = [{'name': 'is-name'}]
# sess.get().json.return_value = resp
# sess.get().json.side_effect = side_effect
#
# data = lib_object.list_objects(
# sess,
# fake_url,
# fake_container,
# full_listing=True,
# )
#
# # Check expected values
# sess.get.assert_called_with(
# fake_url + '/' + fake_container,
# params={
# 'format': 'json',
# 'marker': 'is-name',
# }
# )
# self.assertEqual(resp, data)
def test_object_show(self):
headers = {
@ -323,8 +330,7 @@ class TestObject(TestObjectAPIv1):
'content-length': '577',
'last-modified': '20130101',
'etag': 'qaz',
'properties': {'wife': 'Wilma',
'Husband': 'fred'},
'properties': {'wife': 'Wilma', 'Husband': 'fred'},
}
self.requests_mock.register_uri(
'HEAD',