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

Add Cluster Info Module #204

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
153 changes: 153 additions & 0 deletions plugins/modules/cm_cluster_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import (
ClouderaManagerModule,
)
from cm_client.rest import ApiException
from cm_client import ClustersResourceApi

ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}

DOCUMENTATION = r"""
---
module: cm_cluster_info
short_description: Retrieve information about a cluster based on the provided cluster name
description:
- Module checks the existence of a cluster with the specified name and retrieves detailed information about the cluster.
author:
- "Ronald Suplina (@rsuplina)"
options:
cluster_name:
description:
- Name of the Cloudera Manager cluster
rsuplina marked this conversation as resolved.
Show resolved Hide resolved
type: str
required: True
requirements:
- cm_client
"""

EXAMPLES = r"""
---
- name: Get information about the cluster
cloudera.cluster.cm_cluster_info:
host: example.cloudera.com
username: "jane_smith"
cluster_name: "OneNodeCluster"
password: "S&peR4Ec*re"
port: "7180"

"""

RETURN = r"""
---
cloudera_manager:
description: Details about Cloudera Manager Cluster
type: dict
contains:
cluster_type:
description: The type of Cloudera Manager cluster.
type: str
returned: optional
rsuplina marked this conversation as resolved.
Show resolved Hide resolved
cluster_url:
description: Url of Cloudera Manager cluster.
type: str
returned: optional
display_name:
description: The name of the cluster displayed on the site.
type: str
returned: optional
entity_status:
description: Health status of the cluster.
type: str
returned: optional
full_version:
description: Version of the cluster installed.
type: str
returned: optional
hosts_url:
description: Url of all the hosts on which cluster is installed.
type: str
returned: optional
maintenance_mode:
description: Maintance mode of Cloudera Manager Cluster.
type: bool
returned: optional
maintenance_owners:
description: List of Maintance owners for Cloudera Manager Cluster.
type: list
returned: optional
name:
description: The name of the cluster created.
type: str
returned: optional
tags:
description: List of tags for Cloudera Manager Cluster.
type: list
returned: optional
uuid:
description: Unique ID of created cluster
type: bool
returned: optional
"""


class ClusterInfo(ClouderaManagerModule):
def __init__(self, module):
super(ClusterInfo, self).__init__(module)
self.cluster_name = self.get_param("cluster_name")
self.process()

@ClouderaManagerModule.handle_process
def process(self):
try:
cluster_api_instance = ClustersResourceApi(self.api_client)
self.cm_cluster_info = cluster_api_instance.read_cluster(cluster_name=self.cluster_name).to_dict()

except ApiException as e:
if e.status == 404:
self.cm_cluster_info = (f"Error: Cluster '{self.cluster_name}' not found.")
self.module.fail_json(msg=str(self.cm_cluster_info))
rsuplina marked this conversation as resolved.
Show resolved Hide resolved

def main():
module = ClouderaManagerModule.ansible_module(

argument_spec=dict(
cluster_name=dict(required=True, type="str"),
rsuplina marked this conversation as resolved.
Show resolved Hide resolved
),
supports_check_mode=False
)

result = ClusterInfo(module)



output = dict(
changed=False,
cloudera_manager=result.cm_cluster_info,
)

if result.debug:
log = result.log_capture.getvalue()
output.update(debug=log, debug_lines=log.split("\n"))

module.exit_json(**output)


if __name__ == "__main__":
main()
43 changes: 43 additions & 0 deletions tests/unit/plugins/modules/cm_cluster_info/test_cm_cluster_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import, division, print_function

__metaclass__ = type
import os
import logging
import pytest

from ansible_collections.cloudera.cluster.plugins.modules import cm_cluster_info
from ansible_collections.cloudera.cluster.tests.unit import AnsibleExitJson, AnsibleFailJson

LOG = logging.getLogger(__name__)

def test_pytest_cm_cluster_info(module_args):
module_args(
{
"username": os.getenv('CM_USERNAME'),
"password": os.getenv('CM_PASSWORD'),
"host": os.getenv('CM_HOST'),
"port": "7180",
"verify_tls": "no",
"cluster_name": "OneNodeCluster",
"debug": "no",
}
)

with pytest.raises(AnsibleExitJson) as e:
cm_cluster_info.main()

LOG.info(str(e.value.cloudera_manager))
Loading