Skip to content

Commit cdf4ee0

Browse files
authored
feat: allow nameservers to be updated (#43)
Signed-off-by: Roald Nefs <[email protected]>
1 parent 15ff8db commit cdf4ee0

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
All notable changes in **python-transip** are documented below.
33

44
## [Unreleased]
5+
### Added
6+
- The option to replace all existing nameservers of a single domain at once from the `transip.v6.objects.Domain.nameservers` service.
57

68
## [0.4.0] (2021-01-24)
79
### Added

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
- [Update single DNS entry](#update-single-dns-entry)
5151
- [Update all DNS entries for a domain](#update-all-dns-entries-for-a-domain)
5252
- [Remove a DNS entry from a domain](#remove-a-dns-entry-from-a-domain)
53+
- [Nameservers](#nameserver)
54+
- [The **Nameserver** class](#the-nameserver-class)
55+
- [List nameservers for a domain](#list-nameservers-for-a-domain)
56+
- [Update nameservers for a domain](#update-nameservers-for-a-domain)
5357
- [VPS](#vps)
5458
- [HA-IP](#ha-ip)
5559
- [Colocation](#colocation)
@@ -641,6 +645,61 @@ domain.dns.delete(dns_entry_data)
641645

642646
The **transip.v6.objects.DnsEntry** class also provides a **delete()** method to delete a **DnsEntry** object from an instance.
643647

648+
### Nameservers
649+
Manage the nameserver of a domain.
650+
#### The **Nameserver** class
651+
652+
When listing all nameservers of a **transip.v6.objects.Domain** object, a list of **transip.v6.objects.Nameserver** objects is returned.
653+
654+
**_class_ Nameserver**
655+
656+
The **Nameserver** class makes the following attributes available:
657+
658+
- **hostname**: The hostname of this nameserver.
659+
- **ipv4**: The optional ipv4 glue record for this nameserver.
660+
- **ipv6**: The optional ipv6 glue record for this nameserver
661+
662+
#### List nameservers for a domain
663+
Retrieve the nameserver of a single domain registered in your TransIP account by calling **nameserver.list()** on a **transip.v6.objects.Domain** object. This will return a list of **transip.v6.objects.Nameserver** objects.
664+
665+
For example:
666+
```python
667+
import transip
668+
# Initialize a client using the TransIP demo token.
669+
client = transip.TransIP(access_token=transip.v6.DEMO_TOKEN)
670+
671+
# Retrieve a domain by its name.
672+
domain = client.domains.get('transipdemonstratie.nl')
673+
# Retrieve the nameservers of a single domain.
674+
nameservers = domain.nameservers.list()
675+
# Show the nameserver information on the screen.
676+
for nameserver in nameservers:
677+
print(f"Nameserver: {nameserver.hostname}")
678+
```
679+
680+
#### Update nameservers for a domain
681+
Update all the nameservers of a single domain registered in your TransIP account at once by calling **nameservers.replace()** on a **transip.v6.objects.Domain** object.
682+
683+
**Note:** This will wipe all nameservers previously configured on the domain.
684+
685+
For example:
686+
```python
687+
import transip
688+
# Initialize a client using the TransIP demo token.
689+
client = transip.TransIP(access_token=transip.v6.DEMO_TOKEN)
690+
691+
# Retrieve a domain by its name.
692+
domain = client.domains.get('transipdemonstratie.nl')
693+
# Retrieve the nameservers of the domain.
694+
nameservers = domain.nameservers.list()
695+
696+
# Update the ipv4 glue record of the first nameserver.
697+
nameserver[0].ipv4 = '195.135.195.195'
698+
699+
# Replace all the records with the updated ones
700+
domain.nameservers.replace(nameservers)
701+
```
702+
644703
## VPS
645704
The documentation for managing **VPSs** and related resources has not yet been documented. Feel free to file an [issue](https://github.com/roaldnefs/python-transip/issues/new/choose) for adding the missing section(s) in the documentation.
646705

tests/fixtures/domains.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
"match_json_params": {
348348
"nameservers": [
349349
{
350-
"hostname": "ns0.transip.nl",
350+
"hostname": "ns1.transip.nl",
351351
"ipv4": "",
352352
"ipv6": ""
353353
}

tests/services/test_domains.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,34 @@ def test_contacts_list(self) -> None:
5858

5959
@responses.activate
6060
def test_nameservers_list(self) -> None:
61+
"""
62+
Check if the nameservers of a single domain can be listed.
63+
"""
6164
domain: Domain = self.client.domains.get("example.com") # type: ignore
6265
nameservers: List[Nameserver] = domain.nameservers.list() # type: ignore
66+
self.assertEqual(len(nameservers), 1)
67+
6368
nameserver: Nameserver = nameservers[0]
6469

65-
assert len(nameservers) == 1
66-
assert nameserver.get_id() == "ns0.transip.nl" # type: ignore
70+
self.assertEqual(nameserver.get_id(), "ns0.transip.nl") # type: ignore
71+
72+
@responses.activate
73+
def test_nameservers_replaces(self) -> None:
74+
"""
75+
Check if the nameservers of a single domain can be replaced.
76+
"""
77+
domain: Domain = self.client.domains.get("example.com") # type: ignore
78+
nameservers: List[Nameserver] = domain.nameservers.list() # type: ignore
79+
self.assertEqual(len(nameservers), 1)
80+
81+
# Update the hostname of the first nameserver
82+
nameservers[0].hostname = "ns1.transip.nl"
83+
84+
# Replace all existing nameservers.
85+
try:
86+
domain.nameservers.replace(nameservers) # type: ignore
87+
except Exception as exc:
88+
assert False, f"'transip.v6.objects.Domain.nameservers.replace' raised an exception {exc}"
6789

6890
@responses.activate
6991
def test_dns_list(self) -> None:

transip/v6/objects.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,22 @@ class Nameserver(ApiObject):
314314
_id_attr: Optional[str] = "hostname"
315315

316316

317-
class NameserverService(ListMixin, ApiService):
317+
class NameserverService(ListMixin, ReplaceMixin, ApiService):
318318
"""Service to nameservers of a domain."""
319319

320320
_path: str = "/domains/{parent_id}/nameservers"
321321
_obj_cls: Optional[Type[ApiObject]] = Nameserver
322322

323323
_resp_list_attr: str = "nameservers"
324324

325+
# Additional data for replacing all existing Nameserver objects using the
326+
# ReplaceMixin
327+
_req_replace_attr: str = "nameservers"
328+
_replace_attrs: AttrsTuple = (
329+
("hostname",), # required
330+
("ipv4", "ipv6") # optional
331+
)
332+
325333

326334
class Domain(ApiObject):
327335

0 commit comments

Comments
 (0)