[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
[DEFAULT]
AUTH_NAME = "replace me"
AUTH_SEED = "replace me"
AUTH_REALM = "replace me"
NETWORK_PREFIX = moo
OTP_VERIFY_ENDPOINT = verify/
[api]
NETWORK_PREFIX = foo

View File

@ -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))

View File

@ -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)