From 1a5c16dafc0d1b569c3ee304ac0ff989547df6b9 Mon Sep 17 00:00:00 2001 From: Anil Gadiyar Date: Wed, 27 Nov 2024 11:40:37 +0530 Subject: [PATCH 1/4] initial commit --- meta/runtime.yml | 2 + plugins/modules/dns_forward_nsg.py | 334 ++++++++++++++++++ plugins/modules/dns_forward_nsg_info.py | 252 +++++++++++++ .../targets/dns_forward_nsg/tasks/main.yml | 207 +++++++++++ .../dns_forward_nsg_info/tasks/main.yml | 59 ++++ 5 files changed, 854 insertions(+) create mode 100644 plugins/modules/dns_forward_nsg.py create mode 100644 plugins/modules/dns_forward_nsg_info.py create mode 100644 tests/integration/targets/dns_forward_nsg/tasks/main.yml create mode 100644 tests/integration/targets/dns_forward_nsg_info/tasks/main.yml diff --git a/meta/runtime.yml b/meta/runtime.yml index 55f479e..322eb57 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -11,6 +11,8 @@ action_groups: - dns_auth_zone_info - dns_forward_zone - dns_forward_zone_info + - dns_forward_nsg + - dns_forward_nsg_info ipam: - ipam_ip_space - ipam_ip_space_info diff --git a/plugins/modules/dns_forward_nsg.py b/plugins/modules/dns_forward_nsg.py new file mode 100644 index 0000000..624af6b --- /dev/null +++ b/plugins/modules/dns_forward_nsg.py @@ -0,0 +1,334 @@ +#!/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: dns_forward_nsg +short_description: Manage ForwardNsg +description: + - Manage ForwardNsg +version_added: 2.0.0 +author: Infoblox Inc. (@infobloxopen) +options: + id: + description: + - ID of the object + type: str + required: false + state: + description: + - Indicate desired state of the object + type: str + required: false + choices: + - present + - absent + default: present + comment: + description: + - "Optional. Comment for the object." + type: str + external_forwarders: + description: + - "Optional. External DNS servers to forward to. Order is not significant." + type: list + elements: dict + suboptions: + address: + description: + - "Server IP address." + type: str + fqdn: + description: + - "Server FQDN." + type: str + forwarders_only: + description: + - "Optional. I(true) to only forward." + type: bool + hosts: + description: + - "The resource identifier." + type: list + elements: str + internal_forwarders: + description: + - "The resource identifier." + type: list + elements: str + name: + description: + - "Name of the object." + type: str + nsgs: + description: + - "The resource identifier." + type: list + elements: str + tags: + description: + - "Tagging specifics." + type: dict + +extends_documentation_fragment: + - infoblox.bloxone.common +""" # noqa: E501 + +EXAMPLES = r""" + - name: Create a Forward NSG + infoblox.bloxone.dns_forward_nsg: + name: "example_nsg" + state: "present" + + - name: Create a Forward NSG with Additional Fields + infoblox.bloxone.dns_forward_nsg: + name: "example_nsg" + comment: "Example Forward NSG" + external_forwarders: + - address: "1.1.1.1" + fqdn: "a.com." + type: "forwarder" + state: "present" + tags: + location: "my-location" + + - name: Delete the Forward NSG + infoblox.bloxone.dns_forward_nsg: + name: "example_nsg" + state: "absent" +""" # noqa: E501 + +RETURN = r""" +id: + description: + - ID of the ForwardNsg object + type: str + returned: Always +item: + description: + - ForwardNsg object + type: complex + returned: Always + contains: + comment: + description: + - "Optional. Comment for the object." + type: str + returned: Always + external_forwarders: + description: + - "Optional. External DNS servers to forward to. Order is not significant." + type: list + returned: Always + elements: dict + contains: + address: + description: + - "Server IP address." + type: str + returned: Always + fqdn: + description: + - "Server FQDN." + type: str + returned: Always + protocol_fqdn: + description: + - "Server FQDN in punycode." + type: str + returned: Always + forwarders_only: + description: + - "Optional. I(true) to only forward." + type: bool + returned: Always + hosts: + description: + - "The resource identifier." + type: list + returned: Always + id: + description: + - "The resource identifier." + type: str + returned: Always + internal_forwarders: + description: + - "The resource identifier." + type: list + returned: Always + name: + description: + - "Name of the object." + type: str + returned: Always + nsgs: + description: + - "The resource identifier." + type: list + returned: Always + tags: + description: + - "Tagging specifics." + type: dict + returned: Always +""" # noqa: E501 + +from ansible_collections.infoblox.bloxone.plugins.module_utils.modules import BloxoneAnsibleModule + +try: + from bloxone_client import ApiException, NotFoundException + from dns_config import ForwardNSG, ForwardNsgApi +except ImportError: + pass # Handled by BloxoneAnsibleModule + + +class ForwardNsgModule(BloxoneAnsibleModule): + def __init__(self, *args, **kwargs): + super(ForwardNsgModule, self).__init__(*args, **kwargs) + + exclude = ["state", "csp_url", "api_key", "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 = ForwardNSG.from_dict(self._payload_params) + self._existing = None + + @property + def existing(self): + return self._existing + + @existing.setter + def existing(self, value): + self._existing = value + + @property + def payload_params(self): + return self._payload_params + + @property + def payload(self): + return self._payload + + def payload_changed(self): + if self.existing is None: + # if existing is None, then it is a create operation + return True + + return self.is_changed(self.existing.model_dump(by_alias=True, exclude_none=True), self.payload_params) + + def find(self): + if self.params["id"] is not None: + try: + resp = ForwardNsgApi(self.client).read(self.params["id"], inherit="full") + return resp.result + except NotFoundException as e: + if self.params["state"] == "absent": + return None + raise e + else: + filter = f"name=='{self.params['name']}'" + resp = ForwardNsgApi(self.client).list(filter=filter) + if len(resp.results) == 1: + return resp.results[0] + if len(resp.results) > 1: + self.fail_json(msg=f"Found multiple ForwardNsg: {resp.results}") + if len(resp.results) == 0: + return None + + def create(self): + if self.check_mode: + return None + + resp = ForwardNsgApi(self.client).create(body=self.payload) + return resp.result.model_dump(by_alias=True, exclude_none=True) + + def update(self): + if self.check_mode: + return None + + resp = ForwardNsgApi(self.client).update(id=self.existing.id, body=self.payload) + return resp.result.model_dump(by_alias=True, exclude_none=True) + + def delete(self): + if self.check_mode: + return + + ForwardNsgApi(self.client).delete(self.existing.id) + + def run_command(self): + result = dict(changed=False, object={}, id=None) + + # based on the state that is passed in, we will execute the appropriate + # functions + try: + self.existing = self.find() + item = {} + if self.params["state"] == "present" and self.existing is None: + item = self.create() + result["changed"] = True + result["msg"] = "ForwardNsg created" + elif self.params["state"] == "present" and self.existing is not None: + if self.payload_changed(): + item = self.update() + result["changed"] = True + result["msg"] = "ForwardNsg updated" + elif self.params["state"] == "absent" and self.existing is not None: + self.delete() + result["changed"] = True + result["msg"] = "ForwardNsg deleted" + + if self.check_mode: + # if in check mode, do not update the result or the diff, just return the changed state + self.exit_json(**result) + + result["diff"] = dict( + before=self.existing.model_dump(by_alias=True, exclude_none=True) if self.existing is not None else {}, + after=item, + ) + result["object"] = item + result["id"] = ( + self.existing.id if self.existing is not None else item["id"] if (item and "id" in item) else None + ) + except ApiException as e: + self.fail_json(msg=f"Failed to execute command: {e.status} {e.reason} {e.body}") + + self.exit_json(**result) + + +def main(): + module_args = dict( + id=dict(type="str", required=False), + state=dict(type="str", required=False, choices=["present", "absent"], default="present"), + comment=dict(type="str"), + external_forwarders=dict( + type="list", + elements="dict", + options=dict( + address=dict(type="str"), + fqdn=dict(type="str"), + ), + ), + forwarders_only=dict(type="bool"), + hosts=dict(type="list", elements="str"), + internal_forwarders=dict(type="list", elements="str"), + name=dict(type="str"), + nsgs=dict(type="list", elements="str"), + tags=dict(type="dict"), + ) + + module = ForwardNsgModule( + argument_spec=module_args, + supports_check_mode=True, + required_if=[("state", "present", ["name"])], + ) + + module.run_command() + + +if __name__ == "__main__": + main() diff --git a/plugins/modules/dns_forward_nsg_info.py b/plugins/modules/dns_forward_nsg_info.py new file mode 100644 index 0000000..de07a64 --- /dev/null +++ b/plugins/modules/dns_forward_nsg_info.py @@ -0,0 +1,252 @@ +#!/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: dns_forward_nsg_info +short_description: Manage ForwardNsg +description: + - Manage ForwardNsg +version_added: 2.0.0 +author: Infoblox Inc. (@infobloxopen) +options: + id: + description: + - ID of the object + type: str + required: false + filters: + description: + - Filter dict to filter objects + type: dict + required: false + filter_query: + description: + - Filter query to filter objects + type: str + required: false + inherit: + description: + - Return inheritance information + type: str + required: false + choices: + - full + - partial + - none + default: full + tag_filters: + description: + - Filter dict to filter objects by tags + type: dict + required: false + tag_filter_query: + description: + - Filter query to filter objects by tags + type: str + required: false + +extends_documentation_fragment: + - infoblox.bloxone.common +""" # noqa: E501 + +EXAMPLES = r""" + - name: Get Forward NSG information by ID + infoblox.bloxone.dns_forward_nsg_info: + id: "{{ forward_nsg_id }}" + + - name: Get Forward NSG information by filters (e.g. name) + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "example_nsg" + + - name: Get Forward NSG information by raw filter query + infoblox.bloxone.dns_forward_nsg_info: + filter_query: "name=='example_nsg'" + + - name: Get Forward NSG information by tag filters + infoblox.bloxone.dns_forward_nsg_info: + tag_filters: + location: "site-1" +""" # noqa: E501 + +RETURN = r""" +id: + description: + - ID of the ForwardNsg object + type: str + returned: Always +objects: + description: + - ForwardNsg object + type: list + elements: dict + returned: Always + contains: + comment: + description: + - "Optional. Comment for the object." + type: str + returned: Always + external_forwarders: + description: + - "Optional. External DNS servers to forward to. Order is not significant." + type: list + returned: Always + elements: dict + contains: + address: + description: + - "Server IP address." + type: str + returned: Always + fqdn: + description: + - "Server FQDN." + type: str + returned: Always + protocol_fqdn: + description: + - "Server FQDN in punycode." + type: str + returned: Always + forwarders_only: + description: + - "Optional. I(true) to only forward." + type: bool + returned: Always + hosts: + description: + - "The resource identifier." + type: list + returned: Always + id: + description: + - "The resource identifier." + type: str + returned: Always + internal_forwarders: + description: + - "The resource identifier." + type: list + returned: Always + name: + description: + - "Name of the object." + type: str + returned: Always + nsgs: + description: + - "The resource identifier." + type: list + returned: Always + tags: + description: + - "Tagging specifics." + type: dict + returned: Always +""" # noqa: E501 + +from ansible_collections.infoblox.bloxone.plugins.module_utils.modules import BloxoneAnsibleModule + +try: + from bloxone_client import ApiException, NotFoundException + from dns_config import ForwardNsgApi +except ImportError: + pass # Handled by BloxoneAnsibleModule + + +class ForwardNsgInfoModule(BloxoneAnsibleModule): + def __init__(self, *args, **kwargs): + super(ForwardNsgInfoModule, self).__init__(*args, **kwargs) + self._existing = None + self._limit = 1000 + + def find_by_id(self): + try: + resp = ForwardNsgApi(self.client).read(self.params["id"], inherit="full") + return [resp.result] + except NotFoundException as e: + return None + + def find(self): + if self.params["id"] is not None: + return self.find_by_id() + + filter_str = None + if self.params["filters"] is not None: + filter_str = " and ".join([f"{k}=='{v}'" for k, v in self.params["filters"].items()]) + elif self.params["filter_query"] is not None: + filter_str = self.params["filter_query"] + + tag_filter_str = None + if self.params["tag_filters"] is not None: + tag_filter_str = " and ".join([f"{k}=='{v}'" for k, v in self.params["tag_filters"].items()]) + elif self.params["tag_filter_query"] is not None: + tag_filter_str = self.params["tag_filter_query"] + + all_results = [] + offset = 0 + + while True: + try: + resp = ForwardNsgApi(self.client).list( + offset=offset, limit=self._limit, filter=filter_str, tfilter=tag_filter_str + ) + 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: + all_results.append(r.model_dump(by_alias=True, exclude_none=True)) + + 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=False), + filters=dict(type="dict", required=False), + filter_query=dict(type="str", required=False), + inherit=dict(type="str", required=False, choices=["full", "partial", "none"], default="full"), + tag_filters=dict(type="dict", required=False), + tag_filter_query=dict(type="str", required=False), + ) + + module = ForwardNsgInfoModule( + argument_spec=module_args, + supports_check_mode=True, + mutually_exclusive=[ + ["id", "filters", "filter_query"], + ["id", "tag_filters", "tag_filter_query"], + ], + ) + module.run_command() + + +if __name__ == "__main__": + main() diff --git a/tests/integration/targets/dns_forward_nsg/tasks/main.yml b/tests/integration/targets/dns_forward_nsg/tasks/main.yml new file mode 100644 index 0000000..fb5a7e1 --- /dev/null +++ b/tests/integration/targets/dns_forward_nsg/tasks/main.yml @@ -0,0 +1,207 @@ +--- + +#TODO: add tests +# - nsgs + +- module_defaults: + group/infoblox.bloxone.all: + csp_url: "{{ csp_url }}" + api_key: "{{ api_key }}" + block: + # Create a random Forward Zone name to avoid conflicts + - ansible.builtin.set_fact: + forward_nsg_name: "test-forward-nsg-{{ 999999 | random | string }}.com." + + - name: Create an Forward NSG (check mode) + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: present + check_mode: true + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg is changed + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 0 + + - name: Create an Forward NSG + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: present + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg is changed + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 1 + + - name: Create a Forward NSG (idempotent) + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: present + register: forward_nsg + - assert: + that: + - forward_nsg is not changed + - forward_nsg is not failed + + - name: Delete the Forward NSG (check mode) + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: absent + check_mode: true + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg is changed + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 1 + + - name: Delete the Forward NSG + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: absent + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg is changed + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 0 + + - name: Delete the Forward NSG (idempotent) + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: absent + register: forward_nsg + - assert: + that: + - forward_nsg is not changed + - forward_nsg is not failed + + - name: Create a Forward NSG with a comment + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + comment: "Test Comment" + state: present + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + - forward_nsg_info.objects[0].comment == "Test Comment" + + - name: Create a Forward NSG with External Forwarders + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + external_forwarders: + - address: "192.168.1.0" + state: present + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg is not failed + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + - forward_nsg_info.objects[0].external_forwarders | length == 1 + - forward_nsg_info.objects[0].external_forwarders[0].address == "192.168.1.0" + + - name: Create a Forward NSG with Address and FQDN External Forwarders + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + external_forwarders: + - address: "192.168.1.0" + fqdn: "terraform-acc-forward-ext." + state: present + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg is not failed + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + - forward_nsg_info.objects[0].external_forwarders | length == 1 + - forward_nsg_info.objects[0].external_forwarders[0].address == "192.168.1.0" + - forward_nsg_info.objects[0].external_forwarders[0].fqdn == "terraform-acc-forward-ext." + + - name: Create a Forward NSG with Forwarders Only Enabled + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + forwarders_only: true + state: present + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg is not failed + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + - forward_nsg_info.objects[0].forwarders_only == true + + - name: Create a Forward NSG with Tags + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + comment: "Test Comment" + tags: + location: "site-1" + state: present + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg_info is not failed + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + - forward_nsg_info.objects[0].tags.location == "site-1" + + always: + - name: "Delete Forward NSG" + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: "absent" + ignore_errors: true diff --git a/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml new file mode 100644 index 0000000..740ec68 --- /dev/null +++ b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml @@ -0,0 +1,59 @@ +--- +- module_defaults: + group/infoblox.bloxone.all: + csp_url: "{{ csp_url }}" + api_key: "{{ api_key }}" + block: + # Create a random Auth Zone name to avoid conflicts + - ansible.builtin.set_fact: + forward_nsg_name: "test-forward-nsg-{{ 999999 | random | string }}.com." + tag_value: "site-{{ 999999 | random | string }}" + + - name: Create a Forward NSG + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: present + register: forward_nsg + - name: Get Information about the Forward NSG + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].name == forward_nsg.object.name + + - name: Get Forward NSG information by filters (Name) + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_name }}" + register: forward_nsg_info + - assert: + that: + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + + - name: Get Forward NSG information by filter query + infoblox.bloxone.dns_forward_nsg_info: + filter_query: "name=='{{ forward_nsg_name }}'" + - assert: + that: + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + + - name: Get Forward NSG information by tag filters + infoblox.bloxone.dns_forward_nsg_info: + tag_filters: + location: "{{ tag_value }}" + - assert: + that: + - forward_nsg_info.objects | length == 1 + - forward_nsg_info.objects[0].id == forward_nsg.id + + always: + - name: "Delete Forward NSG" + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: "absent" + ignore_errors: true From a9c93fd564b67ba025b28beeff53b5633b5354f1 Mon Sep 17 00:00:00 2001 From: Anil Gadiyar Date: Wed, 11 Dec 2024 21:29:39 +0530 Subject: [PATCH 2/4] addressed PR review --- plugins/modules/dns_forward_nsg.py | 2 +- plugins/modules/dns_forward_nsg_info.py | 13 +---- .../targets/dns_forward_nsg/tasks/main.yml | 50 ++++++++++++++++++- .../dns_forward_nsg_info/tasks/main.yml | 1 + 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/plugins/modules/dns_forward_nsg.py b/plugins/modules/dns_forward_nsg.py index 624af6b..7288f2a 100644 --- a/plugins/modules/dns_forward_nsg.py +++ b/plugins/modules/dns_forward_nsg.py @@ -224,7 +224,7 @@ def payload_changed(self): def find(self): if self.params["id"] is not None: try: - resp = ForwardNsgApi(self.client).read(self.params["id"], inherit="full") + resp = ForwardNsgApi(self.client).read(self.params["id"]) return resp.result except NotFoundException as e: if self.params["state"] == "absent": diff --git a/plugins/modules/dns_forward_nsg_info.py b/plugins/modules/dns_forward_nsg_info.py index de07a64..eaaa1ee 100644 --- a/plugins/modules/dns_forward_nsg_info.py +++ b/plugins/modules/dns_forward_nsg_info.py @@ -31,16 +31,6 @@ - Filter query to filter objects type: str required: false - inherit: - description: - - Return inheritance information - type: str - required: false - choices: - - full - - partial - - none - default: full tag_filters: description: - Filter dict to filter objects by tags @@ -170,7 +160,7 @@ def __init__(self, *args, **kwargs): def find_by_id(self): try: - resp = ForwardNsgApi(self.client).read(self.params["id"], inherit="full") + resp = ForwardNsgApi(self.client).read(self.params["id"]) return [resp.result] except NotFoundException as e: return None @@ -232,7 +222,6 @@ def main(): id=dict(type="str", required=False), filters=dict(type="dict", required=False), filter_query=dict(type="str", required=False), - inherit=dict(type="str", required=False, choices=["full", "partial", "none"], default="full"), tag_filters=dict(type="dict", required=False), tag_filter_query=dict(type="str", required=False), ) diff --git a/tests/integration/targets/dns_forward_nsg/tasks/main.yml b/tests/integration/targets/dns_forward_nsg/tasks/main.yml index fb5a7e1..0bcc345 100644 --- a/tests/integration/targets/dns_forward_nsg/tasks/main.yml +++ b/tests/integration/targets/dns_forward_nsg/tasks/main.yml @@ -11,6 +11,7 @@ # Create a random Forward Zone name to avoid conflicts - ansible.builtin.set_fact: forward_nsg_name: "test-forward-nsg-{{ 999999 | random | string }}.com." + forward_nsg_secondary_name: "test-forward-nsg-{{ 999999 | random | string }}.com." - name: Create an Forward NSG (check mode) infoblox.bloxone.dns_forward_nsg: @@ -142,7 +143,7 @@ name: "{{ forward_nsg_name }}" external_forwarders: - address: "192.168.1.0" - fqdn: "terraform-acc-forward-ext." + fqdn: "test_external_forwarder." state: present register: forward_nsg - name: Get Information about the Forward NSG @@ -158,7 +159,7 @@ - forward_nsg_info.objects[0].id == forward_nsg.id - forward_nsg_info.objects[0].external_forwarders | length == 1 - forward_nsg_info.objects[0].external_forwarders[0].address == "192.168.1.0" - - forward_nsg_info.objects[0].external_forwarders[0].fqdn == "terraform-acc-forward-ext." + - forward_nsg_info.objects[0].external_forwarders[0].fqdn == "test_external_forwarder." - name: Create a Forward NSG with Forwarders Only Enabled infoblox.bloxone.dns_forward_nsg: @@ -199,9 +200,54 @@ - forward_nsg_info.objects[0].id == forward_nsg.id - forward_nsg_info.objects[0].tags.location == "site-1" + - name: Create Primary Forward NSG + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_name }}" + state: present + register: forward_nsg + - name: Create Secondary Forward NSG referencing Primary + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_secondary_name }}" + nsgs: + - "{{ forward_nsg.id }}" + state: present + register: secondary_forward_nsg + - name: Validate Secondary Forward NSG Creation + infoblox.bloxone.dns_forward_nsg_info: + filters: + name: "{{ forward_nsg_secondary_name }}" + register: ssecondary_forward_nsg_info + - assert: + that: + - ssecondary_forward_nsg_info is not failed + - ssecondary_forward_nsg_info.objects | length == 1 + - ssecondary_forward_nsg_info.objects[0].nsgs | length == 1 + - ssecondary_forward_nsg_info.objects[0].nsgs[0] == forward_nsg.id + # Unlinking block + - block: + - name: Unlink Primary NSG from Secondary + infoblox.bloxone.dns_forward_nsg: + id: "{{ secondary_forward_nsg.id }}" + name: "{{ forward_nsg_secondary_name }}" + nsgs: [ ] + state: present + rescue: + - name: Cleanup in case of failure (force unlink Secondary from Primary) + infoblox.bloxone.dns_forward_nsg: + id: "{{ secondary_forward_nsg.id | default('') }}" + nsgs: [ ] + state: present + when: secondary_forward_nsg is defined + always: - name: "Delete Forward NSG" infoblox.bloxone.dns_forward_nsg: name: "{{ forward_nsg_name }}" state: "absent" ignore_errors: true + + - name: "Delete Secondary Forward NSG" + infoblox.bloxone.dns_forward_nsg: + name: "{{ forward_nsg_secondary_name }}" + state: "absent" + ignore_errors: true diff --git a/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml index 740ec68..27cb1a1 100644 --- a/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml +++ b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml @@ -57,3 +57,4 @@ name: "{{ forward_nsg_name }}" state: "absent" ignore_errors: true + From 907d8d72d1215986cfae0a866dee989c7a738e57 Mon Sep 17 00:00:00 2001 From: Anil Gadiyar Date: Fri, 13 Dec 2024 17:05:34 +0530 Subject: [PATCH 3/4] lint error --- tests/integration/targets/dns_forward_nsg_info/tasks/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml index 27cb1a1..740ec68 100644 --- a/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml +++ b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml @@ -57,4 +57,3 @@ name: "{{ forward_nsg_name }}" state: "absent" ignore_errors: true - From fd1811f066f9aafd8d34f104e51212ca3d967328 Mon Sep 17 00:00:00 2001 From: Anil Gadiyar Date: Mon, 16 Dec 2024 13:21:08 +0530 Subject: [PATCH 4/4] addressed review comments --- plugins/modules/dns_forward_nsg.py | 2 +- .../targets/dns_forward_nsg/tasks/main.yml | 31 +++---------------- .../dns_forward_nsg_info/tasks/main.yml | 1 + 3 files changed, 6 insertions(+), 28 deletions(-) diff --git a/plugins/modules/dns_forward_nsg.py b/plugins/modules/dns_forward_nsg.py index 7288f2a..6b9a0cd 100644 --- a/plugins/modules/dns_forward_nsg.py +++ b/plugins/modules/dns_forward_nsg.py @@ -96,7 +96,7 @@ type: "forwarder" state: "present" tags: - location: "my-location" + location: "site-1" - name: Delete the Forward NSG infoblox.bloxone.dns_forward_nsg: diff --git a/tests/integration/targets/dns_forward_nsg/tasks/main.yml b/tests/integration/targets/dns_forward_nsg/tasks/main.yml index 0bcc345..501eaf5 100644 --- a/tests/integration/targets/dns_forward_nsg/tasks/main.yml +++ b/tests/integration/targets/dns_forward_nsg/tasks/main.yml @@ -1,8 +1,5 @@ --- -#TODO: add tests -# - nsgs - - module_defaults: group/infoblox.bloxone.all: csp_url: "{{ csp_url }}" @@ -200,11 +197,6 @@ - forward_nsg_info.objects[0].id == forward_nsg.id - forward_nsg_info.objects[0].tags.location == "site-1" - - name: Create Primary Forward NSG - infoblox.bloxone.dns_forward_nsg: - name: "{{ forward_nsg_name }}" - state: present - register: forward_nsg - name: Create Secondary Forward NSG referencing Primary infoblox.bloxone.dns_forward_nsg: name: "{{ forward_nsg_secondary_name }}" @@ -223,31 +215,16 @@ - ssecondary_forward_nsg_info.objects | length == 1 - ssecondary_forward_nsg_info.objects[0].nsgs | length == 1 - ssecondary_forward_nsg_info.objects[0].nsgs[0] == forward_nsg.id - # Unlinking block - - block: - - name: Unlink Primary NSG from Secondary - infoblox.bloxone.dns_forward_nsg: - id: "{{ secondary_forward_nsg.id }}" - name: "{{ forward_nsg_secondary_name }}" - nsgs: [ ] - state: present - rescue: - - name: Cleanup in case of failure (force unlink Secondary from Primary) - infoblox.bloxone.dns_forward_nsg: - id: "{{ secondary_forward_nsg.id | default('') }}" - nsgs: [ ] - state: present - when: secondary_forward_nsg is defined always: - - name: "Delete Forward NSG" + - name: "Delete Secondary Forward NSG" infoblox.bloxone.dns_forward_nsg: - name: "{{ forward_nsg_name }}" + name: "{{ forward_nsg_secondary_name }}" state: "absent" ignore_errors: true - - name: "Delete Secondary Forward NSG" + - name: "Delete Forward NSG" infoblox.bloxone.dns_forward_nsg: - name: "{{ forward_nsg_secondary_name }}" + name: "{{ forward_nsg_name }}" state: "absent" ignore_errors: true diff --git a/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml index 740ec68..f0cb3cd 100644 --- a/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml +++ b/tests/integration/targets/dns_forward_nsg_info/tasks/main.yml @@ -14,6 +14,7 @@ name: "{{ forward_nsg_name }}" state: present register: forward_nsg + - name: Get Information about the Forward NSG infoblox.bloxone.dns_forward_nsg_info: filters: