github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/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 path, err := copystructure.Copy(old.Path) 68 if err != nil { 69 return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err) 70 } 71 72 // Outputs needs upgrading to use the new structure 73 outputs := make(map[string]*OutputState) 74 for key, output := range old.Outputs { 75 outputs[key] = &OutputState{ 76 Type: "string", 77 Value: output, 78 Sensitive: false, 79 } 80 } 81 82 resources := make(map[string]*ResourceState) 83 for key, oldResource := range old.Resources { 84 upgraded, err := oldResource.upgradeToV2() 85 if err != nil { 86 return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err) 87 } 88 resources[key] = upgraded 89 } 90 91 dependencies, err := copystructure.Copy(old.Dependencies) 92 if err != nil { 93 return nil, fmt.Errorf("Error upgrading ModuleState V1: %v", err) 94 } 95 96 return &ModuleState{ 97 Path: path.([]string), 98 Outputs: outputs, 99 Resources: resources, 100 Dependencies: dependencies.([]string), 101 }, nil 102 } 103 104 func (old *resourceStateV1) upgradeToV2() (*ResourceState, error) { 105 if old == nil { 106 return nil, nil 107 } 108 109 dependencies, err := copystructure.Copy(old.Dependencies) 110 if err != nil { 111 return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err) 112 } 113 114 primary, err := old.Primary.upgradeToV2() 115 if err != nil { 116 return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err) 117 } 118 119 deposed := make([]*InstanceState, len(old.Deposed)) 120 for i, v := range old.Deposed { 121 upgraded, err := v.upgradeToV2() 122 if err != nil { 123 return nil, fmt.Errorf("Error upgrading ResourceState V1: %v", err) 124 } 125 deposed[i] = upgraded 126 } 127 if len(deposed) == 0 { 128 deposed = nil 129 } 130 131 return &ResourceState{ 132 Type: old.Type, 133 Dependencies: dependencies.([]string), 134 Primary: primary, 135 Deposed: deposed, 136 Provider: old.Provider, 137 }, nil 138 } 139 140 func (old *instanceStateV1) upgradeToV2() (*InstanceState, error) { 141 if old == nil { 142 return nil, nil 143 } 144 145 attributes, err := copystructure.Copy(old.Attributes) 146 if err != nil { 147 return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err) 148 } 149 ephemeral, err := old.Ephemeral.upgradeToV2() 150 if err != nil { 151 return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err) 152 } 153 meta, err := copystructure.Copy(old.Meta) 154 if err != nil { 155 return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err) 156 } 157 158 return &InstanceState{ 159 ID: old.ID, 160 Attributes: attributes.(map[string]string), 161 Ephemeral: *ephemeral, 162 Meta: meta.(map[string]string), 163 }, nil 164 } 165 166 func (old *ephemeralStateV1) upgradeToV2() (*EphemeralState, error) { 167 connInfo, err := copystructure.Copy(old.ConnInfo) 168 if err != nil { 169 return nil, fmt.Errorf("Error upgrading EphemeralState V1: %v", err) 170 } 171 return &EphemeralState{ 172 ConnInfo: connInfo.(map[string]string), 173 }, nil 174 }