koalalorenzo / python-digitalocean

🐍🐳 Python module to manage Digital Ocean droplets
GNU Lesser General Public License v3.0
1.26k stars 301 forks source link

Delete a domain record #330

Closed Zuiluj closed 2 years ago

Zuiluj commented 3 years ago

Any way to delete the domain record? Can't find it in the docs.

the-holger commented 3 years ago

I briefly checked the sources, doesn't seem to be there. What I am doing is using a domain.destroy() followed by a domain.create() and repopulation with the records I want/need. Only makes sense when there's not a lot of records though...

Zuiluj commented 3 years ago

Indeed. I just created a helper for this kind of method.

import requests

def delete_record_in_digitalocean(auth_key, for_domain, record_id):
    """ Deletes a domain record in digitalocean

    :param auth_key: key to use for accessing digitalocean api
    :param for_domain: domain to use for deleting the record
    :param record_id: id of the domain record
    """
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {auth_key}"
    }

    response = requests.delete(
        url=f'{API_URL}/domains/{for_domain}/records/{record_id}',
        headers=headers
    ).json()

    return response

Then for calling:

domain_record = f'{YOUR_DOMAIN_RECORD_NAME}'
domain = digitalocean.Domain(token=DIGITALOCEAN_TOKEN, name=for_domain)
records = domain.get_records()
record_id = [r.id for r in records if r.name == domain_record]
response = delete_record_in_digitalocean(
    auth_key=DIGITALOCEAN_TOKEN,
    for_domain=for_domain,
    record_id=record_id
)

I might create a pull request and tests for some other time but hopefully, they'll add it soon. I hope my snippet could help someone.

joeldeteves commented 3 years ago

Here's how I did it:

# Find all TXT records that start with the string '_acme' and delete each one

    for record in Domain(token=DO_API_TOKEN, name='mydomain.com').get_records():
        if record.type == 'TXT' and record.name[0:5] == '_acme':
            record.destroy()
koalalorenzo commented 2 years ago

Closing for inactivity! Feel free to comment or open a new one if needed!