Skip to content

Commit

Permalink
BloxoneAnsibleModule util and IP space module (#32)
Browse files Browse the repository at this point in the history
* ip space

* add integration action

* change name to object/objects

* fix generated docs

* some documentation fixes

* deprecate b1_ipam_ip_space and b1_ipam_ip_space_gather

* minor fix
  • Loading branch information
mathewab authored Jun 7, 2024
1 parent 52ee8bf commit 4b34551
Show file tree
Hide file tree
Showing 12 changed files with 5,164 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: integration

on:
push:
branches:
- v2
schedule:
# every day at 02:00 UTC
- cron: '0 2 * * *'

jobs:
integration:
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v4
with:
path: infoblox.bloxone

- name: Set up integration_config.yml
run: |
echo "api_key: ${{ secrets.BLOXONE_API_KEY }}" > infoblox.bloxone/tests/integration/integration_config.yml
echo "csp_url: ${{ secrets.BLOXONE_CSP_URL }}" >> infoblox.bloxone/tests/integration/integration_config.yml
- uses: ansible-community/[email protected]
with:
collection-src-directory: infoblox.bloxone
ansible-core-version: 'stable-2.15'
target-python-version: '3.11'
testing-type: integration
3 changes: 3 additions & 0 deletions changelogs/fragments/32-ip-space.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
deprecated_features:
- b1_ipam_ip_space - is deprecated in favor of `ipam_ip_space`.
- b1_ipam_ip_space_gather - is deprecated in favor of `ipam_ip_space_info`.
19 changes: 19 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
action_groups:
all:
- metadata:
extend_group:
- ipam
ipam:
- ipam_ip_space
- ipam_ip_space_info

plugin_routing:
modules:
b1_ipam_ip_space:
deprecation:
removal_version: 3.0.0
warning_text: Use infoblox.bloxone.ipam_ip_space instead.
b1_ipam_ip_space_gather:
deprecation:
removal_version: 3.0.0
warning_text: Use infoblox.bloxone.ipam_ip_space_info instead.
22 changes: 22 additions & 0 deletions plugins/doc_fragments/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-

# Copyright: Infoblox
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


class ModuleDocFragment:
DOCUMENTATION = r"""
options:
api_key:
description:
- The API token for authentication against Infoblox BloxOne API. If not set, the environment variable E(BLOXONE_API_KEY) will be used.
type: str
aliases: [ bloxone_api_key ]
csp_url:
description:
- The Infoblox Cloud Services Portal (CSP) URL. If not set, the environment variable E(BLOXONE_CSP_URL) will be used.
type: str
aliases: [ bloxone_csp_url ]
default: 'https://csp.infoblox.com'
"""
115 changes: 115 additions & 0 deletions plugins/module_utils/modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 Infoblox
# 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

import traceback

from ansible.module_utils.basic import AnsibleModule, env_fallback, missing_required_lib

try:
import bloxone_client

HAS_BLOXONE_CLIENT = True
BLOXONE_CLIENT_IMP_ERR = None
except ImportError:
HAS_BLOXONE_CLIENT = False
BLOXONE_CLIENT_IMP_ERR = traceback.format_exc()


class BloxoneAnsibleModule(AnsibleModule):
def __init__(self, *args, **kwargs):
# Add common arguments to the module argument_spec
args_full = bloxone_client_common_argument_spec()
try:
args_full.update(kwargs["argument_spec"])
except (TypeError, NameError):
pass
kwargs["argument_spec"] = args_full

super(BloxoneAnsibleModule, self).__init__(*args, **kwargs)
self._client = None

if not HAS_BLOXONE_CLIENT:
self.fail_json(
msg=missing_required_lib(
"bloxone_client",
url="https://github.com/infobloxopen/bloxone-python-client",
),
exception=BLOXONE_CLIENT_IMP_ERR,
)

@property
def client(self):
if not self._client:
self._client = _get_client(self)

return self._client

def is_changed(self, existing, payload):
return _is_changed(existing, payload)


def bloxone_client_common_argument_spec():
return dict(
api_key=dict(
type="str", aliases=["bloxone_api_key"], fallback=(env_fallback, ["BLOXONE_API_KEY"]), no_log=True
),
csp_url=dict(
type="str",
aliases=["bloxone_csp_url"],
fallback=(env_fallback, ["BLOXONE_CSP_URL"]),
default="https://csp.infoblox.com",
),
)


def _get_client(module):
config = _get_client_config(module)
client = bloxone_client.ApiClient(config)
return client


def _get_client_config(module):
csp_url = module.params.get("csp_url")
api_key = module.params.get("api_key")

# Use None for empty values, so that the client can handle it
if not csp_url:
csp_url = None
if not api_key:
api_key = None

config = bloxone_client.Configuration(
csp_url=csp_url,
api_key=api_key,
client_name="ansible",
)
config.debug = True
return config


def _is_changed(existing, payload):
"""
Check if the existing object is different from the payload.
The payload keys that are not none are considered for comparison, others are ignored.
If the value is a complex object, the comparison is done recursively.
:param existing:
:param payload:
:return:
"""
changed = False
for k, v in payload.items():
if v is not None:
if isinstance(v, dict):
changed = _is_changed(existing[k], v)
elif existing[k] != v:
changed = True
if changed:
break

return changed
4 changes: 4 additions & 0 deletions plugins/modules/b1_ipam_ip_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
author: "Amit Mishra (@amishra), Sriram Kannan(@kannans)"
short_description: Configure IP space on Infoblox BloxOne DDI
version_added: "1.0.1"
deprecated:
removed_in: 3.0.0
why: This module is deprecated and will be removed in version 3.0.0. Use M(infoblox.bloxone.ipam_ip_space) instead.
alternative: Use M(infoblox.bloxone.ipam_ip_space) instead.
description:
- Create, Update and Delete IP spaces on Infoblox BloxOne DDI. This module manages the IPAM IP space object using BloxOne REST APIs.
requirements:
Expand Down
4 changes: 4 additions & 0 deletions plugins/modules/b1_ipam_ip_space_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
contributor: "Chris Marrison (@ccmarris)"
short_description: Configure IP space on Infoblox BloxOne DDI
version_added: "1.1.0"
deprecated:
removed_in: 3.0.0
why: This module is deprecated and will be removed in version 3.0.0. Use M(infoblox.bloxone.ipam_ip_space) instead.
alternative: Use M(infoblox.bloxone.ipam_ip_space) instead.
description:
- Gather facts about IP spaces in Infoblox BloxOne DDI. This module manages the gather fact of IPAM IP space object using BloxOne REST APIs.
requirements:
Expand Down
Loading

0 comments on commit 4b34551

Please sign in to comment.