[api] config updates and add default values

This commit is contained in:
Nico Schottelius 2019-12-08 13:09:52 +01:00
parent a4bedb01f6
commit cdbfb96e71
3 changed files with 21 additions and 16 deletions

View file

@ -1,8 +1,14 @@
# This section contains default values for all other sections # This section contains default values for all other sections
[DEFAULT] [DEFAULT]
AUTH_NAME = "replace me"
AUTH_SEED = "replace me"
AUTH_REALM = "replace me"
NETWORK_PREFIX = moo NETWORK_PREFIX = moo
OTP_VERIFY_ENDPOINT = verify/
[api] [api]
NETWORK_PREFIX = foo NETWORK_PREFIX = foo

View file

@ -1,7 +1,6 @@
import os import os
from ucloud.config import etcd_client, env_vars from ucloud.config import etcd_client, config
class Optional: class Optional:
pass pass
@ -48,6 +47,6 @@ class VmUUIDField(Field):
self.validation = self.vm_uuid_validation self.validation = self.vm_uuid_validation
def vm_uuid_validation(self): 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: if not r:
self.add_error("VM with uuid {} does not exists".format(self.uuid)) self.add_error("VM with uuid {} does not exists".format(self.uuid))

View file

@ -7,15 +7,15 @@ import requests
from pyotp import TOTP 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): def check_otp(name, realm, token):
try: try:
data = { data = {
"auth_name": env_vars.get("AUTH_NAME"), "auth_name": config['api']["AUTH_NAME"],
"auth_token": TOTP(env_vars.get("AUTH_SEED")).now(), "auth_token": TOTP(config['api']["AUTH_SEED"]).now(),
"auth_realm": env_vars.get("AUTH_REALM"), "auth_realm": config['api']["AUTH_REALM"],
"name": name, "name": name,
"realm": realm, "realm": realm,
"token": token, "token": token,
@ -25,8 +25,8 @@ def check_otp(name, realm, token):
response = requests.post( response = requests.post(
"{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format( "{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
OTP_SERVER=env_vars.get("OTP_SERVER", ""), OTP_SERVER=config['api']["OTP_SERVER"],
OTP_VERIFY_ENDPOINT=env_vars.get("OTP_VERIFY_ENDPOINT", "verify/"), OTP_VERIFY_ENDPOINT=config['api']["OTP_VERIFY_ENDPOINT"]
), ),
json=data, json=data,
) )
@ -35,7 +35,7 @@ def check_otp(name, realm, token):
def resolve_vm_name(name, owner): def resolve_vm_name(name, owner):
"""Return UUID of Virtual Machine of name == name and owner == owner """Return UUID of Virtual Machine of name == name and owner == owner
Input: name of vm, owner of vm. Input: name of vm, owner of vm.
Output: uuid of vm if found otherwise None 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): def resolve_image_name(name, etcd_client):
"""Return image uuid given its name and its store """Return image uuid given its name and its store
* If the provided name is not in correct format * If the provided name is not in correct format
i.e {store_name}:{image_name} return ValueError i.e {store_name}:{image_name} return ValueError
* If no such image found then return KeyError * 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 Examples, where it would work and where it would raise exception
"images:alpine" --> ["images", "alpine"] "images:alpine" --> ["images", "alpine"]
"images" --> ["images"] it would raise Exception as non enough value to unpack "images" --> ["images"] it would raise Exception as non enough value to unpack
"images:alpine:meow" --> ["images", "alpine", "meow"] it would raise Exception "images:alpine:meow" --> ["images", "alpine", "meow"] it would raise Exception
as too many values to unpack as too many values to unpack
""" """
@ -80,7 +80,7 @@ def resolve_image_name(name, etcd_client):
except Exception: except Exception:
raise ValueError("Image name not in correct format i.e {store_name}:{image_name}") 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 to find image with name == image_name and store_name == store_name
try: try:
@ -119,14 +119,14 @@ def generate_mac(uaa=False, multicast=False, oui=None, separator=':', byte_fmt='
def get_ip_addr(mac_address, device): def get_ip_addr(mac_address, device):
"""Return IP address of a device provided its mac address / link local address """Return IP address of a device provided its mac address / link local address
and the device with which it is connected. 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") For Example, if we call get_ip_addr(mac_address="52:54:00:12:34:56", device="br0")
the following two scenarios can happen 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 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 to the arg:mac_address or the mentioned arg:device does not exists or the ip address
we found is local. we found is local.
2. It would return ip_address of device whose mac_address is equal to arg:mac_address 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: try:
output = sp.check_output(['ip', '-6', 'neigh', 'show', 'dev', device], stderr=sp.PIPE) output = sp.check_output(['ip', '-6', 'neigh', 'show', 'dev', device], stderr=sp.PIPE)