Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to show specified types of resources #4232

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions command/format_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type FormatStateOpts struct {
// ModuleDepth is the depth of the modules to expand. By default this
// is zero which will not expand modules at all.
ModuleDepth int

// Types of resources to show, by default will show all resources.
ShowResourceTypes map[string]bool
}

// FormatState takes a state and returns a string
Expand Down Expand Up @@ -85,6 +88,10 @@ func formatStateModuleExpand(

// Go through each resource and begin building up the output.
for _, k := range names {
resourceType := strings.Split(k, ".")[0]
if opts.ShowResourceTypes != nil && !opts.ShowResourceTypes[resourceType] {
continue
}
name := k
if moduleName != "" {
name = moduleName + "." + name
Expand Down
19 changes: 17 additions & 2 deletions command/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ func (c *ShowCommand) Run(args []string) int {

args = c.Meta.process(args, false)

var resourceTypes string
cmdFlags := flag.NewFlagSet("show", flag.ContinueOnError)
c.addModuleDepthFlag(cmdFlags, &moduleDepth)
cmdFlags.StringVar(&resourceTypes, "resource-types", "", "resource-types")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -100,11 +102,21 @@ func (c *ShowCommand) Run(args []string) int {
return 0
}

c.Ui.Output(FormatState(&FormatStateOpts{
formatOptions := FormatStateOpts{
State: state,
Color: c.Colorize(),
ModuleDepth: moduleDepth,
}))
}

if resourceTypes != "" {
resourceTypes := strings.Split(resourceTypes, ",")
formatOptions.ShowResourceTypes = make(map[string]bool, len(resourceTypes))
for _, resourceType := range resourceTypes {
formatOptions.ShowResourceTypes[resourceType] = true
}
}

c.Ui.Output(FormatState(&formatOptions))
return 0
}

Expand All @@ -122,6 +134,9 @@ Options:

-no-color If specified, output won't contain any color.

-resource-types If specified, limits the output to specified resource types.
Comma-separated string.

`
return strings.TrimSpace(helpText)
}
Expand Down
45 changes: 45 additions & 0 deletions command/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,48 @@ func TestShow_state(t *testing.T) {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
}

func TestShow_filter(t *testing.T) {
state := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance_type1.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
"test_instance_type2.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
},
},
}
statePath := testStateFile(t, state)

ui := new(cli.MockUi)
c := &ShowCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}

args := []string{
"-resource-types", "test_instance_type2",
statePath,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}

if strings.Contains(ui.OutputWriter.String(), "test_instance_type1") {
t.Fatalf("bad: test_instance_type1")
}
}