github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/terraform/state_upgrade_v1_to_v2.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/copystructure"
     7  )
     8  
     9  // upgradeStateV1ToV2 is used to upgrade a V1 state representation
    10  // into a V2 state representation
    11  func upgradeStateV1ToV2(old *stateV1) (*State, error) {
    12  	if old == nil {
    13  		return nil, nil
    14  	}
    15  
    16  	remote, err := old.Remote.upgradeToV2()
    17  	if err != nil {
    18  		return nil, fmt.Errorf("Error upgrading State V1: %v", err)
    19  	}
    20  
    21  	modules := make([]*ModuleState, len(old.Modules))
    22  	for i, module := range old.Modules {
    23  		upgraded, err := module.upgradeToV2()
    24  		if err != nil {
    25  			return nil, fmt.Errorf("Error upgrading State V1: %v", err)
    26  		}
    27  		modules[i] = upgraded
    28  	}
    29  	if len(modules) == 0 {
    30  		modules = nil
    31  	}
    32  
    33  	newState := &State{
    34  		Version: 2,
    35  		Serial:  old.Serial,
    36  		Remote:  remote,
    37  		Modules: modules,
    38  	}
    39  
    40  	newState.sort()
    41  
    42  	return newState, nil
    43  }
    44  
    45  func (old *remoteStateV1) upgradeToV2() (*RemoteState, error) {
    46  	if old == nil {
    47  		return nil, nil
    48  	}
    49  
    50  	config, err := copystructure.Copy(old.Config)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("Error upgrading RemoteState V1: %v", err)
    53  	}
    54  
    55  	return &RemoteState{
    56  		Type:   old.Type,
    57  		Config: config.(map[string]string),
    58  	}, nil
    59  }
    60  
    61  func (old *moduleStateV1) upgradeToV2() (*ModuleState, error) {
    62  	if old == nil {
    63  		return nil, nil
    64  	}
    65  
    66  	path, err := copystructure.Copy(old.Path)
    67  	if err != nil {
    68  		return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err)
    69  	}
    70  
    71  	// Outputs needs upgrading to use the new structure
    72  	outputs := make(map[string]*OutputState)
    73  	for key, output := range old.Outputs {
    74  		outputs[key] = &OutputState{
    75  			Type:      "string",
    76  			Value:     output,
    77  			Sensitive: false,
    78  		}
    79  	}
    80  
    81  	resources := make(map[string]*ResourceState)
    82  	for key, oldResource := range old.Resources {
    83  		upgraded, err := oldResource.upgradeToV2()
    84  		if err != nil {
    85  			return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err)
    86  		}
    87  		resources[key] = upgraded
    88  	}
    89  
    90  	dependencies, err := copystructure.Copy(old.Dependencies)
    91  	if err != nil {
    92  		return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err)
    93  	}
    94  
    95  	return &ModuleState{
    96  		Path:         path.([]string),
    97  		Outputs:      outputs,
    98  		Resources:    resources,
    99  		Dependencies: dependencies.([]string),
   100  	}, nil
   101  }
   102  
   103  func (old *resourceStateV1) upgradeToV2() (*ResourceState, error) {
   104  	if old == nil {
   105  		return nil, nil
   106  	}
   107  
   108  	dependencies, err := copystructure.Copy(old.Dependencies)
   109  	if err != nil {
   110  		return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err)
   111  	}
   112  
   113  	primary, err := old.Primary.upgradeToV2()
   114  	if err != nil {
   115  		return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err)
   116  	}
   117  
   118  	deposed := make([]*InstanceState, len(old.Deposed))
   119  	for i, v := range old.Deposed {
   120  		upgraded, err := v.upgradeToV2()
   121  		if err != nil {
   122  			return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err)
   123  		}
   124  		deposed[i] = upgraded
   125  	}
   126  	if len(deposed) == 0 {
   127  		deposed = nil
   128  	}
   129  
   130  	return &ResourceState{
   131  		Type:         old.Type,
   132  		Dependencies: dependencies.([]string),
   133  		Primary:      primary,
   134  		Deposed:      deposed,
   135  		Provider:     old.Provider,
   136  	}, nil
   137  }
   138  
   139  func (old *instanceStateV1) upgradeToV2() (*InstanceState, error) {
   140  	if old == nil {
   141  		return nil, nil
   142  	}
   143  
   144  	attributes, err := copystructure.Copy(old.Attributes)
   145  	if err != nil {
   146  		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
   147  	}
   148  	ephemeral, err := old.Ephemeral.upgradeToV2()
   149  	if err != nil {
   150  		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
   151  	}
   152  	meta, err := copystructure.Copy(old.Meta)
   153  	if err != nil {
   154  		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
   155  	}
   156  
   157  	return &InstanceState{
   158  		ID:         old.ID,
   159  		Attributes: attributes.(map[string]string),
   160  		Ephemeral:  *ephemeral,
   161  		Meta:       meta.(map[string]string),
   162  	}, nil
   163  }
   164  
   165  func (old *ephemeralStateV1) upgradeToV2() (*EphemeralState, error) {
   166  	connInfo, err := copystructure.Copy(old.ConnInfo)
   167  	if err != nil {
   168  		return nil, fmt.Errorf("Error upgrading EphemeralState V1: %v", err)
   169  	}
   170  	return &EphemeralState{
   171  		ConnInfo: connInfo.(map[string]string),
   172  	}, nil
   173  }