|
| 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}") |
0 commit comments