| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Daniil Kupchenko (@oukooveu) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: msk_config |
| 10 |
short_description: Manage Amazon MSK cluster configurations |
| 11 |
version_added: "2.0.0" |
| 12 |
description: |
| 13 |
- Create, delete and modify Amazon MSK (Managed Streaming for Apache Kafka) cluster configurations. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_msk_config). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- Daniil Kupchenko (@oukooveu) |
| 18 |
options: |
| 19 |
state: |
| 20 |
description: Create (C(present)) or delete (C(absent)) cluster configuration. |
| 21 |
choices: ['present', 'absent'] |
| 22 |
default: 'present' |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: The name of the configuration. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
description: |
| 29 |
description: The description of the configuration. |
| 30 |
type: str |
| 31 |
default: '' |
| 32 |
config: |
| 33 |
description: Contents of the server.properties file. |
| 34 |
type: dict |
| 35 |
default: {} |
| 36 |
aliases: ['configuration'] |
| 37 |
kafka_versions: |
| 38 |
description: |
| 39 |
- The versions of Apache Kafka with which you can use this MSK configuration. |
| 40 |
- Required when I(state=present). |
| 41 |
type: list |
| 42 |
elements: str |
| 43 |
extends_documentation_fragment: |
| 44 |
- amazon.aws.common.modules |
| 45 |
- amazon.aws.region.modules |
| 46 |
- amazon.aws.boto3 |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 51 |
|
| 52 |
- community.aws.msk_config: |
| 53 |
name: kafka-cluster-configuration |
| 54 |
state: present |
| 55 |
kafka_versions: |
| 56 |
- 2.6.0 |
| 57 |
- 2.6.1 |
| 58 |
config: |
| 59 |
auto.create.topics.enable: false |
| 60 |
num.partitions: 1 |
| 61 |
default.replication.factor: 3 |
| 62 |
zookeeper.session.timeout.ms: 18000 |
| 63 |
|
| 64 |
- community.aws.msk_config: |
| 65 |
name: kafka-cluster-configuration |
| 66 |
state: absent |
| 67 |
""" |
| 68 |
|
| 69 |
RETURN = r""" |
| 70 |
# These are examples of possible return values, and in general should use other names for return values. |
| 71 |
|
| 72 |
arn: |
| 73 |
description: The Amazon Resource Name (ARN) of the configuration. |
| 74 |
type: str |
| 75 |
returned: I(state=present) |
| 76 |
sample: "arn:aws:kafka:<region>:<account>:configuration/<name>/<resource-id>" |
| 77 |
revision: |
| 78 |
description: The revision number. |
| 79 |
type: int |
| 80 |
returned: I(state=present) |
| 81 |
sample: 1 |
| 82 |
server_properties: |
| 83 |
description: Contents of the server.properties file. |
| 84 |
type: str |
| 85 |
returned: I(state=present) |
| 86 |
sample: "default.replication.factor=3\nnum.io.threads=8\nzookeeper.session.timeout.ms=18000" |
| 87 |
response: |
| 88 |
description: The response from actual API call. |
| 89 |
type: dict |
| 90 |
returned: always |
| 91 |
sample: {} |
| 92 |
""" |
| 93 |
|
| 94 |
try: |
| 95 |
import botocore |
| 96 |
except ImportError: |
| 97 |
pass # handled by AnsibleAWSModule |
| 98 |
|
| 99 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 100 |
|
| 101 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_aws_account_info |
| 102 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 103 |
|
| 104 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 105 |
|
| 106 |
|
| 107 |
def dict_to_prop(d): |
| 108 |
"""convert dictionary to multi-line properties""" |
| 109 |
if len(d) == 0: |
| 110 |
return "" |
| 111 |
return "\n".join(f"{k}={v}" for k, v in d.items()) |
| 112 |
|
| 113 |
|
| 114 |
def prop_to_dict(p): |
| 115 |
"""convert properties to dictionary""" |
| 116 |
if len(p) == 0: |
| 117 |
return {} |
| 118 |
r_dict = {} |
| 119 |
for s in p.decode().split("\n"): |
| 120 |
kv = s.split("=") |
| 121 |
r_dict[kv[0].strip()] = kv[1].strip() |
| 122 |
return r_dict |
| 123 |
# python >= 2.7 is required: |
| 124 |
# return { |
| 125 |
# k.strip(): v.strip() for k, v in (i.split("=") for i in p.decode().split("\n")) |
| 126 |
# } |
| 127 |
|
| 128 |
|
| 129 |
@AWSRetry.jittered_backoff(retries=5, delay=5) |
| 130 |
def get_configurations_with_backoff(client): |
| 131 |
paginator = client.get_paginator("list_configurations") |
| 132 |
return paginator.paginate().build_full_result() |
| 133 |
|
| 134 |
|
| 135 |
def find_active_config(client, module): |
| 136 |
""" |
| 137 |
looking for configuration by name |
| 138 |
""" |
| 139 |
|
| 140 |
name = module.params["name"] |
| 141 |
|
| 142 |
try: |
| 143 |
all_configs = get_configurations_with_backoff(client)["Configurations"] |
| 144 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 145 |
module.fail_json_aws(e, msg="failed to obtain kafka configurations") |
| 146 |
|
| 147 |
active_configs = list(item for item in all_configs if item["Name"] == name and item["State"] == "ACTIVE") |
| 148 |
|
| 149 |
if active_configs: |
| 150 |
if len(active_configs) == 1: |
| 151 |
return active_configs[0] |
| 152 |
else: |
| 153 |
module.fail_json_aws(msg=f"found more than one active config with name '{name}'") |
| 154 |
|
| 155 |
return None |
| 156 |
|
| 157 |
|
| 158 |
def get_configuration_revision(client, module, arn, revision): |
| 159 |
try: |
| 160 |
return client.describe_configuration_revision(Arn=arn, Revision=revision, aws_retry=True) |
| 161 |
except ( |
| 162 |
botocore.exceptions.BotoCoreError, |
| 163 |
botocore.exceptions.ClientError, |
| 164 |
) as e: |
| 165 |
module.fail_json_aws(e, "failed to describe kafka configuration revision") |
| 166 |
|
| 167 |
|
| 168 |
def is_configuration_changed(module, current): |
| 169 |
""" |
| 170 |
compare configuration's description and properties |
| 171 |
python 2.7+ version: |
| 172 |
prop_module = {str(k): str(v) for k, v in module.params.get("config").items()} |
| 173 |
""" |
| 174 |
prop_module = {} |
| 175 |
for k, v in module.params.get("config").items(): |
| 176 |
prop_module[str(k)] = str(v) |
| 177 |
if prop_to_dict(current.get("ServerProperties", "")) == prop_module: |
| 178 |
if current.get("Description", "") == module.params.get("description"): |
| 179 |
return False |
| 180 |
return True |
| 181 |
|
| 182 |
|
| 183 |
def create_config(client, module): |
| 184 |
"""create new or update existing configuration""" |
| 185 |
|
| 186 |
config = find_active_config(client, module) |
| 187 |
|
| 188 |
# create new configuration |
| 189 |
if not config: |
| 190 |
if module.check_mode: |
| 191 |
return True, {} |
| 192 |
|
| 193 |
try: |
| 194 |
response = client.create_configuration( |
| 195 |
Name=module.params.get("name"), |
| 196 |
Description=module.params.get("description"), |
| 197 |
KafkaVersions=module.params.get("kafka_versions"), |
| 198 |
ServerProperties=dict_to_prop(module.params.get("config")).encode(), |
| 199 |
aws_retry=True, |
| 200 |
) |
| 201 |
except ( |
| 202 |
botocore.exceptions.BotoCoreError, |
| 203 |
botocore.exceptions.ClientError, |
| 204 |
) as e: |
| 205 |
module.fail_json_aws(e, "failed to create kafka configuration") |
| 206 |
|
| 207 |
# update existing configuration (creates new revision) |
| 208 |
else: |
| 209 |
# it's required because 'config' doesn't contain 'ServerProperties' |
| 210 |
response = get_configuration_revision( |
| 211 |
client, module, arn=config["Arn"], revision=config["LatestRevision"]["Revision"] |
| 212 |
) |
| 213 |
|
| 214 |
if not is_configuration_changed(module, response): |
| 215 |
return False, response |
| 216 |
|
| 217 |
if module.check_mode: |
| 218 |
return True, {} |
| 219 |
|
| 220 |
try: |
| 221 |
response = client.update_configuration( |
| 222 |
Arn=config["Arn"], |
| 223 |
Description=module.params.get("description"), |
| 224 |
ServerProperties=dict_to_prop(module.params.get("config")).encode(), |
| 225 |
aws_retry=True, |
| 226 |
) |
| 227 |
except ( |
| 228 |
botocore.exceptions.BotoCoreError, |
| 229 |
botocore.exceptions.ClientError, |
| 230 |
) as e: |
| 231 |
module.fail_json_aws(e, "failed to update kafka configuration") |
| 232 |
|
| 233 |
arn = response["Arn"] |
| 234 |
revision = response["LatestRevision"]["Revision"] |
| 235 |
|
| 236 |
result = get_configuration_revision(client, module, arn=arn, revision=revision) |
| 237 |
|
| 238 |
return True, result |
| 239 |
|
| 240 |
|
| 241 |
def delete_config(client, module): |
| 242 |
"""delete configuration""" |
| 243 |
|
| 244 |
config = find_active_config(client, module) |
| 245 |
|
| 246 |
if module.check_mode: |
| 247 |
if config: |
| 248 |
return True, config |
| 249 |
else: |
| 250 |
return False, {} |
| 251 |
|
| 252 |
if config: |
| 253 |
try: |
| 254 |
response = client.delete_configuration(Arn=config["Arn"], aws_retry=True) |
| 255 |
except ( |
| 256 |
botocore.exceptions.BotoCoreError, |
| 257 |
botocore.exceptions.ClientError, |
| 258 |
) as e: |
| 259 |
module.fail_json_aws(e, "failed to delete the kafka configuration") |
| 260 |
return True, response |
| 261 |
|
| 262 |
return False, {} |
| 263 |
|
| 264 |
|
| 265 |
def main(): |
| 266 |
module_args = dict( |
| 267 |
name=dict(type="str", required=True), |
| 268 |
description=dict(type="str", default=""), |
| 269 |
state=dict(choices=["present", "absent"], default="present"), |
| 270 |
config=dict(type="dict", aliases=["configuration"], default={}), |
| 271 |
kafka_versions=dict(type="list", elements="str"), |
| 272 |
) |
| 273 |
|
| 274 |
module = AnsibleAWSModule(argument_spec=module_args, supports_check_mode=True) |
| 275 |
|
| 276 |
client = module.client("kafka", retry_decorator=AWSRetry.jittered_backoff()) |
| 277 |
|
| 278 |
if module.params["state"] == "present": |
| 279 |
changed, response = create_config(client, module) |
| 280 |
|
| 281 |
elif module.params["state"] == "absent": |
| 282 |
changed, response = delete_config(client, module) |
| 283 |
|
| 284 |
# return some useless staff in check mode if configuration doesn't exists |
| 285 |
# can be useful when these options are referenced by other modules during check mode run |
| 286 |
if module.check_mode and not response.get("Arn"): |
| 287 |
account_id, partition = get_aws_account_info(module) |
| 288 |
arn = f"arn:{partition}:kafka:{module.region}:{account_id}:configuration/{module.params['name']}/id" |
| 289 |
revision = 1 |
| 290 |
server_properties = "" |
| 291 |
else: |
| 292 |
arn = response.get("Arn") |
| 293 |
revision = response.get("Revision") |
| 294 |
server_properties = response.get("ServerProperties", "") |
| 295 |
|
| 296 |
module.exit_json( |
| 297 |
changed=changed, |
| 298 |
arn=arn, |
| 299 |
revision=revision, |
| 300 |
server_properties=server_properties, |
| 301 |
response=camel_dict_to_snake_dict(response), |
| 302 |
) |
| 303 |
|
| 304 |
|
| 305 |
if __name__ == "__main__": |
| 306 |
main() |