Skip to content

Commit 13eb364

Browse files
authored
Update tests and filters to include Cloudera versioning scheme (#256)
Signed-off-by: Webster Mudge <[email protected]>
1 parent 8725267 commit 13eb364

File tree

15 files changed

+942
-222
lines changed

15 files changed

+942
-222
lines changed

plugins/filter/cm_version.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

plugins/filter/combine_onto.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2025 Cloudera, Inc.
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+
# https://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+
DOCUMENTATION:
16+
name: combine_onto
17+
author: Webster Mudge (@wmudge) <[email protected]>
18+
short_description: combine two dictionaries
19+
description:
20+
- Create a dictionary (hash/associative array) as a result of merging existing dictionaries.
21+
- This is the reverse of the C(ansible.builtin.combine) filter.
22+
positional: _input, _dicts
23+
options:
24+
_input:
25+
description:
26+
- First dictionary to combine.
27+
type: dict
28+
required: true
29+
_dicts:
30+
description:
31+
- The list of dictionaries to combine
32+
type: list
33+
elements: dict
34+
required: true
35+
recursive:
36+
description:
37+
- If V(True), merge elements recursively.
38+
type: boolean
39+
default: false
40+
list_merge:
41+
description: Behavior when encountering list elements.
42+
type: str
43+
default: replace
44+
choices:
45+
replace: overwrite older entries with newer ones
46+
keep: discard newer entries
47+
append: append newer entries to the older ones
48+
prepend: insert newer entries in front of the older ones
49+
append_rp: append newer entries to the older ones, overwrite duplicates
50+
prepend_rp: insert newer entries in front of the older ones, discard duplicates
51+
52+
EXAMPLES: |
53+
# ab => {'a':1, 'b':2, 'c': 4}
54+
ab: "{{ {'a':1, 'b':2} | cloudera.exe.combine_onto({'b':3, 'c':4}) }}"
55+
56+
many: "{{ dict1 | cloudera.exe.combine_onto(dict2, dict3, dict4) }}"
57+
58+
# defaults => {'a':{'b':3, 'c':4}, 'd': 5}
59+
# customization => {'a':{'c':20}}
60+
# final => {'a':{'b':3, 'c':20}, 'd': 5}
61+
final: "{{ customization | cloudera.exe.combine_onto(defaults, recursive=true) }}"
62+
63+
RETURN:
64+
_value:
65+
description: Resulting merge of supplied dictionaries.
66+
type: dict

plugins/filter/core_exe.py

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,15 @@
1919

2020
__metaclass__ = type
2121

22-
DOCUMENTATION = """
23-
name: combine_onto
24-
author: Webster Mudge (@wmudge) <[email protected]>
25-
short_description: combine two dictionaries
26-
description:
27-
- Create a dictionary (hash/associative array) as a result of merging existing dictionaries.
28-
- This is the reverse of the C(ansible.builtin.combine) filter.
29-
positional: _input, _dicts
30-
options:
31-
_input:
32-
description:
33-
- First dictionary to combine.
34-
type: dict
35-
required: True
36-
_dicts:
37-
description:
38-
- The list of dictionaries to combine
39-
type: list
40-
elements: dict
41-
required: True
42-
recursive:
43-
description:
44-
- If V(True), merge elements recursively.
45-
type: boolean
46-
default: False
47-
list_merge:
48-
description: Behavior when encountering list elements.
49-
type: str
50-
default: replace
51-
choices:
52-
replace: overwrite older entries with newer ones
53-
keep: discard newer entries
54-
append: append newer entries to the older ones
55-
prepend: insert newer entries in front of the older ones
56-
append_rp: append newer entries to the older ones, overwrite duplicates
57-
prepend_rp: insert newer entries in front of the older ones, discard duplicates
58-
"""
59-
60-
EXAMPLES = """
61-
# ab => {'a':1, 'b':2, 'c': 4}
62-
ab: "{{ {'a':1, 'b':2} | cloudera.exe.combine_onto({'b':3, 'c':4}) }}"
63-
64-
many: "{{ dict1 | cloudera.exe.combine_onto(dict2, dict3, dict4) }}"
65-
66-
# defaults => {'a':{'b':3, 'c':4}, 'd': 5}
67-
# customization => {'a':{'c':20}}
68-
# final => {'a':{'b':3, 'c':20}, 'd': 5}
69-
final: "{{ customization | cloudera.exe.combine_onto(defaults, recursive=true) }}"
70-
"""
71-
72-
RETURN = """
73-
_value:
74-
description: Resulting merge of supplied dictionaries.
75-
type: dict
76-
"""
77-
7822
from ansible.errors import AnsibleFilterError
7923
from ansible.plugins.filter.core import flatten
8024
from ansible.template import recursive_check_defined
8125
from ansible.utils.vars import merge_hash
8226

27+
from ansible_collections.cloudera.exe.plugins.module_utils.cldr_version import (
28+
ClouderaVersion,
29+
)
30+
8331

8432
def combine_onto(*terms, **kwargs):
8533
"""
@@ -112,10 +60,31 @@ def combine_onto(*terms, **kwargs):
11260
return result
11361

11462

63+
def cldr_version(version: str):
64+
"""
65+
Parse a Cloudera version string into its parts.
66+
"""
67+
68+
try:
69+
parsed_version = ClouderaVersion(version)
70+
return dict(
71+
major=parsed_version.major,
72+
minor=parsed_version.minor,
73+
patch=parsed_version.patch,
74+
prerelease=parsed_version.prerelease,
75+
buildmetadata=parsed_version.buildmetadata,
76+
)
77+
except Exception as e:
78+
raise AnsibleFilterError(orig_exc=e)
79+
80+
11581
class FilterModule(object):
116-
"""Derivatives of Ansible jinja2 filters"""
82+
"""Cloudera Ansible jinja2 filters"""
11783

11884
def filters(self):
119-
filters = {"combine_onto": combine_onto}
85+
filters = {
86+
"combine_onto": combine_onto,
87+
"version": cldr_version,
88+
}
12089

12190
return filters

plugins/filter/version.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2025 Cloudera, Inc.
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+
# https://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+
DOCUMENTATION:
16+
name: version
17+
author: Cloudera Labs
18+
short_description: Parse a Cloudera Manager version string
19+
description:
20+
- Cloudera Manager version string parsing.
21+
- Returns a dictionary of the version parts.
22+
- Cloudera versioning is an extension of Semantic Versioning that uses the C(prerelease) segment to indicate service packs or extended patch releases.
23+
- Moreover, this versioning scheme allow the C(prerelease) delimiter to use whitespace (C(' ')) and dots (C('.')) in addition to the semantic version's
24+
dash (C('-')).
25+
version_added: "3.0.0"
26+
positional: _input
27+
options:
28+
_input:
29+
description: A version string to parse.
30+
type: dict
31+
required: true
32+
33+
EXAMPLES: |
34+
- name: Parse a standard version string
35+
ansible.builtin.set_fact:
36+
standard: "{{ '1.2.3' | cloudera.exe.version }}"
37+
38+
- name: Parse a version plus build number
39+
ansible.builtin.set_fact:
40+
build: "{{ '1.2.3.4' | cloudera.exe.version }}"
41+
42+
- name: Parse a version plus service pack (as prerelease)
43+
ansible.builtin.set_fact:
44+
service_pack: "{{ '1.2.3 SP2' | cloudera.exe.version }}"
45+
46+
- name: Parse a version plus build metadata string
47+
ansible.builtin.set_fact:
48+
build: "{{ '1.2.3+build7' | clouder.exe.version }}"
49+
50+
- name: Parse a version plus prerelease and build string
51+
ansible.builtin.set_fact:
52+
full: "{{ '1.2.3-rc1+build7' | cloudera.exe.version }}"
53+
54+
RETURN:
55+
_value:
56+
description:
57+
- A dictionary of the version parts.
58+
- If unable to parse the string, returns C(None).
59+
type: dict
60+
options:
61+
major:
62+
description: Major version
63+
minor:
64+
description: Minor version
65+
patch:
66+
description: Patch version
67+
prerelease:
68+
description: Prerelease/Service pack version
69+
returned: when supported
70+
buildmetadata:
71+
description: Build metadata version
72+
returned: when supported

0 commit comments

Comments
 (0)