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

Commit

Permalink
Add support for DNS zones management (#50)
Browse files Browse the repository at this point in the history
* stacks: add DNS zone basic handling (create/delete/update label/index)

* DNS zones: add routines to handle DNS records managing
  • Loading branch information
nitr0man committed Sep 16, 2020
1 parent 25ffa46 commit cfae3d1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pystackpath/stacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pystackpath.stacks.wafsites import WafSites
from pystackpath.stacks.metrics import Metrics
from pystackpath.stacks.certificates import Certificates
from pystackpath.stacks.zones import Zones


class Stacks(BaseObject):
Expand Down Expand Up @@ -95,3 +96,6 @@ def metrics(self):

def certificates(self):
return Certificates(self._client, f"/cdn/v1/stacks/{self.id}")

def zones(self):
return Zones(self._client, f"/dns/v1/stacks/{self.id}")
96 changes: 96 additions & 0 deletions pystackpath/stacks/zones/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from pystackpath.stacks.zones.records import Records
from pystackpath.util import BaseObject, pagination_query, PageInfo


class Zones(BaseObject):
def create(self, **payload):
"""
Create a new zone
:param payload: dict according to https://stackpath.dev/reference/zones#createzone
:return: dict with DNS zone
String id A zone's unique identifier.
String stackId The ID of the stack to which a zone belongs.
String domain A zone's name. Site names correspond to their fully-qualified domain name.
String version A zone's version number. Version numbers are incremented automatically
when a zone is updated
List nameservers A zone's NS hostnames of resolvers that host a zone. Every zone has multiple
name servers assigned by StackPath upon creation for redundancy purposes.
String status A zone's internal state. Zone status is controlled by StackPath as zones
are managed by StackPath's accounting and security teams.
Boolean disabled A zone's disabled flag. Shows whether or not a zone has been disabled by
the user.
String createdAt The date that a zone was created.
String updatedAt The date that a zone was last updated.
String verified The date that a zone's NSes were last audited by StackPath.
dict labels The optional dict of zone's user labels. Zone labels are not processed by StackPath
and are solely used for users to organize their zones.
"""
response = self._client.post(f"{self._base_api}/zones", json=payload)
return self.loaddict(response.json()["zone"])

def index(self, first="", after="", filter="", sort_by=""):
pagination = pagination_query(first=first, after=after, filter=filter, sort_by=sort_by)
response = self._client.get(f"{self._base_api}/zones", params=pagination)
items = []
for item in response.json()["zones"]:
items.append(self.loaddict(item))
pageinfo = PageInfo(**response.json()["pageInfo"])

return {"zones": items, "pageinfo": pageinfo}

def get(self, zone_id):
response = self._client.get(f"{self._base_api}/zones/{zone_id}")
return self.loaddict(response.json()["zone"])

def delete(self):
"""
Delete a zone
:return: a stackpath zone object with the deleted zone
"""
response = self._client.delete(f"{self._base_api}/zones/{self.id}")
return self

def disable(self):
"""
Disable a zone
:return: a stackpath site object with the disabled zone
"""
response = self._client.post(f"{self._base_api}/zones/{self.id}/disable")
return self

def enable(self):
"""
Enable a zone
:return: a stackpath site object with the enabled zone
"""
response = self._client.post(f"{self._base_api}/zones/{self.id}/enable")
return self

def update_labels(self, labels: dict):
"""
Update a zone user labels
:param labels:
:return: dict with DNS zone
String id A zone's unique identifier.
String stackId The ID of the stack to which a zone belongs.
String domain A zone's name. Site names correspond to their fully-qualified domain name.
String version A zone's version number. Version numbers are incremented automatically
when a zone is updated
List nameservers A zone's NS hostnames of resolvers that host a zone. Every zone has multiple
name servers assigned by StackPath upon creation for redundancy purposes.
String status A zone's internal state. Zone status is controlled by StackPath as zones
are managed by StackPath's accounting and security teams.
Boolean disabled A zone's disabled flag. Shows whether or not a zone has been disabled by
the user.
String createdAt The date that a zone was created.
String updatedAt The date that a zone was last updated.
String verified The date that a zone's NSes were last audited by StackPath.
dict labels The optional dict of zone's user labels. Zone labels are not processed by StackPath
and are solely used for users to organize their zones.
"""
response = self._client.put(f"{self._base_api}/zones/{self.id}", json={'labels': labels})
return self.loaddict(response.json()["zone"])


def records(self):
return Records(self._client, f"{self._base_api}/zones/{self.id}")
32 changes: 32 additions & 0 deletions pystackpath/stacks/zones/records.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pystackpath.util import BaseObject, PageInfo, pagination_query


class Records(BaseObject):
def index(self, first="", after="", filter="", sort_by=""):
pagination = pagination_query(first=first, after=after, filter=filter, sort_by=sort_by)
response = self._client.get(f"{self._base_api}/records", params=pagination)

items = list(map(lambda x: self.loaddict(x), response.json()["records"]))
pageinfo = PageInfo(**response.json()["pageInfo"])

return {"records": items, "pageinfo": pageinfo}

def get(self, record_id: str):
response = self._client.get(f"{self._base_api}/records/{record_id}")
return self.loaddict(response.json()["record"])

def add(self, **payload):
response = self._client.post(f"{self._base_api}/records", json=payload)
return self.loaddict(response.json()["record"])

def delete(self):
response = self._client.delete(f"{self._base_api}/records/{self.id}")
return self

def update(self, **payload):
response = self._client.patch(f"{self._base_api}/records/{self.id}", json=payload)
return self.loaddict(response.json()["record"])

def replace(self, **payload):
response = self._client.put(f"{self._base_api}/records/{self.id}", json=payload)
return self.loaddict(response.json()["record"])

0 comments on commit cfae3d1

Please sign in to comment.