Skip to content

Commit

Permalink
Merge pull request #2 from DanNiESh/update
Browse files Browse the repository at this point in the history
Update esi sdk
  • Loading branch information
tzumainn authored Apr 30, 2024
2 parents 6f2b419 + cd4c5e7 commit 8bc522f
Show file tree
Hide file tree
Showing 25 changed files with 482 additions and 124 deletions.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,70 @@
# 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.ESIConnection(
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.ESIConnectionn(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.
```
import esi
import os
TEST_CLOUD = os.getenv('OS_TEST_CLOUD', 'devstack-admin')
conn = esi.connect(cloud=TEST_CLOUD)
# 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)
```
71 changes: 71 additions & 0 deletions esi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import argparse
import typing as ty

import esi.connection

from openstack._log import enable_logging
import openstack.config

__all__ = [
'connect',
'enable_logging',
]


def connect(
cloud: ty.Optional[str] = None,
app_name: ty.Optional[str] = None,
app_version: ty.Optional[str] = None,
options: ty.Optional[argparse.Namespace] = None,
load_yaml_config: bool = True,
load_envvars: bool = True,
**kwargs,
) -> esi.connection.ESIConnection:
"""Create a :class:`~esi.connection.ESIConnection`
:param string cloud:
The name of the configuration to load from clouds.yaml. Defaults
to 'envvars' which will load configuration settings from environment
variables that start with ``OS_``.
:param argparse.Namespace options:
An argparse Namespace object. Allows direct passing in of
argparse options to be added to the cloud config. Values
of None and '' will be removed.
:param bool load_yaml_config:
Whether or not to load config settings from clouds.yaml files.
Defaults to True.
:param bool load_envvars:
Whether or not to load config settings from environment variables.
Defaults to True.
:param kwargs:
Additional configuration options.
:returns: esi.connnection.ESIConnection
:raises: keystoneauth1.exceptions.MissingRequiredOptions
on missing required auth parameters
"""
cloud_region = openstack.config.get_cloud_region(
cloud=cloud,
app_name=app_name,
app_version=app_version,
load_yaml_config=load_yaml_config,
load_envvars=load_envvars,
options=options,
**kwargs,
)
return esi.connection.ESIConnection(
config=cloud_region,
vendor_hook=kwargs.get('vendor_hook'),
)
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
Loading

0 comments on commit 8bc522f

Please sign in to comment.