0
0
mirror of https://github.com/thegeeklab/corenetworks.git synced 2024-11-21 21:20:38 +00:00

add zone methods

This commit is contained in:
Robert Kaussow 2020-04-12 16:27:22 +02:00
parent 87d2de8a11
commit 3d60d511d1
2 changed files with 80 additions and 11 deletions

View File

@ -28,6 +28,7 @@ class CoreNetworks():
self.__user_agent = "Core Networks Python API {version}".format( self.__user_agent = "Core Networks Python API {version}".format(
version=corenetworks.__version__ version=corenetworks.__version__
) )
self.auto_commit = auto_commit
self._schema = { self._schema = {
"type": "object", "type": "object",
@ -101,6 +102,37 @@ class CoreNetworks():
self._auth = CoreNetworksBasicAuth(user, password, self.__endpoint) self._auth = CoreNetworksBasicAuth(user, password, self.__endpoint)
# ZONES
def zones(self):
"""
Get the list of DNS zones.
Args: None
Returns:
list: List of zones.
"""
result = self.__rest_helper("/dnszones/", method="GET")
return self.__normalize(result)
def zone(self, zone):
"""
Get details about a DNS zone.
Args:
zone (str): Name of the target DNS zone.
Returns:
list: List of zones.
"""
result = self.__rest_helper("/dnszones/{zone}".format(zone=zone), method="GET")
return self.__normalize(result)
# RECORDS # RECORDS
def records(self, zone, params={}): def records(self, zone, params={}):
@ -154,6 +186,9 @@ class CoreNetworks():
result = self.records(zone=zone, params=params) result = self.records(zone=zone, params=params)
if self.auto_commit:
self.commit(zone=zone)
return self.__normalize(result) return self.__normalize(result)
def delete_record(self, zone, params): def delete_record(self, zone, params):
@ -189,6 +224,9 @@ class CoreNetworks():
"/dnszones/{zone}/records/delete".format(zone=zone), data=params, method="POST" "/dnszones/{zone}/records/delete".format(zone=zone), data=params, method="POST"
) )
if self.auto_commit:
self.commit(zone=zone)
return self.__normalize(result) return self.__normalize(result)
def commit(self, zone): def commit(self, zone):

View File

@ -39,6 +39,31 @@ def test_request_error(requests_mock, client):
assert client.commit(zone="example.com") assert client.commit(zone="example.com")
def test_zones(requests_mock, client):
requests_mock.get(
"https://beta.api.core-networks.de/dnszones/",
text='[{"name":"example.com","type":"master"}]'
)
resp = client.zones()
assert resp == [{"name": "example.com", "type": "master"}]
def test_zone(requests_mock, client):
requests_mock.get(
"https://beta.api.core-networks.de/dnszones/example.com",
json={
"active": True,
"dnssec": True,
"name": "example.com",
"type": "master"
}
)
resp = client.zone("example.com")
assert resp == {"active": True, "dnssec": True, "name": "example.com", "type": "master"}
def test_records(requests_mock, client): def test_records(requests_mock, client):
requests_mock.get( requests_mock.get(
"https://beta.api.core-networks.de/dnszones/example.com/records/", "https://beta.api.core-networks.de/dnszones/example.com/records/",
@ -58,7 +83,6 @@ def test_records(requests_mock, client):
}] }]
resp = client.records(zone="example.com") resp = client.records(zone="example.com")
assert resp == exp assert resp == exp
@ -95,7 +119,7 @@ def test_filter_records(requests_mock, client):
assert resp == [{"type": "A", "ttl": 1800, "name": "test", "data": "127.0.0.1"}] assert resp == [{"type": "A", "ttl": 1800, "name": "test", "data": "127.0.0.1"}]
def test_add_record(requests_mock, client): def test_add_record(requests_mock, client, mocker):
requests_mock.post( requests_mock.post(
"https://beta.api.core-networks.de/dnszones/example.com/records/", "https://beta.api.core-networks.de/dnszones/example.com/records/",
text=records_post_callback, text=records_post_callback,
@ -105,18 +129,18 @@ def test_add_record(requests_mock, client):
text=records_get_callback, text=records_get_callback,
) )
resp = client.add_record( record = {"type": "A", "ttl": 1800, "name": "test", "data": "127.0.0.1"}
zone="example.com", params={
"type": "A", resp = client.add_record(zone="example.com", params=record)
"ttl": 1800,
"name": "test",
"data": "127.0.0.1"
}
)
assert resp == [{"type": "A", "ttl": 1800, "name": "test", "data": "127.0.0.1"}] assert resp == [{"type": "A", "ttl": 1800, "name": "test", "data": "127.0.0.1"}]
mocker.patch.object(client, "commit")
client.auto_commit = True
client.add_record(zone="example.com", params=record)
client.commit.assert_called_once()
def test_delete_record(requests_mock, client):
def test_delete_record(requests_mock, client, mocker):
requests_mock.post( requests_mock.post(
"https://beta.api.core-networks.de/dnszones/example.com/records/delete", "https://beta.api.core-networks.de/dnszones/example.com/records/delete",
text=records_post_callback, text=records_post_callback,
@ -132,6 +156,13 @@ def test_delete_record(requests_mock, client):
}) })
assert forced == [] assert forced == []
mocker.patch.object(client, "commit")
client.auto_commit = True
client.delete_record(zone="example.com", params={
"type": "A",
})
client.commit.assert_called_once()
def test_delete_record_invalid(requests_mock, client): def test_delete_record_invalid(requests_mock, client):
requests_mock.post( requests_mock.post(