// shimState takes a new *states.State and reverts it to a legacy state for the provider ACC tests
shimState takes a new *states.State and reverts it to a legacy state for the provider ACC tests
// shimState takes a new * states . State and reverts it to a legacy state for the provider ACC tests
func shimNewState(newState *states.State, providers map[string]terraform.ResourceProvider) (*terraform.State, error) {
	state := terraform.NewState()
	if newState == nil {
		return nil, nil
	}
	for _, newMod := range newState.Modules {
		mod := state.AddModule(newMod.Addr)
		for name, out := range newMod.OutputValues {
			outputType := ""
			val := hcl2shim.ConfigValueFromHCL2(out.Value)
			ty := out.Value.Type()
			switch {
			case ty == cty.String:
				outputType = "string"
			case ty.IsTupleType() || ty.IsListType():
				outputType = "list"
			case ty.IsMapType():
				outputType = "map"
			}
			mod.Outputs[name] = &terraform.OutputState{
				Type:      outputType,
				Value:     val,
				Sensitive: out.Sensitive,
			}
		}
		for _, res := range newMod.Resources {
			resType := res.Addr.Type
			providerType := res.ProviderConfig.ProviderConfig.Type
			resource := getResource(providers, providerType, res.Addr)
			for key, i := range res.Instances {
				resState := &terraform.ResourceState{
					Type:     resType,
					Provider: res.ProviderConfig.String(),
				}
				if i.Current != nil {
					flatmap, err := shimmedAttributes(i.Current, resource)
					if err != nil {
						return nil, fmt.Errorf("error decoding state for %q: %s", resType, err)
					}
					var meta map[string]interface{}
					if i.Current.Private != nil {
						err := json.Unmarshal(i.Current.Private, &meta)
						if err != nil {
							return nil, err
						}
					}
					resState.Primary = &terraform.InstanceState{
						ID:         flatmap["id"],
						Attributes: flatmap,
						Tainted:    i.Current.Status == states.ObjectTainted,
						Meta:       meta,
					}
					if i.Current.SchemaVersion != 0 {
						if resState.Primary.Meta == nil {
							resState.Primary.Meta = map[string]interface{}{}
						}
						resState.Primary.Meta["schema_version"] = i.Current.SchemaVersion
					}
					for _, dep := range i.Current.Dependencies {
						resState.Dependencies = append(resState.Dependencies, dep.String())
					}
					idx := ""
					switch key.(type) {
					case addrs.IntKey:
						if len(res.Instances) > 1 {
							idx = fmt.Sprintf(".%d", key)
						}
					case addrs.StringKey:
						idx = "." + key.String()
					}
					mod.Resources[res.Addr.String()+idx] = resState
				}
				for _, dep := range i.Deposed {
					flatmap, err := shimmedAttributes(dep, resource)
					if err != nil {
						return nil, fmt.Errorf("error decoding deposed state for %q: %s", resType, err)
					}
					var meta map[string]interface{}
					if dep.Private != nil {
						err := json.Unmarshal(dep.Private, &meta)
						if err != nil {
							return nil, err
						}
					}
					deposed := &terraform.InstanceState{
						ID:         flatmap["id"],
						Attributes: flatmap,
						Tainted:    dep.Status == states.ObjectTainted,
						Meta:       meta,
					}
					if dep.SchemaVersion != 0 {
						deposed.Meta = map[string]interface{}{
							"schema_version": dep.SchemaVersion,
						}
					}
					resState.Deposed = append(resState.Deposed, deposed)
				}
			}
		}
	}
	return state, nil
}
func shimNewState ( newState * states . State , providers map [ string ] terraform . ResourceProvider ) ( * terraform . State , error ) { state := terraform . NewState ( ) if newState == nil { return nil , nil } for _ , newMod := range newState . Modules { mod := state . AddModule ( newMod . Addr ) for name , out := range newMod . OutputValues { outputType := " " val := hcl2shim . ConfigValueFromHCL2 ( out . Value ) ty := out . Value . Type ( ) switch { case ty == cty . String : outputType = " " case ty . IsTupleType ( ) || ty . IsListType ( ) : outputType = " " case ty . IsMapType ( ) : outputType = " " } mod . Outputs [ name ] = & terraform . OutputState { Type : outputType , Value : val , Sensitive : out . Sensitive , } } for _ , res := range newMod . Resources { resType := res . Addr . Type providerType := res . ProviderConfig . ProviderConfig . Type resource := getResource ( providers , providerType , res . Addr ) for key , i := range res . Instances { resState := & terraform . ResourceState { Type : resType , Provider : res . ProviderConfig . String ( ) , } if i . Current != nil { flatmap , err := shimmedAttributes ( i . Current , resource ) if err != nil { return nil , fmt . Errorf ( " " , resType , err ) } var meta map [ string ] interface { } if i . Current . Private != nil { err := json . Unmarshal ( i . Current . Private , & meta ) if err != nil { return nil , err } } resState . Primary = & terraform . InstanceState { ID : flatmap [ " " ] , Attributes : flatmap , Tainted : i . Current . Status == states . ObjectTainted , Meta : meta , } if i . Current . SchemaVersion != 0 { if resState . Primary . Meta == nil { resState . Primary . Meta = map [ string ] interface { } { } } resState . Primary . Meta [ " " ] = i . Current . SchemaVersion } for _ , dep := range i . Current . Dependencies { resState . Dependencies = append ( resState . Dependencies , dep . String ( ) ) } idx := " " switch key . ( type ) { case addrs . IntKey : if len ( res . Instances ) > 1 { idx = fmt . Sprintf ( " " , key ) } case addrs . StringKey : idx = " " + key . String ( ) } mod . Resources [ res . Addr . String ( ) + idx ] = resState } for _ , dep := range i . Deposed { flatmap , err := shimmedAttributes ( dep , resource ) if err != nil { return nil , fmt . Errorf ( " " , resType , err ) } var meta map [ string ] interface { } if dep . Private != nil { err := json . Unmarshal ( dep . Private , & meta ) if err != nil { return nil , err } } deposed := & terraform . InstanceState { ID : flatmap [ " " ] , Attributes : flatmap , Tainted : dep . Status == states . ObjectTainted , Meta : meta , } if dep . SchemaVersion != 0 { deposed . Meta = map [ string ] interface { } { " " : dep . SchemaVersion , } } resState . Deposed = append ( resState . Deposed , deposed ) } } } } return state , nil }
shimState takes a new *states.State and reverts it to a legacy state for the provider ACC tests
shimState takes a new * states . State and reverts it to a legacy state for the provider ACC tests
// in the odd case of a nil state, let the helper packages handle it // We should always have a Current instance here, but be safe about checking. // convert the indexes to the old style flapmap indexes // don't add numeric index values to resources with a count of 0 // add any deposed instances