From 2254ebebe7567664322a34fcb320c01cfdf309e5 Mon Sep 17 00:00:00 2001 From: sean Date: Tue, 31 Dec 2024 16:25:42 +0000 Subject: [PATCH] manual key loading --- examples/manual-api-key.py | 26 ++++++++++ helpers/cudo_api.py | 98 +++++++++++++++++++++++++++----------- 2 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 examples/manual-api-key.py diff --git a/examples/manual-api-key.py b/examples/manual-api-key.py new file mode 100644 index 0000000..c6bd284 --- /dev/null +++ b/examples/manual-api-key.py @@ -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 = "" +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 = '' +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) diff --git a/helpers/cudo_api.py b/helpers/cudo_api.py index 8d82f87..fcd006e 100644 --- a/helpers/cudo_api.py +++ b/helpers/cudo_api.py @@ -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', "") @@ -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): @@ -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))