github.com/opentofu/opentofu@v1.7.1/internal/legacy/tofu/state_v1.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package tofu
     7  
     8  // stateV1 keeps track of a snapshot state-of-the-world that OpenTofu
     9  // can use to keep track of what real world resources it is actually
    10  // managing.
    11  //
    12  // stateV1 is _only used for the purposes of backwards compatibility
    13  // and is no longer used in OpenTofu.
    14  //
    15  // For the upgrade process, see state_upgrade_v1_to_v2.go
    16  type stateV1 struct {
    17  	// Version is the protocol version. "1" for a StateV1.
    18  	Version int `json:"version"`
    19  
    20  	// Serial is incremented on any operation that modifies
    21  	// the State file. It is used to detect potentially conflicting
    22  	// updates.
    23  	Serial int64 `json:"serial"`
    24  
    25  	// Remote is used to track the metadata required to
    26  	// pull and push state files from a remote storage endpoint.
    27  	Remote *remoteStateV1 `json:"remote,omitempty"`
    28  
    29  	// Modules contains all the modules in a breadth-first order
    30  	Modules []*moduleStateV1 `json:"modules"`
    31  }
    32  
    33  type remoteStateV1 struct {
    34  	// Type controls the client we use for the remote state
    35  	Type string `json:"type"`
    36  
    37  	// Config is used to store arbitrary configuration that
    38  	// is type specific
    39  	Config map[string]string `json:"config"`
    40  }
    41  
    42  type moduleStateV1 struct {
    43  	// Path is the import path from the root module. Modules imports are
    44  	// always disjoint, so the path represents amodule tree
    45  	Path []string `json:"path"`
    46  
    47  	// Outputs declared by the module and maintained for each module
    48  	// even though only the root module technically needs to be kept.
    49  	// This allows operators to inspect values at the boundaries.
    50  	Outputs map[string]string `json:"outputs"`
    51  
    52  	// Resources is a mapping of the logically named resource to
    53  	// the state of the resource. Each resource may actually have
    54  	// N instances underneath, although a user only needs to think
    55  	// about the 1:1 case.
    56  	Resources map[string]*resourceStateV1 `json:"resources"`
    57  
    58  	// Dependencies are a list of things that this module relies on
    59  	// existing to remain intact. For example: an module may depend
    60  	// on a VPC ID given by an aws_vpc resource.
    61  	//
    62  	// OpenTofu uses this information to build valid destruction
    63  	// orders and to warn the user if they're destroying a module that
    64  	// another resource depends on.
    65  	//
    66  	// Things can be put into this list that may not be managed by
    67  	// OpenTofu. If OpenTofu doesn't find a matching ID in the
    68  	// overall state, then it assumes it isn't managed and doesn't
    69  	// worry about it.
    70  	Dependencies []string `json:"depends_on,omitempty"`
    71  }
    72  
    73  type resourceStateV1 struct {
    74  	// This is filled in and managed by OpenTofu, and is the resource
    75  	// type itself such as "mycloud_instance". If a resource provider sets
    76  	// this value, it won't be persisted.
    77  	Type string `json:"type"`
    78  
    79  	// Dependencies are a list of things that this resource relies on
    80  	// existing to remain intact. For example: an AWS instance might
    81  	// depend on a subnet (which itself might depend on a VPC, and so
    82  	// on).
    83  	//
    84  	// OpenTofu uses this information to build valid destruction
    85  	// orders and to warn the user if they're destroying a resource that
    86  	// another resource depends on.
    87  	//
    88  	// Things can be put into this list that may not be managed by
    89  	// OpenTofu. If OpenTofu doesn't find a matching ID in the
    90  	// overall state, then it assumes it isn't managed and doesn't
    91  	// worry about it.
    92  	Dependencies []string `json:"depends_on,omitempty"`
    93  
    94  	// Primary is the current active instance for this resource.
    95  	// It can be replaced but only after a successful creation.
    96  	// This is the instances on which providers will act.
    97  	Primary *instanceStateV1 `json:"primary"`
    98  
    99  	// Tainted is used to track any underlying instances that
   100  	// have been created but are in a bad or unknown state and
   101  	// need to be cleaned up subsequently.  In the
   102  	// standard case, there is only at most a single instance.
   103  	// However, in pathological cases, it is possible for the number
   104  	// of instances to accumulate.
   105  	Tainted []*instanceStateV1 `json:"tainted,omitempty"`
   106  
   107  	// Deposed is used in the mechanics of CreateBeforeDestroy: the existing
   108  	// Primary is Deposed to get it out of the way for the replacement Primary to
   109  	// be created by Apply. If the replacement Primary creates successfully, the
   110  	// Deposed instance is cleaned up. If there were problems creating the
   111  	// replacement, the instance remains in the Deposed list so it can be
   112  	// destroyed in a future run. Functionally, Deposed instances are very
   113  	// similar to Tainted instances in that OpenTofu is only tracking them in
   114  	// order to remember to destroy them.
   115  	Deposed []*instanceStateV1 `json:"deposed,omitempty"`
   116  
   117  	// Provider is used when a resource is connected to a provider with an alias.
   118  	// If this string is empty, the resource is connected to the default provider,
   119  	// e.g. "aws_instance" goes with the "aws" provider.
   120  	// If the resource block contained a "provider" key, that value will be set here.
   121  	Provider string `json:"provider,omitempty"`
   122  }
   123  
   124  type instanceStateV1 struct {
   125  	// A unique ID for this resource. This is opaque to OpenTofu
   126  	// and is only meant as a lookup mechanism for the providers.
   127  	ID string `json:"id"`
   128  
   129  	// Attributes are basic information about the resource. Any keys here
   130  	// are accessible in variable format within OpenTofu configurations:
   131  	// ${resourcetype.name.attribute}.
   132  	Attributes map[string]string `json:"attributes,omitempty"`
   133  
   134  	// Ephemeral is used to store any state associated with this instance
   135  	// that is necessary for the OpenTofu run to complete, but is not
   136  	// persisted to a state file.
   137  	Ephemeral ephemeralStateV1 `json:"-"`
   138  
   139  	// Meta is a simple K/V map that is persisted to the State but otherwise
   140  	// ignored by OpenTofu core. It's meant to be used for accounting by
   141  	// external client code.
   142  	Meta map[string]string `json:"meta,omitempty"`
   143  }
   144  
   145  type ephemeralStateV1 struct {
   146  	// ConnInfo is used for the providers to export information which is
   147  	// used to connect to the resource for provisioning. For example,
   148  	// this could contain SSH or WinRM credentials.
   149  	ConnInfo map[string]string `json:"-"`
   150  }