Skip to content

Commit

Permalink
Merge pull request #51 from tzumainn/lease-update
Browse files Browse the repository at this point in the history
Added "lease update" command
  • Loading branch information
tzumainn authored Apr 2, 2024
2 parents 3b3d208 + 96f9081 commit 562f3a4
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
33 changes: 33 additions & 0 deletions esileapclient/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,39 @@ def _create(self, os_esileap_api_version=None, **kwargs):
else:
raise exceptions.CommandError(json.loads(resp.text)['faultstring'])

def _update(self, resource_id, os_esileap_api_version=None, **kwargs):
"""Update a resource based on a kwargs dictionary of attributes.
:param kwargs: A dictionary containing the attributes of the resource
that will be updated.
"""

new = {}
invalid = []
for (key, value) in kwargs.items():
if key in self.resource_class._update_attributes:
new[key] = value
else:
invalid.append(key)
if invalid:
raise Exception('The attribute(s) "%(attrs)s" '
'are invalid; they are not '
'needed to update %(resource)s.' %
{'resource': self._resource_name,
'attrs': '","'.join(invalid)})

headers = {}
if os_esileap_api_version is not None:
headers['headers'] = {'X-OpenStack-ESI-Leap-API-Version':
os_esileap_api_version}

url = self._path(resource_id)
resp, body = self.api.json_request('PATCH', url, body=new, **headers)

if resp.status_code == 200:
return self.resource_class(self, body)
else:
raise exceptions.CommandError(json.loads(resp.text)['faultstring'])

def _list(self, url, obj_class=None, os_esileap_api_version=None):
if obj_class is None:
obj_class = self.resource_class
Expand Down
34 changes: 34 additions & 0 deletions esileapclient/osc/v1/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ def take_action(self, parsed_args):
return self.dict2columns(data)


class UpdateLease(command.ShowOne):
"""Update a lease."""

log = logging.getLogger(__name__ + ".UpdateLease")

def get_parser(self, prog_name):
parser = super(UpdateLease, self).get_parser(prog_name)

parser.add_argument(
"uuid",
metavar="<uuid>",
help="UUID of the lease")
parser.add_argument(
'--end-time',
dest='end_time',
required=False,
help="Time when the lease will expire.")
return parser

def take_action(self, parsed_args):
client = self.app.client_manager.lease

field_list = LEASE_RESOURCE._update_attributes
fields = dict((k, v) for (k, v) in vars(parsed_args).items()
if k in field_list and v is not None)

lease = client.lease.update(parsed_args.uuid, **fields)

data = dict([(f, getattr(lease, f, '')) for f in
LEASE_RESOURCE.fields])

return self.dict2columns(data)


class ListLease(command.Lister):
"""List leases."""

Expand Down
37 changes: 37 additions & 0 deletions esileapclient/tests/unit/common/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
CREATE_FAKE_RESOURCE = copy.deepcopy(FAKE_RESOURCE)
del CREATE_FAKE_RESOURCE['uuid']

UPDATE_FAKE_RESOURCE = {
'attribute1': '5',
}

INVALID_UPDATE_FAKE_RESOURCE = {
'attribute2': '6',
}

INVALID_ATTRIBUTE_FAKE_RESOURCE = {
'non-existent-attribute': 'bad',
'attribute1': '1',
Expand Down Expand Up @@ -73,6 +81,7 @@ class FakeResource(base.Resource):
}

_creation_attributes = ['attribute1', 'attribute2']
_update_attributes = ['attribute1']

def __repr__(self):
return "<FakeResource %s>" % self._info
Expand Down Expand Up @@ -133,6 +142,34 @@ def test__create_with_invalid_attribute(self):
manager._create,
**INVALID_ATTRIBUTE_FAKE_RESOURCE)

def test__update(self):
manager = FakeResourceManager(None)
with mock.patch.object(manager, 'api') as mock_api:

mock_api.json_request.return_value = (
VALID_RESPONSE,
FAKE_RESOURCE)

resource = manager._update(FAKE_RESOURCE['uuid'],
**UPDATE_FAKE_RESOURCE)

mock_api.json_request.assert_called_once_with(
'PATCH', '/v1/fakeresources/%s' % FAKE_RESOURCE['uuid'],
**{'body': UPDATE_FAKE_RESOURCE})

self.assertIsInstance(resource, FakeResource)
self.assertEqual(resource._info, FAKE_RESOURCE)

def test__update_with_invalid_attribute(self):
manager = FakeResourceManager(None)
with mock.patch.object(manager, 'api'):

self.assertRaises(
Exception,
manager._update,
FAKE_RESOURCE['uuid'],
**INVALID_UPDATE_FAKE_RESOURCE)

def test__list(self):

manager = FakeResourceManager(None)
Expand Down
38 changes: 38 additions & 0 deletions esileapclient/tests/unit/osc/v1/test_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,44 @@ def test_lease_create(self):
self.client_mock.lease.create.assert_called_once_with(**args)


class TestUpdateLease(TestLease):

def setUp(self):
super(TestUpdateLease, self).setUp()

self.client_mock.lease.update.return_value = (
base.FakeResource(copy.deepcopy(fakes.LEASE))
)

# Get the command object to test
self.cmd = lease.UpdateLease(self.app, None)

def test_lease_update(self):

arglist = [
fakes.lease_uuid,
'--end-time', fakes.lease_end_time,
]

verifylist = [
('uuid', fakes.lease_uuid),
]

parsed_args = self.check_parser(self.cmd, arglist, verifylist)

self.cmd.take_action(parsed_args)

self.client_mock.lease.update.assert_called_once_with(
fakes.lease_uuid, end_time=fakes.lease_end_time)

def test_update_show_no_id(self):
arglist = []
verifylist = []
self.assertRaises(osctestutils.ParserException,
self.check_parser,
self.cmd, arglist, verifylist)


class TestLeaseList(TestLease):

def setUp(self):
Expand Down
13 changes: 13 additions & 0 deletions esileapclient/v1/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Lease(base.Resource):
_creation_attributes = ['start_time', 'end_time', 'status', 'name',
'properties', 'project_id', 'resource_type',
'resource_uuid', 'purpose']
_update_attributes = ['end_time']

def __repr__(self):
return "<Lease %s>" % self._info
Expand All @@ -94,6 +95,18 @@ def create(self, os_esileap_api_version=None, **kwargs):

return lease

def update(self, lease_uuid, os_esileap_api_version=None, **kwargs):
"""Update a lease based on a kwargs dictionary of attributes.
:returns: a :class: `Lease` object
"""

lease = self._update(
lease_uuid,
os_esileap_api_version=os_esileap_api_version,
**kwargs)

return lease

def list(self, filters, os_esileap_api_version=None):
"""Retrieve a list of leases.
:returns: A list of leases.
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ openstack.lease.v1 =
esi_event_list = esileapclient.osc.v1.event:ListEvent
esi_lease_list = esileapclient.osc.v1.lease:ListLease
esi_lease_create = esileapclient.osc.v1.lease:CreateLease
esi_lease_update = esileapclient.osc.v1.lease:UpdateLease
esi_lease_show = esileapclient.osc.v1.lease:ShowLease
esi_lease_delete = esileapclient.osc.v1.lease:DeleteLease
esi_mdc_lease_list = esileapclient.osc.v1.mdc.mdc_lease:MDCListLease
Expand Down

0 comments on commit 562f3a4

Please sign in to comment.