2542cf5455
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
import requests
|
|
|
|
|
|
def _headers(api_key):
|
|
return {
|
|
'X-API-Key': api_key,
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
}
|
|
|
|
|
|
def test_connection(plesk_url, api_key, verify_ssl=True):
|
|
resp = requests.get(
|
|
f'{plesk_url.rstrip("/")}/api/v2/server',
|
|
headers=_headers(api_key),
|
|
verify=verify_ssl,
|
|
timeout=10,
|
|
)
|
|
resp.raise_for_status()
|
|
return resp.json()
|
|
|
|
|
|
def update_dns_record(plesk_url, api_key, domain, subdomain, ip, verify_ssl=True):
|
|
base = plesk_url.rstrip('/')
|
|
hdrs = _headers(api_key)
|
|
|
|
# Find existing A record for this subdomain
|
|
resp = requests.get(
|
|
f'{base}/api/v2/dns/records',
|
|
params={'domain_name': domain, 'type': 'A'},
|
|
headers=hdrs,
|
|
verify=verify_ssl,
|
|
timeout=15,
|
|
)
|
|
resp.raise_for_status()
|
|
records = resp.json()
|
|
|
|
# Match by host (Plesk returns just the subdomain part, e.g. "mypc")
|
|
host_lower = subdomain.lower().rstrip('.')
|
|
match = next(
|
|
(r for r in records if r.get('host', '').lower().rstrip('.') == host_lower),
|
|
None
|
|
)
|
|
|
|
if match:
|
|
resp = requests.put(
|
|
f'{base}/api/v2/dns/records/{match["id"]}',
|
|
json={'value': ip},
|
|
headers=hdrs,
|
|
verify=verify_ssl,
|
|
timeout=15,
|
|
)
|
|
resp.raise_for_status()
|
|
else:
|
|
resp = requests.post(
|
|
f'{base}/api/v2/dns/records',
|
|
json={
|
|
'domain_name': domain,
|
|
'type': 'A',
|
|
'host': subdomain,
|
|
'value': ip,
|
|
'ttl': 300,
|
|
},
|
|
headers=hdrs,
|
|
verify=verify_ssl,
|
|
timeout=15,
|
|
)
|
|
resp.raise_for_status()
|