cli: "terraform state list" -id argument
A new -id option to "terraform state list" which constraints the output of the command to resources whose id is equal to the flag value.
This commit is contained in:
parent
81dc23c65d
commit
07a20365de
|
@ -23,6 +23,7 @@ func (c *StateListCommand) Run(args []string) int {
|
||||||
|
|
||||||
cmdFlags := c.Meta.flagSet("state list")
|
cmdFlags := c.Meta.flagSet("state list")
|
||||||
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
||||||
|
lookupId := cmdFlags.String("id", "", "Restrict output to paths with a resource having the specified ID.")
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := cmdFlags.Parse(args); err != nil {
|
||||||
return cli.RunResultHelp
|
return cli.RunResultHelp
|
||||||
}
|
}
|
||||||
|
@ -62,10 +63,12 @@ func (c *StateListCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
if _, ok := result.Value.(*terraform.InstanceState); ok {
|
if i, ok := result.Value.(*terraform.InstanceState); ok {
|
||||||
|
if *lookupId == "" || i.ID == *lookupId {
|
||||||
c.Ui.Output(result.Address)
|
c.Ui.Output(result.Address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -94,6 +97,8 @@ Options:
|
||||||
up Terraform-managed resources. By default it will
|
up Terraform-managed resources. By default it will
|
||||||
use the state "terraform.tfstate" if it exists.
|
use the state "terraform.tfstate" if it exists.
|
||||||
|
|
||||||
|
-id=ID Restricts the output to objects whose id is ID.
|
||||||
|
|
||||||
`
|
`
|
||||||
return strings.TrimSpace(helpText)
|
return strings.TrimSpace(helpText)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,65 @@ func TestStateList(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStateListWithID(t *testing.T) {
|
||||||
|
state := testState()
|
||||||
|
statePath := testStateFile(t, state)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &StateListCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
testingOverrides: metaOverridesForProvider(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-id", "bar",
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that outputs were displayed
|
||||||
|
expected := strings.TrimSpace(testStateListOutput) + "\n"
|
||||||
|
actual := ui.OutputWriter.String()
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStateListWithNonExistentID(t *testing.T) {
|
||||||
|
state := testState()
|
||||||
|
statePath := testStateFile(t, state)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &StateListCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
testingOverrides: metaOverridesForProvider(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-id", "baz",
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that output is empty
|
||||||
|
if ui.OutputWriter != nil {
|
||||||
|
actual := ui.OutputWriter.String()
|
||||||
|
if actual != "" {
|
||||||
|
t.Fatalf("Expected an empty output but got: %q", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStateList_backendState(t *testing.T) {
|
func TestStateList_backendState(t *testing.T) {
|
||||||
// Create a temporary working directory that is empty
|
// Create a temporary working directory that is empty
|
||||||
td := tempDir(t)
|
td := tempDir(t)
|
||||||
|
|
|
@ -31,6 +31,7 @@ The command-line flags are all optional. The list of available flags are:
|
||||||
|
|
||||||
* `-state=path` - Path to the state file. Defaults to "terraform.tfstate".
|
* `-state=path` - Path to the state file. Defaults to "terraform.tfstate".
|
||||||
Ignored when [remote state](/docs/state/remote.html) is used.
|
Ignored when [remote state](/docs/state/remote.html) is used.
|
||||||
|
* `-id=id` - ID of resources to show. Ignored when unset.
|
||||||
|
|
||||||
## Example: All Resources
|
## Example: All Resources
|
||||||
|
|
||||||
|
@ -62,3 +63,14 @@ This example will only list resources in the given module:
|
||||||
$ terraform state list module.elb
|
$ terraform state list module.elb
|
||||||
module.elb.aws_elb.main
|
module.elb.aws_elb.main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Example: Filtering by ID
|
||||||
|
|
||||||
|
This example will only list the resource whose ID is specified on the
|
||||||
|
command line. This is useful to find where in your configuration a
|
||||||
|
specific resource is located.
|
||||||
|
|
||||||
|
```
|
||||||
|
$ terraform state list -id=sg-1234abcd
|
||||||
|
module.elb.aws_security_group.sg
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue