-
Notifications
You must be signed in to change notification settings - Fork 50
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
wmudge
merged 19 commits into
cloudera-labs:devel
from
rsuplina:feature/cm_import_cluster_template
Feb 29, 2024
+1,801
−150
Merged
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c92c313
Add Import_cluster_template module
rsuplina 5ad8f74
Add add_repositories Parametar
rsuplina 8f38619
Add Requested changes
rsuplina ccdd56d
Relocate cluster template merge logic to cm_utils.
wmudge 3212d80
Update logic in cm_cluster module and use shared cluster template mer…
wmudge cf59f6a
Add further unit tests
wmudge 07152f2
Update list merge logic and sort unique list values
wmudge 555ebda
Update merged object to instance variable
wmudge 35bc6d7
Add unit tests for multiple idempotent keys and elements
wmudge 56f1aab
Refactor merged object to instance from class.
wmudge 1ced19c
Short-circuit merger for initial fragment.
wmudge d2761b6
Add unit tests for assemble_cluster_template action
wmudge 88edd46
Refactor test files to be temporary
wmudge 885eb88
Rename bare test to module test
wmudge 945b1d0
Add "fix" for VSCode pytest discovery
wmudge 409019d
Add pythonpath parameter to pytest.ini
wmudge bdba1dc
Update merge logic to handle idempotent fragments within lists
wmudge 212e4ca
Merge pull request #1 from wmudge/update/flow-and-testing
rsuplina 717ce6a
Add wait_for_command_state
rsuplina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
# 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 ClouderaManagerResourceApi | ||
from cm_client import ClustersResourceApi | ||
import json | ||
|
||
ANSIBLE_METADATA = { | ||
"metadata_version": "1.1", | ||
"status": ["preview"], | ||
"supported_by": "community", | ||
} | ||
|
||
DOCUMENTATION = r""" | ||
--- | ||
module: cm_import_cluster_template | ||
short_description: Create a cluster based on the provided cluster template | ||
description: | ||
- Searches for a template file. | ||
- The search for the file starts at the root directory where the Ansible playbook is executed. By default, the template is expected to be placed inside the './files' directory. | ||
- Imports the template file and uses it to create the cluster. | ||
- This module ensures that the cluster is created according to the specified template. | ||
author: | ||
- "Ronald Suplina (@rsuplina)" | ||
options: | ||
template: | ||
description: | ||
- Path to template file which defines the cluster | ||
type: path | ||
elements: str | ||
required: True | ||
add_repositories: | ||
description: | ||
- Install parcel repositories in parcel directory | ||
type: bool | ||
required: False | ||
default: False | ||
requirements: | ||
- cm_client | ||
""" | ||
|
||
EXAMPLES = r""" | ||
--- | ||
- name: Create a cluster on Cloudera Manager host | ||
cloudera.cluster.cm_import_cluster_template: | ||
host: example.cloudera.com | ||
username: "jane_smith" | ||
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: | ||
host: example.cloudera.com | ||
username: "jane_smith" | ||
password: "S&peR4Ec*re" | ||
port: "7180" | ||
template: "./files/cluster-template.json" | ||
add_repositories: "True" | ||
""" | ||
|
||
RETURN = r""" | ||
--- | ||
cloudera_manager: | ||
description: Details about Cloudera Manager Cluster | ||
type: dict | ||
contains: | ||
cluster_type: | ||
description: The type of cluster created from template. | ||
type: str | ||
returned: optional | ||
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 ClusterTemplate(ClouderaManagerModule): | ||
def __init__(self, module): | ||
super(ClusterTemplate, self).__init__(module) | ||
self.template = self.get_param("template") | ||
self.add_repositories = self.get_param("add_repositories") | ||
self.process() | ||
|
||
@ClouderaManagerModule.handle_process | ||
def process(self): | ||
|
||
try: | ||
api_instance = ClouderaManagerResourceApi(self.api_client) | ||
cluster_api_instance = ClustersResourceApi(self.api_client) | ||
|
||
with open(self.template, 'r') as file: | ||
template_json = json.load(file) | ||
if self.add_repositories: | ||
rsuplina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() | ||
rsuplina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.changed = True | ||
self.file_not_found = False | ||
|
||
except ApiException as e: | ||
if e.status == 400: | ||
rsuplina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please call |
||
def main(): | ||
module = ClouderaManagerModule.ansible_module( | ||
|
||
argument_spec=dict( | ||
template=dict(required=True, type="path"), | ||
add_repositories=dict(required=False, type="bool", default=False), | ||
), | ||
supports_check_mode=False | ||
) | ||
|
||
result = ClusterTemplate(module) | ||
|
||
if result.file_not_found: | ||
module.fail_json(msg=str(result.cm_cluster_template_output)) | ||
|
||
changed = result.changed | ||
|
||
output = dict( | ||
changed=changed, | ||
cloudera_manager=result.cm_cluster_template_output, | ||
) | ||
|
||
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() |
47 changes: 47 additions & 0 deletions
47
tests/unit/plugins/modules/cm_import_cluster_template/test_cm_import_cluster_template.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# 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_import_cluster_template | ||
from ansible_collections.cloudera.cluster.tests.unit import AnsibleExitJson, AnsibleFailJson | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
def test_pytest_cm_import_cluster_template(module_args): | ||
module_args( | ||
{ | ||
"username": os.getenv('CM_USERNAME'), | ||
"password": os.getenv('CM_PASSWORD'), | ||
"host": os.getenv('CM_HOST'), | ||
"port": "7180", | ||
"verify_tls": "no", | ||
"debug": "no", | ||
"template": "./files/cluster-template.json", | ||
"add_repositories": "True" | ||
} | ||
) | ||
|
||
with pytest.raises(AnsibleExitJson) as e: | ||
cm_import_cluster_template.main() | ||
|
||
# LOG.info(str(e.value)) | ||
LOG.info(str(e.value.cloudera_manager)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be simply
cluster
-- importing a cluster template is just one function of this module. The initial implementation can only include this functionality, but going forward, we should then add reconciliation and update logic. (This will be calls toPUT /clusters/{clusterName}
.)