Skip to content

Commit

Permalink
manual key loading
Browse files Browse the repository at this point in the history
  • Loading branch information
JungleCatSW committed Dec 31, 2024
1 parent 707eb49 commit 2254ebe
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 27 deletions.
26 changes: 26 additions & 0 deletions examples/manual-api-key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from cudo_compute import cudo_api
from cudo_compute.rest import ApiException
import json

# In normal use the api key is automatically taken from cudoctl commandline tool,
# advanced users may want to supply and api key without installing cudoctl.
# Below is an example of how to do that:

# Create your apis first
# When supplying your api key the vms api should only be created once per key.
api_key = "<KEY>"
vms_api = cudo_api.virtual_machines(api_key)
projects_api = cudo_api.projects(api_key)

# Remember to manually supply your project and not to get project id from the cudo_api
project_id = '<your-project-id>'
try:
vms = vms_api.list_vms(project_id)
print(json.dumps(vms.to_dict(), indent=2))
except ApiException as e:
print(e)

try:
projects = projects_api.list_projects()
except ApiException as e:
print(e)
98 changes: 71 additions & 27 deletions helpers/cudo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ def client():
client.user_agent = 'cudo-compute-python-client/' + version
return client, None

def local_client(key):
configuration = cudo.Configuration()
configuration.api_key['Authorization'] = key
# configuration.debug = True
configuration.api_key_prefix['Authorization'] = 'Bearer'
configuration.host = "https://rest.compute.cudo.org"

client = cudo.ApiClient(configuration)
version = ''
try:
version = importlib.metadata.version('cudo-compute')
except:
pass

client.user_agent = 'cudo-compute-python-client/' + version
return client


def get_api_key():
key_config, context_config, error = cudo.AuthConfig.load_config(home + '/.config/cudo/cudo.yml', "")
Expand Down Expand Up @@ -78,47 +95,72 @@ def project_id_throwable():
raise Exception(err)


def api_keys():
return cudo.APIKeysApi(c)


def disks():
return cudo.DisksApi(c)
def api_keys(key = None):
if key is None:
return cudo.APIKeysApi(c)
else:
return cudo.APIKeysApi(local_client(key))


def networks():
return cudo.NetworksApi(c)
def disks(key = None):
if key is None:
return cudo.DisksApi(c)
else :
return cudo.DisksApi(local_client(key))

def networks(key = None):
if key is None:
return cudo.NetworksApi(c)
else:
return cudo.NetworksApi(local_client(key))

def object_storage():
return cudo.ObjectStorageApi(c)
def object_storage(key = None):
if key is None:
return cudo.ObjectStorageApi(c)
else:
return cudo.ObjectStorageApi(local_client(key))


def permissions():
return cudo.PermissionsApi(c)
def permissions(key = None):
if key is None:
return cudo.PermissionsApi(c)
else:
return cudo.PermissionsApi(local_client(key))


def projects():
return cudo.ProjectsApi(c)
def projects(key = None):
if key is None:
return cudo.ProjectsApi(c)
else:
return cudo.ProjectsApi(local_client(key))


def ssh_keys():
c, err = client()
if err:
raise Exception(err)
return cudo.SSHKeysApi(c)
def ssh_keys(key = None):
if key is None:
return cudo.SSHKeysApi(c)
else:
return cudo.SSHKeysApi(local_client(key))


def search():
return cudo.SearchApi(c)
def search(key = None):
if key is None:
return cudo.SearchApi(c)
else:
return cudo.SearchApi(local_client(key))


def user():
return cudo.UserApi(c)
def user(key = None):
if key is None:
return cudo.UserApi(c)
else:
return cudo.UserApi(local_client(key))


def legacy_virtual_machines():
return cudo.VirtualMachinesApi(c)
def legacy_virtual_machines(key = None):
if key is None:
return cudo.VirtualMachinesApi(c)
else:
return cudo.VirtualMachinesApi(local_client(key))


class PooledVirtualMachinesApi(cudo.VirtualMachinesApi):
Expand Down Expand Up @@ -189,5 +231,7 @@ def stop_workers(self):

pool = PooledVirtualMachinesApi(c)

def virtual_machines():
return pool
def virtual_machines(key = None):
if key is None:
return pool
return PooledVirtualMachinesApi(local_client(key))

0 comments on commit 2254ebe

Please sign in to comment.