-
Notifications
You must be signed in to change notification settings - Fork 1
/
vacli.bash
185 lines (174 loc) · 6.71 KB
/
vacli.bash
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
#!/usr/bin/env bash
# -------------------------------------------------------------------------------
#
# PROPRIETARY/CONFIDENTIAL
# Copyright (c) 2015 Verizon, All Rights Reserved.
# Not for disclosure without written permission.
#
# Author: Slava Vladyshevsky <slava(a)verizon.com>
# Project: Verizon Cloud Automation
#
# Bash auto-completion for vacli tool
#
# Far better idea would be to implement complete state matrix, however, this
# quick-hack function does the trick for the most part of the time.
#
# -------------------------------------------------------------------------------
_vacli()
{
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
#
# Global CLI options that we will auto-complete if no command provided
#
glob_args="--api-access-key --api-account --api-cloud-space --api-endpoint --api-secret-key \
--log-file --log-level --config-file --profile --http-proxy"
#
# Basic CLI commands we'll complete. Note leading and trailing spaces
# in the following string. They needed to distinguish commands possibly
# starting or ending with the same sub-string
#
commands=" get delete options patch post put get-href get-tags get-root get-admin-root get-resource-groups \
job-list job-poll fw-acl-list fw-acl-add fw-acl-del public-ip-list public-ip-add public-ip-del \
list-vdisk-templates list-vm-templates list-vdisks list-vnets list-vms list-vnics vdisk-create vdisk-edit \
vnet-create vnet-edit vm-create vm-edit vm-add-vnic vm-add-vdisk vm-list-mounts vnic-edit update-iops vm-ctl "
#
# Looking for known CLI commands in the command line, so that we will
# auto-complete options corresponding and valid for this command only
#
cmd=""
for word in ${COMP_WORDS[@]}; do
if [[ ${commands} =~ " ${word} " ]] ; then
cmd=${word}
break
fi
done
#
# Auto-complete arguments for the CLI command
#
if [[ ${cmd} && ${cur} != ${cmd} ]] ; then
#
# Suggest the choice for command arguments with known options
#
if [[ ${prev} == -* ]] ; then
case "${prev}" in
--json-file)
COMPREPLY=( $(compgen -f -- ${cur}) )
return 0
;;
--ip)
args=$(./vacli public-ip-list --table address) ;;
--proto)
args="TCP UDP ICMP ESP ALL" ;;
--action)
args="DISCARD REJECT ACCEPT" ;;
--cmd)
args="reboot reset shutdown power-on power-off" ;;
--type)
args="vms vnets vdisks tags vdiskTemplates vmTemplates jobHistory ipAddresses networkBoundaries" ;;
*)
args="" ;;
esac
if [[ ${args} ]] ; then
COMPREPLY=( $(compgen -W "${args}" -- ${cur}) )
return 0
fi
fi
#
# Suggesting context specific command arguments, if command has been provided in the command line
#
case "${cmd}" in
get|delete|options)
args="--href --headers --table" ;;
patch|post|put)
args="--href --headers --json-file --dry-run" ;;
get-href)
args="--id --tag --type" ;;
get-tags)
args="--table" ;;
get-resource-groups)
args="--table" ;;
job-list)
args="--last --table" ;;
job-poll)
args="--job --timeout --poll-interval --table" ;;
fw-acl-list)
args="--ip --ip-ref --table" ;;
fw-acl-add)
args="--ip --ip-ref --idx --proto --action --src-cidr --src-port --dst-cidr --dst-port --dry-run" ;;
fw-acl-del)
args="--ip --ip-ref --idx --all --dry-run" ;;
public-ip-list)
args="--tag --with-vms --table" ;;
public-ip-add)
args="--tags --name --wait --dry-run" ;;
public-ip-del)
args="--ip --ip-ref --dry-run" ;;
public-ip-list)
args="--tag --with-vms --table" ;;
list-vdisk-templates|list-vm-templates)
args="--table" ;;
list-vdisks|list-vnets|list-vms)
args="--tag --table" ;;
list-vnics)
args="--vm --vnet --table" ;;
vdisk-create)
args="--name --description --tags --size --from-template --from-snapshot --fault-tolerance --wait --dry-run" ;;
vdisk-edit)
args="--vdisk --name --tags --description --dry-run" ;;
vnet-create)
args="--name --description --tags --cidr --wait --dry-run" ;;
# vnet-edit)
# args="--vnet --name --tags --description" ;;
vm-create)
args="--name --description --tags --cpus --cpu-speed --memory --iops --vdisks --vdisk-template \
--public-ip --vnet --mac --bandwidth --guest-options --wait --dry-run" ;;
vm-edit)
args="--vm --name --description --tags --cpus --cpu-speed --memory --guest-options --dry-run" ;;
vm-add-vnic)
args="--vm --vnet --ipv4 --mac --description --public-ip --bandwidth --wait --dry-run" ;;
vm-add-vdisk)
args="--vm --vdisk --iops --boot --wait --dry-run" ;;
vm-list-mounts)
args="--vm --table" ;;
vnic-edit)
args="--vnic --description --bandwidth --wait --dry-run" ;;
update-iops)
args="--vdisk-mount --iops" ;;
vm-ctl)
args="--vm --cmd --force --wait" ;;
*)
args=${glob_args} ;;
esac
COMPREPLY=( $(compgen -W "${args}" -- ${cur}) )
return 0
elif [[ ${prev} == -* ]] ; then
#
# Complete the choice for global arguments with known options.
#
case "${prev}" in
--log-level)
args="critical error warning info debug" ;;
--profile)
args="default" ;;
--config-file)
COMPREPLY=( $(compgen -f -- ${cur}) )
return 0
;;
*)
args=${glob_args} ;;
esac
COMPREPLY=( $(compgen -W "${args}" -- ${cur}) )
return 0
fi
if [[ ${cur} == -* ]] ; then
args=${glob_args}
else
args=${commands}
fi
COMPREPLY=($(compgen -W "${args}" -- ${cur}))
return 0
}
complete -F _vacli vacli