mirror of
https://github.com/hashicorp/terraform.git
synced 2026-02-19 02:39:17 -05:00
Some checks are pending
build / Determine intended Terraform version (push) Waiting to run
build / Determine Go toolchain version (push) Waiting to run
build / Generate release metadata (push) Blocked by required conditions
build / Build for freebsd_386 (push) Blocked by required conditions
build / Build for linux_386 (push) Blocked by required conditions
build / Build for openbsd_386 (push) Blocked by required conditions
build / Build for windows_386 (push) Blocked by required conditions
build / Build for darwin_amd64 (push) Blocked by required conditions
build / Build for freebsd_amd64 (push) Blocked by required conditions
build / Build for linux_amd64 (push) Blocked by required conditions
build / Build for openbsd_amd64 (push) Blocked by required conditions
build / Build for solaris_amd64 (push) Blocked by required conditions
build / Build for windows_amd64 (push) Blocked by required conditions
build / Build for freebsd_arm (push) Blocked by required conditions
build / Build for linux_arm (push) Blocked by required conditions
build / Build for darwin_arm64 (push) Blocked by required conditions
build / Build for linux_arm64 (push) Blocked by required conditions
build / Build for windows_arm64 (push) Blocked by required conditions
build / Build Docker image for linux_386 (push) Blocked by required conditions
build / Build Docker image for linux_amd64 (push) Blocked by required conditions
build / Build Docker image for linux_arm (push) Blocked by required conditions
build / Build Docker image for linux_arm64 (push) Blocked by required conditions
build / Build e2etest for linux_386 (push) Blocked by required conditions
build / Build e2etest for windows_386 (push) Blocked by required conditions
build / Build e2etest for darwin_amd64 (push) Blocked by required conditions
build / Build e2etest for linux_amd64 (push) Blocked by required conditions
build / Build e2etest for windows_amd64 (push) Blocked by required conditions
build / Build e2etest for linux_arm (push) Blocked by required conditions
build / Build e2etest for darwin_arm64 (push) Blocked by required conditions
build / Build e2etest for linux_arm64 (push) Blocked by required conditions
build / Run e2e test for linux_386 (push) Blocked by required conditions
build / Run e2e test for windows_386 (push) Blocked by required conditions
build / Run e2e test for darwin_amd64 (push) Blocked by required conditions
build / Run e2e test for linux_amd64 (push) Blocked by required conditions
build / Run e2e test for windows_amd64 (push) Blocked by required conditions
build / Run e2e test for linux_arm (push) Blocked by required conditions
build / Run e2e test for linux_arm64 (push) Blocked by required conditions
build / Run terraform-exec test for linux amd64 (push) Blocked by required conditions
Quick Checks / Unit Tests (push) Waiting to run
Quick Checks / Race Tests (push) Waiting to run
Quick Checks / End-to-end Tests (push) Waiting to run
Quick Checks / Code Consistency Checks (push) Waiting to run
* Add a generic method for loading an operations backend in non-init commands * Refactor commands to use new prepareBackend method: group 1 * Refactor commands to use new prepareBackend method: group 2, where config parsing needs to be explicitly added * Refactor commands to use new prepareBackend method: group 3, where we can use already parsed config * Additional, more nested, places where logic for accessing backends needs to be refactored * Remove duplicated comment * Add test coverage of `(m *Meta) prepareBackend()` * Add TODO related to using plans for backend/state_store config in apply commands * Add `testStateStoreMockWithChunkNegotiation` test helper * Add assertions to tests about the backend (remote-state, local, etc) in use within operations backend * Stop prepareBackend taking locks as argument * Code comment in prepareBackend * Replace c.Meta.prepareBackend with c.prepareBackend * Change `c.Meta.loadSingleModule` to `c.loadSingleModule` * Rename (Meta).prepareBackend to (Meta).backend, update godoc comment to make relationship to (Meta).Backend more obvious. * Revert change from config.Module to config.Root.Module * Update `(m *Meta) backend` method to parse config itself, and also to adhere to calling code's viewtype instructions * Update all tests and calling code following previous commit * Change how an operations backend is obtained by autocomplete code * Update autocomplete to return nil if no workspace names are returned from the backend * Add test coverage for autocompleting workspace names when using a pluggable state store * Fix output command: pass view type data to new `backend` method * Fix in plan command: pass correct view type to `backend` method * Fix `providers schema` command to use correct viewtype when preparing a backend
157 lines
4.4 KiB
Go
157 lines
4.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package command
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/cli"
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
|
"github.com/hashicorp/terraform/internal/states"
|
|
)
|
|
|
|
// StateIdentitiesCommand is a Command implementation that lists the resource identities
|
|
// within a state file.
|
|
type StateIdentitiesCommand struct {
|
|
Meta
|
|
StateMeta
|
|
}
|
|
|
|
func (c *StateIdentitiesCommand) Run(args []string) int {
|
|
args = c.Meta.process(args)
|
|
var statePath string
|
|
var jsonOutput bool
|
|
cmdFlags := c.Meta.defaultFlagSet("state identities")
|
|
cmdFlags.StringVar(&statePath, "state", "", "path")
|
|
cmdFlags.BoolVar(&jsonOutput, "json", false, "produce JSON output")
|
|
lookupId := cmdFlags.String("id", "", "Restrict output to paths with a resource having the specified ID.")
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
|
|
return cli.RunResultHelp
|
|
}
|
|
args = cmdFlags.Args()
|
|
|
|
if !jsonOutput {
|
|
c.Ui.Error(
|
|
"The `terraform state identities` command requires the `-json` flag.\n")
|
|
cmdFlags.Usage()
|
|
return 1
|
|
}
|
|
view := arguments.ViewJSON // See above
|
|
|
|
if statePath != "" {
|
|
c.Meta.statePath = statePath
|
|
}
|
|
|
|
// Load the backend
|
|
b, diags := c.backend(".", view)
|
|
if diags.HasErrors() {
|
|
c.showDiagnostics(diags)
|
|
return 1
|
|
}
|
|
|
|
// This is a read-only command
|
|
c.ignoreRemoteVersionConflict(b)
|
|
|
|
// Get the state
|
|
env, err := c.Workspace()
|
|
if err != nil {
|
|
c.Ui.Error(fmt.Sprintf("Error selecting workspace: %s", err))
|
|
return 1
|
|
}
|
|
stateMgr, sDiags := b.StateMgr(env)
|
|
if sDiags.HasErrors() {
|
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, sDiags.Err()))
|
|
return 1
|
|
}
|
|
if err := stateMgr.RefreshState(); err != nil {
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
return 1
|
|
}
|
|
|
|
state := stateMgr.State()
|
|
if state == nil {
|
|
c.Ui.Error(errStateNotFound)
|
|
return 1
|
|
}
|
|
|
|
var addrs []addrs.AbsResourceInstance
|
|
if len(args) == 0 {
|
|
addrs, diags = c.lookupAllResourceInstanceAddrs(state)
|
|
} else {
|
|
addrs, diags = c.lookupResourceInstanceAddrs(state, args...)
|
|
}
|
|
if diags.HasErrors() {
|
|
c.showDiagnostics(diags)
|
|
return 1
|
|
}
|
|
|
|
output := make(map[string]any)
|
|
for _, addr := range addrs {
|
|
// If the resource exists but identity is nil, skip it, as it is not required to be present
|
|
if is := state.ResourceInstance(addr); is != nil && is.Current.IdentityJSON != nil {
|
|
if *lookupId == "" || *lookupId == states.LegacyInstanceObjectID(is.Current) {
|
|
var rawIdentity map[string]any
|
|
if err := json.Unmarshal(is.Current.IdentityJSON, &rawIdentity); err != nil {
|
|
c.Ui.Error(fmt.Sprintf("Failed to unmarshal identity JSON: %s", err))
|
|
return 1
|
|
}
|
|
output[addr.String()] = rawIdentity
|
|
}
|
|
}
|
|
}
|
|
|
|
outputJSON, err := json.MarshalIndent(output, "", " ")
|
|
if err != nil {
|
|
c.Ui.Error(fmt.Sprintf("Failed to marshal output JSON: %s", err))
|
|
return 1
|
|
}
|
|
|
|
c.Ui.Output(string(outputJSON))
|
|
c.showDiagnostics(diags)
|
|
|
|
return 0
|
|
}
|
|
|
|
func (c *StateIdentitiesCommand) Help() string {
|
|
helpText := `
|
|
Usage: terraform [global options] state identities [options] -json [address...]
|
|
|
|
List the json format of the identities of resources in the Terraform state.
|
|
|
|
This command lists the identities of resource instances in the Terraform state in json format.
|
|
The address argument can be used to filter the instances by resource or module. If
|
|
no pattern is given, identities for all resource instances are listed.
|
|
|
|
The addresses must either be module addresses or absolute resource
|
|
addresses, such as:
|
|
aws_instance.example
|
|
module.example
|
|
module.example.module.child
|
|
module.example.aws_instance.example
|
|
|
|
An error will be returned if any of the resources or modules given as
|
|
filter addresses do not exist in the state.
|
|
|
|
Options:
|
|
|
|
-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.
|
|
|
|
-id=ID Filters the results to include only instances whose
|
|
resource types have an attribute named "id" whose value
|
|
equals the given id string.
|
|
|
|
`
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (c *StateIdentitiesCommand) Synopsis() string {
|
|
return "List the identities of resources in the state"
|
|
}
|