Skip to content

Commit

Permalink
Fix linting and formatting errors reported by pre-commit
Browse files Browse the repository at this point in the history
This commit consists of the changes introduced by using ruff to format all
the source files in the repository and the changes necessary to correct
any linting errors reported by ruff.
  • Loading branch information
larsks committed Jul 13, 2024
1 parent 7dd7660 commit e3b6f74
Show file tree
Hide file tree
Showing 94 changed files with 4,893 additions and 4,214 deletions.
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,3 @@
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

1 change: 0 additions & 1 deletion babel.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[python: **.py]

18 changes: 9 additions & 9 deletions docs/esi-leap-api-ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ The Offer API endpoint can be reached at /v1/offers.
* The /v1/offers/\<uuid_or_name> endpoint is used to retrieve the offer with the given uuid. The response type is 'application/json'.
* The /v1/offers endpoint is used to retrieve a list of offers. This URL supports several URL variables for retrieving offers filtered by given values. The response type is 'application/json'.
* project_id: Returns all offers with given project_id.
* status: Returns all offers with given status.
* status: Returns all offers with given status.
* This value will default to returning offers with status 'available'.
* This value can be set to 'any' to return offers without filtering by status.
* resource_uuid: Returns all offers with given resource_uuid.
* resource_type: Returns all offers with given resource_type
* start_time and end_time: Passing in values for the start_time and end_time variables will return all offers with a start_time and end_time which completely span the given values. These two URL variables must be used together. Passing in only one will throw an error.
* start_time and end_time: Passing in values for the start_time and end_time variables will return all offers with a start_time and end_time which completely span the given values. These two URL variables must be used together. Passing in only one will throw an error.
* available_start_time and available_end_time: Passing in values for the available_start_time and available_end_time variables will return all offers with availabilities which completely span the given values. These two URL variables must be used together. Passing in only one will throw an error.


Expand All @@ -32,9 +32,9 @@ The Offer API endpoint can be reached at /v1/offers.
* properties:
* a json object
* This field is optional. Not setting it will default to {}.
* 'status', 'uuid', and 'availabilities' are read only.
* 'status', 'uuid', and 'availabilities' are read only.
* The response to a POST request will be the newly created offer. The response type is 'application/json'.

An example curl request is shown below.
```
curl -X POST -sH "X-Auth-Token: $token" http://localhost:7777/v1/offers -H 'Content-Type: application/json' -d '{
Expand All @@ -54,13 +54,13 @@ curl -X POST -sH "X-Auth-Token: $token" http://localhost:7777/v1/offers -H 'Con
"size_gb": 500,
"model": "YOYODYNE 1234"
},
{
{
"size_gb": 1024,
"model": "evo840 ssd"
}
]
}
}'
}'
```

##### DELETE
Expand All @@ -80,10 +80,10 @@ The lease api endpoint can be reached at /v1/leases
* The /v1/leases endpoint is used to retrieve a list of leases. This URL supports several URL variables for retrieving offers filtered by given values. The response type is 'application/json'.
* project_id: Returns all leases with given project_id.
* This value will default to the project_id of the request.
* status: Returns all offers with given status.
* status: Returns all offers with given status.
* This value will default to returning leases with status 'open'.
* This value can be set to 'any' to return leases without filtering by status.
* start_time and end_time: Passing in values for the start_time and end_time variables will return all leases with a start_time and end_time which completely span the given values. These two URL variables must be used together. Passing in only one will throw an error.
* start_time and end_time: Passing in values for the start_time and end_time variables will return all leases with a start_time and end_time which completely span the given values. These two URL variables must be used together. Passing in only one will throw an error.
* owner: Returns all leases which are related to offers with project_id 'owner'.
* view: Setting view to 'all' will return all leases in the database. This value can be used in combination with other filters.

Expand All @@ -101,7 +101,7 @@ The lease api endpoint can be reached at /v1/leases
* offer_uuid_or_name:
* A string.
* This field is required.
* 'status' and 'uuid' are read only.
* 'status' and 'uuid' are read only.
* The response to a POST request will be the newly created lease. The response type is 'application/json'.

An example curl request is shown below.
Expand Down
2 changes: 1 addition & 1 deletion docs/esi-leap-reporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To generate a csv file with data on all lessees' usage on different nodes:
openstack esi lease list --all --long -f csv > report.csv
```

The ESI-Leap API allows users to retrieve data based on certain parameters.
The ESI-Leap API allows users to retrieve data based on certain parameters.

For example, it is possible to generate a report based on a lessee's OpenStack project_id. To get a list of all
projects type ``openstack project list`` and from there grab the desired project id and run the command:
Expand Down
8 changes: 4 additions & 4 deletions docs/esi-leap-requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ An offer response has the following fields:
* deleted: an offer is no longer available for leasing. An offer is set to deleted when it is manually revoked by a user before its end_time has passed.
* "properties" is the baremetal properties of an offer.
* "availabilities" is a list of [start, end] datetime pairings representing a continuous time range in which an offer is available for leasing.
* "availabilities" is not kept in the schema and is computed when retrieving an offer.
* "availabilities" is not kept in the schema and is computed when retrieving an offer.
* "created_at", "updated_at", and "id" are only used in the schema and cannot be read or set.


Expand Down Expand Up @@ -164,11 +164,11 @@ A lease response has the following fields:


### Manager Service
An ESI-Leap manager has periodic jobs to manage offers and leases.
An ESI-Leap manager has periodic jobs to manage offers and leases.
* expire offers: out-of-date offers, i.e, the current timestamp > offer's end_time, will be updated with an 'EXPIRED' status.
* fulfill leases: if a lease's start_time <= the current timestamp and is not expired, the manager service will fulfill the resources in the leases and update the status of the leases to 'active'.
* fulfill leases: if a lease's start_time <= the current timestamp and is not expired, the manager service will fulfill the resources in the leases and update the status of the leases to 'active'.
* expire leases: same as 'expire offers', ESI-Leap will expire leases based on timestamp.
* update offers: after the manager fulfills and expires leases, it will update the relevant offers' status. The offers in a fulfilled lease should be unavailable to others. Likewise, when a lease expires, offers in the lease should be updated and be available again.
* update offers: after the manager fulfills and expires leases, it will update the relevant offers' status. The offers in a fulfilled lease should be unavailable to others. Likewise, when a lease expires, offers in the lease should be updated and be available again.

## Reporting API
ESI-Leap admin queries this API to learn about the usage of nodes given a period. The admin enters a date range to get all leases' information within that range. The results could be like this:
Expand Down
12 changes: 6 additions & 6 deletions esi_leap/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def after(self, state):

def get_pecan_config():
cfg_dict = {
'app': {
'root': CONF.pecan.root,
'modules': CONF.pecan.modules,
'debug': CONF.pecan.debug,
'auth_enable': CONF.pecan.auth_enable
"app": {
"root": CONF.pecan.root,
"modules": CONF.pecan.modules,
"debug": CONF.pecan.debug,
"auth_enable": CONF.pecan.auth_enable,
}
}

Expand All @@ -54,7 +54,7 @@ def setup_app(config=None):
hooks=lambda: [ContextHook()],
debug=CONF.pecan.debug,
static_root=config.app.static_root if CONF.pecan.debug else None,
force_canonical=getattr(config.app, 'force_canonical', True),
force_canonical=getattr(config.app, "force_canonical", True),
)

if CONF.pecan.auth_enable:
Expand Down
1 change: 0 additions & 1 deletion esi_leap/api/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@


class ESILEAPBase(wtypes.Base):

def to_dict(self):
esi_leap_dict = {}
if self.fields:
Expand Down
20 changes: 9 additions & 11 deletions esi_leap/api/controllers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@
from esi_leap.api.controllers.v1 import root


API_VERSION = 'v1'
API_VERSION = "v1"


class RootController(rest.RestController):

_versions = [API_VERSION]
_default_version = API_VERSION

v1 = root.Controller()

@pecan.expose(content_type='application/json')
@pecan.expose(content_type="application/json")
def index(self):
pecan.response.status_code = 300
pecan.response.content_type = 'application/json'
pecan.response.content_type = "application/json"
versions = {
'versions': [
"versions": [
{
'id': 'v1.0',
'status': 'CURRENT',
'links': [{
'href': '{0}/v1'.format(pecan.request.host_url),
'rel': 'self'
}]
"id": "v1.0",
"status": "CURRENT",
"links": [
{"href": "{0}/v1".format(pecan.request.host_url), "rel": "self"}
],
}
]
}
Expand Down
26 changes: 13 additions & 13 deletions esi_leap/api/controllers/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class JsonType(wtypes.UserType):
"""A simple JSON type."""

basetype = wtypes.text
name = 'json'
name = "json"

def __str__(self):
# These are the json serializable native types
return ' | '.join(map(str, (wtypes.text, int, float,
bool, list, dict, None)))
return " | ".join(map(str, (wtypes.text, int, float, bool, list, dict, None)))

@staticmethod
def validate(value):
Expand All @@ -38,7 +37,6 @@ def frombasetype(value):


class Collection(wtypes.Base):

@property
def collection(self):
return getattr(self, self._type)
Expand All @@ -52,15 +50,17 @@ def get_next(self, limit, url=None, **kwargs):
return wtypes.Unset

url = url or self._type
q_args = ''.join(['%s=%s&' % item for item in kwargs.items()])
next_args = '?%(args)slimit=%(limit)d&marker=%(marker)s' % {
'args': q_args, 'limit': limit,
'marker': getattr(self.collection[-1], 'uuid')}

next_link = '%(url)s/v1/%(resource)s%(args)s' % {
'url': url,
'resource': self._type,
'args': next_args
q_args = "".join(["%s=%s&" % item for item in kwargs.items()])
next_args = "?%(args)slimit=%(limit)d&marker=%(marker)s" % {
"args": q_args,
"limit": limit,
"marker": getattr(self.collection[-1], "uuid"),
}

next_link = "%(url)s/v1/%(resource)s%(args)s" % {
"url": url,
"resource": self._type,
"args": next_args,
}

return next_link
Expand Down
69 changes: 41 additions & 28 deletions esi_leap/api/controllers/v1/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


class Event(base.ESILEAPBase):

id = wsme.wsattr(int, readonly=True)
event_type = wsme.wsattr(wtypes.text, readonly=True)
event_time = wsme.wsattr(datetime.datetime, readonly=True)
Expand All @@ -42,7 +41,6 @@ class Event(base.ESILEAPBase):
owner_id = wsme.wsattr(wtypes.text, readonly=True)

def __init__(self, **kwargs):

self.fields = event_obj.Event.fields
for field in self.fields:
setattr(self, field, kwargs.get(field, wtypes.Unset))
Expand All @@ -52,28 +50,41 @@ class EventCollection(types.Collection):
events = [Event]

def __init__(self, **kwargs):
self._type = 'events'
self._type = "events"


class EventsController(rest.RestController):

@wsme_pecan.wsexpose(EventCollection, int, wtypes.text,
datetime.datetime, wtypes.text, wtypes.text,
wtypes.text, wtypes.text)
def get_all(self, last_event_id=None, lessee_or_owner_id=None,
last_event_time=None, event_type=None,
resource_type=None, resource_uuid=None):
@wsme_pecan.wsexpose(
EventCollection,
int,
wtypes.text,
datetime.datetime,
wtypes.text,
wtypes.text,
wtypes.text,
wtypes.text,
)
def get_all(
self,
last_event_id=None,
lessee_or_owner_id=None,
last_event_time=None,
event_type=None,
resource_type=None,
resource_uuid=None,
):
request = pecan.request.context
cdict = request.to_policy_values()

try:
utils.policy_authorize('esi_leap:offer:offer_admin', cdict, cdict)
utils.policy_authorize("esi_leap:offer:offer_admin", cdict, cdict)
except exception.HTTPForbidden:
lessee_or_owner_id = cdict['project_id']
lessee_or_owner_id = cdict["project_id"]

if lessee_or_owner_id is not None:
lessee_or_owner_id = keystone.get_project_uuid_from_ident(
lessee_or_owner_id)
lessee_or_owner_id
)

if resource_uuid is not None:
if resource_type is None:
Expand All @@ -82,12 +93,12 @@ def get_all(self, last_event_id=None, lessee_or_owner_id=None,
resource_uuid = resource.get_uuid()

filters = {
'last_event_id': last_event_id,
'last_event_time': last_event_time,
'lessee_or_owner_id': lessee_or_owner_id,
'event_type': event_type,
'resource_type': resource_type,
'resource_uuid': resource_uuid,
"last_event_id": last_event_id,
"last_event_time": last_event_time,
"lessee_or_owner_id": lessee_or_owner_id,
"event_type": event_type,
"resource_type": resource_type,
"resource_uuid": resource_uuid,
}

# unpack iterator to tuple so we can use 'del'
Expand All @@ -99,15 +110,17 @@ def get_all(self, last_event_id=None, lessee_or_owner_id=None,
event_collection = EventCollection()
event_collection.events = []
for event in events:
e = Event(id=event.id,
event_type=event.event_type,
event_time=event.event_time,
object_type=event.object_type,
object_uuid=event.object_uuid,
resource_type=event.resource_type,
resource_uuid=event.resource_uuid,
lessee_id=event.lessee_id,
owner_id=event.owner_id)
e = Event(
id=event.id,
event_type=event.event_type,
event_time=event.event_time,
object_type=event.object_type,
object_uuid=event.object_uuid,
resource_type=event.resource_type,
resource_uuid=event.resource_uuid,
lessee_id=event.lessee_id,
owner_id=event.owner_id,
)
event_collection.events.append(e)

return event_collection
Loading

0 comments on commit e3b6f74

Please sign in to comment.