_terraform 22.4 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'
9
    'force-unlock:Manually unlock the terraform state'
gkze's avatar
gkze committed
10
11
    'get:Download and install modules for the configuration'
    'graph:Create a visual graph of Terraform resources'
12
13
    'import:Import existing infrastructure into Terraform'
    'init:Initialize a Terraform working directory'
14
15
    'login:Obtain and save credentials for a remote host'
    'logout:Remove locally-stored credentials for a remote host'
gkze's avatar
gkze committed
16
17
    'output:Read an output from a state file'
    'plan:Generate and show an execution plan'
18
    'providers:Prints a tree of the providers used in the configuration'
gkze's avatar
gkze committed
19
20
    'refresh:Update local state file against real resources'
    'show:Inspect Terraform state or plan'
21
    'state:Advanced state management'
22
23
24
    'taint:Manually mark a resource for recreation'
    'untaint:Manually unmark a resource as tainted'
    'validate:Validates the Terraform files'
gkze's avatar
gkze committed
25
    'version:Prints the Terraform version'
26
    'workspace:Workspace management'
27
    '0.12upgrade:Rewrites pre-0.12 module source code for v0.12'
28
    '0.13upgrade:Rewrites pre-0.13 module source code for v0.13'
gkze's avatar
gkze committed
29
30
)

31
32
33
34
35
36
__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.]'
}

37
38
39
40
41
__013upgrade() {
  _arguments \
    '-yes[Skip the initial introduction messages and interactive confirmation. This can be used to run this command in batch from a script.]'
}

gkze's avatar
gkze committed
42
43
__apply() {
    _arguments \
44
        '-auto-approve[Skip interactive approval of plan before applying.]' \
45
46
47
        '-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.]:backupfile:_files -g "*.backup"' \
        '-compact-warnings[If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.]' \
        '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
48
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
gkze's avatar
gkze committed
49
        '-input=[(true) Ask for input for variables if not directly set.]' \
50
51
        '-no-color[If specified, output wil be colorless.]' \
        '-parallelism=[(10) Limit the number of parallel resource operations.]' \
gkze's avatar
gkze committed
52
        '-refresh=[(true) Update state prior to checking for differences. This has no effect if a plan file is given to apply.]' \
53
54
55
56
57
        '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]:statefile:_files -g "*.tfstate"' \
        '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -g "*.tfstate"' \
        '*-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \
        '*-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.]:file:_files -g "*.tfvars{,.json}"'
58
59
60
61
62
}

__console() {
    _arguments \
        '-state=[(terraform.tfstate) Path to read state.]' \
63
64
        '*-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.]:file:_files -g "*.tfvars{,.json}"'
gkze's avatar
gkze committed
65
66
67
68
}

__destroy() {
    _arguments \
69
        '-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.]:backupfile:_files -g "*.backup"' \
70
71
        '-auto-approve[Skip interactive approval before destroying.]' \
        '-force[Deprecated: same as auto-approve.]' \
72
        '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
73
74
75
        '-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
76
        '-refresh=[(true) Update state prior to checking for differences. This has no effect if a plan file is given to apply.]' \
77
78
79
80
81
        '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]:statefile:_files -g "*.tfstate"' \
        '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -g "*.tfstate"' \
        '*-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \
        '*-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.]:file:_files -g "*.tfvars{,.json}"'
82
83
84
85
86
87
88
}

__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]' \
89
90
        '-check=[(false) Check if the input is formatted. Exit status will be 0 if all input is properly formatted and non-zero otherwise.]' \
        '-recursive=[(false) Also process files in subdirectories. By default, only the given directory (or current directory) is processed.]'
gkze's avatar
gkze committed
91
92
}

93
94
95
96
97
__force_unlock() {
    _arguments \
        "-force[Don't ask for input for unlock confirmation.]"
}

gkze's avatar
gkze committed
98
99
__get() {
    _arguments \
100
        '-update=[(false) If true, modules already downloaded will be checked for updates and updated if necessary.]' \
101
        '-no-color[Disable text coloring in the output.]'
gkze's avatar
gkze committed
102
103
104
105
}

__graph() {
    _arguments \
106
        '-draw-cycles[Highlight any cycles in the graph with colored edges. This helps when diagnosing cycle errors.]' \
107
108
109
110
111
        '-type=[(plan) Type of graph to output. Can be: plan, plan-destroy, apply, validate, input, refresh.]'
}

__import() {
    _arguments \
112
        '-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.]:backupfile:_files -g "*.backup"' \
113
114
115
        '-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.]' \
116
        '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
117
118
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
        '-no-color[If specified, output will contain no color.]' \
119
120
121
122
        '-state=[(PATH) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -g "*.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.]:statefile:_files -g "*.tfstate"' \
        '*-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.]:file:_files -g "*.tfvars{,.json}"'
gkze's avatar
gkze committed
123
124
125
126
}

__init() {
    _arguments \
127
128
129
        '-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.]' \
130
        '-from-module=[(SOURCE) Copy the contents of the given module into the target directory before initialization.]' \
131
132
133
        '-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.]' \
134
        '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
135
136
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
        '-no-color[If specified, output will contain no color.]' \
137
        '-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.]:plugin_dir:_files -/' \
138
139
140
        '-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
141
142
}

143
144
145
146
147
148
149
150
151
152
__login() {
    _arguments \

}

__logout() {
    _arguments \

}

gkze's avatar
gkze committed
153
154
__output() {
    _arguments \
155
156
        '-state=[(path) Path to the state file to read. Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \
        '-no-color[If specified, output will contain no color.]' \
157
        '-json[If specified, machine readable output will be printed in JSON format]'
gkze's avatar
gkze committed
158
159
160
161
}

__plan() {
    _arguments \
162
163
        '-compact-warnings[If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.]' \
        '-destroy[If set, a plan will be generated to destroy all resources managed by the given configuration and state.]' \
164
        '-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
165
        '-input=[(true) Ask for input for variables if not directly set.]' \
166
        '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
167
168
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
        '-no-color[() If specified, output will contain no color.]' \
gkze's avatar
gkze committed
169
        '-out=[(path) Write a plan file to the given path. This can be used as input to the "apply" command.]' \
170
        '-parallelism=[(10) Limit the number of concurrent operations.]' \
gkze's avatar
gkze committed
171
        '-refresh=[(true) Update state prior to checking for differences.]' \
172
173
174
175
        '-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.]:statefile:_files -g "*.tfstate"' \
        '*-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \
        '*-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.]:file:_files -g "*.tfvars{,.json}"'
176
177
178
}

__providers() {
179
180
181
182
183
184
185
186
    local -a __providers_cmds
    __providers_cmds=(
      'mirror:Mirrors the provider plugins needed for the current configuration'
      'schema:Prints the schemas of the providers used in the configuration'
    )
    _describe -t providers "providers commands" __providers_cmds

}
187

188
189
190
191
__providers_mirror() {
    _arguments \
      '-platform=[(os_arch) Choose which target platform to build a mirror for.]' \
      "*:target_dir:_files -/"
gkze's avatar
gkze committed
192
193
}

194
__providers_schema() {
gkze's avatar
gkze committed
195
    _arguments \
196
197
      '-json[]' \
      '::'
gkze's avatar
gkze committed
198
199
200
201
}

__refresh() {
    _arguments \
202
203
        '-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.]::backupfile:_files -g "*.backup"' \
        '-compact-warnings[If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.]' \
204
        '-input=[(true) Ask for input for variables if not directly set.]' \
205
        '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
206
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
gkze's avatar
gkze committed
207
        '-no-color[If specified, output will not contain any color.]' \
208
209
210
211
212
        '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \
        '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -g "*.tfstate"' \
        '*-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' \
        '*-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.]:file:_files -g "*.tfvars{,.json}"'
gkze's avatar
gkze committed
213
214
215
216
}

__show() {
    _arguments \
217
        '-json[If specified, output the Terraform plan or state in a machine-readable form.]' \
gkze's avatar
gkze committed
218
219
220
        '-no-color[If specified, output will not contain any color.]'
}

221
222
223
224
225
226
227
__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'
228
      'replace-provider:Replace provider for resources in the Terraform state'
229
230
231
232
233
234
235
236
      'rm:Remove instances from the state' 
      'show:Show a resource in the state'
    )
    _describe -t state "state commands" __state_cmds
}

__state_list() {
  _arguments \
237
    '-state=[(statefile) Path to a Terraform state file to use to look up Terraform-managed resources. By default, Terraform will consult the state of the currently-selected workspace.]' \
238
239
240
241
242
243
244
    '-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.]" \
245
246
247
248
249
250
    '-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.]:backupfile:_files -g "*.backup"' \
    '-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.]:backupfile:_files -g "*.backup"' \
    "-lock=[(true) Lock the state files when locking is supported.]:lock:(true false)" \
    "-lock-timeout=[(0s) Duration to retry a state lock.]" \
    '-state=[(path) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -g "*.tfstate"' \
    '-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.]:statefile:_files -g "*.tfstate"' \
251
252
253
254
255
256
257
258
    "::" \
    ":source:__statelist" \
    ":destination: " 
}

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

265
266
267
268
269
270
271
272
273
274
275
__state_replace_provider() {
  _arguments \
    '-auto-approve[Skip interactive approval.]' \
    '-backup=[(PATH) Path where Terraform should write the backup for the state file. This can"t be disabled. If not set, Terraform will write it to the same path as the state file with a ".backup" extension.]:backupfile:_files -g "*.backup"' \
    "-lock=[(true) Lock the state files when locking is supported.]:lock:(true false)" \
    "-lock-timeout=[(0s) Duration to retry a state lock.]" \
    '-state=[(PATH) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -g "*.tfstate"' \
    ":from_provider_fqn:" \
    ":to_provider_fqn:"
}

276
277
278
__state_rm() {
  _arguments \
    "-dry-run[If set, prints out what would've been removed but doesn't actually remove anything.]" \
279
280
281
282
    '-backup=[(PATH) Path where Terraform should write the backup for the original state.]::backupfile:_files -g "*.backup"' \
    "-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)" \
    "-lock-timeout=[(0s) Duration to retry a state lock.]" \
    '-state=[(PATH) Path to the state file to update. Defaults to the current workspace state.]:statefile:_files -g "*.tfstate"' \
283
284
285
286
287
288
    "*:address:__statelist" 
}


__state_show() {
  _arguments \
289
    '-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.]:statefile:_files -g "*.tfstate"' \
290
291
292
293
294
295
296
    "*:address:__statelist" 
}

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

297
298
299
__taint() {
    _arguments \
        '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \
300
301
        '-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.]:backupfile:_files -g "*.backup"' \
        '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
302
        '-lock-timeout=[(0s) Duration to retry a state lock.]' \
303
        '-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).]' \
304
305
        '-state=[(path) Path to read and save state (unless state-out is  specified). Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \
        '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]:statefile:_files -g "*.tfstate"' \
306
        "*:address:__statelist" 
307
308
}

309
310
311
__untaint() {
    _arguments \
    '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \
312
313
    '-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.]:backupfile:_files -g "*.backup"' \
    '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \
314
315
    '-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).]' \
316
317
    '-state=[(path) Path to read and save state (unless state-out is  specified). Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \
    '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]:statefile:_files -g "*.tfstate"'
318
319
320
321
322
}

__validate() {
    _arguments \
    '-no-color[If specified, output will not contain any color.]' \
323
324
325
326
327
328
329
    '-json[Produce output in a machine-readable JSON format, suitable for use in text editor integrations and other automated systems.]' \
    ':dir:_files -/'
}

__version() {
    _arguments \
    '-json[Output the version information as a JSON object.]'
330
331
332
333
334
335
336
337
338
339
340
341
342
343
}

__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
344
345
346
347
348
349
350
351
352
_arguments '*:: :->command'

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

local -a _command_args
case "$words[1]" in
353
354
  0.12upgrade)
    __012upgrade ;;
355
356
  0.13upgrade)
    __013upgrade ;;
gkze's avatar
gkze committed
357
358
  apply)
    __apply ;;
359
360
  console)
    __console;;
gkze's avatar
gkze committed
361
362
  destroy)
    __destroy ;;
363
364
  fmt)
    __fmt;;
365
366
  force-unlock)
    __force_unlock;;
gkze's avatar
gkze committed
367
368
369
370
  get)
    __get ;;
  graph)
    __graph ;;
371
372
  import)
    __import;;
gkze's avatar
gkze committed
373
374
  init)
    __init ;;
375
376
377
378
  login)
    __login ;;
  logout)
    __logout ;;
gkze's avatar
gkze committed
379
380
381
382
  output)
    __output ;;
  plan)
    __plan ;;
383
  providers)
384
385
386
387
    test $CURRENT -lt 3 && __providers
    [[ $words[2] = "mirror" ]] && __providers_mirror
    [[ $words[2] = "schema" ]] && __providers_schema
    ;;
gkze's avatar
gkze committed
388
389
390
391
  refresh)
    __refresh ;;
  show)
    __show ;;
392
393
394
395
396
  state)
    test $CURRENT -lt 3 && __state
    [[ $words[2] = "list" ]] && __state_list
    [[ $words[2] = "mv" ]] && __state_mv
    [[ $words[2] = "push" ]] && __state_push
397
    [[ $words[2] = "replace-provider" ]] && __state_replace_provider
398
399
400
    [[ $words[2] = "rm" ]] && __state_rm
    [[ $words[2] = "show" ]] && __state_show
    ;;
gkze's avatar
gkze committed
401
402
  taint)
    __taint ;;
403
404
405
406
  untaint)
    __untaint ;;
  validate)
    __validate ;;
407
408
  version)
    __version ;;
409
410
  workspace)
    test $CURRENT -lt 3 && __workspace ;;
gkze's avatar
gkze committed
411
esac