-
Notifications
You must be signed in to change notification settings - Fork 26
PENG-7708-python-sdk-zoneexport #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
9d7c48b
12d4ad1
9d1500d
f0d21b1
c2b75da
2e7ecac
ccb6000
ca1896b
c04cadf
8cb5012
421d382
f371731
5161c1d
f7d7251
477bb68
f411f8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||
| # | ||||||
| # Copyright (c) 2025 NSONE, Inc. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is new code and yet to be released, I would suggest we add 2026 to the copyright? @ddevine-NS1 - any thoughts on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah agree, we should update the copyright |
||||||
| # | ||||||
| # License under The MIT License (MIT). See LICENSE in project root. | ||||||
| # | ||||||
|
|
||||||
| from ns1 import NS1 | ||||||
|
|
||||||
| # NS1 will use config in ~/.nsone by default | ||||||
| api = NS1() | ||||||
|
|
||||||
| # to specify an apikey here instead, use: | ||||||
| # api = NS1(apiKey='<<CLEARTEXT API KEY>>') | ||||||
|
|
||||||
| # to load an alternate configuration file: | ||||||
| # api = NS1(configFile='/etc/ns1/api.json') | ||||||
|
|
||||||
| # export a zone to BIND format | ||||||
| zone = api.loadZone("example.com") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you set
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the output filename dynamic using f"{zone_name}.txt" |
||||||
| zone_file = zone.export() | ||||||
| print(zone_file) | ||||||
|
|
||||||
| # save to a file | ||||||
| with open("example.com.zone", "w") as f: | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Usually zone files just have a .txt extension; and
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to example.com.txt in line 31. |
||||||
| f.write(zone_file) | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -188,6 +188,49 @@ def delete_version(self, zone, version_id, callback=None, errback=None): | |||||
| errback=errback, | ||||||
| ) | ||||||
|
|
||||||
| def export(self, zone, callback=None, errback=None): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to get_zonefile_export() |
||||||
| """ | ||||||
| Export zone as BIND-compatible zone file. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably say this downloads the zonefile. |
||||||
|
|
||||||
| :param str zone: zone name | ||||||
| :return: zone file content as string | ||||||
| """ | ||||||
| return self._make_request( | ||||||
| "GET", | ||||||
| f"{self.ROOT}/{zone}/export", | ||||||
| callback=callback, | ||||||
| errback=errback, | ||||||
| ) | ||||||
|
|
||||||
| def initiate_export(self, zone, callback=None, errback=None): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to initiate_zonefile_export() |
||||||
| """ | ||||||
| Initiate zone export job. | ||||||
|
|
||||||
| :param str zone: zone name | ||||||
| :return: export status response | ||||||
| """ | ||||||
| return self._make_request( | ||||||
| "PUT", | ||||||
| f"export/zonefile/{zone}", | ||||||
| body={}, | ||||||
| callback=callback, | ||||||
| errback=errback, | ||||||
| ) | ||||||
|
|
||||||
| def export_status(self, zone, callback=None, errback=None): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to status_zonefile_export() |
||||||
| """ | ||||||
| Check zone export status. | ||||||
|
|
||||||
| :param str zone: zone name | ||||||
| :return: export status response | ||||||
| """ | ||||||
| return self._make_request( | ||||||
| "GET", | ||||||
| f"export/zonefile/{zone}/status", | ||||||
| callback=callback, | ||||||
| errback=errback, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| # successive pages just extend the list of zones | ||||||
| def zone_list_pagination(curr_json, next_json): | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -287,3 +287,37 @@ def usage(self, callback=None, errback=None, **kwargs): | |
| return stats.usage( | ||
| zone=self.zone, callback=callback, errback=errback, **kwargs | ||
| ) | ||
|
|
||
| def export(self, callback=None, errback=None): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you replace these with a single function that:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhanced the example with detailed comments explaining the complete workflow. The export() method now handles all three steps automatically: initiate, poll status, and download. |
||
| """ | ||
| Export zone as a BIND-compatible zone file. | ||
|
|
||
| :param callback: optional callback | ||
| :param errback: optional error callback | ||
| :return: zone file content as string | ||
| """ | ||
| return self._rest.export(self.zone, callback=callback, errback=errback) | ||
|
|
||
| def initiate_export(self, callback=None, errback=None): | ||
| """ | ||
| Initiate zone export job. | ||
|
|
||
| :param callback: optional callback | ||
| :param errback: optional error callback | ||
| :return: export status response | ||
| """ | ||
| return self._rest.initiate_export( | ||
| self.zone, callback=callback, errback=errback | ||
| ) | ||
|
|
||
| def export_status(self, callback=None, errback=None): | ||
| """ | ||
| Check zone export status. | ||
|
|
||
| :param callback: optional callback | ||
| :param errback: optional error callback | ||
| :return: export status response | ||
| """ | ||
| return self._rest.export_status( | ||
| self.zone, callback=callback, errback=errback | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add more to the example? This example just assumes the other endpoints have been called.
It needs to initiate the export; then poll the status until it gets a complete status; then to download the file.