Skip to content

Commit f155947

Browse files
authored
Add cm_license and cm_license_info modules (#199)
Signed-off-by: rsuplina <[email protected]>
1 parent 4ff475c commit f155947

File tree

4 files changed

+349
-0
lines changed

4 files changed

+349
-0
lines changed

plugins/modules/cm_license.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import (
16+
ClouderaManagerModule,
17+
)
18+
from cm_client.rest import ApiException
19+
from cm_client import ClouderaManagerResourceApi
20+
21+
ANSIBLE_METADATA = {
22+
"metadata_version": "1.1",
23+
"status": ["preview"],
24+
"supported_by": "community",
25+
}
26+
27+
DOCUMENTATION = r"""
28+
---
29+
module: cm_license
30+
short_description: Activate the license for Cloudera Manager
31+
description:
32+
- Activates the license if not already activated.
33+
- Return information about the acivate license.
34+
author:
35+
- "Ronald Suplina (@rsuplina)"
36+
requirements:
37+
- cm_client
38+
"""
39+
40+
EXAMPLES = r"""
41+
---
42+
- name: Activate Cloudera Manager license
43+
cloudera.cluster.cm_license:
44+
host: example.cloudera.com
45+
port: "7180"
46+
username: "jane_smith"
47+
password: "S&peR4Ec*re"
48+
license: "./files/license.txt"
49+
"""
50+
51+
RETURN = r"""
52+
---
53+
cloudera_manager:
54+
description: Details about a active license
55+
type: dict
56+
contains:
57+
owner:
58+
description: Owner of the license
59+
type: str
60+
returned: optional
61+
uuid:
62+
description: Unique ID of the license
63+
type: bool
64+
returned: optional
65+
expiration:
66+
description: Expiration date of the license
67+
type: date
68+
returned: optional
69+
features:
70+
description: List of features within the license
71+
type: list
72+
returned: optional
73+
deactivation_date:
74+
description: Date until license is valid
75+
type: date
76+
returned: optional
77+
start_date:
78+
description: License activation date
79+
type: date
80+
returned: optional
81+
"""
82+
83+
84+
class ClouderaLicense(ClouderaManagerModule):
85+
def __init__(self, module):
86+
super(ClouderaLicense, self).__init__(module)
87+
self.license = self.get_param("license")
88+
self.process()
89+
90+
@ClouderaManagerModule.handle_process
91+
def process(self):
92+
93+
try:
94+
api_instance = ClouderaManagerResourceApi(self.api_client)
95+
96+
self.cm_license_output = api_instance.read_license().to_dict()
97+
self.changed = False
98+
99+
except ApiException as e:
100+
if e.status == 404:
101+
if not self.module.check_mode:
102+
103+
api_instance.update_license(license=self.license).to_dict()
104+
self.cm_license_output = api_instance.read_license().to_dict()
105+
self.changed = True
106+
107+
except FileNotFoundError:
108+
self.cm_license_output = (f"Error: File '{self.license}' not found.")
109+
self.module.fail_json(msg=str(self.cm_license_output))
110+
111+
def main():
112+
module = ClouderaManagerModule.ansible_module(
113+
114+
argument_spec=
115+
dict(
116+
license=dict(required=True, type="path")
117+
),
118+
supports_check_mode=True
119+
)
120+
121+
result = ClouderaLicense(module)
122+
123+
124+
output = dict(
125+
changed=result.changed,
126+
cloudera_manager=result.cm_license_output,
127+
)
128+
129+
if result.debug:
130+
log = result.log_capture.getvalue()
131+
output.update(debug=log, debug_lines=log.split("\n"))
132+
133+
module.exit_json(**output)
134+
135+
136+
if __name__ == "__main__":
137+
main()

plugins/modules/cm_license_info.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import (
16+
ClouderaManagerModule,
17+
)
18+
from cm_client.rest import ApiException
19+
from cm_client import ClouderaManagerResourceApi
20+
21+
ANSIBLE_METADATA = {
22+
"metadata_version": "1.1",
23+
"status": ["preview"],
24+
"supported_by": "community",
25+
}
26+
27+
DOCUMENTATION = r"""
28+
---
29+
module: cm_license
30+
short_description: Returns details about current license
31+
description:
32+
- Returns details about current active license.
33+
author:
34+
- "Ronald Suplina (@rsuplina)"
35+
requirements:
36+
- cm_client
37+
"""
38+
39+
EXAMPLES = r"""
40+
---
41+
- name: Get details about a license
42+
cloudera.cluster.cm_license_info:
43+
host: example.cloudera.com
44+
port: "7180"
45+
username: "jane_smith"
46+
password: "S&peR4Ec*re"
47+
"""
48+
49+
RETURN = r"""
50+
---
51+
cloudera_manager:
52+
description: Details about an active license
53+
type: dict
54+
contains:
55+
owner:
56+
description: Owner of the active license
57+
type: str
58+
returned: optional
59+
uuid:
60+
description: Unique ID of the license
61+
type: bool
62+
returned: optional
63+
expiration:
64+
description: Expiration date of the license
65+
type: date
66+
returned: optional
67+
features:
68+
description: List of features within the license
69+
type: list
70+
returned: optional
71+
deactivation_date:
72+
description: Date until license is valid
73+
type: date
74+
returned: optional
75+
start_date:
76+
description: License activation date
77+
type: date
78+
returned: optional
79+
"""
80+
81+
82+
class ClouderaLicenseInfo(ClouderaManagerModule):
83+
def __init__(self, module):
84+
super(ClouderaLicenseInfo, self).__init__(module)
85+
86+
# Initialize the return values
87+
self.cm_license_info = dict()
88+
89+
# Execute the logic
90+
self.process()
91+
92+
@ClouderaManagerModule.handle_process
93+
def process(self):
94+
try:
95+
api_instance = ClouderaManagerResourceApi(self.api_client)
96+
self.cm_license_output = api_instance.read_license().to_dict()
97+
except ApiException as e:
98+
if e.status == 404:
99+
self.cm_cluster_info = (f"Error: License not found.")
100+
self.module.fail_json(msg=str(self.cm_license_output))
101+
102+
def main():
103+
module = ClouderaManagerModule.ansible_module(supports_check_mode=False)
104+
105+
result = ClouderaLicenseInfo(module)
106+
107+
108+
output = dict(
109+
changed=False,
110+
cloudera_manager=result.cm_license_output,
111+
)
112+
113+
if result.debug:
114+
log = result.log_capture.getvalue()
115+
output.update(debug=log, debug_lines=log.split("\n"))
116+
117+
module.exit_json(**output)
118+
119+
120+
if __name__ == "__main__":
121+
main()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from __future__ import absolute_import, division, print_function
18+
19+
__metaclass__ = type
20+
import os
21+
import logging
22+
import pytest
23+
24+
from ansible_collections.cloudera.cluster.plugins.modules import cm_license
25+
from ansible_collections.cloudera.cluster.tests.unit import AnsibleExitJson, AnsibleFailJson
26+
27+
LOG = logging.getLogger(__name__)
28+
29+
def test_pytest_cm_license(module_args):
30+
module_args(
31+
{
32+
"username": os.getenv('CM_USERNAME'),
33+
"password": os.getenv('CM_PASSWORD'),
34+
"host": os.getenv('CM_HOST'),
35+
"port": "7180",
36+
"verify_tls": "no",
37+
"debug": "no",
38+
"license": "./files/license.txt",
39+
}
40+
)
41+
42+
with pytest.raises(AnsibleExitJson) as e:
43+
cm_license.main()
44+
45+
# LOG.info(str(e.value))
46+
LOG.info(str(e.value.cloudera_manager))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from __future__ import absolute_import, division, print_function
18+
19+
__metaclass__ = type
20+
import os
21+
import logging
22+
import pytest
23+
24+
from ansible_collections.cloudera.cluster.plugins.modules import cm_license_info
25+
from ansible_collections.cloudera.cluster.tests.unit import AnsibleExitJson, AnsibleFailJson
26+
27+
LOG = logging.getLogger(__name__)
28+
29+
def test_pytest_cm_license_info(module_args):
30+
module_args(
31+
{
32+
"username": os.getenv('CM_USERNAME'),
33+
"password": os.getenv('CM_PASSWORD'),
34+
"host": os.getenv('CM_HOST'),
35+
"port": "7180",
36+
"verify_tls": "no",
37+
"debug": "no",
38+
}
39+
)
40+
41+
with pytest.raises(AnsibleExitJson) as e:
42+
cm_license_info.main()
43+
44+
# LOG.info(str(e.value))
45+
LOG.info(str(e.value.cloudera_manager))

0 commit comments

Comments
 (0)