|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package command |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/hashicorp/cli" |
| 12 | + "github.com/hashicorp/terraform/internal/addrs" |
| 13 | + "github.com/hashicorp/terraform/internal/states" |
| 14 | + "github.com/hashicorp/terraform/internal/tfdiags" |
| 15 | +) |
| 16 | + |
| 17 | +// StateIdentitiesCommand is a Command implementation that lists the resource identities |
| 18 | +// within a state file. |
| 19 | +type StateIdentitiesCommand struct { |
| 20 | + Meta |
| 21 | + StateMeta |
| 22 | +} |
| 23 | + |
| 24 | +func (c *StateIdentitiesCommand) Run(args []string) int { |
| 25 | + args = c.Meta.process(args) |
| 26 | + var statePath string |
| 27 | + var jsonOutput bool |
| 28 | + cmdFlags := c.Meta.defaultFlagSet("state identities") |
| 29 | + cmdFlags.StringVar(&statePath, "state", "", "path") |
| 30 | + cmdFlags.BoolVar(&jsonOutput, "json", false, "produce JSON output") |
| 31 | + lookupId := cmdFlags.String("id", "", "Restrict output to paths with a resource having the specified ID.") |
| 32 | + if err := cmdFlags.Parse(args); err != nil { |
| 33 | + c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) |
| 34 | + return cli.RunResultHelp |
| 35 | + } |
| 36 | + args = cmdFlags.Args() |
| 37 | + |
| 38 | + if !jsonOutput { |
| 39 | + c.Ui.Error( |
| 40 | + "The `terraform state identities` command requires the `-json` flag.\n") |
| 41 | + cmdFlags.Usage() |
| 42 | + return 1 |
| 43 | + } |
| 44 | + |
| 45 | + if statePath != "" { |
| 46 | + c.Meta.statePath = statePath |
| 47 | + } |
| 48 | + |
| 49 | + // Load the backend |
| 50 | + b, backendDiags := c.Backend(nil) |
| 51 | + if backendDiags.HasErrors() { |
| 52 | + c.showDiagnostics(backendDiags) |
| 53 | + return 1 |
| 54 | + } |
| 55 | + |
| 56 | + // This is a read-only command |
| 57 | + c.ignoreRemoteVersionConflict(b) |
| 58 | + |
| 59 | + // Get the state |
| 60 | + env, err := c.Workspace() |
| 61 | + if err != nil { |
| 62 | + c.Ui.Error(fmt.Sprintf("Error selecting workspace: %s", err)) |
| 63 | + return 1 |
| 64 | + } |
| 65 | + stateMgr, err := b.StateMgr(env) |
| 66 | + if err != nil { |
| 67 | + c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) |
| 68 | + return 1 |
| 69 | + } |
| 70 | + if err := stateMgr.RefreshState(); err != nil { |
| 71 | + c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) |
| 72 | + return 1 |
| 73 | + } |
| 74 | + |
| 75 | + state := stateMgr.State() |
| 76 | + if state == nil { |
| 77 | + c.Ui.Error(errStateNotFound) |
| 78 | + return 1 |
| 79 | + } |
| 80 | + |
| 81 | + var addrs []addrs.AbsResourceInstance |
| 82 | + var diags tfdiags.Diagnostics |
| 83 | + if len(args) == 0 { |
| 84 | + addrs, diags = c.lookupAllResourceInstanceAddrs(state) |
| 85 | + } else { |
| 86 | + addrs, diags = c.lookupResourceInstanceAddrs(state, args...) |
| 87 | + } |
| 88 | + if diags.HasErrors() { |
| 89 | + c.showDiagnostics(diags) |
| 90 | + return 1 |
| 91 | + } |
| 92 | + |
| 93 | + output := make(map[string]any) |
| 94 | + for _, addr := range addrs { |
| 95 | + // If the resource exists but identity is nil, skip it, as it is not required to be present |
| 96 | + if is := state.ResourceInstance(addr); is != nil && is.Current.IdentityJSON != nil { |
| 97 | + if *lookupId == "" || *lookupId == states.LegacyInstanceObjectID(is.Current) { |
| 98 | + var rawIdentity map[string]any |
| 99 | + if err := json.Unmarshal(is.Current.IdentityJSON, &rawIdentity); err != nil { |
| 100 | + c.Ui.Error(fmt.Sprintf("Failed to unmarshal identity JSON: %s", err)) |
| 101 | + return 1 |
| 102 | + } |
| 103 | + output[addr.String()] = rawIdentity |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + outputJSON, err := json.MarshalIndent(output, "", " ") |
| 109 | + if err != nil { |
| 110 | + c.Ui.Error(fmt.Sprintf("Failed to marshal output JSON: %s", err)) |
| 111 | + return 1 |
| 112 | + } |
| 113 | + |
| 114 | + c.Ui.Output(string(outputJSON)) |
| 115 | + c.showDiagnostics(diags) |
| 116 | + |
| 117 | + return 0 |
| 118 | +} |
| 119 | + |
| 120 | +func (c *StateIdentitiesCommand) Help() string { |
| 121 | + helpText := ` |
| 122 | +Usage: terraform [global options] state identities [options] -json [address...] |
| 123 | +
|
| 124 | + List the json format of the identities of resources in the Terraform state. |
| 125 | +
|
| 126 | + This command lists the identities of resource instances in the Terraform state in json format. |
| 127 | + The address argument can be used to filter the instances by resource or module. If |
| 128 | + no pattern is given, identities for all resource instances are listed. |
| 129 | +
|
| 130 | + The addresses must either be module addresses or absolute resource |
| 131 | + addresses, such as: |
| 132 | + aws_instance.example |
| 133 | + module.example |
| 134 | + module.example.module.child |
| 135 | + module.example.aws_instance.example |
| 136 | +
|
| 137 | + An error will be returned if any of the resources or modules given as |
| 138 | + filter addresses do not exist in the state. |
| 139 | +
|
| 140 | +Options: |
| 141 | +
|
| 142 | + -state=statefile Path to a Terraform state file to use to look |
| 143 | + up Terraform-managed resources. By default, Terraform |
| 144 | + will consult the state of the currently-selected |
| 145 | + workspace. |
| 146 | +
|
| 147 | + -id=ID Filters the results to include only instances whose |
| 148 | + resource types have an attribute named "id" whose value |
| 149 | + equals the given id string. |
| 150 | +
|
| 151 | +` |
| 152 | + return strings.TrimSpace(helpText) |
| 153 | +} |
| 154 | + |
| 155 | +func (c *StateIdentitiesCommand) Synopsis() string { |
| 156 | + return "List the identities of resources in the state" |
| 157 | +} |
0 commit comments