-
Notifications
You must be signed in to change notification settings - Fork 22
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
Next Available Address Block Support for Ansible V2 #58
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5428b6f
added code for next_available_address_block
VishrutiBuddhadev f8b1b40
added test cases for next_available_address_block
VishrutiBuddhadev c5f20bd
modified examples
VishrutiBuddhadev 1f98e6f
added ipam_next_available_address_block.py to runtime.yml
VishrutiBuddhadev 72650f2
modified cleanup.yml
VishrutiBuddhadev ba80dcd
addressed review comment
VishrutiBuddhadev b0ed28b
modified examples
VishrutiBuddhadev 4cff777
modified examples
VishrutiBuddhadev 01f63ef
Merge branch 'v2' into next-available
VishrutiBuddhadev e2b4db5
Merge branch 'v2' of github.com:infobloxopen/bloxone-ansible into nex…
VishrutiBuddhadev 1def603
Merge branch 'next-available' of github.com:VishrutiBuddhadev/bloxone…
VishrutiBuddhadev 8d46439
addressed review comments
VishrutiBuddhadev 58bb73c
addressed review comments
VishrutiBuddhadev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
major_changes: | ||
- added support for creating and retrieving next available address blocks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
plugins/modules/ipam_next_available_address_block_info.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: Infoblox Inc. | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
from __future__ import absolute_import, division, print_function | ||
|
||
__metaclass__ = type | ||
|
||
DOCUMENTATION = r""" | ||
--- | ||
module: ipam_next_available_address_block_info | ||
short_description: Manage NextAvailableAddressBlock | ||
description: | ||
- Manage NextAvailableAddressBlock | ||
version_added: 2.0.0 | ||
author: Infoblox Inc. (@infobloxopen) | ||
options: | ||
id: | ||
description: | ||
- ID of the object | ||
type: str | ||
required: true | ||
cidr: | ||
description: | ||
- The CIDR value of the object | ||
type: int | ||
required: true | ||
count: | ||
description: | ||
- Number of objects to generate. Default 1 if not set | ||
type: int | ||
required: false | ||
extends_documentation_fragment: | ||
- infoblox.bloxone.common | ||
""" # noqa: E501 | ||
|
||
EXAMPLES = r""" | ||
- name: "Create an Address Block" | ||
infoblox.bloxone.ipam_address_block: | ||
address: "10.0.0.0/16" | ||
space: "{{ ip_space.id }}" | ||
state: "present" | ||
|
||
- name: Get Next Available Address Block Information by ID | ||
infoblox.bloxone.ipam_next_available_address_block_info: | ||
id: "{{ address_block.id }}" | ||
cidr: 20 | ||
|
||
- name: Get Next Available Address Block Information by ID and Count | ||
infoblox.bloxone.ipam_next_available_address_block_info: | ||
id: "{{ address_block.id }}" | ||
cidr: 24 | ||
count: 5 | ||
""" | ||
|
||
RETURN = r""" | ||
id: | ||
description: | ||
- ID of the AddressBlock object. | ||
type: str | ||
returned: Always | ||
objects: | ||
description: | ||
- List of next available address block's addresses. | ||
type: list | ||
elements: str | ||
returned: Always | ||
""" | ||
|
||
from ansible_collections.infoblox.bloxone.plugins.module_utils.modules import BloxoneAnsibleModule | ||
|
||
try: | ||
from bloxone_client import ApiException | ||
from ipam import AddressBlockApi | ||
except ImportError: | ||
pass # Handled by BloxoneAnsibleModule | ||
|
||
|
||
class NextAvailableAddressBlockInfoModule(BloxoneAnsibleModule): | ||
def __init__(self, *args, **kwargs): | ||
super(NextAvailableAddressBlockInfoModule, self).__init__(*args, **kwargs) | ||
self._existing = None | ||
self._limit = 1000 | ||
|
||
def find(self): | ||
all_results = [] | ||
offset = 0 | ||
while True: | ||
try: | ||
resp = AddressBlockApi(self.client).list_next_available_ab( | ||
id=self.params["id"], cidr=self.params["cidr"], count=self.params["count"] | ||
) | ||
all_results.extend(resp.results) | ||
|
||
if len(resp.results) < self._limit: | ||
break | ||
offset += self._limit | ||
|
||
except ApiException as e: | ||
self.fail_json(msg=f"Failed to execute command: {e.status} {e.reason} {e.body}") | ||
|
||
return all_results | ||
|
||
def run_command(self): | ||
result = dict(objects=[]) | ||
|
||
if self.check_mode: | ||
self.exit_json(**result) | ||
|
||
find_results = self.find() | ||
|
||
all_results = [] | ||
for r in find_results: | ||
# The expected output is a list of addresses as strings. | ||
# Therefore, we extract only the 'address' field from each object in the results. | ||
all_results.append(r.address) | ||
|
||
result["objects"] = all_results | ||
self.exit_json(**result) | ||
|
||
|
||
def main(): | ||
# define available arguments/parameters a user can pass to the module | ||
module_args = dict( | ||
id=dict(type="str", required=True), | ||
cidr=dict(type="int", required=True), | ||
count=dict(type="int", required=False), | ||
) | ||
|
||
module = NextAvailableAddressBlockInfoModule( | ||
argument_spec=module_args, | ||
supports_check_mode=True, | ||
) | ||
module.run_command() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
dependencies: [setup_ip_space] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
will this work, next available can only be created as a child block /20 here is not a child block of /24
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.
Modified address block cidr to 16
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.
what is {{ address_block }} and {{ ip_space }}. They haven't been put in to registers in the task above.
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.
Modified! aded register for ip_space and address_block