Skip to content

Commit

Permalink
Adding protection policies module dev
Browse files Browse the repository at this point in the history
  • Loading branch information
george-ghawali committed Dec 30, 2024
1 parent 031ee19 commit 1c6b356
Show file tree
Hide file tree
Showing 5 changed files with 713 additions and 0 deletions.
2 changes: 2 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,5 @@ action_groups:
- ntnx_storage_containers_stats_v2
- ntnx_storage_containers_info_v2
- ntnx_storage_containers_v2
- ntnx_protection_policies_info_v2
- ntnx_protection_policies_v2
13 changes: 13 additions & 0 deletions plugins/module_utils/v4/data_protection/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SDK_IMP_ERROR = None
try:
import ntnx_dataprotection_py_client
import ntnx_datapolicies_py_client
except ImportError:
SDK_IMP_ERROR = traceback.format_exc()

Expand Down Expand Up @@ -70,3 +71,15 @@ def get_recovery_point_api_instance(module):
"""
client = get_api_client(module)
return ntnx_dataprotection_py_client.RecoveryPointsApi(client)


def get_protection_policies_api_instance(module):
"""
This method will return data protection api instance.
Args:
module (object): Ansible module object
Returns:
api_instance (object): data protection api instance
"""
client = get_api_client(module)
return ntnx_datapolicies_py_client.ProtectionPoliciesApi(client)
19 changes: 19 additions & 0 deletions plugins/module_utils/v4/data_protection/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,22 @@ def get_recovery_point(module, api_instance, ext_id):
exception=e,
msg="Api Exception raised while fetching recovery point info using ext_id",
)

def get_protection_policy(module, api_instance, ext_id):
"""
This method will return protection policy info using external ID.
Args:
module: Ansible module
api_instance: ProtectionPoliciesApi instance from ntnx_datapolicies_py_clien sdk
ext_id (str): top level recovery point external ID
Returns:
protection_policy_info (object): protection policy info
"""
try:
return api_instance.get_protection_policy_by_id(extId=ext_id).data
except Exception as e:
raise_api_exception(
module=module,
exception=e,
msg="Api Exception raised while fetching protection policy info using ext_id",
)
173 changes: 173 additions & 0 deletions plugins/modules/ntnx_protection_policies_info_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2024, Nutanix
# 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: ntnx_protection_policies_info_v2
short_description: Fetch protection policies info in Nutanix Prism Central
description:
- This module allows you to fetch protection policies info in Nutanix Prism Central.
- This module uses PC v4 APIs based SDKs
options:
ext_id:
description:
- The external identifier of the protection policy.
type: str
extends_documentation_fragment:
- nutanix.ncp.ntnx_credentials
- nutanix.ncp.ntnx_info_v2
author:
- George Ghawali (@george-ghawali)
"""
EXAMPLES = r"""
- name: Fetch details for a protection policy using External ID
nutanix.ncp.ntnx_protection_policies_info_v2:
nutanix_host: "{{ ip }}"
nutanix_username: "{{ username }}"
nutanix_password: "{{ password }}"
ext_id: "1ca2963d-77b6-453a-ae23-2c19e7a954a3"
register: result
- name: List all protection policies
nutanix.ncp.ntnx_protection_policies_info_v2:
nutanix_host: "{{ ip }}"
nutanix_username: "{{ username }}"
nutanix_password: "{{ password }}"
register: result
- name: Fetch details for a protection policy using filter
nutanix.ncp.ntnx_protection_policies_info_v2:
nutanix_host: "{{ ip }}"
nutanix_username: "{{ username }}"
nutanix_password: "{{ password }}"
filter: extId eq '840c6c3f-2b01-47d5-81fb-b285e45e89ba'
register: result
ignore_errors: true
"""
RETURN = r"""
response:
description:
- Response for fetching protection policies info
- One protection policy info if External ID is provided
- List of multiple protection policies info if External ID is not provided
returned: always
type: dict
changed:
description: This indicates whether the task resulted in any changes
returned: always
type: bool
sample: false
error:
description: This field typically holds information about if the task have errors that occurred during the task execution
type: str
returned: when an error occurs
sample: null
failed:
description: This field typically holds information about if the task have failed
returned: always
type: bool
sample: false
ext_id:
description: External ID of the protection policy
type: str
returned: when external ID is provided
sample: "1ca2963d-77b6-453a-ae23-2c19e7a954a3"
"""

import warnings # noqa: E402

from ..module_utils.utils import remove_param_with_none_value # noqa: E402
from ..module_utils.v4.base_info_module import BaseInfoModule # noqa: E402
from ..module_utils.v4.data_protection.api_client import ( # noqa: E402
get_protection_policies_api_instance,
)
from ..module_utils.v4.data_protection.helpers import ( # noqa: E402
get_protection_policy,
)
from ..module_utils.v4.spec_generator import SpecGenerator # noqa: E402
from ..module_utils.v4.utils import ( # noqa: E402
raise_api_exception,
strip_internal_attributes,
)

# Suppress the InsecureRequestWarning
warnings.filterwarnings("ignore", message="Unverified HTTPS request is being made")


def get_module_spec():

module_args = dict(
ext_id=dict(type="str"),
)

return module_args


def get_protection_policy_using_ext_id(module, protection_policies, result):
ext_id = module.params.get("ext_id")
resp = get_protection_policy(module, protection_policies, ext_id)
result["ext_id"] = ext_id
result["response"] = strip_internal_attributes(resp.to_dict())


def get_protection_policies(module, protection_policies, result):

sg = SpecGenerator(module)
kwargs, err = sg.get_info_spec(attr=module.params)

if err:
result["error"] = err
module.fail_json(
msg="Failed generating protection policies info Spec", **result
)

try:
resp = protection_policies.list_protection_policies(**kwargs)
except Exception as e:
raise_api_exception(
module=module,
exception=e,
msg="Api Exception raised while fetching protection policies info",
)

resp = strip_internal_attributes(resp.to_dict()).get("data")
if not resp:
resp = []
result["response"] = resp


def run_module():
module = BaseInfoModule(
argument_spec=get_module_spec(),
supports_check_mode=False,
mutually_exclusive=[
("ext_id", "filter"),
],
)
remove_param_with_none_value(module.params)
result = {"changed": False, "error": None, "response": None}
protection_policies = get_protection_policies_api_instance(module)
if module.params.get("ext_id"):
get_protection_policy_using_ext_id(module, protection_policies, result)
else:
get_protection_policies(module, protection_policies, result)
module.exit_json(**result)


def main():
run_module()


if __name__ == "__main__":
main()
Loading

0 comments on commit 1c6b356

Please sign in to comment.