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

All Modules under Prism namespace #571

Open
wants to merge 16 commits into
base: release/2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions examples/prism_v2/prism.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
---
- name: Prism playbook
hosts: localhost
gather_facts: false
module_defaults:
group/nutanix.ncp.ntnx:
nutanix_host: <pc_ip>
nutanix_username: <user>
nutanix_password: <pass>
validate_certs: false
tasks:
- name: Setting Variables
ansible.builtin.set_fact:
cluster:
uuid: "00095bb3-1234-1122-5312-ac1f6b6f97e2"
ip_pe: "10.0.0.1"

- name: List all clusters to get prism central external ID
nutanix.ncp.ntnx_clusters_info_v2:
filter: "config/clusterFunction/any(t:t eq Clustermgmt.Config.ClusterFunctionRef'PRISM_CENTRAL')"
register: result
ignore_errors: true

- name: Get prism central external ID
ansible.builtin.set_fact:
domain_manager_ext_id: "{{ result.response[0].ext_id }}"

- name: Create backup target cluster
nutanix.ncp.ntnx_pc_backup_target_v2:
domain_manager_ext_id: "{{ domain_manager_ext_id }}"
location:
cluster_location:
config:
ext_id: "{{ cluster.uuid }}"
register: result
ignore_errors: true

- name: List all backup targets and set backup target external ID
nutanix.ncp.ntnx_pc_backup_target_info_v2:
domain_manager_ext_id: "{{ domain_manager_ext_id }}"
register: result
ignore_errors: true

- name: Set backup target cluster external ID
ansible.builtin.set_fact:
backup_target_ext_id: "{{ result.response[0].ext_id }}"

- name: Create restore source cluster
nutanix.ncp.ntnx_pc_restore_source_v2:
nutanix_host: "{{ ip_pe }}"
location:
cluster_location:
config:
ext_id: "{{ cluster.uuid }}"
register: result
ignore_errors: true

- name: Get restore source cluster
nutanix.ncp.ntnx_pc_restore_source_info_v2:
nutanix_host: "{{ ip_pe }}"
ext_id: "{{ result.response.ext_id }}"
register: result
ignore_errors: true

- name: Set restore source cluster external ID
ansible.builtin.set_fact:
restore_source_ext_id: "{{ result.response.ext_id }}"

- name: List all backup targets
nutanix.ncp.ntnx_pc_backup_target_info_v2:
domain_manager_ext_id: "{{ domain_manager_ext_id }}"
register: result
ignore_errors: true

- name: List all backup targets with filter
nutanix.ncp.ntnx_pc_backup_target_info_v2:
domain_manager_ext_id: "{{ domain_manager_ext_id }}"
filter: location/clusterLocation/config/ext_id eq '{{ cluster.uuid }}'
register: result
ignore_errors: true

- name: List all backup targets with limit
nutanix.ncp.ntnx_pc_backup_target_info_v2:
domain_manager_ext_id: "{{ domain_manager_ext_id }}"
limit: 1
register: result
ignore_errors: true

- name: Fetch backup target details using external ID
nutanix.ncp.ntnx_pc_backup_target_info_v2:
domain_manager_ext_id: "{{ domain_manager_ext_id }}"
ext_id: "{{ backup_target_ext_id }}"
register: result
ignore_errors: true

- name: Delete restore source cluster
nutanix.ncp.ntnx_pc_restore_source_v2:
nutanix_host: "{{ ip_pe }}"
ext_id: "{{ restore_source_ext_id }}"
state: absent
register: result
ignore_errors: true

- name: Delete backup target cluster
nutanix.ncp.ntnx_pc_backup_target_v2:
ext_id: "{{ backup_target_ext_id }}"
domain_manager_ext_id: "{{ domain_manager_ext_id }}"
state: absent
register: result
ignore_errors: true

- name: List all PCs
nutanix.ncp.ntnx_pc_config_info_v2:
register: result
ignore_errors: true

- name: Set PC external ID and name
ansible.builtin.set_fact:
pc_external_id: "{{ result.response[0].ext_id }}"
pc_name: "{{ result.response[0].config.name }}"

- name: List all PCs with filter
nutanix.ncp.ntnx_pc_config_info_v2:
filter: name eq '{{ pc_name }}'
register: result
ignore_errors: true

- name: List all PCs with limit
nutanix.ncp.ntnx_pc_config_info_v2:
limit: 1
register: result
ignore_errors: true

- name: Fetch PC details using external ID
nutanix.ncp.ntnx_pc_config_info_v2:
ext_id: "{{ pc_external_id }}"
register: result
ignore_errors: true
10 changes: 10 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ action_groups:
- ntnx_storage_containers_stats_v2
- ntnx_storage_containers_info_v2
- ntnx_storage_containers_v2
- ntnx_pc_unregistration_v2
- ntnx_pc_backup_target_info_v2
- ntnx_pc_backup_target_v2
- ntnx_pc_config_info_v2
- ntnx_pc_deploy_v2
- ntnx_pc_restore_v2
- ntnx_pc_restore_source_info_v2
- ntnx_pc_restore_source_v2
- ntnx_pc_restorable_domain_managers_info_v2
- ntnx_pc_restore_points_info_v2
31 changes: 31 additions & 0 deletions plugins/module_utils/v4/pe/base_info_module.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this file

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright: 2021, Ansible Project
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause )
from __future__ import absolute_import, division, print_function

from copy import deepcopy

from .base_module import BasePEModule

__metaclass__ = type


class BaseInfoModule(BasePEModule):
"""
Base Info module class for Nutanix PC v4 list APIs based modules
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Base Info module class for Nutanix PC v4 list APIs based modules
Base Info module class for Nutanix PE v4 list APIs based modules

"""

info_argument_spec = dict(
filter=dict(type="str"),
page=dict(type="int"),
limit=dict(type="int"),
orderby=dict(type="str"),
select=dict(type="str"),
)

def __init__(self, skip_info_args=False, **kwargs):
self.argument_spec = deepcopy(BasePEModule.argument_spec)
self.argument_spec.pop("state")
self.argument_spec.pop("wait")
if not skip_info_args:
self.argument_spec.update(self.info_argument_spec)
super(BaseInfoModule, self).__init__(**kwargs)
60 changes: 60 additions & 0 deletions plugins/module_utils/v4/pe/base_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright: 2021, Ansible Project
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause )
from __future__ import absolute_import, division, print_function

from copy import deepcopy

from ansible.module_utils.basic import AnsibleModule, env_fallback

__metaclass__ = type


class BasePEModule(AnsibleModule):
"""Basic module with common arguments for PE"""

unsupported_spec_keys = ["obj"]
argument_spec = dict(
nutanix_host_pe=dict(
type="str", fallback=(env_fallback, ["NUTANIX_PE_HOST"]), required=True
),
nutanix_port=dict(
default="9440", type="str", fallback=(env_fallback, ["NUTANIX_PORT"])
),
nutanix_username=dict(
type="str", fallback=(env_fallback, ["NUTANIX_USERNAME"]), required=True
),
nutanix_password=dict(
type="str",
no_log=True,
fallback=(env_fallback, ["NUTANIX_PASSWORD"]),
required=True,
),
validate_certs=dict(
type="bool", default=True, fallback=(env_fallback, ["VALIDATE_CERTS"])
),
state=dict(type="str", choices=["present", "absent"], default="present"),
wait=dict(type="bool", default=True),
)

def __init__(self, **kwargs):
argument_spec = deepcopy(self.argument_spec)
if kwargs.get("argument_spec"):
argument_spec.update(deepcopy(kwargs["argument_spec"]))
self.argument_spec_with_extra_keys = deepcopy(argument_spec)
self.strip_extra_attributes(argument_spec)
kwargs["argument_spec"] = argument_spec

if not kwargs.get("supports_check_mode"):
kwargs["supports_check_mode"] = True

super(BasePEModule, self).__init__(**kwargs)

def strip_extra_attributes(self, argument_spec):
"""
This recursive method checks argument spec and remove extra spec definations which are not allowed in ansible
"""
for spec in argument_spec.values():
for k in self.unsupported_spec_keys:
spec.pop(k, None)
if spec.get("options"):
self.strip_extra_attributes(spec["options"])
98 changes: 98 additions & 0 deletions plugins/module_utils/v4/prism/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from __future__ import absolute_import, division, print_function

__metaclass__ = type

from ..utils import raise_api_exception # noqa: E402


def get_restore_source(module, api_instance, ext_id):
"""
This method will return restore source info using external ID.
Args:
module: Ansible module
api_instance: DomainManagerBackupApi instance from ntnx_prism_py_client sdk
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
api_instance: DomainManagerBackupApi instance from ntnx_prism_py_client sdk
api_instance: DomainManagerBackupsApi instance from ntnx_prism_py_client sdk

ext_id (str): restore source info external ID
return:
restore_source_info (object): restore source info
"""
try:
return api_instance.get_restore_source_by_id(extId=ext_id).data
except Exception as e:
raise_api_exception(
module=module,
exception=e,
msg="Api Exception raised while fetching restore source info using ext_id",
)


def get_backup_target(module, api_instance, ext_id):
"""
This method will return backup target info using external ID.
Args:
module: Ansible module
api_instance: DomainManagerBackupApi instance from ntnx_prism_py_client sdk
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
api_instance: DomainManagerBackupApi instance from ntnx_prism_py_client sdk
api_instance: DomainManagerBackupsApi instance from ntnx_prism_py_client sdk

ext_id (str): backup target info external ID
return:
backup_target_info (object): backup target info
"""
domain_manager_ext_id = module.params.get("domain_manager_ext_id")
try:
return api_instance.get_backup_target_by_id(
extId=ext_id, domainManagerExtId=domain_manager_ext_id
).data
except Exception as e:
raise_api_exception(
module=module,
exception=e,
msg="Api Exception raised while fetching backup target info using ext_id and domain_manager_ext_id",
)


def get_pc_config(module, api_instance, ext_id):
"""
This method will return pc config info using external ID.
Args:
module: Ansible module
api_instance: DomainManagerBackupApi instance from ntnx_prism_py_client sdk
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
api_instance: DomainManagerBackupApi instance from ntnx_prism_py_client sdk
api_instance: DomainManagerBackupsApi instance from ntnx_prism_py_client sdk

ext_id (str): pc external ID
return:
pc_config_info (object): pc config info
"""
try:
return api_instance.get_domain_manager_by_id(extId=ext_id).data
except Exception as e:
raise_api_exception(
module=module,
exception=e,
msg="Api Exception raised while fetching pc config info using ext_id",
)


def get_restore_point(
module,
api_instance,
ext_id,
restore_source_ext_id,
restorable_domain_manager_ext_id,
):
"""
This method will return restore point info using external ID.
Args:
module: Ansible module
api_instance: DomainManagerBackupApi instance from ntnx_prism_py_client sdk
ext_id (str): restore point info external ID
return:
restore_point_info (object): restore point info
"""
try:
return api_instance.get_restore_point_by_id(
restoreSourceExtId=restore_source_ext_id,
restorableDomainManagerExtId=restorable_domain_manager_ext_id,
extId=ext_id,
).data
except Exception as e:
raise_api_exception(
module=module,
exception=e,
msg="Api Exception raised while fetching restore point info using ext_id",
)
12 changes: 12 additions & 0 deletions plugins/module_utils/v4/prism/pc_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ def get_domain_manager_api_instance(module):
"""
api_client = get_pc_api_client(module)
return ntnx_prism_py_client.DomainManagerApi(api_client=api_client)


def get_domain_manager_backup_api_instance(module):
"""
This method will return domain manager backup api instance.
Args:
module (object): Ansible module object
return:
api_instance (object): domain manager backup api instance
"""
api_client = get_pc_api_client(module)
return ntnx_prism_py_client.DomainManagerBackupsApi(api_client=api_client)
Loading
Loading