forked from oracle-samples/oracle-functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.py
82 lines (73 loc) · 2.97 KB
/
func.py
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
#
# oci-compute-control-python version 1.0.
#
# Copyright (c) 2020 Oracle, Inc.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
#
import io
import json
import oci
from fdk import response
def instance_status(compute_client, instance_id):
return compute_client.get_instance(instance_id).data.lifecycle_state
def instance_start(compute_client, instance_id):
print('Starting Instance: {}'.format(instance_id))
try:
if instance_status(compute_client, instance_id) in 'STOPPED':
try:
resp = compute_client.instance_action(instance_id, 'START')
print('Start response code: {0}'.format(resp.status))
except oci.exceptions.ServiceError as e:
print('Starting instance failed. {0}' .format(e))
raise
else:
print('The instance was in the incorrect state to start' .format(instance_id))
raise
except oci.exceptions.ServiceError as e:
print('Starting instance failed. {0}'.format(e))
raise
print('Started Instance: {}'.format(instance_id))
return instance_status(compute_client, instance_id)
def instance_stop(compute_client, instance_id):
print('Stopping Instance: {}'.format(instance_id))
try:
if instance_status(compute_client, instance_id) in 'RUNNING':
try:
resp = compute_client.instance_action(instance_id, 'STOP')
print('Stop response code: {0}'.format(resp.status))
except oci.exceptions.ServiceError as e:
print('Stopping instance failed. {0}' .format(e))
raise
else:
print('The instance was in the incorrect state to stop' .format(instance_id))
raise
except oci.exceptions.ServiceError as e:
print('Stopping instance failed. {0}'.format(e))
raise
print('Stopped Instance: {}'.format(instance_id))
return instance_status(compute_client, instance_id)
def handler(ctx, data: io.BytesIO=None):
try:
body = json.loads(data.getvalue())
instance_ocid = body.get("instance_ocid")
command = body.get("command")
except (Exception) as ex:
print("Two arguments need to be passed to the function, instance_ocid and the command")
print(str(ex), flush=True)
raise
signer = oci.auth.signers.get_resource_principals_signer()
compute_client = oci.core.ComputeClient(config={}, signer=signer)
if command == 'status':
resp = instance_status(compute_client, instance_ocid)
elif command == 'start':
resp = instance_start(compute_client, instance_ocid)
elif command == 'stop':
resp = instance_stop(compute_client, instance_ocid)
else:
print("command not supported", flush=True)
raise
return response.Response(
ctx,
response_data=json.dumps({"status": "{0}".format(resp)}),
headers={"Content-Type": "application/json"}
)