2014-05-28 15:56:43 -05:00
|
|
|
package terraform
|
|
|
|
|
2014-06-05 09:27:01 -05:00
|
|
|
import (
|
2014-06-18 18:38:08 -05:00
|
|
|
"bytes"
|
2014-06-18 22:54:22 -05:00
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
2014-06-18 18:38:08 -05:00
|
|
|
"fmt"
|
2014-06-18 22:54:22 -05:00
|
|
|
"io"
|
2014-06-23 14:19:41 -05:00
|
|
|
"sort"
|
2014-06-05 09:27:01 -05:00
|
|
|
"sync"
|
2014-06-12 19:51:38 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-06-05 09:27:01 -05:00
|
|
|
)
|
|
|
|
|
2014-05-28 15:56:43 -05:00
|
|
|
// State keeps track of a snapshot state-of-the-world that Terraform
|
|
|
|
// can use to keep track of what real world resources it is actually
|
|
|
|
// managing.
|
|
|
|
type State struct {
|
2014-07-04 17:36:28 -05:00
|
|
|
Outputs map[string]string
|
2014-06-30 19:40:56 -05:00
|
|
|
Resources map[string]*ResourceState
|
2014-07-22 12:09:11 -05:00
|
|
|
Tainted map[string]struct{}
|
2014-06-18 17:35:03 -05:00
|
|
|
|
|
|
|
once sync.Once
|
2014-06-05 09:27:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) init() {
|
|
|
|
s.once.Do(func() {
|
2014-07-22 12:09:11 -05:00
|
|
|
if s.Resources == nil {
|
|
|
|
s.Resources = make(map[string]*ResourceState)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Tainted == nil {
|
|
|
|
s.Tainted = make(map[string]struct{})
|
|
|
|
}
|
2014-06-05 09:27:01 -05:00
|
|
|
})
|
2014-05-28 15:56:43 -05:00
|
|
|
}
|
2014-06-05 08:57:06 -05:00
|
|
|
|
2014-07-07 01:03:51 -05:00
|
|
|
func (s *State) deepcopy() *State {
|
|
|
|
result := new(State)
|
|
|
|
result.init()
|
|
|
|
if s != nil {
|
|
|
|
for k, v := range s.Resources {
|
|
|
|
result.Resources[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-07-13 13:07:31 -05:00
|
|
|
// prune is a helper that removes any empty IDs from the state
|
|
|
|
// and cleans it up in general.
|
|
|
|
func (s *State) prune() {
|
|
|
|
for k, v := range s.Resources {
|
|
|
|
if v.ID == "" {
|
|
|
|
delete(s.Resources, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 16:58:57 -05:00
|
|
|
// Orphans returns a list of keys of resources that are in the State
|
|
|
|
// but aren't present in the configuration itself. Hence, these keys
|
|
|
|
// represent the state of resources that are orphans.
|
|
|
|
func (s *State) Orphans(c *config.Config) []string {
|
|
|
|
keys := make(map[string]struct{})
|
|
|
|
for k, _ := range s.Resources {
|
|
|
|
keys[k] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range c.Resources {
|
|
|
|
delete(keys, r.Id())
|
2014-07-03 23:42:18 -05:00
|
|
|
|
|
|
|
// If there is only one of this instance, then we alias that
|
|
|
|
// to the ".0" version as well so that it can count
|
|
|
|
if r.Count == 1 {
|
|
|
|
delete(keys, r.Id()+".0")
|
|
|
|
}
|
2014-06-24 16:58:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]string, 0, len(keys))
|
|
|
|
for k, _ := range keys {
|
|
|
|
result = append(result, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-06-18 18:38:08 -05:00
|
|
|
func (s *State) String() string {
|
2014-06-25 20:22:42 -05:00
|
|
|
if len(s.Resources) == 0 {
|
|
|
|
return "<no state>"
|
|
|
|
}
|
|
|
|
|
2014-06-18 18:38:08 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2014-06-23 14:19:41 -05:00
|
|
|
names := make([]string, 0, len(s.Resources))
|
|
|
|
for name, _ := range s.Resources {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
for _, k := range names {
|
|
|
|
rs := s.Resources[k]
|
2014-06-30 13:14:03 -05:00
|
|
|
id := rs.ID
|
|
|
|
if id == "" {
|
|
|
|
id = "<not created>"
|
|
|
|
}
|
2014-06-23 14:19:41 -05:00
|
|
|
|
2014-07-22 12:09:11 -05:00
|
|
|
taintStr := ""
|
|
|
|
if _, ok := s.Tainted[k]; ok {
|
|
|
|
taintStr = " (tainted)"
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s:%s\n", k, taintStr))
|
2014-06-30 13:14:03 -05:00
|
|
|
buf.WriteString(fmt.Sprintf(" ID = %s\n", id))
|
2014-06-18 18:38:08 -05:00
|
|
|
|
2014-07-01 12:28:42 -05:00
|
|
|
attrKeys := make([]string, 0, len(rs.Attributes))
|
|
|
|
for ak, _ := range rs.Attributes {
|
2014-07-08 19:15:41 -05:00
|
|
|
if ak == "id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-01 12:28:42 -05:00
|
|
|
attrKeys = append(attrKeys, ak)
|
|
|
|
}
|
|
|
|
sort.Strings(attrKeys)
|
|
|
|
|
|
|
|
for _, ak := range attrKeys {
|
|
|
|
av := rs.Attributes[ak]
|
2014-06-18 18:38:08 -05:00
|
|
|
buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av))
|
|
|
|
}
|
2014-06-30 19:40:56 -05:00
|
|
|
|
|
|
|
if len(rs.Dependencies) > 0 {
|
|
|
|
buf.WriteString(fmt.Sprintf("\n Dependencies:\n"))
|
|
|
|
for _, dep := range rs.Dependencies {
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s\n", dep.ID))
|
|
|
|
}
|
|
|
|
}
|
2014-06-18 18:38:08 -05:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:36:28 -05:00
|
|
|
if len(s.Outputs) > 0 {
|
|
|
|
buf.WriteString("\nOutputs:\n\n")
|
|
|
|
|
|
|
|
ks := make([]string, 0, len(s.Outputs))
|
|
|
|
for k, _ := range s.Outputs {
|
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
for _, k := range ks {
|
|
|
|
v := s.Outputs[k]
|
|
|
|
buf.WriteString(fmt.Sprintf("%s = %s\n", k, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 18:38:08 -05:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2014-07-10 14:01:26 -05:00
|
|
|
// sensitiveState is used to store sensitive state information
|
|
|
|
// that should not be serialized. This is only used temporarily
|
|
|
|
// and is restored into the state.
|
|
|
|
type sensitiveState struct {
|
2014-07-15 13:30:21 -05:00
|
|
|
ConnInfo map[string]map[string]string
|
2014-07-10 14:01:26 -05:00
|
|
|
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sensitiveState) init() {
|
|
|
|
s.once.Do(func() {
|
2014-07-15 13:30:21 -05:00
|
|
|
s.ConnInfo = make(map[string]map[string]string)
|
2014-07-10 14:01:26 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-18 22:54:22 -05:00
|
|
|
// The format byte is prefixed into the state file format so that we have
|
|
|
|
// the ability in the future to change the file format if we want for any
|
|
|
|
// reason.
|
2014-07-11 23:42:09 -05:00
|
|
|
const stateFormatMagic = "tfstate"
|
|
|
|
const stateFormatVersion byte = 1
|
2014-06-18 22:54:22 -05:00
|
|
|
|
|
|
|
// ReadState reads a state structure out of a reader in the format that
|
|
|
|
// was written by WriteState.
|
|
|
|
func ReadState(src io.Reader) (*State, error) {
|
|
|
|
var result *State
|
2014-07-11 23:42:09 -05:00
|
|
|
var err error
|
|
|
|
n := 0
|
|
|
|
|
|
|
|
// Verify the magic bytes
|
|
|
|
magic := make([]byte, len(stateFormatMagic))
|
|
|
|
for n < len(magic) {
|
|
|
|
n, err = src.Read(magic[n:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error while reading magic bytes: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if string(magic) != stateFormatMagic {
|
|
|
|
return nil, fmt.Errorf("not a valid state file")
|
|
|
|
}
|
2014-06-18 22:54:22 -05:00
|
|
|
|
2014-07-11 23:42:09 -05:00
|
|
|
// Verify the version is something we can read
|
2014-06-18 22:54:22 -05:00
|
|
|
var formatByte [1]byte
|
2014-07-11 23:42:09 -05:00
|
|
|
n, err = src.Read(formatByte[:])
|
2014-06-18 22:54:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != len(formatByte) {
|
|
|
|
return nil, errors.New("failed to read state version byte")
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:42:09 -05:00
|
|
|
if formatByte[0] != stateFormatVersion {
|
2014-06-18 22:54:22 -05:00
|
|
|
return nil, fmt.Errorf("unknown state file version: %d", formatByte[0])
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:42:09 -05:00
|
|
|
// Decode
|
2014-06-18 22:54:22 -05:00
|
|
|
dec := gob.NewDecoder(src)
|
|
|
|
if err := dec.Decode(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteState writes a state somewhere in a binary format.
|
|
|
|
func WriteState(d *State, dst io.Writer) error {
|
2014-07-11 23:42:09 -05:00
|
|
|
// Write the magic bytes so we can determine the file format later
|
|
|
|
n, err := dst.Write([]byte(stateFormatMagic))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(stateFormatMagic) {
|
|
|
|
return errors.New("failed to write state format magic bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a version byte so we can iterate on version at some point
|
|
|
|
n, err = dst.Write([]byte{stateFormatVersion})
|
2014-06-18 22:54:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
return errors.New("failed to write state version byte")
|
|
|
|
}
|
|
|
|
|
2014-07-10 14:01:26 -05:00
|
|
|
// Prevent sensitive information from being serialized
|
|
|
|
sensitive := &sensitiveState{}
|
|
|
|
sensitive.init()
|
|
|
|
for name, r := range d.Resources {
|
|
|
|
if r.ConnInfo != nil {
|
|
|
|
sensitive.ConnInfo[name] = r.ConnInfo
|
|
|
|
r.ConnInfo = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the state
|
|
|
|
err = gob.NewEncoder(dst).Encode(d)
|
|
|
|
|
|
|
|
// Restore the state
|
|
|
|
for name, info := range sensitive.ConnInfo {
|
|
|
|
d.Resources[name].ConnInfo = info
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2014-06-18 22:54:22 -05:00
|
|
|
}
|
|
|
|
|
2014-06-05 08:57:06 -05:00
|
|
|
// ResourceState holds the state of a resource that is used so that
|
|
|
|
// a provider can find and manage an existing resource as well as for
|
|
|
|
// storing attributes that are uesd to populate variables of child
|
|
|
|
// resources.
|
|
|
|
//
|
|
|
|
// Attributes has attributes about the created resource that are
|
|
|
|
// queryable in interpolation: "${type.id.attr}"
|
|
|
|
//
|
|
|
|
// Extra is just extra data that a provider can return that we store
|
|
|
|
// for later, but is not exposed in any way to the user.
|
|
|
|
type ResourceState struct {
|
2014-06-30 19:40:56 -05:00
|
|
|
// This is filled in and managed by Terraform, and is the resource
|
|
|
|
// type itself such as "mycloud_instance". If a resource provider sets
|
|
|
|
// this value, it won't be persisted.
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// The attributes below are all meant to be filled in by the
|
|
|
|
// resource providers themselves. Documentation for each are above
|
|
|
|
// each element.
|
|
|
|
|
|
|
|
// A unique ID for this resource. This is opaque to Terraform
|
|
|
|
// and is only meant as a lookup mechanism for the providers.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Attributes are basic information about the resource. Any keys here
|
|
|
|
// are accessible in variable format within Terraform configurations:
|
|
|
|
// ${resourcetype.name.attribute}.
|
2014-06-05 08:57:06 -05:00
|
|
|
Attributes map[string]string
|
2014-06-30 19:40:56 -05:00
|
|
|
|
2014-07-07 17:08:33 -05:00
|
|
|
// ConnInfo is used for the providers to export information which is
|
|
|
|
// used to connect to the resource for provisioning. For example,
|
|
|
|
// this could contain SSH or WinRM credentials.
|
2014-07-15 13:30:21 -05:00
|
|
|
ConnInfo map[string]string
|
2014-07-07 17:08:33 -05:00
|
|
|
|
2014-06-30 19:40:56 -05:00
|
|
|
// Extra information that the provider can store about a resource.
|
|
|
|
// This data is opaque, never shown to the user, and is sent back to
|
|
|
|
// the provider as-is for whatever purpose appropriate.
|
|
|
|
Extra map[string]interface{}
|
|
|
|
|
|
|
|
// Dependencies are a list of things that this resource relies on
|
|
|
|
// existing to remain intact. For example: an AWS instance might
|
|
|
|
// depend on a subnet (which itself might depend on a VPC, and so
|
|
|
|
// on).
|
|
|
|
//
|
|
|
|
// Terraform uses this information to build valid destruction
|
|
|
|
// orders and to warn the user if they're destroying a resource that
|
|
|
|
// another resource depends on.
|
|
|
|
//
|
|
|
|
// Things can be put into this list that may not be managed by
|
|
|
|
// Terraform. If Terraform doesn't find a matching ID in the
|
|
|
|
// overall state, then it assumes it isn't managed and doesn't
|
|
|
|
// worry about it.
|
|
|
|
Dependencies []ResourceDependency
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergeDiff takes a ResourceDiff and merges the attributes into
|
|
|
|
// this resource state in order to generate a new state. This new
|
|
|
|
// state can be used to provide updated attribute lookups for
|
|
|
|
// variable interpolation.
|
2014-06-05 09:01:51 -05:00
|
|
|
//
|
|
|
|
// If the diff attribute requires computing the value, and hence
|
|
|
|
// won't be available until apply, the value is replaced with the
|
|
|
|
// computeID.
|
2014-06-19 16:08:10 -05:00
|
|
|
func (s *ResourceState) MergeDiff(d *ResourceDiff) *ResourceState {
|
2014-06-05 09:04:44 -05:00
|
|
|
var result ResourceState
|
|
|
|
if s != nil {
|
|
|
|
result = *s
|
|
|
|
}
|
|
|
|
|
2014-06-05 08:57:06 -05:00
|
|
|
result.Attributes = make(map[string]string)
|
2014-06-05 09:04:44 -05:00
|
|
|
if s != nil {
|
|
|
|
for k, v := range s.Attributes {
|
|
|
|
result.Attributes[k] = v
|
|
|
|
}
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|
2014-06-23 14:32:04 -05:00
|
|
|
if d != nil {
|
|
|
|
for k, diff := range d.Attributes {
|
2014-07-09 12:04:14 -05:00
|
|
|
if diff.NewRemoved {
|
|
|
|
delete(result.Attributes, k)
|
|
|
|
continue
|
|
|
|
}
|
2014-06-23 14:32:04 -05:00
|
|
|
if diff.NewComputed {
|
|
|
|
result.Attributes[k] = config.UnknownVariableValue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Attributes[k] = diff.New
|
2014-06-05 09:01:51 -05:00
|
|
|
}
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:27:01 -05:00
|
|
|
return &result
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|
2014-06-30 19:40:56 -05:00
|
|
|
|
|
|
|
// ResourceDependency maps a resource to another resource that it
|
|
|
|
// depends on to remain intact and uncorrupted.
|
|
|
|
type ResourceDependency struct {
|
|
|
|
// ID of the resource that we depend on. This ID should map
|
|
|
|
// directly to another ResourceState's ID.
|
|
|
|
ID string
|
|
|
|
}
|