_terraform 21 KB
Newer Older
gkze's avatar
gkze committed
1
2
#compdef terraform

3
local -a _terraform_cmds opt_args
gkze's avatar
gkze committed
4
5
_terraform_cmds=(
    'apply:Builds or changes infrastructure'
6
    'console:Interactive console for Terraform interpolations'
gkze's avatar
gkze committed
7
    'destroy:Destroy Terraform-managed infrastructure'
8
    'fmt:Rewrites config files to canonical format'
gkze's avatar
gkze committed
9
10
    'get:Download and install modules for the configuration'
    'graph:Create a visual graph of Terraform resources'
11
12
    'import:Import existing infrastructure into Terraform'
    'init:Initialize a Terraform working directory'
gkze's avatar
gkze committed
13
14
    'output:Read an output from a state file'
    'plan:Generate and show an execution plan'
15
16
    'providers:Prints a tree of the providers used in the configuration'
    'push:Upload this Terraform module to Atlas to run'
gkze's avatar
gkze committed
17
18
    'refresh:Update local state file against real resources'
    'show:Inspect Terraform state or plan'
19
    'state:Advanced state management'
20
21
22
    'taint:Manually mark a resource for recreation'
    'untaint:Manually unmark a resource as tainted'
    'validate:Validates the Terraform files'
gkze's avatar
gkze committed
23
    'version:Prints the Terraform version'
24
    'workspace:Workspace management'
25
    '0.12upgrade:Rewrites pre-0.12 module source code for v0.12'
gkze's avatar
gkze committed
26
27
)

28
29
30
31
32
33
__012upgrade() {
  _arguments \
    '-yes[Skip the initial introduction messages and interactive confirmation. This can be used to run this command in batch from a script.]' \
    '-force[ Override the heuristic that attempts to detect if a configuration is already written for v0.12 or later.  Some of the transformations made by this command are not idempotent, so re-running against the same module may change the meanings expressions in the module.]'
}

gkze's avatar
gkze committed
34
35
36
__apply() {
    _arguments \
        '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \
37
38
39
        '-auto-approve[Skip interactive approval of plan before applying.]' \
        '-lock=[(true) Lock the state file when locking is supported.]' \
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
gkze's avatar
gkze committed
40
        '-input=[(true) Ask for input for variables if not directly set.]' \
41
42
        '-no-color[If specified, output wil be colorless.]' \
        '-parallelism=[(10) Limit the number of parallel resource operations.]' \
gkze's avatar
gkze committed
43
        '-refresh=[(true) Update state prior to checking for differences. This has no effect if a plan file is given to apply.]' \
44
        '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]' \
gkze's avatar
gkze committed
45
        '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]' \
46
        '-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \
gkze's avatar
gkze committed
47
        '-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
48
49
50
51
52
53
54
55
        '-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
}

__console() {
    _arguments \
        '-state=[(terraform.tfstate) Path to read state.]' \
        '-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
        '-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
gkze's avatar
gkze committed
56
57
58
59
60
}

__destroy() {
    _arguments \
        '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \
61
62
63
64
65
66
        '-auto-approve[Skip interactive approval before destroying.]' \
        '-force[Deprecated: same as auto-approve.]' \
        '-lock=[(true) Lock the state file when locking is supported.]' \
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
        '-no-color[If specified, output will contain no color.]' \
        '-parallelism=[(10) Limit the number of concurrent operations.]' \
gkze's avatar
gkze committed
67
        '-refresh=[(true) Update state prior to checking for differences. This has no effect if a plan file is given to apply.]' \
68
        '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]' \
69
        '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]' \
70
        '-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \
gkze's avatar
gkze committed
71
        '-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
72
73
74
75
76
77
78
79
80
        '-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
}

__fmt() {
    _arguments \
        '-list=[(true) List files whose formatting differs (always false if using STDIN)]' \
        '-write=[(true) Write result to source file instead of STDOUT (always false if using STDIN or -check)]' \
        '-diff=[(false) Display diffs of formatting changes]' \
        '-check=[(false) Check if the input is formatted. Exit status will be 0 if all input is properly formatted and non-zero otherwise.]'
gkze's avatar
gkze committed
81
82
83
84
}

__get() {
    _arguments \
85
86
        '-update=[(false) If true, modules already downloaded will be checked for updates and updated if necessary.]' \
        '-no-color[If specified, output will contain no color.]'
gkze's avatar
gkze committed
87
88
89
90
}

__graph() {
    _arguments \
91
        '-draw-cycles[Highlight any cycles in the graph with colored edges. This helps when diagnosing cycle errors.]' \
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        '-no-color[If specified, output will contain no color.]' \
        '-type=[(plan) Type of graph to output. Can be: plan, plan-destroy, apply, validate, input, refresh.]'
}

__import() {
    _arguments \
        '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \
        '-config=[(path) Path to a directory of Terraform configuration files to use to configure the provider. Defaults to pwd. If no config files are present, they must be provided via the input prompts or env vars.]' \
        '-allow-missing-config[Allow import when no resource configuration block exists.]' \
        '-input=[(true) Ask for input for variables if not directly set.]' \
        '-lock=[(true) Lock the state file when locking is supported.]' \
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
        '-no-color[If specified, output will contain no color.]' \
        '-provider=[(provider) Specific provider to use for import. This is used for specifying aliases, such as "aws.eu". Defaults to the normal provider prefix of the resource being imported.]' \
        '-state=[(PATH) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]' \
        '-state-out=[(PATH) Path to the destination state file to write to. If this is not specified, the source state file will be used. This can be a new or existing path.]' \
        '-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times. This is only useful with the "-config" flag.]' \
        '-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
gkze's avatar
gkze committed
110
111
112
113
}

__init() {
    _arguments \
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        '-backend=[(true) Configure the backend for this configuration.]' \
        '-backend-config=[This can be either a path to an HCL file with key/value assignments (same format as terraform.tfvars) or a 'key=value' format. This is merged with what is in the configuration file. This can be specified multiple times. The backend type must be in the configuration itself.]' \
        '-force-copy[Suppress prompts about copying state data. This is equivalent to providing a "yes" to all confirmation prompts.]' \
        '-from-module=[Copy the contents of the given module into the target directory before initialization.]' \
        '-get=[(true) Download any modules for this configuration.]' \
        '-get-plugins=[(true) Download any missing plugins for this configuration.]' \
        '-input=[(true) Ask for input if necessary. If false, will error if input was required.]' \
        '-lock=[(true) Lock the state file when locking is supported.]' \
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
        '-no-color[If specified, output will contain no color.]' \
        '-plugin-dir[Directory containing plugin binaries. This overrides all default search paths for plugins, and prevents the automatic installation of plugins. This flag can be used multiple times.]' \
        '-reconfigure[Reconfigure the backend, ignoring any saved configuration.]' \
        '-upgrade=[(false) If installing modules (-get) or plugins (-get-plugins), ignore previously-downloaded objects and install the latest version allowed within configured constraints.]' \
        '-verify-plugins=[(true) Verify the authenticity and integrity of automatically downloaded plugins.]'
gkze's avatar
gkze committed
128
129
130
131
}

__output() {
    _arguments \
132
        '-state=[(path) Path to the state file to read. Defaults to "terraform.tfstate".]' \
133
134
135
        '-no-color[ If specified, output will contain no color.]' \
        '-module=[(name) If specified, returns the outputs for a specific module]' \
        '-json[If specified, machine readable output will be printed in JSON format]'
gkze's avatar
gkze committed
136
137
138
139
}

__plan() {
    _arguments \
140
141
        '-destroy[() If set, a plan will be generated to destroy all resources managed by the given configuration and state.]' \
        '-detailed-exitcode[() Return detailed exit codes when the command exits. This will change the meaning of exit codes to: 0 - Succeeded, diff is empty (no changes); 1 - Errored, 2 - Succeeded; there is a diff]' \
gkze's avatar
gkze committed
142
        '-input=[(true) Ask for input for variables if not directly set.]' \
143
144
145
146
        '-lock=[(true) Lock the state file when locking is supported.]' \
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
        '-module-depth=[(n) Specifies the depth of modules to show in the output. This does not affect the plan itself, only the output shown. By default, this is -1, which will expand all.]' \
        '-no-color[() If specified, output will contain no color.]' \
gkze's avatar
gkze committed
147
        '-out=[(path) Write a plan file to the given path. This can be used as input to the "apply" command.]' \
148
        '-parallelism=[(10) Limit the number of concurrent operations.]' \
gkze's avatar
gkze committed
149
150
        '-refresh=[(true) Update state prior to checking for differences.]' \
        '-state=[(statefile) Path to a Terraform state file to use to look up Terraform-managed resources. By default it will use the state "terraform.tfstate" if it exists.]' \
151
        '-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \
gkze's avatar
gkze committed
152
        '-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
153
154
155
156
157
158
        '-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]' \
}

__providers() {
    _arguments \

gkze's avatar
gkze committed
159
160
161
162
}

__push() {
    _arguments \
163
164
165
166
167
168
169
170
        '-atlas-address=[(url) An alternate address to an Atlas instance. Defaults to https://atlas.hashicorp.com.]' \
        '-upload-modules=[(true) If true (default), then the modules being used are all locked at their current checkout and uploaded completely to Atlas. This prevents Atlas from running terraform get for you.]' \
        '-name=[(name) Name of the infrastructure configuration in Atlas. The format of this is: "username/name" so that you can upload configurations not just to your account but to other accounts and organizations. This setting can also be set in the configuration in the Atlas section.]' \
        '-no-color[Disables output with coloring]' \
        '-overwrite=[(foo) Marks a specific variable to be updated on Atlas. Normally, if a variable is already set in Atlas, Terraform will not send the local value (even if it is different). This forces it to send the local value to Atlas. This flag can be repeated multiple times.]' \
        '-token=[(token) Atlas API token to use to authorize the upload. If blank or unspecified, the ATLAS_TOKEN environmental variable will be used.]' \
        '-var=[("foo=bar") Set the value of a variable for the Terraform configuration.]' \
        '-var-file=[(foo) Set the value of variables using a variable file.]' \
171
        '-vcs=[(true) If true (default), then Terraform will detect if a VCS is in use, such as Git, and will only upload files that are committed to version control. If no version control system is detected, Terraform will upload all files in path (parameter to the command).]'
gkze's avatar
gkze committed
172
173
174
175
176
}

__refresh() {
    _arguments \
        '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \
177
178
179
        '-input=[(true) Ask for input for variables if not directly set.]' \
        '-lock=[(true) Lock the state file when locking is supported.]' \
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
gkze's avatar
gkze committed
180
181
182
        '-no-color[If specified, output will not contain any color.]' \
        '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]' \
        '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]' \
183
        '-target=[(resource) A Resource Address to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \
gkze's avatar
gkze committed
184
185
186
187
188
189
190
191
192
193
        '-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
        '-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" is present, it will be automatically loaded if this flag is not specified.]'
}

__show() {
    _arguments \
        '-module-depth=[(n) The maximum depth to expand modules. By default this is zero, which will not expand modules at all.]' \
        '-no-color[If specified, output will not contain any color.]'
}

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
__state() {
    local -a __state_cmds
    __state_cmds=(
      'list:List resources in the state'
      'mv:Move an item in the state'
      'pull:Pull current state and output to stdout'
      'push:Update remote state from a local state file'
      'rm:Remove instances from the state' 
      'show:Show a resource in the state'
    )
    _describe -t state "state commands" __state_cmds
}

__state_list() {
  _arguments \
    '-state=[(path) Path to a Terraform state file to use to look up Terraform-managed resources. By default it will use the state "terraform.tfstate" if it exists.]' \
    '-id=[(id) Filters the results to include only instances whose resource types have an attribute named id whose value equals the given id string.]' \
    "*:address:__statelist" 
}

__state_mv() {
  _arguments \
    "-dry-run[If set, prints out what would've been moved but doesn't actually move anything.]" \
    "-backup=[(path) Path where Terraform should write the backup for the original state. This can't be disabled. If not set, Terraform will write it to the same path as the statefile with a \".backup\" extension.]:file:_files" \
    "-backup-out=[(path) Path where Terraform should write the backup for the destination state. This can't be disabled. If not set, Terraform will write it to the same path as the destination state file with a backup extension. This only needs to be specified if -state-out is set to a different path than -state.]:file:_files" \
    "-lock=[(true|false) Lock the state files when locking is supported.]:lock:(true false)" \
    "-lock-timeout=[(seconds) Duration to retry a state lock.]" \
    '-state=[(path) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:file:_files' \
    "-state-out=[(path) Path to the destination state file to write to. If this isn't specified, the source state file will be used. This can be a new or existing path.]:file:_files" \
    "::" \
    ":source:__statelist" \
    ":destination: " 
}

__state_push() {
  _arguments \
    "-force[Write the state even if lineages don't match or the remote serial is higher.]" \
    '-lock=[(true|false) Lock the state file when locking is supported.]:lock:(true false)' \
    "-lock-timeout=[(seconds) Duration to retry a state lock.]" \
    "::" \
    ":destination:_files"
}

__state_rm() {
  _arguments \
    "-dry-run[If set, prints out what would've been removed but doesn't actually remove anything.]" \
    "-backup=[(path) Path where Terraform should write the backup for the original state.]:file:_files" \
    "-lock=[(true|false) Lock the state files when locking is supported.]:lock:(true false)" \
    "-lock-timeout=[(seconds) Duration to retry a state lock.]" \
    '-state=[(path) Path to the state file to update. Defaults to the current workspace state.]:file:_files' \
    "*:address:__statelist" 
}


__state_show() {
  _arguments \
    '-state=[(path) Path to a Terraform state file to use to look up Terraform-managed resources. By default it will use the state "terraform.tfstate" if it exists.]' \
    "*:address:__statelist" 
}

__statelist() {
  compadd $(terraform state list $opt_args[-state])
}

258
259
260
261
__taint() {
    _arguments \
        '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \
        '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \
262
263
        '-lock=[(true) Lock the state file when locking is supported.]' \
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
264
265
266
        '-module=[(path)  The module path where the resource lives. By default this will be root. Child modules can be specified by names. Ex. "consul" or "consul.vpc" (nested modules).]' \
        '-no-color[If specified, output will not contain any color.]' \
        '-state=[(path) Path to read and save state (unless state-out is  specified). Defaults to "terraform.tfstate".]' \
267
268
        '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]' \
        "*:address:__statelist" 
269
270
}

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
__untaint() {
    _arguments \
    '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \
    '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \
    '-lock=[(true) Lock the state file when locking is supported.]' \
    '-lock-timeout=[(0s) Duration to retry a state lock.]' \
    '-module=[(path)  The module path where the resource lives. By default this will be root. Child modules can be specified by names. Ex. "consul" or "consul.vpc" (nested modules).]' \
    '-no-color[If specified, output will not contain any color.]' \
    '-state=[(path) Path to read and save state (unless state-out is  specified). Defaults to "terraform.tfstate".]' \
    '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]'
}

__validate() {
    _arguments \
    '-check-variables=[(true) If set to true (default), the command will check whether all required variables have been specified.]' \
    '-no-color[If specified, output will not contain any color.]' \
    '-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
    '-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" is present, it will be automatically loaded if this flag is not specified.]'
}

__workspace() {
    local -a __workspace_cmds
    __workspace_cmds=(
        'delete:Delete a workspace'
        'list:List Workspaces'
        'new:Create a new workspace'
        'select:Select a workspace'
        'show:Show the name of the current workspace'
    )
    _describe -t workspace "workspace commands" __workspace_cmds
}

gkze's avatar
gkze committed
303
304
305
306
307
308
309
310
311
_arguments '*:: :->command'

if (( CURRENT == 1 )); then
  _describe -t commands "terraform command" _terraform_cmds
  return
fi

local -a _command_args
case "$words[1]" in
312
313
  0.12upgrade)
    __012upgrade ;;
gkze's avatar
gkze committed
314
315
  apply)
    __apply ;;
316
317
  console)
    __console;;
gkze's avatar
gkze committed
318
319
  destroy)
    __destroy ;;
320
321
  fmt)
    __fmt;;
gkze's avatar
gkze committed
322
323
324
325
  get)
    __get ;;
  graph)
    __graph ;;
326
327
  import)
    __import;;
gkze's avatar
gkze committed
328
329
330
331
332
333
  init)
    __init ;;
  output)
    __output ;;
  plan)
    __plan ;;
334
335
  providers)
    __providers ;;
gkze's avatar
gkze committed
336
337
338
339
340
341
  push)
    __push ;;
  refresh)
    __refresh ;;
  show)
    __show ;;
342
343
344
345
346
347
348
349
  state)
    test $CURRENT -lt 3 && __state
    [[ $words[2] = "list" ]] && __state_list
    [[ $words[2] = "mv" ]] && __state_mv
    [[ $words[2] = "push" ]] && __state_push
    [[ $words[2] = "rm" ]] && __state_rm
    [[ $words[2] = "show" ]] && __state_show
    ;;
gkze's avatar
gkze committed
350
351
  taint)
    __taint ;;
352
353
354
355
356
357
  untaint)
    __untaint ;;
  validate)
    __validate ;;
  workspace)
    test $CURRENT -lt 3 && __workspace ;;
gkze's avatar
gkze committed
358
esac