From cdbfb96e712f51970729bdd999aeb531ba072336 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 8 Dec 2019 13:09:52 +0100 Subject: [PATCH] [api] config updates and add default values --- conf/ucloud.conf | 6 ++++++ ucloud/api/common_fields.py | 5 ++--- ucloud/api/helper.py | 26 +++++++++++++------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/conf/ucloud.conf b/conf/ucloud.conf index 7f83d80..0ebb705 100644 --- a/conf/ucloud.conf +++ b/conf/ucloud.conf @@ -1,8 +1,14 @@ # This section contains default values for all other sections [DEFAULT] +AUTH_NAME = "replace me" +AUTH_SEED = "replace me" +AUTH_REALM = "replace me" + NETWORK_PREFIX = moo +OTP_VERIFY_ENDPOINT = verify/ + [api] NETWORK_PREFIX = foo diff --git a/ucloud/api/common_fields.py b/ucloud/api/common_fields.py index e9903ac..1ceb1b0 100755 --- a/ucloud/api/common_fields.py +++ b/ucloud/api/common_fields.py @@ -1,7 +1,6 @@ import os -from ucloud.config import etcd_client, env_vars - +from ucloud.config import etcd_client, config class Optional: pass @@ -48,6 +47,6 @@ class VmUUIDField(Field): self.validation = self.vm_uuid_validation def vm_uuid_validation(self): - r = etcd_client.get(os.path.join(env_vars.get('VM_PREFIX'), self.uuid)) + r = etcd_client.get(os.path.join(config['api']['VM_PREFIX'], self.uuid)) if not r: self.add_error("VM with uuid {} does not exists".format(self.uuid)) diff --git a/ucloud/api/helper.py b/ucloud/api/helper.py index 63d2f90..6735f05 100755 --- a/ucloud/api/helper.py +++ b/ucloud/api/helper.py @@ -7,15 +7,15 @@ import requests from pyotp import TOTP -from ucloud.config import vm_pool, env_vars +from ucloud.config import vm_pool, config def check_otp(name, realm, token): try: data = { - "auth_name": env_vars.get("AUTH_NAME"), - "auth_token": TOTP(env_vars.get("AUTH_SEED")).now(), - "auth_realm": env_vars.get("AUTH_REALM"), + "auth_name": config['api']["AUTH_NAME"], + "auth_token": TOTP(config['api']["AUTH_SEED"]).now(), + "auth_realm": config['api']["AUTH_REALM"], "name": name, "realm": realm, "token": token, @@ -25,8 +25,8 @@ def check_otp(name, realm, token): response = requests.post( "{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format( - OTP_SERVER=env_vars.get("OTP_SERVER", ""), - OTP_VERIFY_ENDPOINT=env_vars.get("OTP_VERIFY_ENDPOINT", "verify/"), + OTP_SERVER=config['api']["OTP_SERVER"], + OTP_VERIFY_ENDPOINT=config['api']["OTP_VERIFY_ENDPOINT"] ), json=data, ) @@ -35,7 +35,7 @@ def check_otp(name, realm, token): def resolve_vm_name(name, owner): """Return UUID of Virtual Machine of name == name and owner == owner - + Input: name of vm, owner of vm. Output: uuid of vm if found otherwise None """ @@ -54,7 +54,7 @@ def resolve_vm_name(name, owner): def resolve_image_name(name, etcd_client): """Return image uuid given its name and its store - + * If the provided name is not in correct format i.e {store_name}:{image_name} return ValueError * If no such image found then return KeyError @@ -70,9 +70,9 @@ def resolve_image_name(name, etcd_client): """ Examples, where it would work and where it would raise exception "images:alpine" --> ["images", "alpine"] - + "images" --> ["images"] it would raise Exception as non enough value to unpack - + "images:alpine:meow" --> ["images", "alpine", "meow"] it would raise Exception as too many values to unpack """ @@ -80,7 +80,7 @@ def resolve_image_name(name, etcd_client): except Exception: raise ValueError("Image name not in correct format i.e {store_name}:{image_name}") - images = etcd_client.get_prefix(env_vars.get('IMAGE_PREFIX'), value_in_json=True) + images = etcd_client.get_prefix(config['api']['IMAGE_PREFIX'], value_in_json=True) # Try to find image with name == image_name and store_name == store_name try: @@ -119,14 +119,14 @@ def generate_mac(uaa=False, multicast=False, oui=None, separator=':', byte_fmt=' def get_ip_addr(mac_address, device): """Return IP address of a device provided its mac address / link local address and the device with which it is connected. - + For Example, if we call get_ip_addr(mac_address="52:54:00:12:34:56", device="br0") the following two scenarios can happen 1. It would return None if we can't be able to find device whose mac_address is equal to the arg:mac_address or the mentioned arg:device does not exists or the ip address we found is local. 2. It would return ip_address of device whose mac_address is equal to arg:mac_address - and is connected/neighbor of arg:device + and is connected/neighbor of arg:device """ try: output = sp.check_output(['ip', '-6', 'neigh', 'show', 'dev', device], stderr=sp.PIPE)