From cffa40c0e36f10702ade513b9f4cb379c7584295 Mon Sep 17 00:00:00 2001 From: 3wc <3wc.cyberia@doesthisthing.work> Date: Fri, 23 Jul 2021 23:28:06 +0200 Subject: [PATCH] Fix up API --- capsulflask/console.py | 2 +- capsulflask/hub_model.py | 1 + capsulflask/publicapi.py | 11 ++++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/capsulflask/console.py b/capsulflask/console.py index f6caabc..5da5cc8 100644 --- a/capsulflask/console.py +++ b/capsulflask/console.py @@ -248,7 +248,7 @@ def _create(vm_sizes, operating_systems, public_keys_for_account, affordable_vm_ template_image_file_name=operating_systems[os]['template_image_file_name'], vcpus=vm_sizes[size]['vcpus'], memory_mb=vm_sizes[size]['memory_mb'], - ssh_authorized_keys=list(map(lambda x: x["content"], posted_keys)) + ssh_authorized_keys=posted_keys ) return id, errors diff --git a/capsulflask/hub_model.py b/capsulflask/hub_model.py index 375bb9f..9306c4b 100644 --- a/capsulflask/hub_model.py +++ b/capsulflask/hub_model.py @@ -44,6 +44,7 @@ class MockHub(VirtualizationInterface): validate_capsul_id(id) current_app.logger.info(f"mock create: {id} for {email}") sleep(1) + get_model().create_vm( email=email, id=id, diff --git a/capsulflask/publicapi.py b/capsulflask/publicapi.py index 90c12ec..3e8598d 100644 --- a/capsulflask/publicapi.py +++ b/capsulflask/publicapi.py @@ -17,7 +17,7 @@ bp = Blueprint("publicapi", __name__, url_prefix="/api") def capsul_create(): email = session["account"] - from .console import _create,get_account_balance, get_payments, get_vms + from .console import _create, get_account_balance, get_payments, get_vms vm_sizes = get_model().vm_sizes_dict() operating_systems = get_model().operating_systems_dict() @@ -25,12 +25,21 @@ def capsul_create(): account_balance = get_account_balance(get_vms(), get_payments(), datetime.datetime.utcnow()) capacity_avaliable = current_app.config["HUB_MODEL"].capacity_avaliable(512*1024*1024) + affordable_vm_sizes = dict() + for key, vm_size in vm_sizes.items(): + # if a user deposits $7.50 and then creates an f1-s vm which costs 7.50 a month, + # then they have to delete the vm and re-create it, they will not be able to, they will have to pay again. + # so for UX it makes a lot of sense to give a small margin of 25 cents for usability sake + if vm_size["dollars_per_month"] <= account_balance+0.25: + affordable_vm_sizes[key] = vm_size + request.json['ssh_authorized_key_count'] = 1 id, errors = _create( vm_sizes, operating_systems, public_keys_for_account, + affordable_vm_sizes, request.json) if id is not None: