summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/cloud/ovirt/ovirt_external_provider.py
blob: f36d5a4a9f3be6da8894f0c8678a6326d50c8691 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 Red Hat, Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: ovirt_external_provider
short_description: Module to manage external providers in oVirt/RHV
version_added: "2.3"
author: "Ondra Machacek (@machacekondra)"
description:
    - "Module to manage external providers in oVirt/RHV"
options:
    name:
        description:
            - "Name of the external provider to manage."
    state:
        description:
            - "Should the external be present or absent"
            - "When you are using absent for I(os_volume), you need to make
               sure that SD is not attached to the data center!"
        choices: ['present', 'absent']
        default: present
    description:
        description:
            - "Description of the external provider."
    type:
        description:
            - "Type of the external provider."
        choices: ['os_image', 'network', 'os_volume', 'foreman']
    url:
        description:
            - "URL where external provider is hosted."
            - "Applicable for those types: I(os_image), I(os_volume), I(network) and I(foreman)."
    username:
        description:
            - "Username to be used for login to external provider."
            - "Applicable for all types."
    password:
        description:
            - "Password of the user specified in C(username) parameter."
            - "Applicable for all types."
    tenant_name:
        description:
            - "Name of the tenant."
            - "Applicable for those types: I(os_image), I(os_volume) and I(network)."
        aliases: ['tenant']
    authentication_url:
        description:
            - "Keystone authentication URL of the openstack provider."
            - "Applicable for those types: I(os_image), I(os_volume) and I(network)."
        aliases: ['auth_url']
    data_center:
        description:
            - "Name of the data center where provider should be attached."
            - "Applicable for those type: I(os_volume)."
    read_only:
        description:
            - "Specify if the network should be read only."
            - "Applicable if C(type) is I(network)."
        type: bool
    network_type:
        description:
            - "Type of the external network provider either external (for example OVN) or neutron."
            - "Applicable if C(type) is I(network)."
        choices: ['external', 'neutron']
        default: ['external']
    authentication_keys:
        description:
            - "List of authentication keys. Each key is represented by dict
               like {'uuid': 'our-uuid', 'value': 'YourSecretValue=='}"
            - "When you will not pass these keys and there are already some
               of them defined in the system they will be removed."
            - "Applicable for I(os_volume)."
        default: []
        version_added: "2.6"
extends_documentation_fragment: ovirt
'''

EXAMPLES = '''
# Examples don't contain auth parameter for simplicity,
# look at ovirt_auth module to see how to reuse authentication:

# Add image external provider:
- ovirt_external_provider:
    name: image_provider
    type: os_image
    url: http://1.2.3.4:9292
    username: admin
    password: 123456
    tenant: admin
    auth_url: http://1.2.3.4:35357/v2.0

# Add volume external provider:
- ovirt_external_provider:
    name: image_provider
    type: os_volume
    url: http://1.2.3.4:9292
    username: admin
    password: 123456
    tenant: admin
    auth_url: http://1.2.3.4:5000/v2.0
    authentication_keys:
      -
        uuid: "1234567-a1234-12a3-a234-123abc45678"
        value: "ABCD00000000111111222333445w=="

# Add foreman provider:
- ovirt_external_provider:
    name: foreman_provider
    type: foreman
    url: https://foreman.example.com
    username: admin
    password: 123456

# Add external network provider for OVN:
- ovirt_external_provider:
    name: ovn_provider
    type: network
    network_type: external
    url: http://1.2.3.4:9696

# Remove image external provider:
- ovirt_external_provider:
    state: absent
    name: image_provider
    type: os_image
'''

RETURN = '''
id:
    description: ID of the external provider which is managed
    returned: On success if external provider is found.
    type: str
    sample: 7de90f31-222c-436c-a1ca-7e655bd5b60c
external_host_provider:
    description: "Dictionary of all the external_host_provider attributes. External provider attributes can be found on your oVirt/RHV instance
                  at following url: http://ovirt.github.io/ovirt-engine-api-model/master/#types/external_host_provider."
    returned: "On success and if parameter 'type: foreman' is used."
    type: dict
openstack_image_provider:
    description: "Dictionary of all the openstack_image_provider attributes. External provider attributes can be found on your oVirt/RHV instance
                  at following url: http://ovirt.github.io/ovirt-engine-api-model/master/#types/openstack_image_provider."
    returned: "On success and if parameter 'type: os_image' is used."
    type: dict
openstack_volume_provider:
    description: "Dictionary of all the openstack_volume_provider attributes. External provider attributes can be found on your oVirt/RHV instance
                  at following url: http://ovirt.github.io/ovirt-engine-api-model/master/#types/openstack_volume_provider."
    returned: "On success and if parameter 'type: os_volume' is used."
    type: dict
openstack_network_provider:
    description: "Dictionary of all the openstack_network_provider attributes. External provider attributes can be found on your oVirt/RHV instance
                  at following url: http://ovirt.github.io/ovirt-engine-api-model/master/#types/openstack_network_provider."
    returned: "On success and if parameter 'type: network' is used."
    type: dict
'''

import traceback

try:
    import ovirtsdk4.types as otypes
except ImportError:
    pass

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ovirt import (
    BaseModule,
    check_params,
    check_sdk,
    create_connection,
    equal,
    ovirt_full_argument_spec,
)


OS_VOLUME = 'os_volume'
OS_IMAGE = 'os_image'
NETWORK = 'network'
FOREMAN = 'foreman'


class ExternalProviderModule(BaseModule):

    non_provider_params = ['type', 'authentication_keys', 'data_center']

    def provider_type(self, provider_type):
        self._provider_type = provider_type

    def provider_module_params(self):
        provider_params = [
            (key, value) for key, value in self._module.params.items() if key
            not in self.non_provider_params
        ]
        provider_params.append(('data_center', self.get_data_center()))
        return provider_params

    def get_data_center(self):
        dc_name = self._module.params.get("data_center", None)
        if dc_name:
            system_service = self._connection.system_service()
            data_centers_service = system_service.data_centers_service()
            return data_centers_service.list(
                search='name=%s' % dc_name,
            )[0]
        return dc_name

    def build_entity(self):
        provider_type = self._provider_type(
            requires_authentication=self._module.params.get('username') is not None,
        )
        if self._module.params.pop('type') == NETWORK:
            setattr(
                provider_type,
                'type',
                otypes.OpenStackNetworkProviderType(self._module.params.pop('network_type'))
            )

        for key, value in self.provider_module_params():
            if hasattr(provider_type, key):
                setattr(provider_type, key, value)

        return provider_type

    def update_check(self, entity):
        return (
            equal(self._module.params.get('description'), entity.description) and
            equal(self._module.params.get('url'), entity.url) and
            equal(self._module.params.get('authentication_url'), entity.authentication_url) and
            equal(self._module.params.get('tenant_name'), getattr(entity, 'tenant_name', None)) and
            equal(self._module.params.get('username'), entity.username)
        )

    def update_volume_provider_auth_keys(
        self, provider, providers_service, keys
    ):
        """
        Update auth keys for volume provider, if not exist add them or remove
        if they are not specified and there are already defined in the external
        volume provider.

        Args:
            provider (dict): Volume provider details.
            providers_service (openstack_volume_providers_service): Provider
                service.
            keys (list): Keys to be updated/added to volume provider, each key
                is represented as dict with keys: uuid, value.
        """

        provider_service = providers_service.provider_service(provider['id'])
        auth_keys_service = provider_service.authentication_keys_service()
        provider_keys = auth_keys_service.list()
        # removing keys which are not defined
        for key in [
            k.id for k in provider_keys if k.uuid not in [
                defined_key['uuid'] for defined_key in keys
            ]
        ]:
            self.changed = True
            if not self._module.check_mode:
                auth_keys_service.key_service(key).remove()
        if not (provider_keys or keys):
            # Nothing need to do when both are empty.
            return
        for key in keys:
            key_id_for_update = None
            for existing_key in provider_keys:
                if key['uuid'] == existing_key.uuid:
                    key_id_for_update = existing_key.id

            auth_key_usage_type = (
                otypes.OpenstackVolumeAuthenticationKeyUsageType("ceph")
            )
            auth_key = otypes.OpenstackVolumeAuthenticationKey(
                usage_type=auth_key_usage_type,
                uuid=key['uuid'],
                value=key['value'],
            )

            if not key_id_for_update:
                self.changed = True
                if not self._module.check_mode:
                    auth_keys_service.add(auth_key)
            else:
                # We cannot really distinguish here if it was really updated cause
                # we cannot take key value to check if it was changed or not. So
                # for sure we update here always.
                self.changed = True
                if not self._module.check_mode:
                    auth_key_service = (
                        auth_keys_service.key_service(key_id_for_update)
                    )
                    auth_key_service.update(auth_key)


def _external_provider_service(provider_type, system_service):
    if provider_type == OS_IMAGE:
        return otypes.OpenStackImageProvider, system_service.openstack_image_providers_service()
    elif provider_type == NETWORK:
        return otypes.OpenStackNetworkProvider, system_service.openstack_network_providers_service()
    elif provider_type == OS_VOLUME:
        return otypes.OpenStackVolumeProvider, system_service.openstack_volume_providers_service()
    elif provider_type == FOREMAN:
        return otypes.ExternalHostProvider, system_service.external_host_providers_service()


def main():
    argument_spec = ovirt_full_argument_spec(
        state=dict(
            choices=['present', 'absent'],
            default='present',
        ),
        name=dict(default=None),
        description=dict(default=None),
        type=dict(
            default=None,
            required=True,
            choices=[
                OS_IMAGE, NETWORK, OS_VOLUME, FOREMAN,
            ],
            aliases=['provider'],
        ),
        url=dict(default=None),
        username=dict(default=None),
        password=dict(default=None, no_log=True),
        tenant_name=dict(default=None, aliases=['tenant']),
        authentication_url=dict(default=None, aliases=['auth_url']),
        data_center=dict(default=None),
        read_only=dict(default=None, type='bool'),
        network_type=dict(
            default='external',
            choices=['external', 'neutron'],
        ),
        authentication_keys=dict(
            default=[], aliases=['auth_keys'], type='list', no_log=True,
        ),
    )
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    check_sdk(module)
    check_params(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        provider_type_param = module.params.get('type')
        provider_type, external_providers_service = _external_provider_service(
            provider_type=provider_type_param,
            system_service=connection.system_service(),
        )
        external_providers_module = ExternalProviderModule(
            connection=connection,
            module=module,
            service=external_providers_service,
        )
        external_providers_module.provider_type(provider_type)

        state = module.params.pop('state')
        if state == 'absent':
            ret = external_providers_module.remove()
        elif state == 'present':
            ret = external_providers_module.create()
            openstack_volume_provider_id = ret.get('id')
            if (
                provider_type_param == OS_VOLUME and
                openstack_volume_provider_id
            ):
                external_providers_module.update_volume_provider_auth_keys(
                    ret, external_providers_service,
                    module.params.get('authentication_keys'),
                )

        module.exit_json(**ret)

    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None)


if __name__ == "__main__":
    main()