summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/vca.py
blob: 56341ec55596ff0216ed8688daf78c311efed62d (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
#
# 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/>.

import os
try:
    from pyvcloud.vcloudair import VCA
    HAS_PYVCLOUD = True
except ImportError:
    HAS_PYVCLOUD = False

SERVICE_MAP = {'vca': 'ondemand', 'vchs': 'subscription', 'vcd': 'vcd'}
LOGIN_HOST = {'vca': 'vca.vmware.com', 'vchs': 'vchs.vmware.com'}

DEFAULT_SERVICE_TYPE = 'vca'
DEFAULT_VERSION = '5.7'

class VcaError(Exception):

    def __init__(self, msg, **kwargs):
        self.kwargs = kwargs
        super(VcaError, self).__init__(msg)

def vca_argument_spec():
    return dict(
        username=dict(),
        password=dict(),
        org=dict(),
        service_id=dict(),
        instance_id=dict(),
        host=dict(),
        api_version=dict(default=DEFAULT_VERSION),
        service_type=dict(default=DEFAULT_SERVICE_TYPE, choices=SERVICE_MAP.keys()),
        vdc_name=dict(),
        gateway_name=dict(default='gateway')
    )

class VcaAnsibleModule(AnsibleModule):

    def __init__(self, *args, **kwargs):
        argument_spec = vca_argument_spec()
        argument_spec.update(kwargs.get('argument_spec', dict()))
        kwargs['argument_spec'] = argument_spec

        super(VcaAnsibleModule, self).__init__(*args, **kwargs)

        if not HAS_PYVCLOUD:
            self.fail("python module pyvcloud is required for this module")

        self._vca = self.create_instance()
        self.login()

        self._gateway = None
        self._vdc = None

    @property
    def vca(self):
        return self._vca

    @property
    def gateway(self):
        if self._gateway is not None:
            return self._gateway
        vdc_name = self.params['vdc_name']
        gateway_name = self.params['gateway_name']
        _gateway = self.vca.get_gateway(vdc_name, gateway_name)
        if not _gateway:
            raise VcaError('vca instance has no gateway named %s' % gateway_name)
        self._gateway = _gateway
        return _gateway

    @property
    def vdc(self):
        if self._vdc is not None:
            return self._vdc
        vdc_name = self.params['vdc_name']
        _vdc = self.vca.get_vdc(vdc_name)
        if not _vdc:
            raise VcaError('vca instance has no vdc named %s' % vdc_name)
        self._vdc = _vdc
        return _vdc

    def get_vapp(self, vapp_name):
        vapp = self.vca.get_vapp(self.vdc, vapp_name)
        if not vapp:
            raise VcaError('vca instance has no vapp named %s' % vapp_name)
        return vapp

    def get_vm(self, vapp_name, vm_name):
        vapp = self.get_vapp(vapp_name)
        vms = [vm for vm in children.get_Vm() if vm.name == vm_name]
        try:
            return vms[0]
        except IndexError:
            raise VcaError('vapp has no vm named %s' % vm_name)

    def create_instance(self):
        service_type = self.params.get('service_type', DEFAULT_SERVICE_TYPE)
        host = self.params.get('host', LOGIN_HOST.get('service_type'))
        username = self.params['username']

        version = self.params.get('api_version')
        if service_type == 'vchs':
            version = '5.6'

        verify = self.params.get('verify_certs')

        return VCA(host=host, username=username,
                   service_type=SERVICE_MAP[service_type],
                   version=version, verify=verify)

    def login(self):
        service_type = self.params['service_type']
        password = self.params['password']

        if not self.vca.login(password=password):
            self.fail('Login to VCA failed', response=self.vca.response.content)

        try:
            method_name = 'login_%s' % service_type
            meth = getattr(self, method_name)
            meth()
        except AttributeError:
            self.fail('no login method exists for service_type %s' % service_type)
        except VcaError, e:
            self.fail(e.message, response=self.vca.response.content, **e.kwargs)

    def login_vca(self):
        instance_id = self.params['instance_id']
        if not instance_id:
            raise VcaError('missing required instance_id for service_type vca')
        self.vca.login_to_instance_sso(instance=instance_id)

    def login_vchs(self):
        service_id = self.params['service_id']
        if not service_id:
            raise VcaError('missing required service_id for service_type vchs')

        org = self.params['org']
        if not org:
            raise VcaError('missing required or for service_type vchs')

        self.vca.login_to_org(service_id, org)

    def login_vcd(self):
        org = self.params['org']
        if not org:
            raise VcaError('missing required or for service_type vchs')

        if not self.vca.token:
            raise VcaError('unable to get token for service_type vcd')

        if not self.vca.vcloud_session.org_url:
            raise VcaError('unable to get org_url for service_type vcd')

        self.vca.login(token=self.vca.token, org=org,
                       org_url=self.vca.vcloud_session.org_url)

    def save_services_config(self, blocking=True):
        task = self.gateway.save_services_configuration()
        if not task:
            self.fail(msg='unable to save gateway services configuration')
        if blocking:
            self.vca.block_until_completed(task)

    def fail(self, msg, **kwargs):
        self.fail_json(msg=msg, **kwargs)

    def exit(self, **kwargs):
        self.exit_json(**kwargs)



# -------------------------------------------------------------
# 9/18/2015 @privateip
# All of the functions below here were migrated from the original
# vca_* modules.  All functions below should be considered deprecated
# and will be removed once all of the vca_* modules have been updated
# to use the new instance module above
# -------------------------------------------------------------

VCA_REQ_ARGS = ['instance_id', 'vdc_name']
VCHS_REQ_ARGS = ['service_id']


def _validate_module(module):
    if not HAS_PYVCLOUD:
        module.fail_json(msg="python module pyvcloud is needed for this module")

    service_type = module.params.get('service_type', DEFAULT_SERVICE_TYPE)

    if service_type == 'vca':
        for arg in VCA_REQ_ARGS:
            if module.params.get(arg) is None:
                module.fail_json(msg="argument %s is mandatory when service type "
                                 "is vca" % arg)

    if service_type == 'vchs':
        for arg in VCHS_REQ_ARGS:
            if module.params.get(arg) is None:
                module.fail_json(msg="argument %s is mandatory when service type "
                                 "is vchs" % arg)

    if service_type == 'vcd':
        for arg in VCD_REQ_ARGS:
            if module.params.get(arg) is None:
                module.fail_json(msg="argument %s is mandatory when service type "
                                 "is vcd" % arg)


def serialize_instances(instance_list):
    instances = []
    for i in instance_list:
        instances.append(dict(apiUrl=i['apiUrl'], instance_id=i['id']))
    return instances

def _vca_login(vca, password, instance):
    if not vca.login(password=password):
        raise VcaError("Login Failed: Please check username or password",
                       error=vca.response.content)

    if not vca.login_to_instance_sso(instance=instance):
        s_json = serialize_instances(vca.instances)
        raise VcaError("Login to Instance failed: Seems like instance_id provided "
                        "is wrong .. Please check", valid_instances=s_json)

    return vca

def _vchs_login(vca, password, service, org):
    if not vca.login(password=password):
        raise VcaError("Login Failed: Please check username or password",
                       error=vca.response.content)

    if not vca.login_to_org(service, org):
        raise VcaError("Failed to login to org, Please check the orgname",
                        error=vca.response.content)


def _vcd_login(vca, password, org):
    # TODO: this function needs to be refactored
    if not vca.login(password=password, org=org):
        raise VcaError("Login Failed: Please check username or password "
                       "or host parameters")

    if not vca.login(password=password, org=org):
        raise VcaError("Failed to get the token",
                       error=vca.response.content)

    if not vca.login(token=vca.token, org=org, org_url=vca.vcloud_session.org_url):
        raise VcaError("Failed to login to org", error=vca.response.content)

def vca_login(module):
    service_type = module.params.get('service_type')
    username = module.params.get('username')
    password = module.params.get('password')
    instance = module.params.get('instance_id')
    org = module.params.get('org')
    vdc_name = module.params.get('vdc_name')
    service = module.params.get('service_id')
    version = module.params.get('api_version')
    verify = module.params.get('verify_certs')

    _validate_module(module)

    if not vdc_name and service_type == 'vchs':
        vdc_name = module.params.get('service_id')

    if not org and service_type == 'vchs':
        org = vdc_name or service

    if service_type == 'vcd':
        host = module.params.get('host')
    else:
        host = LOGIN_HOST[service_type]

    username = os.environ.get('VCA_USER', username)
    password = os.environ.get('VCA_PASS', password)

    if not username or not password:
        msg = "Either the username or password is not set, please check args"
        module.fail_json(msg=msg)

    if service_type == 'vchs':
        version = '5.6'
    elif service_type == 'vcd' and not version:
        version == '5.6'

    vca = VCA(host=host, username=username,
              service_type=SERVICE_MAP[service_type],
              version=version, verify=verify)

    try:
        if service_type == 'vca':
            _vca_login(vca, password, instance)
        elif service_type == 'vchs':
            _vchs_login(vca, password, service, org)
        elif service_type == 'vcd':
            _vcd_login(vca, password, org)
    except VcaError, e:
        module.fail_json(msg=e.message, **e.kwargs)

    return vca