lemur/lemur/dns_providers/service.py

135 lines
3.6 KiB
Python
Raw Normal View History

2018-04-27 20:18:41 +02:00
import json
2018-04-25 20:19:34 +02:00
from flask import current_app
2018-06-30 00:24:31 +02:00
from lemur import database
from lemur.dns_providers.models import DnsProvider
2018-04-11 01:04:07 +02:00
def render(args):
2018-04-11 01:04:07 +02:00
"""
Helper that helps us render the REST Api responses.
:param args:
:return:
"""
query = database.session_query(DnsProvider)
return database.sort_and_page(query, DnsProvider, args)
def get(dns_provider_id):
provider = database.get(DnsProvider, dns_provider_id)
2018-05-08 00:14:32 +02:00
return provider
2018-04-27 20:18:41 +02:00
def get_all_dns_providers():
"""
Retrieves all dns providers within Lemur.
:return:
"""
return DnsProvider.query.all()
2018-04-27 20:18:41 +02:00
def get_friendly(dns_provider_id):
"""
Retrieves a dns provider by its lemur assigned ID.
2018-04-11 01:04:07 +02:00
:param dns_provider_id: Lemur assigned ID
2021-02-19 02:23:02 +01:00
:rtype: DnsProvider
2018-04-11 01:04:07 +02:00
:return:
"""
2018-04-27 20:18:41 +02:00
dns_provider = get(dns_provider_id)
dns_provider_friendly = {
"name": dns_provider.name,
"description": dns_provider.description,
2018-05-08 00:14:32 +02:00
"providerType": dns_provider.provider_type,
2018-04-27 20:18:41 +02:00
"options": dns_provider.options,
2018-05-08 00:14:32 +02:00
"credentials": dns_provider.credentials,
2018-04-27 20:18:41 +02:00
}
if dns_provider.provider_type == "route53":
2019-05-16 16:57:02 +02:00
dns_provider_friendly["account_id"] = json.loads(dns_provider.credentials).get(
"account_id"
)
2018-04-27 20:18:41 +02:00
return dns_provider_friendly
def delete(dns_provider_id):
"""
Deletes a DNS provider.
:param dns_provider_id: Lemur assigned ID
"""
database.delete(get(dns_provider_id))
2018-04-25 20:19:34 +02:00
def get_types():
provider_config = current_app.config.get(
2019-05-16 16:57:02 +02:00
"ACME_DNS_PROVIDER_TYPES",
{
"items": [
{
"name": "route53",
"requirements": [
{
"name": "account_id",
"type": "int",
"required": True,
"helpMessage": "AWS Account number",
}
],
},
{
"name": "cloudflare",
"requirements": [
{
"name": "email",
"type": "str",
"required": True,
"helpMessage": "Cloudflare Email",
},
{
"name": "key",
"type": "str",
"required": True,
"helpMessage": "Cloudflare Key",
},
],
},
{"name": "dyn"},
{"name": "ultradns"},
{"name": "powerdns"},
2019-05-16 16:57:02 +02:00
]
},
)
2018-04-25 20:19:34 +02:00
if not provider_config:
raise Exception("No DNS Provider configuration specified.")
2018-04-27 20:18:41 +02:00
provider_config["total"] = len(provider_config.get("items"))
2018-04-25 20:19:34 +02:00
return provider_config
2018-04-27 20:18:41 +02:00
def set_domains(dns_provider, domains):
"""
Increments pending certificate attempt counter and updates it in the database.
"""
dns_provider.domains = domains
database.update(dns_provider)
return dns_provider
2018-04-27 20:18:41 +02:00
def create(data):
provider_name = data.get("name")
credentials = {}
for item in data.get("provider_type", {}).get("requirements", []):
credentials[item["name"]] = item["value"]
dns_provider = DnsProvider(
2018-04-27 20:18:41 +02:00
name=provider_name,
description=data.get("description"),
provider_type=data.get("provider_type").get("name"),
credentials=json.dumps(credentials),
)
created = database.create(dns_provider)
return created.id