summaryrefslogtreecommitdiffstats
path: root/ansible_collections/sensu/sensu_go/plugins/module_utils/arguments.py
blob: aed632dcf5ab90b1465a7b2fca424cbc0f3d7310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, XLAB Steampunk <steampunk@xlab.si>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

from ansible.module_utils.basic import env_fallback

from . import client


SHARED_SPECS = dict(
    auth=dict(
        type="dict",
        apply_defaults=True,
        options=dict(
            user=dict(
                default="admin",
                fallback=(env_fallback, ["SENSU_USER"]),
            ),
            password=dict(
                default="P@ssw0rd!",
                no_log=True,
                fallback=(env_fallback, ["SENSU_PASSWORD"]),
            ),
            url=dict(
                default="http://localhost:8080",
                fallback=(env_fallback, ["SENSU_URL"]),
            ),
            api_key=dict(
                fallback=(env_fallback, ["SENSU_API_KEY"]),
                no_log=True,
            ),
            verify=dict(
                default=True,
                fallback=(env_fallback, ["SENSU_VERIFY"]),
                type="bool",
            ),
            ca_path=dict(
                fallback=(env_fallback, ["SENSU_CA_PATH"]),
                type="path",
            ),
        ),
    ),
    state=dict(
        default="present",
        choices=["present", "absent"],
    ),
    name=dict(
        required=True,
    ),
    namespace=dict(
        default="default",
        fallback=(env_fallback, ["SENSU_NAMESPACE"]),
    ),
    labels=dict(
        type="dict",
        default={},
    ),
    annotations=dict(
        type="dict",
        default={},
    ),
    secrets=dict(
        type="list",
        elements="dict",
        no_log=False,
        options=dict(
            name=dict(type="str", required=True),
            secret=dict(type="str", required=True, no_log=False),
        ),
    ),
)


def get_spec(*param_names):
    return dict((p, SHARED_SPECS[p]) for p in param_names)


def get_spec_payload(source, *wanted_params):
    return dict(
        (k, source[k]) for k in wanted_params if source.get(k) is not None
    )


def get_renamed_spec_payload(source, param_mapping):
    return dict(
        (n, source[k]) for k, n in param_mapping.items()
        if source.get(k) is not None
    )


def get_mutation_payload(source, *wanted_params):
    payload = get_spec_payload(source, *wanted_params)
    payload["metadata"] = dict(
        name=source["name"],
    )
    # Cluster-wide objects are not limited to a single namespace. This is why we set
    # metadata.namespace field only if namespace is present in parameters.
    if "namespace" in source:
        if not source["namespace"]:
            # We are raising an exception here for the sake of sanity test.
            raise AssertionError("BUG: namespace should not be None")
        payload["metadata"]["namespace"] = source["namespace"]

    for kind in "labels", "annotations":
        if source.get(kind):
            payload["metadata"][kind] = dict(
                (k, str(v)) for k, v in source[kind].items()
            )
    return payload


def get_sensu_client(auth):
    return client.Client(
        auth["url"], auth["user"], auth["password"], auth["api_key"],
        auth["verify"], auth["ca_path"],
    )