Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.

Commit cfae3d1

Browse files
authored
Add support for DNS zones management (#50)
* stacks: add DNS zone basic handling (create/delete/update label/index) * DNS zones: add routines to handle DNS records managing
1 parent 25ffa46 commit cfae3d1

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

pystackpath/stacks/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pystackpath.stacks.wafsites import WafSites
55
from pystackpath.stacks.metrics import Metrics
66
from pystackpath.stacks.certificates import Certificates
7+
from pystackpath.stacks.zones import Zones
78

89

910
class Stacks(BaseObject):
@@ -95,3 +96,6 @@ def metrics(self):
9596

9697
def certificates(self):
9798
return Certificates(self._client, f"/cdn/v1/stacks/{self.id}")
99+
100+
def zones(self):
101+
return Zones(self._client, f"/dns/v1/stacks/{self.id}")
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from pystackpath.stacks.zones.records import Records
2+
from pystackpath.util import BaseObject, pagination_query, PageInfo
3+
4+
5+
class Zones(BaseObject):
6+
def create(self, **payload):
7+
"""
8+
Create a new zone
9+
:param payload: dict according to https://stackpath.dev/reference/zones#createzone
10+
:return: dict with DNS zone
11+
String id A zone's unique identifier.
12+
String stackId The ID of the stack to which a zone belongs.
13+
String domain A zone's name. Site names correspond to their fully-qualified domain name.
14+
String version A zone's version number. Version numbers are incremented automatically
15+
when a zone is updated
16+
List nameservers A zone's NS hostnames of resolvers that host a zone. Every zone has multiple
17+
name servers assigned by StackPath upon creation for redundancy purposes.
18+
String status A zone's internal state. Zone status is controlled by StackPath as zones
19+
are managed by StackPath's accounting and security teams.
20+
Boolean disabled A zone's disabled flag. Shows whether or not a zone has been disabled by
21+
the user.
22+
String createdAt The date that a zone was created.
23+
String updatedAt The date that a zone was last updated.
24+
String verified The date that a zone's NSes were last audited by StackPath.
25+
dict labels The optional dict of zone's user labels. Zone labels are not processed by StackPath
26+
and are solely used for users to organize their zones.
27+
"""
28+
response = self._client.post(f"{self._base_api}/zones", json=payload)
29+
return self.loaddict(response.json()["zone"])
30+
31+
def index(self, first="", after="", filter="", sort_by=""):
32+
pagination = pagination_query(first=first, after=after, filter=filter, sort_by=sort_by)
33+
response = self._client.get(f"{self._base_api}/zones", params=pagination)
34+
items = []
35+
for item in response.json()["zones"]:
36+
items.append(self.loaddict(item))
37+
pageinfo = PageInfo(**response.json()["pageInfo"])
38+
39+
return {"zones": items, "pageinfo": pageinfo}
40+
41+
def get(self, zone_id):
42+
response = self._client.get(f"{self._base_api}/zones/{zone_id}")
43+
return self.loaddict(response.json()["zone"])
44+
45+
def delete(self):
46+
"""
47+
Delete a zone
48+
:return: a stackpath zone object with the deleted zone
49+
"""
50+
response = self._client.delete(f"{self._base_api}/zones/{self.id}")
51+
return self
52+
53+
def disable(self):
54+
"""
55+
Disable a zone
56+
:return: a stackpath site object with the disabled zone
57+
"""
58+
response = self._client.post(f"{self._base_api}/zones/{self.id}/disable")
59+
return self
60+
61+
def enable(self):
62+
"""
63+
Enable a zone
64+
:return: a stackpath site object with the enabled zone
65+
"""
66+
response = self._client.post(f"{self._base_api}/zones/{self.id}/enable")
67+
return self
68+
69+
def update_labels(self, labels: dict):
70+
"""
71+
Update a zone user labels
72+
:param labels:
73+
:return: dict with DNS zone
74+
String id A zone's unique identifier.
75+
String stackId The ID of the stack to which a zone belongs.
76+
String domain A zone's name. Site names correspond to their fully-qualified domain name.
77+
String version A zone's version number. Version numbers are incremented automatically
78+
when a zone is updated
79+
List nameservers A zone's NS hostnames of resolvers that host a zone. Every zone has multiple
80+
name servers assigned by StackPath upon creation for redundancy purposes.
81+
String status A zone's internal state. Zone status is controlled by StackPath as zones
82+
are managed by StackPath's accounting and security teams.
83+
Boolean disabled A zone's disabled flag. Shows whether or not a zone has been disabled by
84+
the user.
85+
String createdAt The date that a zone was created.
86+
String updatedAt The date that a zone was last updated.
87+
String verified The date that a zone's NSes were last audited by StackPath.
88+
dict labels The optional dict of zone's user labels. Zone labels are not processed by StackPath
89+
and are solely used for users to organize their zones.
90+
"""
91+
response = self._client.put(f"{self._base_api}/zones/{self.id}", json={'labels': labels})
92+
return self.loaddict(response.json()["zone"])
93+
94+
95+
def records(self):
96+
return Records(self._client, f"{self._base_api}/zones/{self.id}")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from pystackpath.util import BaseObject, PageInfo, pagination_query
2+
3+
4+
class Records(BaseObject):
5+
def index(self, first="", after="", filter="", sort_by=""):
6+
pagination = pagination_query(first=first, after=after, filter=filter, sort_by=sort_by)
7+
response = self._client.get(f"{self._base_api}/records", params=pagination)
8+
9+
items = list(map(lambda x: self.loaddict(x), response.json()["records"]))
10+
pageinfo = PageInfo(**response.json()["pageInfo"])
11+
12+
return {"records": items, "pageinfo": pageinfo}
13+
14+
def get(self, record_id: str):
15+
response = self._client.get(f"{self._base_api}/records/{record_id}")
16+
return self.loaddict(response.json()["record"])
17+
18+
def add(self, **payload):
19+
response = self._client.post(f"{self._base_api}/records", json=payload)
20+
return self.loaddict(response.json()["record"])
21+
22+
def delete(self):
23+
response = self._client.delete(f"{self._base_api}/records/{self.id}")
24+
return self
25+
26+
def update(self, **payload):
27+
response = self._client.patch(f"{self._base_api}/records/{self.id}", json=payload)
28+
return self.loaddict(response.json()["record"])
29+
30+
def replace(self, **payload):
31+
response = self._client.put(f"{self._base_api}/records/{self.id}", json=payload)
32+
return self.loaddict(response.json()["record"])

0 commit comments

Comments
 (0)