cloudflare / python-cloudflare

Python wrapper for the Cloudflare Client API v4
MIT License
693 stars 158 forks source link

[Question] how to allow my SaaS enduser call this API to create new DNS A record on their zone? #139

Closed simkimsia closed 2 years ago

simkimsia commented 2 years ago

i have a Django SaaS that already has oAuth connections to DigitalOcean and GitHub.

I like to add a feature where my enduser can also create DNS A record of their zones inside CloudFlare through my SaaS app.

The way I have done similar things with DigitalOcean and GitHub is

  1. my enduser needs to connect with OAuth to these services
  2. these services return a token after my enduser connects their SaaS account with DigitalOcean and GitHub
  3. i then use python libraries that interact with these services' API using the token.

I was wondering if the process is the same for CloudFlare as well?

I am happy to pay to have this question answered more quickly. Just point me to the right place and i will make payment. Am already a cloudflare account holder and paid for several support plans in the past.

mahtin commented 2 years ago

There are a few DNS programs within the examples folder. Maybe the closest would be the example_update_dynamic_dns.py code as it sets a DNS value. Here's the key code ...

    # no exsiting dns record to update - so create dns record
    dns_record = {
        'name':dns_name,
        'type':ip_address_type,
        'content':ip_address
    }
    try:
        dns_record = cf.zones.dns_records.post(zone_id, data=dns_record)
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/zones.dns_records.post %s - %d %s - api call failed' % (dns_name, e, e))
    print('CREATED: %s %s' % (dns_name, ip_address))

Please note that this code can set both A and/or AAAA records. Code the ip_address_type variable as needed. You should also see the put() vs post() parts of the code. If the DNS record already exists, then use the put() method.

This GitHub repository really only handles issues with the library. It codes up https://api.cloudflare.com/ entries (which should also be reviewed).

If you want more Cloudflare help, I would highly recommend you also visit https://developers.cloudflare.com/ and/or https://support.cloudflare.com/ as that's more useful for ongoing API support.

simkimsia commented 2 years ago

If you want more Cloudflare help, I would highly recommend you also visit developers.cloudflare.com and/or support.cloudflare.com as that's more useful for ongoing API support.

i did engage business support plan (i paid for this) for this specific issue. I thought I will write here as well in case any answer I do get from CloudFlare team can also help other people. There's a ticket I have filed for this.

There are a few DNS programs within the examples folder. Maybe the closest would be the example_update_dynamic_dns.py code as it sets a DNS value.

Thanks for the example pointed out. I now beginning to realize that cloudflare doesn't really have oauth and the API doesn't allow for oauth token to be used to identify the specific user account.

I will ask those questions to the support specifically. thank you

UPDATE

Sorry I just realized CloudFlare does indeed have oAuth, so I asked a follow up question about using oAuth token for this wrapper at #140