summaryrefslogtreecommitdiffstats
path: root/ansible_collections/vultr/cloud/plugins/modules/user.py
blob: dd51243453b822dfc1ae869e369143701a644b41 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, René Moser <mail@renemoser.net>
# 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


DOCUMENTATION = """
---
module: user
short_description: Manages users on Vultr
description:
  - Create, update and remove users.
version_added: "1.0.0"
author: "René Moser (@resmo)"
options:
  name:
    description:
      - Name of the user
    required: true
    type: str
  email:
    description:
      - Email of the user.
      - Required if C(state=present).
    type: str
  password:
    description:
      - Password of the user.
      - Only considered while creating a user or when C(force=true).
    type: str
  force:
    description:
      - Password will only be changed with enforcement.
    default: false
    type: bool
  api_enabled:
    description:
      - Whether the API is enabled or not.
    default: true
    type: bool
  acls:
    description:
      - List of ACLs this users should have.
      - Required if C(state=present).
      - One or more of the choices list, some depend on each other.
    choices:
      - manage_users
      - subscriptions_view
      - subscriptions
      - provisioning
      - billing
      - support
      - abuse
      - dns
      - upgrade
      - objstore
      - loadbalancer
    aliases: [ acl ]
    type: list
    elements: str
  state:
    description:
      - State of the user.
    default: present
    choices: [ present, absent ]
    type: str
extends_documentation_fragment:
  - vultr.cloud.vultr_v2
"""

EXAMPLES = """
- name: Ensure a user exists
  vultr.cloud.user:
    name: john
    email: john.doe@example.com
    password: s3cr3t
    acls:
    - manage_users
    - subscriptions

- name: Remove a user
  vultr.cloud.user:
    name: john
    state: absent
"""

RETURN = """
---
vultr_api:
  description: Response from Vultr API with a few additions/modification.
  returned: success
  type: dict
  contains:
    api_timeout:
      description: Timeout used for the API requests.
      returned: success
      type: int
      sample: 60
    api_retries:
      description: Amount of max retries for the API requests.
      returned: success
      type: int
      sample: 5
    api_retry_max_delay:
      description: Exponential backoff delay in seconds between retries up to this max delay value.
      returned: success
      type: int
      sample: 12
    api_endpoint:
      description: Endpoint used for the API requests.
      returned: success
      type: str
      sample: "https://api.vultr.com/v2"
vultr_user:
  description: Response from Vultr API.
  returned: success
  type: dict
  contains:
    id:
      description: ID of the user.
      returned: success
      type: str
      sample: 7d726ffe-9be2-4f88-8cda-fa7eba1da2b5
    api_key:
      description: API key of the user.
      returned: only after resource was created
      type: str
      sample: 567E6K567E6K567E6K567E6K567E6K
    name:
      description: Name of the user.
      returned: success
      type: str
      sample: john
    email:
      description: Email of the user.
      returned: success
      type: str
      sample: "john@example.com"
    api_enabled:
      description: Whether the API is enabled or not.
      returned: success
      type: bool
      sample: true
    acls:
      description: List of ACLs of the user.
      returned: success
      type: list
      sample: [manage_users, support, upgrade]
"""

from ansible.module_utils.basic import AnsibleModule

from ..module_utils.vultr_v2 import AnsibleVultr, vultr_argument_spec

ACLS = [
    "manage_users",
    "subscriptions_view",
    "subscriptions",
    "provisioning",
    "billing",
    "support",
    "abuse",
    "dns",
    "upgrade",
    "objstore",
    "loadbalancer",
]


class AnsibleVultrUser(AnsibleVultr):
    def create(self):
        # Password is required in create mode.
        self.module.fail_on_missing_params(required_params=["password"])
        return super(AnsibleVultrUser, self).create()

    def update(self, resource):
        # Password is never returned and we can not compare.
        # That is why we update it only if forced
        force = self.module.params.get("force")
        if force:
            self.resource_update_param_keys.append("password")
        return super(AnsibleVultrUser, self).update(resource=resource)


def main():
    argument_spec = vultr_argument_spec()
    argument_spec.update(
        dict(
            name=dict(type="str", required=True),
            email=dict(type="str"),
            password=dict(type="str", no_log=True),
            force=dict(type="bool", default=False),
            api_enabled=dict(type="bool", default=True),
            acls=dict(type="list", elements="str", choices=ACLS, aliases=["acl"]),
            state=dict(type="str", choices=["present", "absent"], default="present"),
        )  # type: ignore
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        required_if=[
            ("state", "present", ["email", "acls"]),
        ],
        supports_check_mode=True,
    )

    vultr = AnsibleVultrUser(
        module=module,
        namespace="vultr_user",
        resource_path="/users",
        ressource_result_key_singular="user",
        resource_create_param_keys=["name", "email", "password", "api_enabled", "acls"],
        resource_update_param_keys=["name", "email", "api_enabled", "acls"],
        resource_key_name="name",
    )

    if module.params.get("state") == "absent":  # type: ignore
        vultr.absent()
    else:
        vultr.present()


if __name__ == "__main__":
    main()