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

Next Available Address Block Support for Ansible V2 #58

Merged
merged 13 commits into from
Jan 15, 2025
Merged
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ action_groups:
- ipam_address_block_info
- ipam_host
- ipam_host_info
- ipam_next_available_address_block_info

infra:
- infra_join_token
Expand Down
42 changes: 33 additions & 9 deletions plugins/modules/ipam_address_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,24 +727,34 @@
description:
- "The low threshold value for the percentage of used IP addresses relative to the total IP addresses available in the scope of the object. Thresholds are inclusive in the comparison test."
type: int
next_available_id:
description:
- "The resource identifier for the address block where the next available address block should be generated."
type: str

extends_documentation_fragment:
- infoblox.bloxone.common
""" # noqa: E501

EXAMPLES = r"""
- name: "Create an ip space"
- name: "Create an ip space (required as parent)"
infoblox.bloxone.ipam_ip_space:
name: "my-ip-space"
state: "present"
register: ip_space

- name: "Create an address block"
infoblox.bloxone.ipam_address_block:
address: "10.0.0.0/24"
address: "10.0.0.0/16"
space: "{{ ip_space.id }}"
state: "present"

- name: "Create Next Available Address Block"
infoblox.bloxone.ipam_address_block:
space: "{{ ip_space.id }}"
cidr: 20
next_available_id: "{{ address_block.id }}"
Comment on lines +756 to +757
Copy link
Collaborator

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

Copy link
Author

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

Copy link
Collaborator

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.

Copy link
Author

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

state: "present"

- name: "Delete an Address Block"
infoblox.bloxone.ipam_address_block:
address: "10.0.0.0/16"
Expand Down Expand Up @@ -2349,12 +2359,15 @@
class AddressBlockModule(BloxoneAnsibleModule):
def __init__(self, *args, **kwargs):
super(AddressBlockModule, self).__init__(*args, **kwargs)
self.next_available_id = self.params.get("next_available_id")

if "/" in self.params["address"]:
self.params["address"], netmask = self.params["address"].split("/")
self.params["cidr"] = int(netmask)
# If address is None, next_available_id will be utilized along with a separately provided CIDR value
if self.params["address"] is not None:
if "/" in self.params["address"]:
self.params["address"], netmask = self.params["address"].split("/")
self.params["cidr"] = int(netmask)

exclude = ["state", "csp_url", "api_key", "id"]
exclude = ["state", "csp_url", "api_key", "id", "next_available_id"]
self._payload_params = {k: v for k, v in self.params.items() if v is not None and k not in exclude}
self._payload = AddressBlock.from_dict(self._payload_params)

Expand Down Expand Up @@ -2399,6 +2412,9 @@ def find(self):
return None
raise e
else:
# If address is None, return None, indicating next_available_address block should be created and not updated
if self.params["address"] is None:
return None
filter = f"address=='{self.params['address']}' and space=='{self.params['space']}' and cidr=={self.params['cidr']}"
resp = AddressBlockApi(self.client).list(filter=filter, inherit="full")
if len(resp.results) == 1:
Expand All @@ -2412,6 +2428,11 @@ def create(self):
if self.check_mode:
return None

# If next_available_id is not None, set the address to the next available ID.
if self.next_available_id is not None:
naId = f"{self.next_available_id}/nextavailableaddressblock"
self._payload.address = naId

resp = AddressBlockApi(self.client).create(body=self.payload, inherit="full")
return resp.result.model_dump(by_alias=True, exclude_none=True)

Expand Down Expand Up @@ -2475,7 +2496,8 @@ def main():
module_args = dict(
id=dict(type="str", required=False),
state=dict(type="str", required=False, choices=["present", "absent"], default="present"),
address=dict(type="str"),
address=dict(type="str", required=False),
next_available_id=dict(type="str", required=False),
asm_config=dict(
type="dict",
options=dict(
Expand Down Expand Up @@ -2766,7 +2788,9 @@ def main():
module = AddressBlockModule(
argument_spec=module_args,
supports_check_mode=True,
required_if=[("state", "present", ["address", "space"])],
mutually_exclusive=[["address", "next_available_id"]],
required_if=[("state", "present", ["space"])],
required_one_of=[["address", "next_available_id"]],
)

module.run_command()
Expand Down
5 changes: 2 additions & 3 deletions plugins/modules/ipam_address_block_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@
address: "10.0.0.0/16"
space: "{{ ip_space.id }}"
tags:
location: "{{ tag_value }}"
location: "site-1"
state: "present"
register: address_block

- name: Get Address Block information by ID
infoblox.bloxone.ipam_address_block_info:
Expand All @@ -83,7 +82,7 @@
- name: Get Address Block information by tag filters
infoblox.bloxone.ipam_address_block_info:
tag_filters:
location: "{{ tag_value }}"
location: "site-1"
"""

RETURN = r"""
Expand Down
Loading
Loading