-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.py
111 lines (100 loc) · 4.02 KB
/
helper.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
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
from ibm_vpc import VpcV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_cloud_sdk_core import ApiException
from json import JSONDecoder
service = None
# Main entry function
def helper(params):
authenticator = IAMAuthenticator(
params["IAM_API_KEY"])
global service
service = VpcV1('2020-06-02', authenticator=authenticator)
response = get_instance_id(params)
return response
# To extract JSON objects from the incoming activity tracker with LogDNA alert
def extract_json_objects(text, decoder=JSONDecoder()):
pos = 0
while True:
match = text.find('{', pos)
if match == -1:
break
try:
result, index = decoder.raw_decode(text[match:])
yield result
pos = match + index
except ValueError:
pos = match + 1
# Get the network interface ID associated with an Instance(VSI)
def get_network_interface_id(instance_Id):
# Get instance
try:
instance = service.get_instance(
id=instance_Id
).get_result()
except ApiException as e:
print("Get instance failed with status code " +
str(e.code) + ": " + e.message)
return instance['network_interfaces'][0]['id']
# Get an unbound floating IP. If none exists, create a new floating IP
def get_floating_ip_id(fip_name,zone):
print("List Floating IPs")
try:
floating_ips = service.list_floating_ips().get_result()['floating_ips']
except ApiException as e:
print("List floating IPs failed with status code " + str(e.code) + ": " + e.message)
unassigned_fip_List=[]
floating_ip_id=""
key='target'
for floating_ip in floating_ips:
if key not in floating_ip:
unassigned_fip_List.append(floating_ip['id'])
if not unassigned_fip_List:
zone_identity_model = {}
zone_identity_model['name'] = zone
# Construct a dict representation of a FloatingIPPrototypeFloatingIPByZone model
floating_ip_prototype_model = {}
floating_ip_prototype_model["name"] = fip_name
# floating_ip_prototype_model['resource_group'] = resource_group_identity_model
floating_ip_prototype_model["zone"] = zone_identity_model
# Set up parameter values
floating_ip_prototype = floating_ip_prototype_model
# Invoke method
print("Reserve Floating IP")
try:
ip = service.create_floating_ip(
floating_ip_prototype=floating_ip_prototype, headers={}).get_result()
except ApiException as e:
print("Create Floating IP failed with status code " +
str(e.code) + ": " + e.message)
floating_ip_id=ip['id']
else:
floating_ip_id=unassigned_fip_List[0]
print("A floating IP is created with an id:",floating_ip_id)
return floating_ip_id
# Reserve the floating IP to the provisioned instance
def add_instance_floating_ip(instance_id, network_interface_id,zone):
# Reserve a floating IP
floating_ip_id=get_floating_ip_id(instance_id.split('-')[-1]+"-floating-ip",zone)
try:
response = service.add_instance_network_interface_floating_ip(
instance_id=instance_id,
network_interface_id=network_interface_id,
id=floating_ip_id
).get_result()
except ApiException as e:
print("Attaching floating IP to the instance failed with status code " + str(e.code) + ": " + e.message)
return response
# Get the instance ID of the provisioned VSI
def get_instance_id(dict):
finalResponse=[]
for result in extract_json_objects(dict['lines']):
try:
instance_Id=result['_app'][result['_app'].find('instance:')+9:]
instance_Id=instance_Id.replace(" ","")
zone=result['_app'].split(':')[5]
network_interface_id=get_network_interface_id(instance_Id)
response=add_instance_floating_ip(instance_Id,network_interface_id,zone)
finalResponse.append(response)
except:
continue
return finalResponse