Skip to content

Commit

Permalink
updates to object attributes, addition of functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNiESh committed Apr 29, 2024
1 parent 6f2b419 commit 1241804
Show file tree
Hide file tree
Showing 24 changed files with 409 additions and 124 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
# esisdk
Unified SDK for ESI

## Use the SDK in python scripts
### Install ESI SDK:
```
python setup.py install
```

### Create a connection to ESI SDK

There are several methods to establish a connection using the ESI SDK. Since the ***esi.connection.ESIConnection*** class inherits from ***openstack.connection.Connection***, [methods](https://docs.openstack.org/openstacksdk/latest/user/connection.html) applicable for creating connections in the OpenStack SDK can also be used with the ESI SDK. Below are some common ways to create an ESIConnection:

**Create a connection using only keyword arguments**
```
from esi import connection
conn = connection.Connection(
region_name='example-region',
auth={
'auth_url': 'https://auth.example.com',
'username': 'user',
'password': 'password',
'project_name': 'project_name',
'user_domain_name': 'user_domain_name',
'project_domain_name': 'project_domain_name'
},
interface='public'
)
```
**Create a connection from existing CloudRegion**
```
from esi import connection
import openstack.config
config = openstack.config.get_cloud_region(
cloud='example',
region_name='earth',
)
conn = connection.Connection(config=config)
```

### Make API calls
Detailed APIs can be found in the `esi/lease/v1/_proxy.py` file. Below are simple examples demonstrating lease resource CRUD operations.

**Create a lease**
```
def lease_create(conn, resource_uuid, project_id, **kwargs):
lease = conn.lease.create_lease(resource_uuid=resource_uuid,
project_id=project_id,
**kwargs)
```
**List leases**
```
def lease_list(conn, **kwargs):
leases = conn.lease.leases(**kwargs)
```
**Update a lease**
```
def lease_update(conn, lease, **kwargs):
lease_dict = conn.lease.update_lease(lease, **kwargs)
```
**Delete a lease**
```
def lease_delete(conn, lease_id):
leases = conn.lease.delete_lease(lease_id)
```
8 changes: 4 additions & 4 deletions esi/cloud/_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def list_offers(self, **kwargs):
"""Return a list of all offers."""
return list(self.lease.offers(**kwargs))

def create_offer(self, resource_id, node_type, **kwargs):
def create_offer(self, resource_uuid, node_type, **kwargs):
"""Create an offer"""
return self.lease.create_offer(resource_id=resource_id,
return self.lease.create_offer(resource_uuid=resource_uuid,
node_type=node_type,
**kwargs)

Expand All @@ -42,9 +42,9 @@ def list_leases(self, **kwargs):
"""Return a list of all leases"""
return list(self.lease.leases(**kwargs))

def create_lease(self, resource_id, project_id, **kwargs):
def create_lease(self, resource_uuid, project_id, **kwargs):
"""Create a lease"""
return self.lease.create_lease(resource_id=resource_id,
return self.lease.create_lease(resource_uuid=resource_uuid,
project_id=project_id,
**kwargs)

Expand Down
17 changes: 15 additions & 2 deletions esi/lease/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ def create_lease(self, **attrs):
"""
return self._create(_lease.Lease, **attrs)

def update_lease(self, lease, **attrs):
"""Update a lease.
:param lease: The value can be the ID of a lease or a
:class:`~esi_leap.v1.lease.Lease` instance.
:param dict attrs: The attributes to update on the lease.
:returns: The updated lease
:rtype: :class:`~esi_leap.v1.lease.Lease`.
"""
res = self._get_resource(_lease.Lease, lease)
return res.update(self, **attrs)

def get_lease(self, lease, fields=None):
"""Get a specific lease.
Expand Down Expand Up @@ -203,12 +216,12 @@ def delete_lease(self, lease, ignore_missing=True):
"""
return self._delete(_lease.Lease, lease, ignore_missing=ignore_missing)

def nodes(self):
def nodes(self, **query):
"""Retrieve a generator of nodes.
:returns: A generator of lease instances.
"""
return _node.Node.list(self)
return _node.Node.list(self, **query)

def events(self, **query):
"""Retrieve a generator of events.
Expand Down
15 changes: 11 additions & 4 deletions esi/lease/v1/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,29 @@ class Event(resource.Resource):

# client-side query parameter
_query_mapping = resource.QueryParameters(
'ID',
'last_event_id',
'event_type',
'event_time',
'last_event_time',
'resource_type',
'resource_uuid'
'resource_uuid',
'lessee_or_owner_id'
)

#: The transaction date and time.
timestamp = resource.Header("x-timestamp")
#: The value of the resource. Also available in headers.
id = resource.Body("id", alternate_id=True)
event_type = resource.Body("event_type")
last_event_id = resource.Body("last_event_id")
last_event_time = resource.Body("last_event_time")
event_id = resource.Body("event_id")
event_time = resource.Body("event_time")
object_type = resource.Body("object_type")
object_uuid = resource.Body("object_uuid")
node_type = resource.Body("resource_type")
resource_uuid = resource.Body("resource_uuid")
lease_id = resource.Body("lease_id")
lessee_or_owner_id = resource.Body("lessee_or_owner_id")
lessee_id = resource.Body("lessee_id")
owner_id = resource.Body("owner_id")

_attr_aliases = {'resource_type': 'node_type'}
46 changes: 42 additions & 4 deletions esi/lease/v1/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.

from openstack import exceptions
from openstack import resource


Expand All @@ -23,6 +24,7 @@ class Lease(resource.Resource):
allow_commit = True
allow_delete = True
allow_list = True
allow_patch = True
commit_method = 'PATCH'
commit_jsonpatch = True

Expand All @@ -32,14 +34,22 @@ class Lease(resource.Resource):
'resource_type',
'status',
'uuid',
'project_id',
'start_time',
'end_time',
'owner_id',
'resource_class',
'purpose',
'properties',
)

#: The transaction date and time.
timestamp = resource.Header("x-timestamp")
#: The value of the resource. Also available in headers.
id = resource.Body("uuid", alternate_id=True)
uuid = resource.Body("uuid", alternate_id=True)
node_type = resource.Body("resource_type")
resource_id = resource.Body("resource_uuid")
resource_name = resource.Body("resource")
resource_uuid = resource.Body("resource_uuid")
resource_class = resource.Body("resource_class")
offer_uuid = resource.Body("offer_uuid")
owner = resource.Body("owner")
Expand All @@ -50,9 +60,37 @@ class Lease(resource.Resource):
fulfill_time = resource.Body("fulfill_time")
expire_time = resource.Body("expire_time")
status = resource.Body("status")
name = resource.Body("name")
project = resource.Body("project")
project_id = resource.Body("project_id")
lease_resource = resource.Body("resource")
properties = resource.Body("properties")
resource_properties = resource.Body("resource_properties")
purpose = resource.Body("purpose")

_attr_aliases = {'resource_type': 'node_type',
'resource': 'resource_name'}

def update(self, session, **kwargs):
"""Update a lease.
:param session: The session to use for making this request.
:type session: :class:`~keystoneauth1.adapter.Adapter`
:returns: The result of update.
:rtype: Response json data.
"""
session = self._get_session(session)

request = self._prepare_request(requires_id=True)
response = session.patch(
request.url,
json=kwargs,
headers=request.headers,
microversion=None,
retriable_status_codes=None,
)

msg = (
"Failed to update lease {lease} ".format(lease=self.id)
)
exceptions.raise_from_response(response, error_message=msg)
return response.json()
9 changes: 5 additions & 4 deletions esi/lease/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ class Node(resource.Resource):
#: The transaction date and time.
timestamp = resource.Header("x-timestamp")
#: The value of the resource. Also available in headers.
id = resource.Body("uuid", alternate_id=True)
name = resource.Body("name")
uuid = resource.Body("uuid", alternate_id=True)
owner = resource.Body("owner")
lessee = resource.Body("lessee")
provision_state = resource.Body("provision_state")
maintenance = resource.Body("maintenance")
offer_id = resource.Body("offer_uuid")
lease_id = resource.Body("lease_uuid")
offer_uuid = resource.Body("offer_uuid")
lease_uuid = resource.Body("lease_uuid")
future_offers = resource.Body("future_offers")
future_leases = resource.Body("future_leases")
properties = resource.Body("properties")
resource_class = resource.Body("resource_class")
22 changes: 18 additions & 4 deletions esi/lease/v1/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,44 @@ class Offer(resource.Resource):
_query_mapping = resource.QueryParameters(
'resource_uuid',
'resource_type',
'resource_class',
'status',
'uuid',
'lessee',
'start_time',
'end_time',
'lessee_id',
'name',
'properties',
'project_id',
'available_start_time',
'available_end_time',
)

#: The transaction date and time.
timestamp = resource.Header("x-timestamp")
#: The value of the resource. Also available in headers.
id = resource.Body("uuid", alternate_id=True)
uuid = resource.Body("uuid", alternate_id=True)
node_type = resource.Body("resource_type")
resource_id = resource.Body("resource_uuid")
resource_uuid = resource.Body("resource_uuid")
resource_class = resource.Body("resource_class")
lessee = resource.Body("lessee")
lessee_id = resource.Body("lessee_id")
parent_lease_uuid = resource.Body("parent_lease_uuid")
start_time = resource.Body("start_time")
end_time = resource.Body("end_time")
status = resource.Body("status")
available_start_time = resource.Body("available_start_time")
available_end_time = resource.Body("available_end_time")
availabilities = resource.Body("availabilities")
name = resource.Body("name")
project = resource.Body("project")
project_id = resource.Body("project_id")
offer_resource = resource.Body("resource")
resource_name = resource.Body("resource")
properties = resource.Body("properties")
resource_properties = resource.Body("resource_properties")

_attr_aliases = {'resource_type': 'node_type',
'resource': 'resource_name'}

def claim_offer(self, session, **kwargs):
"""Claim an offer.
Expand Down
24 changes: 12 additions & 12 deletions esi/tests/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@
class FakeOffer:
def __init__(self, id, node_id, node_type):
self.id = id
self.resource_id = node_id
self.resource_uuid = node_id
self.node_type = node_type


class FakeLease:
def __init__(self, id, node_id, node_type, offer_uuid):
self.id = id
self.resource_id = node_id
self.resource_uuid = node_id
self.node_type = node_type
self.offer_uuid = offer_uuid


class FakeNode:
def __init__(self, id, offer_id, lease_id):
def __init__(self, id, offer_uuid, lease_uuid):
self.id = id
self.offer_id = offer_id
self.lease_id = lease_id
self.offer_uuid = offer_uuid
self.lease_uuid = lease_uuid


class FakeEvent:
def __init__(self, id, event_type, event_time):
def __init__(self, id, event_type, last_event_time):
self.id = id
self.event_type = event_type
self.event_time = event_time
self.last_event_time = last_event_time


def make_fake_offer(id, node_id, node_type):
Expand All @@ -63,16 +63,16 @@ def make_fake_lease(id, node_id, node_type, offer_uuid):
offer_uuid=offer_uuid))


def make_fake_node(id, offer_id, lease_id):
def make_fake_node(id, offer_uuid, lease_uuid):
return meta.obj_to_munch(FakeNode(id=id,
offer_id=offer_id,
lease_id=lease_id))
offer_uuid=offer_uuid,
lease_uuid=lease_uuid))


def make_fake_event(id, event_type, event_time):
def make_fake_event(id, event_type, last_event_time):
return meta.obj_to_munch(FakeEvent(id=id,
event_type=event_type,
event_time=event_time))
last_event_time=last_event_time))


def get_lease_endpoint():
Expand Down
25 changes: 25 additions & 0 deletions esi/tests/functional/lease/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### Prerequisites

These tests are intended to be run against a functioning OpenStack cloud with esi-leap services enabled and running (https://github.com/CCI-MOC/esi-leap). Please set the following environment variables in order to run full tests:
* TestESILEAPLease and TestESILEAPOffer: set `NODE_1_UUID`, `NODE_1_TYPE`, `NODE_2_UUID`, `NODE_2_TYPE` in tox.ini. These nodes should not be associated with any existing leases/offers during testing.
* TestESILEAPEvent: set `LAST_EVENT_ID`, `NODE_1_UUID` and `NODE_1_TYPE` in tox.ini.
* TestESILEAPNode: set `NODE_3_NAME` in tox.ini. This node should be associated with leases/offers.

The clouds.yaml file should be like this: https://github.com/openstack/openstacksdk/blob/master/doc/source/contributor/clouds.yaml

### Running the tests

By default, the functional tests will not run when invoking `tox` with no additional options. To run them, you must specify the 'functional' testenv like this:

```
$ tox -e functional
```

To run specific tests,
```
$ tox -e functional -- "test_node_list"
```
or
```
$ tox -e functional -- "TestESILEAPOffer"
```
Loading

0 comments on commit 1241804

Please sign in to comment.