Skip to content

Commit

Permalink
Add gandi dns api and first test.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiarn committed Apr 22, 2023
1 parent 781c0e3 commit 14640b1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions plugins/dns_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
from .main import ItemProvider
from .views import Handler
from .providers.gandi import *


logging.info('dns_api.__init__.py: dns_api loaded')
Empty file.
40 changes: 40 additions & 0 deletions plugins/dns_api/providers/gandi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import requests
import json

from jadi import component
from aj.plugins.dns_api.api import ApiDnsManager
from aj.plugins.dns_api.record import Record
import aj

@component(ApiDnsManager)
class GandiApiDnsProvider(ApiDnsManager):
id = 'gandi'
name = 'Gandi DNS API'

def __init__(self, context):
apikey = aj.config.data.get('dns_apikey', '') # TODO : wrong config entry for this
self.baseUrl = 'https://api.gandi.net/v5/livedns/domains'
self.headers = {"Authorization": f"Apikey {apikey}"}

def _req(self, method, apiurl="", data=None):
if method not in ["get", "put", "post", "delete"]:
return

func = getattr(requests, method)
if data is None:
resp = func(f"{self.baseUrl}{apiurl}", headers=self.headers)
else:
resp = func(f"{self.baseUrl}{apiurl}", data=data, headers=self.headers)

return resp

def get_domains(self):
domains = json.loads(self._req("get").content)
return [domain['fqdn'] for domain in domains]

def get_records(self, domain):
resp = self._req('get', apiurl=f"/{domain.fqdn}/records")
records = json.loads(resp.content)

for record in sorted(records, key=lambda d: d['rrset_name']):
domain.records.append(Record(domain.fqdn, record))
2 changes: 2 additions & 0 deletions plugins/dns_api/resources/js/controllers/index.controller.es
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
angular.module('ajenti.dns_api').controller('DnsAPIIndexController', function($scope, $http, pageTitle, gettext, notify) {
pageTitle.set(gettext('DNS API'));

$http.get('/api/dns_api');

});

6 changes: 5 additions & 1 deletion plugins/dns_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from aj.api.http import get, HttpPlugin
from aj.auth import authorize
from aj.api.endpoint import endpoint, EndpointError
from aj.plugins.dns_api.manager import DomainManager

@component(HttpPlugin)
class Handler(HttpPlugin):
def __init__(self, context):
self.context = context
self.mgr = DomainManager(self.context)


@get(r'/api/dns_api')
@endpoint(api=True)
def handle_api_example_dns_api(self, http_context):
pass
self.mgr.load()
print(self.mgr.domains)

0 comments on commit 14640b1

Please sign in to comment.