question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] DNS Zone Import

See original GitHub issue

Hi,

I’m trying to call the dns_records/import endpoint using this python lib, but I can’t find how to do it.

Please someone provide an example (add this one to examples directory).

Regards

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
mahtincommented, Jul 18, 2021

@skyscooby - you are absolutely correct. While the word import is a reserved word in Python; using it via the packages command line program cli4 works because it’s searched in the class dictionary vs used in a python language statement. I tried your code and it fails.

This is unfortunate and your comment (Does this even work?) is in fact correct - the code was never tested (by me) as python code. I only tested the cli4 version. You can see a working example in the README file.

So … two solutions. The README has the command line example:

$cli4 --post file=@zone.txt /zones/:example.com/dns_records/import
...
$

This does work.

To code this in Python, some shenanigans is needed. This code works. Here is the core work-around for the reserved name issue:

    m = cf.zones.dns_records
    m = getattr(m, 'import')
    r = m.post(zone_id, files={'file':fd})

The full example is as follows:

#!/usr/bin/env python
"""Cloudflare API code - example"""

import os
import sys
sys.path.insert(0, os.path.abspath('..'))

import json

import CloudFlare

def main():
    """Cloudflare API code - example"""

    try:
        zone_name = sys.argv[1]
        file_name = sys.argv[2]
    except IndexError:
        exit('usage: example_dns_import.py zone zone-file')

    try:
        fd = open(file_name, 'rb')
    except:
        exit('file open - %s' % (e))

    cf = CloudFlare.CloudFlare()

    # grab the zone identifier
    try:
        params = {'name': zone_name}
        zones = cf.zones.get(params=params)
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/zones %d %s - api call failed' % (e, e))
    except Exception as e:
        exit('/zones.get - %s - api call failed' % (e))

    if len(zones) == 0:
        exit('/zones.get - %s - zone not found' % (zone_name))

    if len(zones) != 1:
        exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones)))

    zone_id = zones[0]['id']

    m = cf.zones.dns_records
    m = getattr(m, 'import')

    try:
        r = m.post(zone_id, files={'file':fd})
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/zones/dns_records/import %s - %d %s - api call failed' % (dns_name, e, e))

    print(json.dumps(r))

    exit(0)

if __name__ == '__main__':
    main()

I will add this to both the README and to the examples folder.

@skyscooby - Thank you for pointing this out. I will reopen this issue and close once the code is updated. @rogeriolino - Sorry for completely missing this year - my bad!

0reactions
skyscoobycommented, Jul 18, 2021

Does this even work? For whatever reason 2.8.15 doesn’t even recognize import as a valid operation on dns_records… export works tho.

>>> r = cf.zones.dns_records.import.post(zone_id, files={‘file’: fd})
  File "<stdin>", line 1
    r = cf.zones.dns_records.import.post(zone_id, files={‘file’: fd})
                             ^
SyntaxError: invalid syntax
Read more comments on GitHub >

github_iconTop Results From Across the Web

bind dns zone file import format - Microsoft Q&A
I am using this method to perform the import: az network dns zone import -g Azure_DNS -n example.org -f test.txt · azure-dns. Comment....
Read more >
How can I import a DNS zone file? - GoDaddy Community
I downloaded (exported) the zone file for my domain and made some changes, then imported it back and received an error. To confirm...
Read more >
Export and Import DNS Zone with PowerShell from One ...
Copy the exported DNS zone file to the C:WindowsSystem32dns directory on the destination server. · Rename it to have a .dns extension ·...
Read more >
Can I upload/import a DNS zone file? | Linode Questions
Sadly your best option if you want to use our DNS Manager is that you would have to create all new records. I...
Read more >
How can you import external zone files to Azure DNS zone?
A DNS zone file is a text file that contains details of every Domain Name System (DNS) record in the zone. It follows...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found