Skip to content

Commit 386a2e9

Browse files
authored
Merge pull request #76 from amiller/fix/namecheap-dns-provider
fix(namecheap): Fix XML namespace and DNSRecord type conversion
2 parents 668295a + 472ab10 commit 386a2e9

File tree

1 file changed

+31
-28
lines changed
  • custom-domain/dstack-ingress/scripts/dns_providers

1 file changed

+31
-28
lines changed

custom-domain/dstack-ingress/scripts/dns_providers/namecheap.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _make_request(self, command: str, **params) -> Dict:
9191
root = ET.fromstring(response.content)
9292

9393
# Check for API errors
94-
errors = root.find('.//{https://api.namecheap.com/xml.response}Errors')
94+
errors = root.find('.//{http://api.namecheap.com/xml.response}Errors')
9595
if errors is not None and len(errors) > 0:
9696
error_messages = []
9797
for error in errors:
@@ -154,7 +154,7 @@ def get_dns_records(
154154

155155
# Parse the host records from XML response
156156
records = []
157-
host_elements = result["result"].findall('.//{https://api.namecheap.com/xml.response}host')
157+
host_elements = result["result"].findall('.//{http://api.namecheap.com/xml.response}host')
158158

159159
for host in host_elements:
160160
record_name = host.get("Name")
@@ -215,43 +215,46 @@ def create_dns_record(self, record: DNSRecord) -> bool:
215215
else:
216216
hostname = record.name.replace("." + sld + "." + tld, "")
217217

218-
# Remove existing records of the same type and name
219-
filtered_records = [
220-
r for r in existing_records
221-
if not (r.name == record.name and r.type == record.type)
222-
]
223-
218+
# Remove existing records of the same type and name, convert to dicts
219+
all_records = []
220+
for r in existing_records:
221+
if r.name == record.name and r.type == record.type:
222+
continue
223+
r_hostname = "@" if r.name == sld + "." + tld else r.name.replace("." + sld + "." + tld, "")
224+
d = {"HostName": r_hostname, "RecordType": r.type.value, "Address": r.content, "TTL": str(r.ttl)}
225+
if r.type == RecordType.MX and r.priority:
226+
d["MXPref"] = str(r.priority)
227+
all_records.append(d)
228+
224229
# Add new record
225-
new_record = {
226-
"HostName": hostname,
227-
"RecordType": record.type.value,
228-
"Address": record.content,
229-
"TTL": str(record.ttl)
230-
}
231-
230+
new_record = {"HostName": hostname, "RecordType": record.type.value, "Address": record.content, "TTL": str(record.ttl)}
232231
if record.type == RecordType.MX and record.priority:
233232
new_record["MXPref"] = str(record.priority)
234-
235-
filtered_records.append(new_record)
236-
237-
# Set all records
238-
return self._set_dns_records(sld, tld, filtered_records)
233+
all_records.append(new_record)
234+
235+
return self._set_dns_records(sld, tld, all_records)
239236

240237
def delete_dns_record(self, record_id: str, domain: str) -> bool:
241238
"""Delete a DNS record."""
242-
# Namecheap doesn't support individual record deletion
243-
# We need to get all records, remove the one with the matching ID, and set them all
244239
domain_info = self._get_domain_info(domain)
245240
if not domain_info:
246241
return False
247-
242+
248243
sld, tld = domain_info
249244
existing_records = self.get_dns_records(domain)
250-
251-
# Remove the record with the matching ID
252-
filtered_records = [r for r in existing_records if r.id != record_id]
253-
254-
return self._set_dns_records(sld, tld, filtered_records)
245+
246+
# Remove record with matching ID, convert rest to dicts
247+
all_records = []
248+
for r in existing_records:
249+
if r.id == record_id:
250+
continue
251+
r_hostname = "@" if r.name == sld + "." + tld else r.name.replace("." + sld + "." + tld, "")
252+
d = {"HostName": r_hostname, "RecordType": r.type.value, "Address": r.content, "TTL": str(r.ttl)}
253+
if r.type == RecordType.MX and r.priority:
254+
d["MXPref"] = str(r.priority)
255+
all_records.append(d)
256+
257+
return self._set_dns_records(sld, tld, all_records)
255258

256259
def create_caa_record(self, caa_record: CAARecord) -> bool:
257260
"""Create a CAA record."""

0 commit comments

Comments
 (0)