github.com/jayaramimmaneni/terraform@v0.11.12-beta1/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  	newState.init()
    42  
    43  	return newState, nil
    44  }
    45  
    46  func (old *remoteStateV1) upgradeToV2() (*RemoteState, error) {
    47  	if old == nil {
    48  		return nil, nil
    49  	}
    50  
    51  	config, err := copystructure.Copy(old.Config)
    52  	if err != nil {
    53  		return nil, fmt.Errorf("Error upgrading RemoteState V1: %v", err)
    54  	}
    55  
    56  	return &RemoteState{
    57  		Type:   old.Type,
    58  		Config: config.(map[string]string),
    59  	}, nil
    60  }
    61  
    62  func (old *moduleStateV1) upgradeToV2() (*ModuleState, error) {
    63  	if old == nil {
    64  		return nil, nil
    65  	}
    66  
    67  	pathRaw, err := copystructure.Copy(old.Path)
    68  	if err != nil {
    69  		return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err)
    70  	}
    71  	path, ok := pathRaw.([]string)
    72  	if !ok {
    73  		return nil, fmt.Errorf("Error upgrading ModuleState V1: path is not a list of strings")
    74  	}
    75  	if len(path) == 0 {
    76  		// We found some V1 states with a nil path. Assume root and catch
    77  		// duplicate path errors later (as part of Validate).
    78  		path = rootModulePath
    79  	}
    80  
    81  	// Outputs needs upgrading to use the new structure
    82  	outputs := make(map[string]*OutputState)
    83  	for key, output := range old.Outputs {
    84  		outputs[key] = &OutputState{
    85  			Type:      "string",
    86  			Value:     output,
    87  			Sensitive: false,
    88  		}
    89  	}
    90  
    91  	resources := make(map[string]*ResourceState)
    92  	for key, oldResource := range old.Resources {
    93  		upgraded, err := oldResource.upgradeToV2()
    94  		if err != nil {
    95  			return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err)
    96  		}
    97  		resources[key] = upgraded
    98  	}
    99  
   100  	dependencies, err := copystructure.Copy(old.Dependencies)
   101  	if err != nil {
   102  		return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err)
   103  	}
   104  
   105  	return &ModuleState{
   106  		Path:         path,
   107  		Outputs:      outputs,
   108  		Resources:    resources,
   109  		Dependencies: dependencies.([]string),
   110  	}, nil
   111  }
   112  
   113  func (old *resourceStateV1) upgradeToV2() (*ResourceState, error) {
   114  	if old == nil {
   115  		return nil, nil
   116  	}
   117  
   118  	dependencies, err := copystructure.Copy(old.Dependencies)
   119  	if err != nil {
   120  		return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err)
   121  	}
   122  
   123  	primary, err := old.Primary.upgradeToV2()
   124  	if err != nil {
   125  		return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err)
   126  	}
   127  
   128  	deposed := make([]*InstanceState, len(old.Deposed))
   129  	for i, v := range old.Deposed {
   130  		upgraded, err := v.upgradeToV2()
   131  		if err != nil {
   132  			return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err)
   133  		}
   134  		deposed[i] = upgraded
   135  	}
   136  	if len(deposed) == 0 {
   137  		deposed = nil
   138  	}
   139  
   140  	return &ResourceState{
   141  		Type:         old.Type,
   142  		Dependencies: dependencies.([]string),
   143  		Primary:      primary,
   144  		Deposed:      deposed,
   145  		Provider:     old.Provider,
   146  	}, nil
   147  }
   148  
   149  func (old *instanceStateV1) upgradeToV2() (*InstanceState, error) {
   150  	if old == nil {
   151  		return nil, nil
   152  	}
   153  
   154  	attributes, err := copystructure.Copy(old.Attributes)
   155  	if err != nil {
   156  		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
   157  	}
   158  	ephemeral, err := old.Ephemeral.upgradeToV2()
   159  	if err != nil {
   160  		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
   161  	}
   162  
   163  	meta, err := copystructure.Copy(old.Meta)
   164  	if err != nil {
   165  		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
   166  	}
   167  
   168  	newMeta := make(map[string]interface{})
   169  	for k, v := range meta.(map[string]string) {
   170  		newMeta[k] = v
   171  	}
   172  
   173  	return &InstanceState{
   174  		ID:         old.ID,
   175  		Attributes: attributes.(map[string]string),
   176  		Ephemeral:  *ephemeral,
   177  		Meta:       newMeta,
   178  	}, nil
   179  }
   180  
   181  func (old *ephemeralStateV1) upgradeToV2() (*EphemeralState, error) {
   182  	connInfo, err := copystructure.Copy(old.ConnInfo)
   183  	if err != nil {
   184  		return nil, fmt.Errorf("Error upgrading EphemeralState V1: %v", err)
   185  	}
   186  	return &EphemeralState{
   187  		ConnInfo: connInfo.(map[string]string),
   188  	}, nil
   189  }