-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.sh
268 lines (243 loc) · 8.98 KB
/
functions.sh
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
#!/bin/bash
source config.sh
##########################################################################################
# Function: quiesce
# Description: temporarily pauses system processing
#
# Inputs: param($1) Time (in seconds) to pause
#
##########################################################################################
quiesce() {
echo "waiting for system to stablize..."
sleep $1
echo
}
##########################################################################################
# Function: taskStatus
# Description: temporarily blocks while checking task status. When the task status
# reaches the condition set by param($2), the function unblocks, and resumes
# processing.
#
# Inputs: param($1): task id json
# param($2): status to check for
#
# Return: Task status (succeeded, failed) detected by system
##########################################################################################
taskStatus() {
TASK_ID=$(echo $1 | jq -r ".${KEY_TASK_UUID}")
JSON=$(getResource $RESOURCE_TK$TASK_ID)
TASK_STATUS=$(echo $JSON | jq -r ".${KEY_TASK_STATUS}")
while [ $TASK_STATUS == $2 ]; do
JSON=$(getResource $RESOURCE_TK$TASK_ID)
TASK_STATUS=$(echo $JSON | jq -r ".${KEY_TASK_STATUS}")
sleep 1
done
echo $TASK_STATUS
}
##########################################################################################
# Function: getResourceEntityProperty
# Description: Calls function::getResource{$1} and determines the number entities/resurce.
# Iterates through the entity[] array for the number of entities while
# comparing the value for a given key{$2} with a given value{$}. If the value
# is found, the function returns a value for an lternate key{$4}
#
# Inputs: param{$1}: resource (i.e. /vms/, /images/, etc...)
# param{$2}: key to compare its associated value
# param{$3}: value to compare the value from key{$2}
# param{$4}: key to return the value
#
# return: json body for specified resource
##########################################################################################
getResourceEntityValue() {
RET=" value not found..."
JSON=$(getResource $1)
TOTAL=$(echo $JSON | jq -r ".${KEY_METADATA}.${KEY_TOTAL_ENTITIES}")
for ((i=0; i < $TOTAL; i++))
do
VAL=$(echo $JSON | jq -r ".${KEY_ENTITY_ARRAY}[${i}].${2}")
if [[ $VAL == *"${3}"* ]]
then
RET=$(echo $JSON | jq -r ".${KEY_ENTITY_ARRAY}[${i}].${4}")
break
fi
done
echo $RET
}
##########################################################################################
# Function: getResource
# Description: Issues HTTP "GET /resource/" using curl and returns raw json for a i
# specified resource:{$1}.
#
# Inputs: param{$1}: resource (i.e. /vms/, /images/, etc...)
#
# return: raw json body for a specified resource
##########################################################################################
getResource() {
VALUE=$(curl --insecure -s -H $CT -X GET -u $USER:$PASSWD "$SERVICE_URL$1")
echo $VALUE
}
##########################################################################################
# Function: getResourceValue
# Description: Issues HTTP "GET /resource/" using curl and returns a value for a given
# key:{$2}
#
# Inputs: param{$1}: resource (i.e. /vms/, /images/, etc...)
# param{$2}: key (i.e. name, uuid, etc...)
#
# return: $VALUE representing a value for a spcified key given a resource name
##########################################################################################
getResourceValue() {
JSON=$(getResource $1)
VALUE=$(echo $JSON | jq -r $2)
echo $VALUE
}
##########################################################################################
# Function: getResourceValues
# Description: Calls function::getResource{$1} and determines the number entities/resource
# instance. Iterates through the number of entities in the entity[] array,
# capturing values for a given key{$2} and concatenating them to a string
# delimited with ";".
#
# Inputs: param{$1}: resource (i.e. /vms/, /images/, etc...)
# param{$2}: key for associated value
#
# return: A an array [] of values, where each value represents a resource instance, for a
# given key.
##########################################################################################
getResourceValues() {
ARRAY=()
JSON=$(getResource $1)
TOTAL=$(echo $JSON | jq -r ".${KEY_METADATA}.${KEY_TOTAL_ENTITIES}")
for ((i=0; i < $TOTAL; i++))
do
VAL=$(echo $JSON | jq -r ".${KEY_ENTITY_ARRAY}[${i}].${2}")
ARRAY+=("$VAL;")
done
echo "${ARRAY[@]}"
}
##########################################################################################
# Function: createResource
# Description: creates a resource{$1} instance.
#
# Inputs: param{$1}: resource (i.e. /vms/, /images/, etc...)
# param{$@}: func name and params needed to create & return the JSON message-body.
#
# return: response-body containing a uuid of the create process for task-management.
#
##########################################################################################
createResource() {
VALUE=$(curl --insecure -s -H $ACCEPT -H $CT -X POST -u $USER:$PASSWD \
--data "$(${@:2})" "$SERVICE_URL$1")
echo $VALUE
}
##########################################################################################
# Function: deleteResource
# Description: Deletes a resource{$1} instance referenced by the uuid param{$2}
#
# Inputs: param{$1}: resource (i.e. /vms/, /images/, etc...)
# param{$2}: uuid value representing resource instance to be deleted
#
# return: response-body containing uuid of the delete process for task-management.
#
##########################################################################################
deleteResource() {
VALUE=$(curl --insecure -s -H $ACCEPT -H $CT -X DELETE -u $USER:$PASSWD "$SERVICE_URL$1$2")
echo $VALUE
}
##########################################################################################
# Function: setPowerState
# Description: powers a resource instance{$1} ON/OFF, referenced by the uuid param{$2}
#
# Inputs: param{$1}: resource (i.e. /vms/, /images/, etc...)
# param{$2}: uuid value representing resource instance to be deleted
# param{$3}: power state ON/OFF
#
# return: response-body containing uuid for task management
#
##########################################################################################
setPowerState() {
VALUE=$(curl --insecure -s -H $ACCEPT -H $CT -X POST -u $USER:$PASSWD \
--data "$(${@:2})" "$SERVICE_URL$1$3$RESOURCE_VM_PWR_STATE")
echo $VALUE
}
##########################################################################################
# Function: _GEN_VMPOST_MSG
# Description: creates and returns a JSON message-body for VM creation
#
# Inputs: param{$1}: vmdisk_uuid for windows image
# param{$2}: storage_container_uuid for default-container
# param{$3}: vmdisk_uuid for Nutanix Virt-IO image
# param{$4}: vmdisk size in bytes
#
# return: JSON
#
##########################################################################################
GEN_VM_CREATE_MSG() {
JSON='{
"description":"Tech Summit 2017",
"guest_os":"Windows Server 2012 R2",
"memory_mb":4096,
"name":"W2K12R2",
"num_cores_per_vcpu":1,
"num_vcpus":1,
"vm_disks":[
{
"disk_address":{
"device_bus":"ide",
"device_index":0
},
"is_cdrom":true,
"is_empty":false,
"vm_disk_clone":{
"disk_address":{
"vmdisk_uuid":"'"$1"'"
}
}
},
{
"disk_address":{
"device_bus":"scsi",
"device_index":0
},
"vm_disk_create":{
"storage_container_uuid":"'"$2"'",
"size":"'"$4"'"
}
},
{
"disk_address":{
"device_bus":"ide",
"device_index":1
},
"is_cdrom":true,
"is_empty":false,
"vm_disk_clone":{
"disk_address":{
"vmdisk_uuid":"'"$3"'"
}
}
}
],
"hypervisor_type":"ACROPOLIS",
"affinity":null
}'
echo $JSON
}
##########################################################################################
# Function: GEN_VM_POWER_MSG
# Description: creates and returns a JSON message-body for VM Power State Transition
#
# Inputs: param{$1}: uuid of VM
# param{$2}: power state ON/OFF
#
# return: JSON
#
##########################################################################################
GEN_VM_POWER_MSG() {
JSON='{
"transition": "'"$2"'",
"uuid": "'"$1"'"
}'
echo $JSON
}
#eof