2019-10-29 09:47:50 +00:00
|
|
|
import urllib.parse
|
2021-07-02 11:08:06 +00:00
|
|
|
|
|
|
|
import requests
|
2019-10-29 09:47:50 +00:00
|
|
|
from django.conf import settings
|
2019-12-17 09:25:10 +00:00
|
|
|
from django.http import Http404
|
2019-10-29 09:47:50 +00:00
|
|
|
from django.urls.exceptions import NoReverseMatch
|
2019-12-17 09:25:10 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-10-29 09:47:50 +00:00
|
|
|
|
2021-09-27 10:40:52 +00:00
|
|
|
from .models import Address, DatabaseService, Domain, Mailbox, SaasService, UserAccount, WebSite
|
2019-12-10 12:04:04 +00:00
|
|
|
|
2019-10-29 09:47:50 +00:00
|
|
|
DOMAINS_PATH = 'domains/'
|
|
|
|
TOKEN_PATH = '/api-token-auth/'
|
|
|
|
|
|
|
|
API_PATHS = {
|
|
|
|
# auth
|
|
|
|
'token-auth': '/api-token-auth/',
|
2019-10-30 13:00:12 +00:00
|
|
|
'my-account': 'accounts/',
|
2019-10-29 09:47:50 +00:00
|
|
|
|
|
|
|
# services
|
2019-11-13 11:27:25 +00:00
|
|
|
'database-list': 'databases/',
|
2019-10-29 09:47:50 +00:00
|
|
|
'domain-list': 'domains/',
|
2019-12-13 14:08:01 +00:00
|
|
|
'domain-detail': 'domains/{pk}/',
|
2019-11-13 10:08:19 +00:00
|
|
|
'address-list': 'addresses/',
|
2021-06-24 11:08:16 +00:00
|
|
|
'address-detail': 'addresses/{pk}/',
|
2019-11-13 10:08:19 +00:00
|
|
|
'mailbox-list': 'mailboxes/',
|
2021-10-06 09:07:22 +00:00
|
|
|
'mailbox-detail': 'mailboxes/{pk}/',
|
2021-10-14 09:09:59 +00:00
|
|
|
'mailbox-password': 'mailboxes/{pk}/set_password/',
|
2019-10-31 13:08:49 +00:00
|
|
|
'mailinglist-list': 'lists/',
|
2019-12-06 09:27:18 +00:00
|
|
|
'saas-list': 'saas/',
|
2020-01-20 09:45:18 +00:00
|
|
|
'website-list': 'websites/',
|
2019-11-20 19:07:35 +00:00
|
|
|
|
|
|
|
# other
|
2019-12-17 13:48:21 +00:00
|
|
|
'bill-list': 'bills/',
|
2019-12-17 14:15:58 +00:00
|
|
|
'bill-document': 'bills/{pk}/document/',
|
2019-11-20 19:07:35 +00:00
|
|
|
'payment-source-list': 'payment-sources/',
|
2019-10-29 09:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-30 12:05:46 +00:00
|
|
|
class Orchestra(object):
|
|
|
|
def __init__(self, *args, username=None, password=None, **kwargs):
|
|
|
|
self.base_url = kwargs.pop('base_url', settings.API_BASE_URL)
|
|
|
|
self.username = username
|
|
|
|
self.session = requests.Session()
|
|
|
|
self.auth_token = kwargs.pop("auth_token", None)
|
2019-10-29 09:47:50 +00:00
|
|
|
|
2019-10-30 12:05:46 +00:00
|
|
|
if self.auth_token is None:
|
|
|
|
self.auth_token = self.authenticate(self.username, password)
|
|
|
|
|
|
|
|
def build_absolute_uri(self, path_name):
|
|
|
|
path = API_PATHS.get(path_name, None)
|
|
|
|
if path is None:
|
|
|
|
raise NoReverseMatch(
|
|
|
|
"Not found API path name '{}'".format(path_name))
|
|
|
|
|
|
|
|
return urllib.parse.urljoin(self.base_url, path)
|
|
|
|
|
|
|
|
def authenticate(self, username, password):
|
|
|
|
url = self.build_absolute_uri('token-auth')
|
|
|
|
response = self.session.post(
|
|
|
|
url,
|
|
|
|
data={"username": username, "password": password},
|
|
|
|
)
|
|
|
|
|
|
|
|
return response.json().get("token", None)
|
|
|
|
|
2021-06-23 11:47:27 +00:00
|
|
|
def request(self, verb, resource=None, url=None, data=None, render_as="json", querystring=None, raise_exception=True):
|
2019-10-30 12:05:46 +00:00
|
|
|
assert verb in ["HEAD", "GET", "POST", "PATCH", "PUT", "DELETE"]
|
2019-12-13 14:08:01 +00:00
|
|
|
if resource is not None:
|
|
|
|
url = self.build_absolute_uri(resource)
|
|
|
|
elif url is None:
|
|
|
|
raise AttributeError("Provide `resource` or `url` params")
|
|
|
|
|
2019-12-12 13:18:29 +00:00
|
|
|
if querystring is not None:
|
|
|
|
url = "{}?{}".format(url, querystring)
|
2019-10-30 12:05:46 +00:00
|
|
|
|
|
|
|
verb = getattr(self.session, verb.lower())
|
2021-06-23 11:47:27 +00:00
|
|
|
headers = {
|
|
|
|
"Authorization": "Token {}".format(self.auth_token),
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
}
|
|
|
|
response = verb(url, json=data, headers=headers, allow_redirects=False)
|
2019-10-30 12:05:46 +00:00
|
|
|
|
2019-10-30 13:00:12 +00:00
|
|
|
if raise_exception:
|
|
|
|
response.raise_for_status()
|
2019-10-30 12:05:46 +00:00
|
|
|
|
|
|
|
status = response.status_code
|
2021-10-05 11:10:53 +00:00
|
|
|
if status < 500 and render_as == "json":
|
2019-12-17 14:15:58 +00:00
|
|
|
output = response.json()
|
|
|
|
else:
|
|
|
|
output = response.content
|
2019-10-30 12:05:46 +00:00
|
|
|
|
|
|
|
return status, output
|
|
|
|
|
2019-12-12 13:18:29 +00:00
|
|
|
def retrieve_service_list(self, service_name, querystring=None):
|
2019-10-31 13:08:49 +00:00
|
|
|
pattern_name = '{}-list'.format(service_name)
|
|
|
|
if pattern_name not in API_PATHS:
|
|
|
|
raise ValueError("Unknown service {}".format(service_name))
|
2019-12-12 13:18:29 +00:00
|
|
|
_, output = self.request("GET", pattern_name, querystring=querystring)
|
2019-10-30 12:05:46 +00:00
|
|
|
return output
|
2019-10-30 13:00:12 +00:00
|
|
|
|
2019-12-10 12:04:04 +00:00
|
|
|
def retrieve_profile(self):
|
|
|
|
status, output = self.request("GET", 'my-account')
|
|
|
|
if status >= 400:
|
|
|
|
raise PermissionError("Cannot retrieve profile of an anonymous user.")
|
|
|
|
return UserAccount.new_from_json(output[0])
|
|
|
|
|
2019-12-17 14:15:58 +00:00
|
|
|
def retrieve_bill_document(self, pk):
|
|
|
|
path = API_PATHS.get('bill-document').format_map({'pk': pk})
|
|
|
|
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
status, bill_pdf = self.request("GET", render_as="html", url=url, raise_exception=False)
|
|
|
|
if status == 404:
|
|
|
|
raise Http404(_("No domain found matching the query"))
|
|
|
|
return bill_pdf
|
|
|
|
|
2021-06-23 11:47:27 +00:00
|
|
|
def create_mail_address(self, data):
|
2021-07-02 11:08:06 +00:00
|
|
|
resource = '{}-list'.format(Address.api_name)
|
2021-06-24 11:08:16 +00:00
|
|
|
return self.request("POST", resource=resource, data=data)
|
|
|
|
|
|
|
|
def retrieve_mail_address(self, pk):
|
|
|
|
path = API_PATHS.get('address-detail').format_map({'pk': pk})
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
status, data = self.request("GET", url=url, raise_exception=False)
|
|
|
|
if status == 404:
|
|
|
|
raise Http404(_("No object found matching the query"))
|
2021-06-23 11:47:27 +00:00
|
|
|
|
2021-07-02 11:08:06 +00:00
|
|
|
return Address.new_from_json(data)
|
2021-06-23 11:47:27 +00:00
|
|
|
|
2021-06-24 11:08:16 +00:00
|
|
|
def update_mail_address(self, pk, data):
|
|
|
|
path = API_PATHS.get('address-detail').format_map({'pk': pk})
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
return self.request("PUT", url=url, data=data)
|
2021-06-23 11:47:27 +00:00
|
|
|
|
2020-02-17 10:07:21 +00:00
|
|
|
def retrieve_mail_address_list(self, querystring=None):
|
|
|
|
# retrieve mails applying filters (if any)
|
|
|
|
raw_data = self.retrieve_service_list(
|
2021-07-02 11:08:06 +00:00
|
|
|
Address.api_name,
|
2020-02-17 10:07:21 +00:00
|
|
|
querystring=querystring,
|
|
|
|
)
|
|
|
|
|
2021-10-05 08:04:41 +00:00
|
|
|
addresses = [Address.new_from_json(data) for data in raw_data]
|
2020-02-17 10:07:21 +00:00
|
|
|
|
2020-02-17 10:29:58 +00:00
|
|
|
# PATCH to include Pangea addresses not shown by orchestra
|
|
|
|
# described on issue #4
|
2021-07-02 10:57:55 +00:00
|
|
|
# TODO(@slamora) disabled hacky patch because breaks another funtionalities
|
|
|
|
# XXX Fix it on orchestra instead of here???
|
|
|
|
# raw_mailboxes = self.retrieve_mailbox_list()
|
|
|
|
# for mailbox in raw_mailboxes:
|
|
|
|
# if mailbox['addresses'] == []:
|
|
|
|
# address_data = {
|
|
|
|
# 'names': [mailbox['name']],
|
|
|
|
# 'forward': '',
|
|
|
|
# 'domain': {
|
|
|
|
# 'name': 'pangea.org.',
|
|
|
|
# },
|
|
|
|
# 'mailboxes': [mailbox],
|
|
|
|
# }
|
2021-07-02 11:08:06 +00:00
|
|
|
# pangea_address = Address.new_from_json(address_data)
|
2021-07-02 10:57:55 +00:00
|
|
|
# addresses.append(pangea_address)
|
2020-02-17 10:29:58 +00:00
|
|
|
|
2020-02-17 10:07:21 +00:00
|
|
|
return addresses
|
|
|
|
|
2021-10-01 11:36:52 +00:00
|
|
|
def delete_mail_address(self, pk):
|
|
|
|
path = API_PATHS.get('address-detail').format_map({'pk': pk})
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
return self.request("DELETE", url=url, render_as=None)
|
|
|
|
|
2021-10-05 11:10:53 +00:00
|
|
|
def create_mailbox(self, data):
|
|
|
|
resource = '{}-list'.format(Mailbox.api_name)
|
|
|
|
return self.request("POST", resource=resource, data=data, raise_exception=False)
|
|
|
|
|
2021-10-06 09:07:22 +00:00
|
|
|
def retrieve_mailbox(self, pk):
|
|
|
|
path = API_PATHS.get('mailbox-detail').format_map({'pk': pk})
|
|
|
|
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
status, data_json = self.request("GET", url=url, raise_exception=False)
|
|
|
|
if status == 404:
|
|
|
|
raise Http404(_("No mailbox found matching the query"))
|
|
|
|
return Mailbox.new_from_json(data_json)
|
|
|
|
|
2021-10-07 11:51:31 +00:00
|
|
|
def update_mailbox(self, pk, data):
|
|
|
|
path = API_PATHS.get('mailbox-detail').format_map({'pk': pk})
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
status, response = self.request("PATCH", url=url, data=data, raise_exception=False)
|
|
|
|
return status, response
|
|
|
|
|
2021-06-23 11:47:27 +00:00
|
|
|
def retrieve_mailbox_list(self):
|
2021-09-27 10:40:52 +00:00
|
|
|
mailboxes = self.retrieve_service_list(Mailbox.api_name)
|
|
|
|
return [Mailbox.new_from_json(mailbox_data) for mailbox_data in mailboxes]
|
2021-06-23 11:47:27 +00:00
|
|
|
|
2021-10-06 09:07:22 +00:00
|
|
|
def delete_mailbox(self, pk):
|
|
|
|
path = API_PATHS.get('mailbox-detail').format_map({'pk': pk})
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
# Mark as inactive instead of deleting
|
|
|
|
# return self.request("DELETE", url=url, render_as=None)
|
|
|
|
return self.request("PATCH", url=url, data={"is_active": False})
|
|
|
|
|
2021-10-14 09:09:59 +00:00
|
|
|
def set_password_mailbox(self, pk, data):
|
|
|
|
path = API_PATHS.get('mailbox-password').format_map({'pk': pk})
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
|
|
|
status, response = self.request("POST", url=url, data=data, raise_exception=False)
|
|
|
|
return status, response
|
|
|
|
|
|
|
|
|
2019-12-13 14:08:01 +00:00
|
|
|
def retrieve_domain(self, pk):
|
|
|
|
path = API_PATHS.get('domain-detail').format_map({'pk': pk})
|
|
|
|
|
|
|
|
url = urllib.parse.urljoin(self.base_url, path)
|
2019-12-17 09:25:10 +00:00
|
|
|
status, domain_json = self.request("GET", url=url, raise_exception=False)
|
|
|
|
if status == 404:
|
|
|
|
raise Http404(_("No domain found matching the query"))
|
2019-12-13 14:08:01 +00:00
|
|
|
return Domain.new_from_json(domain_json)
|
|
|
|
|
2019-12-12 13:18:29 +00:00
|
|
|
def retrieve_domain_list(self):
|
|
|
|
output = self.retrieve_service_list(Domain.api_name)
|
2020-01-20 09:45:18 +00:00
|
|
|
websites = self.retrieve_website_list()
|
|
|
|
|
2019-12-12 13:18:29 +00:00
|
|
|
domains = []
|
|
|
|
for domain_json in output:
|
|
|
|
# filter querystring
|
|
|
|
querystring = "domain={}".format(domain_json['id'])
|
|
|
|
|
|
|
|
# retrieve services associated to a domain
|
2021-10-14 10:56:50 +00:00
|
|
|
domain_json['addresses'] = self.retrieve_service_list(
|
2021-07-02 11:08:06 +00:00
|
|
|
Address.api_name, querystring)
|
2020-01-20 09:45:18 +00:00
|
|
|
|
|
|
|
# retrieve websites (as they cannot be filtered by domain on the API we should do it here)
|
|
|
|
domain_json['websites'] = self.filter_websites_by_domain(websites, domain_json['id'])
|
|
|
|
|
2019-12-12 13:18:29 +00:00
|
|
|
# TODO(@slamora): update when backend provides resource disk usage data
|
|
|
|
domain_json['usage'] = {
|
2020-02-17 11:41:30 +00:00
|
|
|
# 'usage': 300,
|
|
|
|
# 'total': 650,
|
|
|
|
# 'unit': 'MB',
|
|
|
|
# 'percent': 50,
|
2019-12-12 13:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# append to list a Domain object
|
|
|
|
domains.append(Domain.new_from_json(domain_json))
|
|
|
|
|
|
|
|
return domains
|
2019-10-30 13:00:12 +00:00
|
|
|
|
2020-01-20 09:45:18 +00:00
|
|
|
def retrieve_website_list(self):
|
|
|
|
output = self.retrieve_service_list(WebSite.api_name)
|
|
|
|
return [WebSite.new_from_json(website_data) for website_data in output]
|
|
|
|
|
|
|
|
def filter_websites_by_domain(self, websites, domain_id):
|
|
|
|
matching = []
|
|
|
|
for website in websites:
|
|
|
|
web_domains = [web_domain.id for web_domain in website.domains]
|
|
|
|
if domain_id in web_domains:
|
|
|
|
matching.append(website)
|
|
|
|
|
|
|
|
return matching
|
|
|
|
|
2019-10-30 13:00:12 +00:00
|
|
|
def verify_credentials(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
A user profile info if the
|
|
|
|
credentials are valid, None otherwise.
|
|
|
|
"""
|
|
|
|
status, output = self.request("GET", 'my-account', raise_exception=False)
|
|
|
|
|
|
|
|
if status < 400:
|
|
|
|
return output
|
|
|
|
|
|
|
|
return None
|