-
Notifications
You must be signed in to change notification settings - Fork 1
/
oracle_cloud_dbpaas
387 lines (347 loc) · 17.4 KB
/
oracle_cloud_dbpaas
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: oracle_cloud_dbpaas
short_description: Manage Oracle Instance Services in the Oracle Cloud
description:
- Best way to use this module in task playbooks is to delegate_to: 127.0.0.1
version_added: "0.1.0"
options:
identityDomain:
description:
- Name of the identity domain housing the Database as a Service subscription or trial to which the request applies.
required: true
default:
user:
description:
-
required: true
default:
password:
description:
-
required: true
default:
state:
description:
- The intended state of the Oracle Cloud Instance
- present = Create New Cloud Service
- absent = Delete Existen Cloud Service
- start = Start Existen Cloud Service VM
- stop = Stop Existen Cloud Service VM
- restart = Restart Existen Cloud Service VM
- scale = Scale Up or Down Cloud Service VM
required: true
choices=["present","absent","start", "stop", "restart","scale"]
serviceName:
description:
- A string containing the name for service instance.
required: true
default:
level:
description:
- A string containing the service level for the service instance:
- PAAS: The Oracle Database Cloud Service service level
- BASIC: The Oracle Database Cloud Service - Virtual Image service level
required: false
default: PAAS
choices=["PAAS","BASIC"]
subscriptionType:
description:
- A string containing the billing frequency for the service instance; either MONTHLY or HOURLY.
required: false
default:
version:
description:
- A string containing the Oracle Database version for the service instance:
- 12.1.0.2
- 11.2.0.4
required: false
default: 12.1.0.2
choices=["12.1.0.2","11.2.0.4"]
edition:
description:
- A string containing the database edition for the service instance:
- SE: Standard Edition
- EE: Enterprise Edition
- EE_HP–Enterprise Edition: High Performance
- EE_EP–Enterprise Edition: Extreme Performance
required: false
default: "EE"
choices=["SE","EE","EE_HP","EE_EP"]
description:
description:
- A string containing a description of the service instance, if desired.
required: false
default:
shape:
description:
- A string containing the Oracle Compute Cloud shape to scale the service instance up to.
required: false
default: oc3
choices=["oc3","oc4","oc5","oc6","oc7","oc1m","oc2m","oc3m","oc4m","oc5m"]
vmPublicKey:
description:
- A string containing the fully qualified name of an SSH public key already uploaded to Oracle Compute Cloud service. This string has the form:
- Either this parameter or the vmPublicKey parameter must be provided, but not both of them.
required: false
default:
vmPublicKeyText:
description:
- A string containing the text of an SSH public key. This key is added to Oracle Compute Cloud Service as part of the instance creation operation.
- Either this parameter or the vmPublicKey parameter must be provided, but not both of them.
required: false
default:
usableStorage:
description:
- A string containing the number of GB of usable data storage for the Oracle Database server .
required: false
default:
adminPassword:
description:
- A string containing the administrator password for the service instance.
required: false
default:
sid:
description:
- A string containing the SID for the database instance.
required: false
default:
pdbName:
description:
- A string containing the name for the default PDB (pluggable database).
- Include this parameter only if the level is "PAAS", the version is "12.1.0.2" and the edition is EE, EE_HP or EE_EP.
required: false
default:
backupDestination:
description:
- A string containing the backup configuration for the service instance:
- NONE: Configure no backups.
- DISK: Configure backups to local storage on the service instance.
- BOTH: Configure backups to local storage on the service instance and to an Oracle Storage Cloud container.
- Include this parameter only if the level is "PAAS".
required: false
default: NONE
choices=["NONE","DISK","BOTH"]
cloudStorageContainer:
description:
- A string containing the Oracle Storage Cloud container for backups. This string has the form: instance-id_domain\/container
required: false
default:
cloudStorageUser:
description:
- The user name of an Oracle Cloud user with read/write access to the specified
required: false
default:
cloudStoragePwd:
description:
- A string containing the password of the specified cloudStorageUser.
required: false
default:
hostname:
description:
- The Oracle database host
required: false
default: localhost
notes:
- required=True needs to be installed
requirements: [ "request" ]
author: Björn Ahl, bjorn.ahl@gmail.com, @TwittAhl
'''
EXAMPLES = '''
# Stop VM in Oracle Cloud
oracle_cloud_dbpaas: identityDomain: domain1 user: oracle_cloud_admin_user password: oracle_cloud_admin_password serviceName: Oracle Cloud Service Name state: stop
# start VM in Oracle Cloud
oracle_cloud_dbpaas: identityDomain: domain1 user: oracle_cloud_admin_user password: oracle_cloud_admin_password serviceName: Oracle Cloud Service Name state: start
# restart VM in Oracle Cloud
oracle_cloud_dbpaas: identityDomain: domain1 user: oracle_cloud_admin_user password: oracle_cloud_admin_password serviceName: Oracle Cloud Service Name state: restart
# Delete Cloud Service in Oracle Cloud (Service that you want to delete need to be stopped state)
oracle_cloud_dbpaas: identityDomain: domain1 user: oracle_cloud_admin_user password: oracle_cloud_admin_password serviceName: Oracle Cloud Service Name state: absent
# Scale Up/Down Cloud Service
oracle_cloud_dbpaas: identityDomain: domain1 user: oracle_cloud_admin_user password: oracle_cloud_admin_password serviceName: Oracle Cloud Service Name state: scale shape: oc5/oc6/oc1m/oc2m/oc3m..
# Create new Oracle Cloud Service
oracle_cloud_dbpaas: identityDomain: domain1 user: oracle_cloud_admin_user password: oracle_cloud_admin_password serviceName: Oracle Cloud Service Name state: present serviceName: "vm1" level: PAAS subscriptionType: MONTHLY version: "12.1.0.2" edition: EE description: test01 shape: oc3 vmPublicKey: "/Compute-domain/user@test.se/host" usableStorage: "30" adminPassword: "Pa55_word" sid: "cdb1"pdbName: "pdb1" backupDestination: "NONE"
'''
try:
import requests
import time
except ImportError:
requests_exists = False
else:
requests_exists = True
"""Return jobStatus of job send to Oracle Cloud
"""
def dbpaasJobStatus(url, restSession):
returnData = restSession.get(url)
jobStatus = json.loads(returnData.text)
return jobStatus['job_status']
def getServiceInformation(module, restSession):
url = 'https://dbaas.oraclecloud.com/paas/service/dbcs/api/v1.1/instances/' + module.params["identityDomain"] + "/" + module.params["serviceName"]
r = restSession.get(url)
ServiceInformation = json.loads(r.text)
return ServiceInformation
def dbpaasExists(module, restSession):
url = 'https://dbaas.oraclecloud.com/paas/service/dbcs/api/v1.1/instances/' + module.params["identityDomain"]
r = restSession.get(url)
parsed_json = json.loads(r.text)
if any(x['service_name']==module.params["serviceName"] for x in parsed_json['services']):
return True
else:
return False
def scaleUpOrDownService(module, restSession):
serviceInformation = getServiceInformation(module, restSession)
if serviceInformation["shape"] != module.params["shape"]:
payLoad = {}
payLoad["shape"] = module.params["shape"]
url = 'https://dbaas.oraclecloud.com/paas/service/dbcs/api/v1.1/instances/' + module.params["identityDomain"] + "/" + module.params["serviceName"]
returnHeaders = restSession.put(url, data=json.dumps(payLoad))
if returnHeaders.status_code == 202:
url = returnHeaders.headers['Location']
while dbpaasJobStatus(url,restSession) in ('InProgress','WaitingOnResource',"Maintenance"):
time.sleep(5)
message = 'Service %s successfully scaled' % (module.params["serviceName"])
module.exit_json(msg=message, changed=True)
else:
module.fail_json(msg="Wrong ResponseCode")
elif serviceInformation["shape"] == module.params["shape"]:
message = 'Service %s in same shape' % (module.params["serviceName"])
module.exit_json(msg=message, changed=False)
def deleteService(module,restSession):
serviceInformation = getServiceInformation(module, restSession)
if dbpaasExists(module, restSession) and serviceInformation["status"] == "Stopped":
if module.check_mode:
message = 'Service %s successfully will be deleted' % (module.params["serviceName"])
module.exit_json(msg=message, changed=True)
else:
url = 'https://dbaas.oraclecloud.com/paas/service/dbcs/api/v1.1/instances/' + module.params["identityDomain"] + "/" + module.params["serviceName"]
returnHeaders = restSession.delete(url)
if returnHeaders.status_code == 202:
url = returnHeaders.headers['Location']
while dbpaasJobStatus(url,restSession) in ('InProgress','WaitingOnResource',"Terminating"):
time.sleep(5)
message = 'Service %s successfully will be deleted' % (module.params["serviceName"])
module.exit_json(msg=message, changed=True)
else:
module.fail_json(msg="Wrong ResponseCode")
elif not dbpaasExists(module, restSession):
message = 'Service %s not exists' % (module.params["serviceName"])
module.exit_json(msg=message, changed=False)
else:
module.fail_json(msg="Service in wrong state")
def createService(module,restSession):
payload = {}
payload["serviceName"] = module.params["serviceName"]
payload["version"] = module.params["version"]
payload["level"] = module.params["level"]
payload["edition"] = module.params["edition"]
payload["subscriptionType"] = module.params["subscriptionType"]
payload["description"] = module.params["description"]
payload["shape"] = module.params["shape"]
payload["vmPublicKey"] = module.params["vmPublicKey"]
payload["parameters"] = []
if module.params["level"] == 'BASIC':
payload["parameters"].append(dict(type="db",adminPassword=module.params["adminPassword"],usableStorage=module.params["usableStorage"],sid=module.params["sid"],pdb=module.params["pdbName"],failoverDatabase="no")) #,backupDestination=module.params["backupDestination"]))
elif module.params["level"] == 'PAAS':
payload["parameters"].append(dict(type="db",adminPassword=module.params["adminPassword"],usableStorage=module.params["usableStorage"],sid=module.params["sid"],pdb=module.params["pdbName"],failoverDatabase="no",backupDestination=module.params["backupDestination"]))
if not dbpaasExists(module, restSession):
if module.check_mode:
message = 'Service %s successfully will be created' % (module.params["serviceName"])
module.exit_json(msg=message, changed=True)
else:
url = 'https://dbaas.oraclecloud.com/paas/service/dbcs/api/v1.1/instances/' + module.params["identityDomain"]
returnHeaders = restSession.post(url, data=json.dumps(payload))
if returnHeaders.status_code == 202:
url = returnHeaders.headers['Location']
while dbpaasJobStatus(url,restSession) in ('InProgress','WaitingOnResource'):
time.sleep(5)
message = 'Service %s successfully created' % (module.params["serviceName"])
module.exit_json(msg=message, changed=True)
else:
module.fail_json(msg="Wrong ResponseCode")
else:
message = 'Service %s already exists' % (module.params["serviceName"])
module.exit_json(msg=message, changed=False)
def setChangeLifecycleState(module, restSession):
payLoad = {}
payLoad["lifecycleState"] = module.params["state"]
url = "https://dbaas.oraclecloud.com/paas/service/dbcs/api/v1.1/instances/" + module.params["identityDomain"] + "/" + module.params["serviceName"]
returnHeaders = restSession.post(url, data=json.dumps(payLoad))
if returnHeaders.status_code == 202:
url = returnHeaders.headers['Location']
while dbpaasJobStatus(url,restSession) in ('InProgress','WaitingOnResource'):
time.sleep(5)
else:
module.fail_json(msg="Wrong ResponseCode")
def changeLifecycleState(module, restSession):
serviceInformation = getServiceInformation(module, restSession)
if module.params["state"] in ("stop","restart"):
if serviceInformation["status"] == "Running":
if module.check_mode:
message = 'Service %s successfully %s' % (module.params["serviceName"], module.params["state"])
module.exit_json(msg=message, changed=True)
else:
setChangeLifecycleState(module, restSession)
message = 'Service %s successfully %s' % (module.params["serviceName"], module.params["state"])
module.exit_json(msg=message, changed=True)
else:
msg[0] = 'Service %s already %s' % (module.params["serviceName"], module.params["state"])
module.exit_json(msg=message, changed=False)
elif module.params["state"] in ("start"):
if serviceInformation["status"] == "Stopped":
if module.check_mode:
message = 'Service %s successfully %s' % (module.params["serviceName"], module.params["state"])
module.exit_json(message, changed=True)
else:
setChangeLifecycleState(module, restSession)
message = 'Service %s successfully %s' % (module.params["serviceName"], module.params["state"])
module.exit_json(msg=message, changed=True)
else:
msg = 'Service %s already %s' % (module.params["serviceName"], module.params["state"])
module.exit_json(msg=message, changed=False)
else:
null
def main():
msg = ['']
module = AnsibleModule(
argument_spec = dict(
identityDomain = dict(required=True),
user = dict(required=True),
password = dict(required=True),
state = dict(choices=["present","absent","start", "stop", "restart","scale"]),
serviceName = dict(required=True),
level = dict(default="PAAS", choices=["PAAS","BASIC"]),
subscriptionType = dict(default="MONTHLY", choices=["MONTHLY","HOURLY"]),
version = dict(default="12.1.0.2", choices=["12.1.0.2","11.2.0.4"]),
edition = dict(default="EE", choices=["SE","EE","EE_HP","EE_EP"]),
description = dict(required=False),
shape = dict(default="oc3", choices=["oc3","oc4","oc5","oc6","oc7","oc1m","oc2m","oc3m","oc4m","oc5m"]),
vmPublicKey = dict(required=False),
vmPublicKeyText = dict(required=False),
parameters = dict(required=False),
usableStorage = dict(required=False),
adminPassword = dict(required=False),
sid = dict(required=False),
pdbName = dict(required=False),
#failoverDatabase = 'no',
backupDestination = dict(default="NONE", choices=["NONE","DISK","BOTH"]),
cloudStorageContainer = dict(required=False),
cloudStorageUser = dict(required=False),
cloudStoragePwd = dict(required=False),
),
supports_check_mode=True
)
restSession = requests.Session()
restSession.auth = (module.params["user"], module.params["password"])
restSession.headers.update({'X-ID-TENANT-NAME':module.params["identityDomain"] ,'Content-Type':'application/json'})
if module.params["state"] == "present":
createService(module, restSession)
elif module.params["state"] == "absent":
deleteService(module, restSession)
elif module.params["state"] in ("start", "stop", "restart"):
changeLifecycleState(module, restSession)
elif module.params["state"] == "scale":
scaleUpOrDownService(module, restSession)
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()