Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions namecheap.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,54 @@ def domains_dns_addHost(self, domain, host_record):
})
self._call("namecheap.domains.dns.setHosts", extra_payload)

def domains_dns_addHosts(self, domain, host_records):
"""This method is absent in the original API. host_records is a list of
dicts. The advantage over `domains_dns_addHost` above is a single
network request rather than one for each record add having no knowledge
about the other records.

Example setting CNAME records for e.g. parking domains at bodis:

api.domains_dns_addHosts('example.com', [
{
"RecordType": "CNAME",
"HostName": "@",
"Address": "xyz.bodis.com",
"TTL": 1800
},
{
"RecordType": "CNAME",
"HostName": "*",
"Address": "xyz.bodis.com",
"TTL": 1800
},
{
"RecordType": "CNAME",
"HostName": "www",
"Address": "xyz.bodis.com",
"TTL": 1800
},
])
"""
host_records_remote = self.domains_dns_getHosts(domain)

print("Remote: %i" % len(host_records_remote))

for host_record in host_records:
host_records_remote.append(host_record)

host_records_remote = [self._elements_names_fix(x) for x in host_records_remote]

print("To set: %i" % len(host_records_remote))

extra_payload = self._list_of_dictionaries_to_numbered_payload(host_records_remote)
sld, tld = domain.split(".")
extra_payload.update({
'SLD': sld,
'TLD': tld
})
self._call("namecheap.domains.dns.setHosts", extra_payload)

def domains_dns_delHost(self, domain, host_record):
"""This method is absent in original API as well. It executes non-atomic
remove operation over the host record which has the following Type,
Expand Down
83 changes: 83 additions & 0 deletions namecheap_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,89 @@ def test_domains_dns_addHost():
]
assert_equal(hosts, expected_result)

def test_domains_dns_addHosts():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = test_register_domain()
api.domains_dns_setHosts(
domain_name,
[{
'HostName': '@',
'RecordType': 'TXT',
'Address': 'specialtxtcode',
'TTL': '1800'
}]
)
api.domains_dns_addHosts(
domain_name,
[{
'RecordType': 'CNAME',
'HostName': '@',
'Address': 'xyz.bodis.com',
'TTL': '1800'
}, {
'RecordType': 'CNAME',
'HostName': '*',
'Address': 'xyz.bodis.com',
'TTL': '1800'
}, {
'RecordType': 'CNAME',
'HostName': 'www',
'Address': 'xyz.bodis.com',
'TTL': '1800'
}]
)

hosts = api.domains_dns_getHosts(domain_name)

# these might change
del hosts[0]['HostId']
del hosts[1]['HostId']

expected_result = [
{
'Name': '@',
'Address': 'xyz.bodis.com',
'TTL': '1800',
'Type': 'CNAME',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}, {
'Name': '*',
'Address': 'xyz.bodis.com',
'TTL': '1800',
'Type': 'CNAME',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}, {
'Name': 'www',
'Address': 'xyz.bodis.com',
'TTL': '1800',
'Type': 'CNAME',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}, {
'Name': '@',
'Address': 'specialtxtcode',
'TTL': '1800',
'Type': '@',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}
]
assert_equal(hosts, expected_result)


def test_domains_dns_bulkAddHosts():
api = Api(username, api_key, username, ip_address, sandbox=True)
Expand Down