Skip to content

Commit 3a14f25

Browse files
authored
Merge pull request #370 from hashicorp/restore-func-naming
Restore original function names in schema.Provider
2 parents 0f8429e + 2fb6a6e commit 3a14f25

File tree

6 files changed

+16
-540
lines changed

6 files changed

+16
-540
lines changed

helper/schema/provider.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,13 @@ func (p *Provider) ValidateResource(
210210
return r.Validate(c)
211211
}
212212

213-
// ConfigureContext configures the provider itself with the configuration
213+
// Configure configures the provider itself with the configuration
214214
// given. This is useful for setting things like access keys.
215215
//
216216
// This won't be called at all if no provider configuration is given.
217217
//
218218
// Configure returns an error if it occurred.
219-
func (p *Provider) ConfigureContext(ctx context.Context, c *terraform.ResourceConfig) error {
219+
func (p *Provider) Configure(ctx context.Context, c *terraform.ResourceConfig) error {
220220
// No configuration
221221
if p.ConfigureFunc == nil && p.ConfigureContextFunc == nil {
222222
return nil
@@ -282,7 +282,7 @@ func (p *Provider) Resources() []terraform.ResourceType {
282282
return result
283283
}
284284

285-
// ImportStateContext requests that the given resource be imported.
285+
// ImportState requests that the given resource be imported.
286286
//
287287
// The returned InstanceState only requires ID be set. Importing
288288
// will always call Refresh after the state to complete it.
@@ -296,7 +296,7 @@ func (p *Provider) Resources() []terraform.ResourceType {
296296
// to multiple. For example, an AWS security group may contain many rules.
297297
// Each rule is represented by a separate resource in Terraform,
298298
// therefore multiple states are returned.
299-
func (p *Provider) ImportStateContext(
299+
func (p *Provider) ImportState(
300300
ctx context.Context,
301301
info *terraform.InstanceInfo,
302302
id string) ([]*terraform.InstanceState, error) {

helper/schema/provider_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func TestProviderConfigure(t *testing.T) {
177177

178178
for i, tc := range cases {
179179
c := terraform.NewResourceConfigRaw(tc.Config)
180-
err := tc.P.ConfigureContext(context.Background(), c)
180+
err := tc.P.Configure(context.Background(), c)
181181
if err != nil != tc.Err {
182182
t.Fatalf("%d: %s", i, err)
183183
}
@@ -411,7 +411,7 @@ func TestProviderImportState_default(t *testing.T) {
411411
},
412412
}
413413

414-
states, err := p.ImportStateContext(context.Background(), &terraform.InstanceInfo{
414+
states, err := p.ImportState(context.Background(), &terraform.InstanceInfo{
415415
Type: "foo",
416416
}, "bar")
417417
if err != nil {
@@ -443,7 +443,7 @@ func TestProviderImportState_setsId(t *testing.T) {
443443
},
444444
}
445445

446-
_, err := p.ImportStateContext(context.Background(), &terraform.InstanceInfo{
446+
_, err := p.ImportState(context.Background(), &terraform.InstanceInfo{
447447
Type: "foo",
448448
}, "bar")
449449
if err != nil {
@@ -473,7 +473,7 @@ func TestProviderImportState_setsType(t *testing.T) {
473473
},
474474
}
475475

476-
_, err := p.ImportStateContext(context.Background(), &terraform.InstanceInfo{
476+
_, err := p.ImportState(context.Background(), &terraform.InstanceInfo{
477477
Type: "foo",
478478
}, "bar")
479479
if err != nil {

helper/schema/resource.go

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -541,130 +541,6 @@ func (r *Resource) RefreshWithoutUpgrade(
541541
return r.recordCurrentSchemaVersion(state), err
542542
}
543543

544-
// Refresh refreshes the state of the resource.
545-
func (r *Resource) Refresh(
546-
ctx context.Context,
547-
s *terraform.InstanceState,
548-
meta interface{}) (*terraform.InstanceState, error) {
549-
// If the ID is already somehow blank, it doesn't exist
550-
if s.ID == "" {
551-
return nil, nil
552-
}
553-
554-
rt := ResourceTimeout{}
555-
if _, ok := s.Meta[TimeoutKey]; ok {
556-
if err := rt.StateDecode(s); err != nil {
557-
log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
558-
}
559-
}
560-
561-
if r.existsFuncSet() {
562-
// Make a copy of data so that if it is modified it doesn't
563-
// affect our Read later.
564-
data, err := schemaMap(r.Schema).Data(s, nil)
565-
data.timeouts = &rt
566-
567-
if err != nil {
568-
return s, err
569-
}
570-
571-
exists, err := r.exists(ctx, data, meta)
572-
573-
if err != nil {
574-
return s, err
575-
}
576-
if !exists {
577-
return nil, nil
578-
}
579-
}
580-
581-
// there may be new StateUpgraders that need to be run
582-
s, err := r.upgradeState(ctx, s, meta)
583-
if err != nil {
584-
return s, err
585-
}
586-
587-
data, err := schemaMap(r.Schema).Data(s, nil)
588-
data.timeouts = &rt
589-
if err != nil {
590-
return s, err
591-
}
592-
593-
err = r.read(ctx, data, meta)
594-
595-
state := data.State()
596-
if state != nil && state.ID == "" {
597-
state = nil
598-
}
599-
600-
return r.recordCurrentSchemaVersion(state), err
601-
}
602-
603-
func (r *Resource) upgradeState(ctx context.Context, s *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
604-
var err error
605-
606-
needsMigration, stateSchemaVersion := r.checkSchemaVersion(s)
607-
migrate := needsMigration && r.MigrateState != nil
608-
609-
if migrate {
610-
s, err = r.MigrateState(stateSchemaVersion, s, meta)
611-
if err != nil {
612-
return s, err
613-
}
614-
}
615-
616-
if len(r.StateUpgraders) == 0 {
617-
return s, nil
618-
}
619-
620-
// If we ran MigrateState, then the stateSchemaVersion value is no longer
621-
// correct. We can expect the first upgrade function to be the correct
622-
// schema type version.
623-
if migrate {
624-
stateSchemaVersion = r.StateUpgraders[0].Version
625-
}
626-
627-
schemaType := r.CoreConfigSchema().ImpliedType()
628-
// find the expected type to convert the state
629-
for _, upgrader := range r.StateUpgraders {
630-
if stateSchemaVersion == upgrader.Version {
631-
schemaType = upgrader.Type
632-
}
633-
}
634-
635-
// StateUpgraders only operate on the new JSON format state, so the state
636-
// need to be converted.
637-
stateVal, err := StateValueFromInstanceState(s, schemaType)
638-
if err != nil {
639-
return nil, err
640-
}
641-
642-
jsonState, err := StateValueToJSONMap(stateVal, schemaType)
643-
if err != nil {
644-
return nil, err
645-
}
646-
647-
for _, upgrader := range r.StateUpgraders {
648-
if stateSchemaVersion != upgrader.Version {
649-
continue
650-
}
651-
652-
jsonState, err = upgrader.Upgrade(ctx, jsonState, meta)
653-
if err != nil {
654-
return nil, err
655-
}
656-
stateSchemaVersion++
657-
}
658-
659-
// now we need to re-flatmap the new state
660-
stateVal, err = JSONMapToStateValue(jsonState, r.CoreConfigSchema())
661-
if err != nil {
662-
return nil, err
663-
}
664-
665-
return r.ShimInstanceStateFromValue(stateVal)
666-
}
667-
668544
func (r *Resource) createFuncSet() bool {
669545
return (r.Create != nil || r.CreateContext != nil)
670546
}
@@ -895,36 +771,6 @@ func (r *Resource) isTopLevel() bool {
895771
return (r.createFuncSet() || r.readFuncSet())
896772
}
897773

898-
// Determines if a given InstanceState needs to be migrated by checking the
899-
// stored version number with the current SchemaVersion
900-
func (r *Resource) checkSchemaVersion(is *terraform.InstanceState) (bool, int) {
901-
// Get the raw interface{} value for the schema version. If it doesn't
902-
// exist or is nil then set it to zero.
903-
raw := is.Meta["schema_version"]
904-
if raw == nil {
905-
raw = "0"
906-
}
907-
908-
// Try to convert it to a string. If it isn't a string then we pretend
909-
// that it isn't set at all. It should never not be a string unless it
910-
// was manually tampered with.
911-
rawString, ok := raw.(string)
912-
if !ok {
913-
rawString = "0"
914-
}
915-
916-
stateSchemaVersion, _ := strconv.Atoi(rawString)
917-
918-
// Don't run MigrateState if the version is handled by a StateUpgrader,
919-
// since StateMigrateFuncs are not required to handle unknown versions
920-
maxVersion := r.SchemaVersion
921-
if len(r.StateUpgraders) > 0 {
922-
maxVersion = r.StateUpgraders[0].Version
923-
}
924-
925-
return stateSchemaVersion < maxVersion, stateSchemaVersion
926-
}
927-
928774
func (r *Resource) recordCurrentSchemaVersion(
929775
state *terraform.InstanceState) *terraform.InstanceState {
930776
if state != nil && r.SchemaVersion > 0 {

helper/schema/resource_importer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (r *ResourceImporter) InternalValidate() error {
6262
return nil
6363
}
6464

65-
// ImportStatePassthrough is an implementation of StateContextFunc that can be
65+
// ImportStatePassthrough is an implementation of StateFunc that can be
6666
// used to simply pass the ID directly through.
6767
//
6868
// Deprecated: Please use the context aware ImportStatePassthroughContext instead

0 commit comments

Comments
 (0)