forked from gnosek/oktawave-ansible
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoktawave_container
239 lines (208 loc) · 7.97 KB
/
oktawave_container
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
#!/usr/bin/env python
import os
import json
from oktawave.api import OktawaveApi
# -- universal helper methods --
# copied/pasted between modules as they must fit in a single file
import requests.exceptions
import datetime
import time
from oktawave.exceptions import OktawaveAPIError
def serializable(obj):
# make obj json-serializable
if isinstance(obj, (basestring, int)):
return obj
elif isinstance(obj, datetime.datetime):
return obj.strftime('%c')
elif isinstance(obj, dict):
return {k: serializable(v) for k, v in obj.items()}
elif hasattr(obj, 'label'):
return obj.label
elif obj in (True, False, None):
return obj
else:
try:
iterator = iter(obj)
except TypeError:
return unicode(obj)
return [serializable(elt) for elt in obj]
class APIWrapper(object):
def __init__(self, module, api):
self.module = module
self.api = api
def request_wrapper(self, reqf):
def wrapper(*args, **kwargs):
while True:
try:
return reqf(*args, **kwargs)
except requests.exceptions.HTTPError as exc:
arg_msg = ', '.join(repr(a) for a in args)
kwarg_msg = ', '.join('%s=%s' % (k, repr(v)) for k, v in kwargs.items())
call_msg = '%s(%s)' % (reqf.__name__, ', '.join((arg_msg, kwarg_msg)))
self.module.fail_json(msg='Call to %s failed: %s' % (call_msg, exc.response.text))
except OktawaveAPIError as exc:
if exc.code == exc.OCI_PENDING_OPS: # wait a bit before retrying
time.sleep(5)
else:
raise
return wrapper
def __getattr__(self, name):
method = getattr(self.api, name)
return self.request_wrapper(method)
# -- end --
def get_container_by_name(api, name):
containers = api.Container_List()
container_id = None
for container in containers:
if container['name'] == name:
container_id = container['id']
if container_id:
return api.Container_Get(container_id)
def wait_for_container(api, name, timeout):
if timeout == 0:
return get_container_by_name(api, name)
while timeout > 0:
container = get_container_by_name(api, name)
if container:
break
time.sleep(10)
timeout -= 10
return container
PARAMS_MAP = {
'autoscaling': {
'On': True,
'Off': False,
},
}
REVERSE_PARAMS_MAP = {
'autoscaling': {
True: 'on',
False: 'off',
},
'service': {
'http': 'HTTP',
'https': 'HTTPS',
'smtp': 'SMTP',
'mysql': 'MySQL',
'port': 'Port',
},
}
KEY_MAP = {
'load_balancer_algorithm': 'lb_algorithm',
'session_type': 'session',
'master_service_id': 'master_id',
}
def build_api_params(params):
for k, v in KEY_MAP.items():
params[v] = params.pop(k)
for k, v in REVERSE_PARAMS_MAP.items():
params[k] = v[params[k]]
return params
def fixup_container_quirks(container):
# fix inconsistencies between ansible interface and API
container['service'].label = container['service'].label.lower()
container['autoscaling'].label = (container['service'].label == 'on')
def manage_container(module, api, name, params, state):
module_ret = dict(changed=False)
container = get_container_by_name(api, name)
if container:
if state == 'present':
fixup_container_quirks(container)
module_ret.update(serializable(container))
diff = False
for k, pv in params.items():
cv = serializable(container[k])
if cv != pv:
diff = True
if diff:
module_ret['changed'] = True
if not module.check_mode:
params = build_api_params(params)
api.Container_Edit(container['id'], name, **params)
new_container = api.Container_Get(container['id'])
fixup_container_quirks(new_container)
module_ret.update(serializable(new_container))
else:
module_ret['changed'] = True
if not module.check_mode:
api.Container_Delete(container['id'])
else:
if state == 'absent':
pass
else:
module_ret['changed'] = True
if not module.check_mode:
api_params = build_api_params(params)
container_id = api.Container_Create(name, **api_params)
module_ret.update(id=container_id, name=name, **params)
module.exit_json(**module_ret)
def manage_container_membership(module, api, name, oci_id, state):
module_ret = dict(changed=False)
container = get_container_by_name(api, name)
if not container:
module.fail_json(msg='Container does not exist')
vms = set(vm['oci_id'] for vm in api.Container_OCIList(container['id']))
if state == 'present':
if oci_id not in vms:
module_ret['changed'] = True
if not module.check_mode:
api.Container_AddOCI(container['id'], oci_id)
else:
if oci_id in vms:
module_ret['changed'] = True
if not module.check_mode:
api.Container_RemoveOCI(container['id'], oci_id)
module.exit_json(**module_ret)
def main():
module = AnsibleModule(
argument_spec=dict(
okta_username=dict(required=True),
okta_password=dict(required=True),
name=dict(required=True),
state=dict(default="present", choices=["absent", "present"]),
oci_id=dict(),
autoscaling=dict(default='no', choices=BOOLEANS),
load_balancer=dict(default='no', choices=BOOLEANS),
ssl=dict(default='no', choices=BOOLEANS),
proxy_cache=dict(default='no', choices=BOOLEANS),
healthcheck=dict(default='no', choices=BOOLEANS),
ip_version=dict(default='4', choices=['4', '6', 'both']),
load_balancer_algorithm=dict(default='least_response_time', choices=[
'least_response_time', 'least_connections', 'source_ip_hash', 'round_robin']),
service=dict(default='http', choices=['http', 'https', 'smtp', 'mysql', 'port']),
port=dict(),
session_type=dict(default='none', choices=['none', 'by_source_ip', 'by_cookie']),
master_service_id=dict(),
),
supports_check_mode=True
)
okta_username = module.params.get('okta_username')
okta_password = module.params.get('okta_password')
name = module.params.get('name')
state = module.params.get('state')
oci_id = module.params.get('oci_id')
if oci_id is not None:
oci_id = int(oci_id)
params = {
'autoscaling': module.boolean(module.params.get('autoscaling')),
'load_balancer': module.boolean(module.params.get('load_balancer')),
'ssl': module.boolean(module.params.get('ssl')),
'proxy_cache': module.boolean(module.params.get('proxy_cache')),
'healthcheck': module.boolean(module.params.get('healthcheck')),
'ip_version': module.params.get('ip_version'),
'load_balancer_algorithm': module.params.get('load_balancer_algorithm'),
'service': module.params.get('service'),
'port': module.params.get('port'),
'session_type': module.params.get('session_type'),
'master_service_id': module.params.get('master_service_id'),
}
if params['port']:
params['port'] = int(params['port'])
api = APIWrapper(module, OktawaveApi(username=okta_username, password=okta_password))
if oci_id:
manage_container_membership(module, api, name, oci_id, state)
else:
manage_container(module, api, name, params, state)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()