mirror of
https://git.isptech.ru/ISPsystem/isp-maintenance.git
synced 2025-10-30 07:03:02 +01:00
Add: auth cmd prototype, dummy_platform for api
This commit is contained in:
@@ -3,18 +3,52 @@ import json
|
||||
import urllib
|
||||
import requests
|
||||
|
||||
from mgrctl.settings.api import INPUT_HOSTNAME, INPUT_PORT, HEADERS
|
||||
from mgrctl.settings.api import INPUT_URL, HEADERS
|
||||
from mgrctl.settings.platform import (
|
||||
PLATFORM_TYPE,
|
||||
PLATFORM_VERIFY_SSL,
|
||||
PLATFORM_DUMMY,
|
||||
PLATFORM_DUMMY_VM6_API_URL,
|
||||
PLATFORM_DUMMY_VM6_EMAIL,
|
||||
PLATFORM_DUMMY_VM6_PASSWORD,
|
||||
PLATFORM_DUMMY_VM6_TOKEN,
|
||||
PLATFORM_DUMMY_DCI6_API_URL,
|
||||
PLATFORM_DUMMY_DCI6_EMAIL,
|
||||
PLATFORM_DUMMY_DCI6_PASSWORD,
|
||||
PLATFORM_DUMMY_DCI6_TOKEN
|
||||
)
|
||||
|
||||
|
||||
class BaseAPI(object):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
def __init__(self):
|
||||
"""Announces required parameters"""
|
||||
internal_api_url = f'http://{INPUT_HOSTNAME}:{INPUT_PORT}'
|
||||
self.API_URL = api_url if api_url else internal_api_url
|
||||
if PLATFORM_TYPE == 'vm':
|
||||
if PLATFORM_DUMMY:
|
||||
self.API_URL = PLATFORM_DUMMY_VM6_API_URL
|
||||
self.AUTH_TYPE = 'Public'
|
||||
self.HEADERS = {'x-xsrf-token': PLATFORM_DUMMY_VM6_TOKEN}
|
||||
self.EMAIL = PLATFORM_DUMMY_VM6_EMAIL
|
||||
self.PASSWORD = PLATFORM_DUMMY_VM6_PASSWORD
|
||||
else:
|
||||
self.API_URL = INPUT_URL
|
||||
self.AUTH_TYPE = 'Internal'
|
||||
self.HEADERS = HEADERS
|
||||
|
||||
if PLATFORM_TYPE == 'dci':
|
||||
if PLATFORM_DUMMY:
|
||||
self.API_URL = PLATFORM_DUMMY_DCI6_API_URL
|
||||
self.AUTH_TYPE = 'Public'
|
||||
self.HEADERS = {'x-xsrf-token': PLATFORM_DUMMY_DCI6_TOKEN}
|
||||
self.EMAIL = PLATFORM_DUMMY_DCI6_EMAIL
|
||||
self.PASSWORD = PLATFORM_DUMMY_DCI6_PASSWORD
|
||||
else:
|
||||
self.API_URL = INPUT_URL
|
||||
self.AUTH_TYPE = 'Internal'
|
||||
self.HEADERS = HEADERS
|
||||
|
||||
self.API_VERSION = 'v3'
|
||||
self.API_DEFINITION = 'api'
|
||||
self.HEADERS = HEADERS
|
||||
self.VERIFY_SSL = verify_ssl
|
||||
self.VERIFY_SSL = PLATFORM_VERIFY_SSL
|
||||
|
||||
def _gen_request_url(self, url):
|
||||
return f'{self.API_URL}/{self.API_DEFINITION}/{self.API_VERSION}{url}'
|
||||
@@ -61,69 +95,26 @@ class BaseAPI(object):
|
||||
print(response)
|
||||
raise sys.exit()
|
||||
|
||||
def get(self, url, headers={}, data={}):
|
||||
return self.call_api(url, headers, data, method='GET')
|
||||
|
||||
def post(self, url, headers={}, data={}):
|
||||
return self.call_api(url, headers, data, method='POST')
|
||||
|
||||
|
||||
class BaseAuthAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
"""
|
||||
Init class for /auth/v4 requests
|
||||
|
||||
Args:
|
||||
api_url (str, optional): url api host. Defaults to None.
|
||||
If None will use from settings.api.INPUT_HOSTNAME:INPUT_PORT
|
||||
|
||||
verify_ssl (bool, optional): Isn't recommended. Defaults to True.
|
||||
Use only for testing if you don't have root sign SSL cert.
|
||||
"""
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_VERSION = 'v4'
|
||||
self.API_DEFINITION = 'auth'
|
||||
|
||||
def get_auth_token(self, email: str, password: str) -> dict:
|
||||
"""
|
||||
Get auth token for authentication
|
||||
|
||||
Arg:
|
||||
email (str): user email
|
||||
password (str): user password
|
||||
|
||||
Returns:
|
||||
response (dict): {
|
||||
"confirmed": true,
|
||||
"expires_at": "date time",
|
||||
"id": "int",
|
||||
"token": "str"
|
||||
}
|
||||
"""
|
||||
def get_auth_token(self, email=None, password=None) -> dict:
|
||||
email = self.EMAIL if not email else email
|
||||
password = self.PASSWORD if not password else password
|
||||
return self.call_api(
|
||||
url='/public/token',
|
||||
method='POST',
|
||||
data={'email': email, 'password': password}
|
||||
)
|
||||
|
||||
def get_auth_key(self, token: str, user, auth_type='Internal') -> dict:
|
||||
"""
|
||||
Key authentication
|
||||
|
||||
Args:
|
||||
token (str): auth token
|
||||
user (str or int): user id or email
|
||||
auth_type (str, optional): May be Public for public auth.
|
||||
Defaults to 'Internal'.
|
||||
|
||||
Returns:
|
||||
response (dict): {
|
||||
"id": "int",
|
||||
"key": "str"
|
||||
}
|
||||
"""
|
||||
def get_auth_key(self, token=None, user=None) -> dict:
|
||||
headers = {}
|
||||
if auth_type == 'Public':
|
||||
user = self.EMAIL if not user else user
|
||||
if token:
|
||||
headers = self.make_auth_header(token)
|
||||
return self.call_api(
|
||||
url=f'/user/{user}/key',
|
||||
@@ -132,32 +123,22 @@ class BaseAuthAPI(BaseAPI):
|
||||
)
|
||||
|
||||
def make_auth_header(self, token: str) -> dict:
|
||||
"""
|
||||
Generate dict for auth header
|
||||
|
||||
Args:
|
||||
token (str): auth token
|
||||
|
||||
Returns:
|
||||
dict: {'x-xsrf-token': token} use it for request headers
|
||||
"""
|
||||
return {'x-xsrf-token': token}
|
||||
|
||||
def whoami(self, token: str) -> dict:
|
||||
def whoami(self) -> dict:
|
||||
return self.call_api(
|
||||
url='/whoami',
|
||||
method='GET',
|
||||
headers=self.make_auth_header(token)
|
||||
method='GET'
|
||||
)
|
||||
|
||||
|
||||
class BaseIpAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_DEFINITION = 'ip'
|
||||
|
||||
|
||||
class BaseDnsProxyAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_DEFINITION = 'dnsproxy'
|
||||
|
||||
@@ -6,8 +6,8 @@ class AuthAPI(BaseAuthAPI):
|
||||
|
||||
|
||||
class BackupAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_VERSION = 'v4'
|
||||
self.API_DEFINITION = 'backup'
|
||||
|
||||
@@ -17,20 +17,20 @@ class DnsProxyAPI(BaseDnsProxyAPI):
|
||||
|
||||
|
||||
class EserviceAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_DEFINITION = 'eservice'
|
||||
|
||||
|
||||
class IsoAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_DEFINITION = 'iso'
|
||||
|
||||
|
||||
class IpmiAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_DEFINITION = 'ipmiproxy'
|
||||
|
||||
|
||||
@@ -39,13 +39,13 @@ class IpAPI(BaseIpAPI):
|
||||
|
||||
|
||||
class ReportAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_VERSION = 'v4'
|
||||
self.API_DEFINITION = 'report'
|
||||
|
||||
|
||||
class UpdaterAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_DEFINITION = 'updater'
|
||||
|
||||
@@ -14,6 +14,6 @@ class IpAPI(BaseIpAPI):
|
||||
|
||||
|
||||
class VmAPI(BaseAPI):
|
||||
def __init__(self, api_url=None, verify_ssl=True):
|
||||
super().__init__(api_url, verify_ssl)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.API_DEFINITION = 'vm'
|
||||
|
||||
Reference in New Issue
Block a user