-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_sh.functions
98 lines (82 loc) · 2.75 KB
/
vm_sh.functions
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
#!/bin/bash
function vm_creds () {
[[ -z $vmcenter ]] && read -p "vCenter: " vmcenter
[[ -z $vmuser ]] && read -p "User: " vmuser
[[ -z $vmpass ]] && read -s -p "Password: " vmpass
export vmcenter vmuser vmpass
}
#### end function vm_creds
function vm_session_start () {
vm_creds
curl -v -L -k -c $vm_cookie_jar -X POST -H 'Accept: application/json' \
--basic -u $vmuser:$vmpass \
https://$vmcenter/rest/com/vmware/cis/session?~action=create 2>$vm_res_err > $vm_res
vm_session=$(jq -e -r .value $vm_res)
}
#### end function vm_session_start
function vm_session_get () {
vm_creds
curl -v -L -k -c $vm_cookie_jar -X GET -H 'Accept: application/json' \
-H "vmware-api-session-id: $vm_session" \
"https://$vmcenter/rest/com/vmware/cis/session" 2>$vm_res_err | tee $vm_res
res=$(jq -e $vm_res)
}
#### end function vm_session_check
function vm_session_check () {
vm_creds
curl -v -L -k -c $vm_cookie_jar -X POST -H 'Accept: application/json' \
--basic -u $vmuser:$vmpass \
-H "vmware-api-session-id: $vm_session" \
"https://$vmcenter/rest/com/vmware/cis/session?~action=get" 2>$vm_res_err | tee $vm_res
res=$(jq -e $vm_res)
}
#### end function vm_session_check
function vm_session_delete () {
vm_creds
curl -v -L -k -c $vm_cookie_jar -X DELETE -H 'Accept: application/json' \
-H "vmware-api-session-id: $vm_session" \
https://$vmcenter/rest/com/vmware/cis/session 2>$vm_res_err | tee $vm_res
#--basic -u $vmuser:$vmpass \
res=$(jq -e $vm_res)
}
#### end function vm_session_delete
function vm_help () {
vm_api -X GET https://$vmcenter/rest/com/vmware/vapi/rest/navigation/component | jq .value[]
echo ; echo
vm_api -X GET https://$vmcenter/rest/com/vmware/vapi/rest/navigation/resource | jq .value[]
echo ; echo
vm_api -X GET https://$vmcenter/rest/com/vmware/content/type | jq .value[]
}
#### end function vm_help
function vm_ls_host () {
vm api -X GET https://$vmcenter/rest/vcenter/host
}
#### end function vm_ls_host
function vm_api () {
if [[ -z $vm_session ]] ; then
if ! vm_session_start ; then
return 11
fi
fi
curl -v -L -k -c $vm_cookie_jar -H 'Accept: application/json' -H "vmware-api-session-id: $vm_session" "$@" 2>$vm_res_err | tee $vm_res
}
#### end function vm_api
function vm () {
[[ -z $vm_cookie_jar ]] && vm_cookie_jar=$(mktemp)
[[ -z $vm_res ]] && vm_res=$(mktemp)
[[ -z $vm_res_err ]] && vm_res_err=$(mktemp)
# echo $vm_res $vm_res_err
vm_creds
local action=$1
shift
case $action in
session|ls)
vm_${action}_"$@"
;;
*)
vm_${action} "$@"
;;
esac
#rm -f $vm_res $vm_res_err
}
#### end function vm