forked from nutanixdev/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_vm_v3_basic
More file actions
executable file
·54 lines (45 loc) · 1.41 KB
/
create_vm_v3_basic
File metadata and controls
executable file
·54 lines (45 loc) · 1.41 KB
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
#!/bin/bash
# shell script to create a basic VM using the Prism v3 REST API
# requires the existence of a JSON-formatted file that contains:
#
# cluster/CVM IP
# username
# the name of the VM to be created
#
# this file must be named "create_vm_v3_basic.json" and be formatted as follows:
#
# {"cluster_ip":"10.0.0.1","username":"admin","vm_name":"BasicVMViaAPIv3"}
JSON_FILE="./create_vm_v3_basic.json"
JSON_CONTENTS="`cat ${JSON_FILE}`"
JQ=`command -v jq`
CURL=`command -v curl`
if [ "$JQ" = "" ] || [ "$CURL" = "" ]; then
echo "jq command not found."
else
CLUSTER_IP=`echo -n $JSON_CONTENTS | jq -r ".cluster_ip"`
USERNAME=`echo -n $JSON_CONTENTS | jq -r ".username"`
VM_NAME=`echo -n $JSON_CONTENTS | jq -r ".vm_name"`
# get the password from the user
echo "Please enter your cluster password (will not be shown on screen):"
read -s PASSWORD
# generate the HTTP Basic Authorization header
AUTH_HEADER="`echo -n $USERNAME:$PASSWORD | base64`"
# submit the request
curl --insecure -X POST \
--connect-timeout 5 \
https://$CLUSTER_IP:9440/api/nutanix/v3/vms \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Basic $AUTH_HEADER" \
-H "cache-control: no-cache" \
-d "{
\"spec\":{
\"name\":\"$VM_NAME\",
\"resources\":{
}
},
\"metadata\":{
\"kind\":\"vm\"
}
}"
fi