ucloud-api/helper.py

46 lines
1.1 KiB
Python
Raw Normal View History

2019-06-24 10:46:06 +00:00
import binascii
import requests
from decouple import config
from pyotp import TOTP
from config import VM_POOL
2019-06-24 10:46:06 +00:00
def check_otp(name, realm, token):
2019-06-24 10:46:06 +00:00
try:
data = {
"auth_name": config("AUTH_NAME", ""),
"auth_token": TOTP(config("AUTH_SEED", "")).now(),
"auth_realm": config("AUTH_REALM", ""),
2019-06-24 10:46:06 +00:00
"name": name,
"realm": realm,
"token": token,
2019-06-24 10:46:06 +00:00
}
except binascii.Error:
return 400
2019-10-15 15:27:10 +00:00
response = requests.get(
2019-06-24 10:46:06 +00:00
"{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
OTP_SERVER=config("OTP_SERVER", ""),
2019-10-15 15:27:10 +00:00
OTP_VERIFY_ENDPOINT=config("OTP_VERIFY_ENDPOINT", "verify"),
2019-06-24 10:46:06 +00:00
),
2019-10-15 15:27:10 +00:00
json=data,
2019-06-24 10:46:06 +00:00
)
return response.status_code
def resolve_vm_name(name, owner):
"""
Input: name of vm, owner of vm
Output: uuid of vm if found otherwise None
"""
result = next(
filter(
lambda vm: vm.value["owner"] == owner and vm.value["name"] == name,
VM_POOL.vms,
),
None,
)
if result:
return result.key.split("/")[-1]