tfstate

package module
v0.0.0-...-9b9568d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2022 License: MPL-2.0 Imports: 6 Imported by: 2

README

PkgGoDev

tfstate

Helper types for Terraform state on top of https://github.com/hashicorp/terraform-json.

Why

Currently, there is no good way to interact with Terraform state. This makes sense as Terraform core intentionally hide the details of the state as it is evolving quickly. If you look into the Terraform core codebase, you'll notce that there are multiple formats defined for states along the lifetime of Terraform.

Whilst for some reason, developers still want a way to inspect the Terraform state file via some means. Currently, the correct way to do so is via https://github.com/hashicorp/terraform-exec. Where it provides a method Show(), that returns you a tfjson.State.

Everything works just fine, the only problem is for each resource instance inside tfjson.State, its main content AttributeValues is of type map[string]interface{}. This makes the user can hardly do some fancy inspection on the resource attributes, as they are not typed.

This package aims to fix this last gap by defining a thin wrapper tfstate.State around the tfjson.State, which has almost the same structure, except the AttributeValues is replaced with Value, which is of type cty.Value.

Note

This package only works for the V4 format of state file, which is the used since Terraform v0.12.

Example

See: https://github.com/magodo/tfstate/blob/main/state_example_test.go.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type State

type State struct {
	TerraformVersion string
	Values           *StateValues
}

func FromJSONState

func FromJSONState(rawState *tfjson.State, schemas *tfjson.ProviderSchemas) (*State, error)
Example
package main

import (
	"context"
	"log"

	"github.com/hashicorp/hc-install/fs"
	"github.com/hashicorp/hc-install/product"
	"github.com/hashicorp/terraform-exec/tfexec"
	"github.com/magodo/tfstate"
)

func main() {
	ctx := context.TODO()
	av := fs.AnyVersion{
		Product: &product.Terraform,
	}
	execPath, err := av.Find(ctx)
	if err != nil {
		log.Fatal(err)
	}
	tf, err := tfexec.NewTerraform(".", execPath)
	if err != nil {
		log.Fatal(err)
	}
	schema, err := tf.ProvidersSchema(ctx)
	if err != nil {
		log.Fatalf("get provider schema: %v", err)
	}
	rawState, err := tf.Show(ctx)
	if err != nil {
		log.Fatalf("show state: %v", err)
	}
	state, err := tfstate.FromJSONState(rawState, schema)
	if err != nil {
		log.Fatal(err)
	}
	_ = state
}
Output:

type StateModule

type StateModule struct {
	Resources    []*StateResource
	Address      string
	ChildModules []*StateModule
}

func FromJSONStateModule

func FromJSONStateModule(module *tfjson.StateModule, schemas *tfjson.ProviderSchemas) (*StateModule, error)

type StateOutput

type StateOutput struct {
	Sensitive bool
	Value     interface{}
}

func FromJSONStateOutput

func FromJSONStateOutput(output *tfjson.StateOutput) *StateOutput

type StateResource

type StateResource struct {
	Address         string
	Mode            tfjson.ResourceMode
	Type            string
	Name            string
	Index           interface{}
	ProviderName    string
	SchemaVersion   uint64
	Value           cty.Value
	SensitiveValues json.RawMessage
	DependsOn       []string
	Tainted         bool
	DeposedKey      string
}

func FromJSONStateResource

func FromJSONStateResource(resource *tfjson.StateResource, schemas *tfjson.ProviderSchemas) (*StateResource, error)

type StateValues

type StateValues struct {
	RootModule *StateModule
	Outputs    map[string]*StateOutput
}

Directories

Path Synopsis
terraform

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL