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 Import_cluster_template module #197

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

DOCUMENTATION = r"""
---
module: cm_import_cluster_template
module: cm_cluster
short_description: Create a cluster based on the provided cluster template
description:
- Searches for a template file.
Expand All @@ -50,22 +50,29 @@
type: bool
required: False
default: False
clusterName:
description:
- Name of Cloudera Manager Cluster
Copy link
Member

Choose a reason for hiding this comment

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

Just "Name of cluster"

type: str
required: False
default: False
requirements:
- cm_client
"""

EXAMPLES = r"""
---
- name: Create a cluster on Cloudera Manager host
cloudera.cluster.cm_import_cluster_template:
cloudera.cluster.cm_cluster:
host: example.cloudera.com
username: "jane_smith"
clusterName: "OneNodeCluster"
password: "S&peR4Ec*re"
port: "7180"
template: "./files/cluster-template.json"

- name: Create a cluster and install the repositories defined in template
cloudera.cluster.cm_import_cluster_template:
cloudera.cluster.cm_cluster:
host: example.cloudera.com
username: "jane_smith"
password: "S&peR4Ec*re"
Expand Down Expand Up @@ -127,11 +134,12 @@
"""


class ClusterTemplate(ClouderaManagerModule):
class Cluster(ClouderaManagerModule):
def __init__(self, module):
super(ClusterTemplate, self).__init__(module)
super(Cluster, self).__init__(module)
self.template = self.get_param("template")
self.add_repositories = self.get_param("add_repositories")
self.clusterName = self.get_param("clusterName")
self.process()

@ClouderaManagerModule.handle_process
Expand All @@ -143,42 +151,46 @@ def process(self):

with open(self.template, 'r') as file:
template_json = json.load(file)
if self.add_repositories:
import_template_request = api_instance.import_cluster_template(add_repositories=True,body=template_json).to_dict()
else:
import_template_request = api_instance.import_cluster_template(body=template_json).to_dict()

command_id = import_template_request['id']

self.wait_for_command_state(command_id=command_id,polling_interval=60)

self.cm_cluster_template_output = cluster_api_instance.read_clusters().to_dict()
self.changed = True
self.file_not_found = False

if not self.module.check_mode:
if self.add_repositories:
import_template_request = api_instance.import_cluster_template(add_repositories=True,body=template_json).to_dict()
else:
import_template_request = api_instance.import_cluster_template(body=template_json).to_dict()

command_id = import_template_request['id']
self.wait_for_command_state(command_id=command_id,polling_interval=60)

if self.clusterName:
self.cm_cluster_template_output = cluster_api_instance.read_cluster(cluster_name=self.clusterName).to_dict()
self.changed = True
else:
self.cm_cluster_template_output = cluster_api_instance.read_clusters().to_dict()
self.changed = True

except ApiException as e:
# If cluster already exsists it will reply with 400 Error
if e.status == 400:
self.cm_cluster_template_output = json.loads(e.body)
self.changed = False
self.file_not_found = False

except FileNotFoundError:
self.cm_cluster_template_output = (f"Error: File '{self.template}' not found.")
self.file_not_found = True
self.module.fail_json(msg=str(self.cm_cluster_template_output))

def main():
module = ClouderaManagerModule.ansible_module(

argument_spec=dict(
template=dict(required=True, type="path"),
add_repositories=dict(required=False, type="bool", default=False),
clusterName=dict(required=False, type="str"),
),
supports_check_mode=False
)

result = ClusterTemplate(module)
result = Cluster(module)

if result.file_not_found:
module.fail_json(msg=str(result.cm_cluster_template_output))

changed = result.changed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# Copyright 2024 Cloudera, Inc. All Rights Reserved.
# Copyright 2023 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.
Expand All @@ -21,27 +21,28 @@
import logging
import pytest

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

LOG = logging.getLogger(__name__)

def test_pytest_cm_import_cluster_template(module_args):
def test_pytest_cm_cluster(module_args):
module_args(
{
"username": os.getenv('CM_USERNAME'),
"password": os.getenv('CM_PASSWORD'),
"host": os.getenv('CM_HOST'),
"port": "7180",
"verify_tls": "no",
"clusterName": "OneNodeCluster",
"debug": "no",
"template": "./files/cluster-template.json",
"add_repositories": "True"
}
)

with pytest.raises(AnsibleExitJson) as e:
cm_import_cluster_template.main()
cm_cluster.main()

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