Skip to content

Commit

Permalink
Merge pull request #17 from larsks/fix/precommit
Browse files Browse the repository at this point in the history
Add pre-commit configuration and workflow
  • Loading branch information
tzumainn authored Jan 6, 2025
2 parents c7a57fa + efafaa5 commit 63773ee
Show file tree
Hide file tree
Showing 37 changed files with 1,633 additions and 1,412 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Set update schedule for GitHub Actions

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every week
interval: "weekly"
31 changes: 31 additions & 0 deletions .github/workflows/precommit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "Run pre-commit checks"

on:
push:
pull_request:

jobs:
run-linters:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Configure caching
uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: precommit-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }}

- name: Install pre-commit
run: |
pip install pre-commit
- name: Run linters
run: |
pre-commit run --all-files
13 changes: 8 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: esisdk
name: "Run tests"

on: [push, pull_request]
on:
- push
- pull_request

jobs:
build:
run-tests:

runs-on: ubuntu-20.04
strategy:
Expand All @@ -12,15 +14,16 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Lint
run: tox -epep8
- name: Unit Tests
run: tox -epy3
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: check-merge-conflict
- id: end-of-file-fixer
- id: check-added-large-files
- id: check-case-conflict
- id: check-json
- id: check-symlinks
- id: detect-private-key
- id: check-executables-have-shebangs

- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
- id: yamllint
files: \.(yaml|yml)$
types: [file, yaml]
entry: yamllint --strict

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: ruff
- id: ruff-format
10 changes: 10 additions & 0 deletions .yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends: default
rules:
line-length: disable
document-start: disable
indentation:
indent-sequences: whatever
hyphens:
max-spaces-after: 4
truthy:
check-keys: false
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# esisdk

Unified SDK for ESI

## Use the SDK in python scripts
### Install ESI SDK:
## Install ESI SDK:

```
python setup.py install
```

### Create a connection to ESI SDK
## 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 applicable for creating connections](https://docs.openstack.org/openstacksdk/latest/user/connection.html)
in the OpenStack SDK can also be used with the ESI SDK. Below are some common ways to create an `ESIConnection`:

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

**Create a connection using only keyword arguments**
```
from esi import connection
Expand All @@ -28,7 +31,9 @@ conn = connection.ESIConnection(
interface='public'
)
```
**Create a connection from existing CloudRegion**

### Create a connection from existing CloudRegion

```
from esi import connection
import openstack.config
Expand All @@ -40,8 +45,10 @@ config = openstack.config.get_cloud_region(
conn = connection.ESIConnectionn(config=config)
```

### Make API calls
## 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
Expand All @@ -66,5 +73,4 @@ def lease_update(conn, lease, **kwargs):
# Delete a lease
def lease_delete(conn, lease_id):
leases = conn.lease.delete_lease(lease_id)
```
```
6 changes: 3 additions & 3 deletions esi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import openstack.config

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


Expand Down Expand Up @@ -67,5 +67,5 @@ def connect(
)
return esi.connection.ESIConnection(
config=cloud_region,
vendor_hook=kwargs.get('vendor_hook'),
vendor_hook=kwargs.get("vendor_hook"),
)
2 changes: 1 addition & 1 deletion esi/_services_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@


class ServicesMixin:
lease = lease_service.LeaseService(service_type='lease')
lease = lease_service.LeaseService(service_type="lease")
12 changes: 6 additions & 6 deletions esi/cloud/_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def list_offers(self, **kwargs):

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

def delete_offer(self, offer):
"""Delete an offer"""
Expand All @@ -44,9 +44,9 @@ def list_leases(self, **kwargs):

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

def delete_lease(self, lease):
"""Delete a lease"""
Expand Down
6 changes: 3 additions & 3 deletions esi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from openstack import connection


class ESIConnection(connection.Connection,
_services_mixin.ServicesMixin,
_lease.LeaseCloudMixin):
class ESIConnection(
connection.Connection, _services_mixin.ServicesMixin, _lease.LeaseCloudMixin
):
def __init__(self, **kwargs):
super(ESIConnection, self).__init__(**kwargs)
47 changes: 21 additions & 26 deletions esi/lease/lease_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LeaseService(service_description.ServiceDescription):
"""The esi-leap lease service."""

supported_versions = {
'1': _proxy.Proxy,
"1": _proxy.Proxy,
}

def _make_proxy(self, instance):
Expand All @@ -36,8 +36,8 @@ def _make_proxy(self, instance):

# Check to see if we've got config that matches what we
# understand in the SDK.
version_string = config.get_api_version('lease')
endpoint_override = config.get_endpoint('lease')
version_string = config.get_api_version("lease")
endpoint_override = config.get_endpoint("lease")

# If the user doesn't give a version in config, but we only support
# one version, then just use that version.
Expand All @@ -51,7 +51,7 @@ def _make_proxy(self, instance):
proxy_class = self.supported_versions.get(version_string[0])
if proxy_class:
proxy_obj = config.get_session_client(
'lease',
"lease",
constructor=proxy_class,
)
else:
Expand All @@ -63,12 +63,12 @@ def _make_proxy(self, instance):
category=os_warnings.UnsupportedServiceVersion,
)
elif endpoint_override:
temp_adapter = config.get_session_client('lease')
temp_adapter = config.get_session_client("lease")
api_version = temp_adapter.get_endpoint_data().api_version
proxy_class = self.supported_versions.get(str(api_version[0]))
if proxy_class:
proxy_obj = config.get_session_client(
'lease',
"lease",
constructor=proxy_class,
)
else:
Expand Down Expand Up @@ -96,52 +96,47 @@ def _make_proxy(self, instance):
# we need to be explicit that this service has an endpoint_override
# so that subsequent discovery calls don't get made incorrectly.
if data.catalog_url != data.service_url:
ep_key = 'lease_endpoint_override'
ep_key = "lease_endpoint_override"
config.config[ep_key] = data.service_url
proxy_obj = config.get_session_client(
'lease',
"lease",
constructor=proxy_class,
)
return proxy_obj

# Make an adapter to let discovery take over
version_kwargs = {}
if version_string:
version_kwargs['version'] = version_string
version_kwargs["version"] = version_string
if getattr(
self.supported_versions[str(version_string)],
'skip_discovery',
"skip_discovery",
False,
):
# set the endpoint_override to the current
# catalog endpoint value + version number,
# otherwise next request will try to perform discovery.
temp_adapter = config.get_session_client('lease')
temp_adapter = config.get_session_client("lease")
ep_override = temp_adapter.get_endpoint(skip_discovery=True)
ep_key = '{service_type}_endpoint_override'.format(
service_type=self.service_type.replace('-', '_')
ep_key = "{service_type}_endpoint_override".format(
service_type=self.service_type.replace("-", "_")
)
config.config[ep_key] = '{}/v{}'.format(
ep_override, version_string)
config.config[ep_key] = "{}/v{}".format(ep_override, version_string)
return config.get_session_client(
'lease',
"lease",
allow_version_hack=True,
constructor=self.supported_versions[
str(version_string)
],
constructor=self.supported_versions[str(version_string)],
**version_kwargs,
)
else:
supported_versions = sorted(
[int(f) for f in self.supported_versions]
)
version_kwargs['min_version'] = str(supported_versions[0])
version_kwargs['max_version'] = '{version}.latest'.format(
supported_versions = sorted([int(f) for f in self.supported_versions])
version_kwargs["min_version"] = str(supported_versions[0])
version_kwargs["max_version"] = "{version}.latest".format(
version=str(supported_versions[-1])
)

temp_adapter = config.get_session_client(
'lease', allow_version_hack=True, **version_kwargs
"lease", allow_version_hack=True, **version_kwargs
)
found_version = temp_adapter.get_api_major_version()
if found_version is None:
Expand All @@ -165,7 +160,7 @@ def _make_proxy(self, instance):
proxy_class = self.supported_versions.get(str(found_version[0]))
if proxy_class:
return config.get_session_client(
'lease',
"lease",
allow_version_hack=True,
constructor=proxy_class,
**version_kwargs,
Expand Down
8 changes: 4 additions & 4 deletions esi/lease/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


class Proxy(proxy.Proxy):

_resource_registry = {
"offer": _offer.Offer,
"lease": _lease.Lease,
Expand All @@ -47,13 +46,13 @@ def _get_with_fields(self, resource_type, value, fields=None):
res = self._get_resource(resource_type, value)
kwargs = {}
if fields:
kwargs['fields'] = _common.fields_type(fields, resource_type)
kwargs["fields"] = _common.fields_type(fields, resource_type)
return res.fetch(
self,
error_message="No {resource_type} found for {value}".format(
resource_type=resource_type.__name__, value=value
),
**kwargs
**kwargs,
)

def offers(self, **query):
Expand Down Expand Up @@ -274,4 +273,5 @@ def delete_console_auth_token(self, node_uuid_or_name, ignore_missing=False):
return self._delete(
_console_auth_token.ConsoleAuthToken,
node_uuid_or_name,
ignore_missing=ignore_missing)
ignore_missing=ignore_missing,
)
Loading

0 comments on commit 63773ee

Please sign in to comment.