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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

hhellyer
Copy link

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.

Add support for missing parameters when importing zone files:
- networks
- views
- name

Support for the "name" field also added to create without zonefile.
@hhellyer
Copy link
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. 😄

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
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.

networksStrs = map(str, fields["networks"])
networksParam = ",".join(networksStrs)
params["networks"] = networksParam
if fields.get("views") is not None:
Copy link
Contributor

Choose a reason for hiding this comment

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

But we don't expect people to pass views as numbers?

Copy link
Author

Choose a reason for hiding this comment

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

No, views are passed by name.
🤷

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
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

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