Skip to content
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

Zone file import - support missing params. #126

Merged
merged 9 commits into from
Oct 31, 2024
Merged

Conversation

hhellyer
Copy link
Contributor

This PR adds support for missing parameters when importing zone files:

  • networks
  • views
  • name

As both zone creation with or without a zone file to prepopulate it use the same call, just with or without the zonefile specificed, the "name" field also added to the api to create a zone and "views" is added to the list of fields passed through on zone creation.

@hhellyer
Copy link
Contributor Author

Since this will need an update to the changelog before releasing I've added my proposed changelog update below. (It is a bit wordy because of the need to differentiate between behaviour changes when creating a zone and new things when creating a zone with a zonefile.)

## 0.20.1 (June ??th, 2024)

ENHANCEMENTS:
* Adds support for specifying a list of views when creating zones with or without a provided zone file.
* Adds support for specifying a zone name other than the FQDN when creating zones with or without a provided zone file.
* A specified list of networks for a zone was only applied to zone creation when a zone file was not provided. If a list of networks is specified it will now be applied when creating zones by uploading a zone file as well.

Copy link
Contributor

@shane-ns1 shane-ns1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo (zone instead of name in a docstring), but more important is that this changes order of parameters passed to the API.

ns1/__init__.py Outdated
@@ -371,7 +377,8 @@ def createZone(
If zoneFile is specified, upload the specific zone definition file
to populate the zone with.

:param str zone: zone name, like 'example.com'
:param str zone: zone FQDN, like 'example.com'
:param str zone: zone name override, name will be zone FQDN if omitted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:param str zone: zone name override, name will be zone FQDN if omitted
:param str name: zone name override, name will be zone FQDN if omitted

It is possible in Python to declare keyword-only arguments:

https://peps.python.org/pep-3102/

But we don't have that now, so order matters.

If you want to reorder the parameters like this you'll need to make a major release, as it can break the API. Alternately you can put it at the end. 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're < 1.0 so we don't have to do a major release for such things (I think). But we will be breaking stuff!

def _buildBody(self, zone, **kwargs):
body = {}
body["zone"] = zone
self._buildStdBody(body, kwargs)
return body

def import_file(
self, zone, zoneFile, callback=None, errback=None, **kwargs
self, zone, zoneFile, name=None, callback=None, errback=None, **kwargs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the change in __init__.py, this reorders the parameters and is a breaking change.

# parameter but the zonefile API expects parameters containing comma
# seperated values.
if fields.get("networks") is not None:
networksStrs = map(str, fields["networks"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is because people may pass networks as numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our API takes a list of network numbers.
Unfortunately it takes them in a single query parameter of the form networks=1,2,3 which is not how you should pass an array via parameters, as I've found out while working on the other side of our import API, and not how the request library will pass an array. So we need to convert the array to a string.
If allowed to the libs will pass an array using multiple params of the same name:
networks=1&networks=2&networks=3
but our API won't accept that.

ns1/rest/zones.py Show resolved Hide resolved
params["name"] = name
return params

def create(self, zone, name=None, callback=None, errback=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also, changing the order of parameters may break things for some users.

ns1/zones.py Outdated
@@ -90,14 +90,21 @@ def success(result, *args):
self.zone, callback=success, errback=errback, **kwargs
)

def create(self, zoneFile=None, callback=None, errback=None, **kwargs):
def create(
self, name=None, zoneFile=None, callback=None, errback=None, **kwargs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may break the API for folks.

# parameter but the zonefile API expects parameters containing comma
# seperated values.
if fields.get("networks") is not None:
networksStrs = map(str, fields["networks"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snake case in Python.

return self._make_request(
"PUT",
"%s/%s" % (self.ROOT, zone),
"%s/%s" % (self.ROOT, name),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use f-strings. I know you're just changing a variable name, but might as well update while we're at it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, on this function, create(), and on import(). I've left the others in this file as those were the two I altered and (more importantly) wrote tests for. There aren't specific tests for the other functions in this file.

if name is not None:
params["name"] = name

zoneFilePath = "{}/../../examples/importzone.db".format(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f-string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comment, but also a nitpick. So change it if you'd like, but I won't block on it.

ns1/rest/zones.py Outdated Show resolved Hide resolved
ns1/rest/zones.py Outdated Show resolved Hide resolved
ns1/rest/zones.py Outdated Show resolved Hide resolved
Use f-strings and be more pythonic...

Co-authored-by: Shane Kerr <[email protected]>
@hhellyer hhellyer merged commit 0e0ac36 into master Oct 31, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants