Skip to content

Commit

Permalink
client helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
JungleCatSW committed Nov 16, 2023
1 parent 154a726 commit 3f3e158
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 277 deletions.
276 changes: 4 additions & 272 deletions README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ cd swagger-codegen
-l python -o /gen/out -DpackageName=src.cudo_compute
cd ..
cp -r swagger-codegen/out/src src
rm -rf swagger-codegen

cp helpers/* src/cudo_compute
echo "import src.cudo_compute.auth_config as AuthConfig" >> src/cudo_compute/__init__.py
echo "import src.cudo_compute.cudo_client as CudoClient" >> src/cudo_compute/__init__.py
29 changes: 29 additions & 0 deletions exp/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import cudo_compute as client
def get_client():
configuration = client.Configuration()
# key, err = config.get_api_key()

# if err != None:
# return None, err

configuration.api_key['Authorization'] = "derp"
# configuration.debug = True
configuration.api_key_prefix['Authorization'] = 'Bearer'
configuration.host = "https://rest.compute.cudo.org"

sclient = client.ApiClient(configuration)
vms_api_client = client.VirtualMachinesApi(sclient)
return vms_api_client, None

def machine_types(gpu_model, mem_gib, vcpu_count, gpu_count):
try:
c, e = get_client()
types = c.list_vm_machine_types(mem_gib, vcpu_count, gpu=gpu_count, gpu_model=gpu_model)
types_dict = types.to_dict()
return types_dict
except Exception as e: # TODO what to do with errors ?
raise e


t = machine_types("",4,4,0)
print(t)
File renamed without changes.
9 changes: 5 additions & 4 deletions helpers/cudo_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import src.cudo_compute as cudo
def get_client():
configuration = client.Configuration()
key, err = config.get_api_key()
configuration = cudo.Configuration()
key, err = cudo.config.get_api_key()

if err != None:
return None, err
Expand All @@ -10,6 +11,6 @@ def get_client():
configuration.api_key_prefix['Authorization'] = 'Bearer'
configuration.host = "https://rest.compute.cudo.org"

sclient = client.ApiClient(configuration)
vms_api_client = client.VirtualMachinesApi(sclient)
sclient = cudo.ApiClient(configuration)
vms_api_client = cudo.VirtualMachinesApi(sclient)
return vms_api_client, None
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "cudo-compute"
version = "0.0.3"
version = "0.0.4"
authors = [
{ name = "Cudo Ventures", email = "[email protected]" },
]
Expand All @@ -20,6 +20,7 @@ classifiers = [
dependencies = [
"six>=1.0.0",
"urllib3>=1.10.0",
"certifi>=2023.0.0",
]

[project.urls]
Expand Down
2 changes: 2 additions & 0 deletions src/cudo_compute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@
from src.cudo_compute.models.vm_monitoring_item import VMMonitoringItem
from src.cudo_compute.models.vmnic import VMNIC
from src.cudo_compute.models.v_router_size import VRouterSize
import src.cudo_compute.auth_config as AuthConfig
import src.cudo_compute.cudo_client as CudoClient
59 changes: 59 additions & 0 deletions src/cudo_compute/auth_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import yaml
import os

home = os.path.expanduser("~")


def load_config(path, context_name):
key_config = None
context_config = None
error = None

with open(path, 'r') as file:
config_data = yaml.safe_load(file)

if config_data['configVersion'] != "v0":
error = Exception("Only config version v0 is supported")
return key_config, context_config, error

if not context_name:
if 'current-context' in config_data:
context_name = config_data['current-context']
else:
return key_config, context_config, Exception("No current context selected")

if 'contexts' in config_data:
for context_data in config_data['contexts']:
if context_data['name'] == context_name:
context_config = context_data
break

if not context_config:
error = Exception("Context not found")
return key_config, context_config, error

if 'keys' in config_data:
for key_data in config_data['keys']:
if key_data['name'] == context_config['key']:
key_config = key_data

if not key_config:
error = Exception("Key not found")

return key_config, context_config, error


def get_api_key():
key_config, context_config, error = load_config(home + '/.config/cudo/cudo.yml', "")
if not error:
return key_config['key'], None
else:
return None, error


def get_project():
key_config, context_config, error = load_config(home + '/.config/cudo/cudo.yml', "")
if not error:
return context_config['project'], None
else:
return None, error
16 changes: 16 additions & 0 deletions src/cudo_compute/cudo_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import src.cudo_compute as cudo
def get_client():
configuration = cudo.Configuration()
key, err = cudo.AuthConfig.get_api_key()

if err != None:
return None, err

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

sclient = cudo.ApiClient(configuration)
vms_api_client = cudo.VirtualMachinesApi(sclient)
return vms_api_client, None
File renamed without changes.

0 comments on commit 3f3e158

Please sign in to comment.