Skip to content

Commit da652a5

Browse files
committed
use state upgrader instead of migrate
1 parent 26444db commit da652a5

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

cloudfoundry/resource_cf_route_service_binding.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package cloudfoundry
22

33
import (
44
"context"
5-
"fmt"
65
"log"
76
"strings"
87

98
"github.com/cloudfoundry/go-cfclient/v3/client"
109
"github.com/cloudfoundry/go-cfclient/v3/resource"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1311
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
1412

1513
"encoding/json"
@@ -46,7 +44,35 @@ func resourceRouteServiceBinding() *schema.Resource {
4644
},
4745
},
4846
SchemaVersion: 1,
49-
MigrateState: resourceRouteServiceBindingMigrateState,
47+
StateUpgraders: []schema.StateUpgrader{
48+
{
49+
Type: resourceRouteServiceBindingResourceV0().CoreConfigSchema().ImpliedType(),
50+
Upgrade: upgradeStateRouteServiceBindingStateV0toV1ChangeID,
51+
Version: 0,
52+
},
53+
},
54+
}
55+
}
56+
57+
func resourceRouteServiceBindingResourceV0() *schema.Resource {
58+
return &schema.Resource{
59+
Schema: map[string]*schema.Schema{
60+
"service_instance": &schema.Schema{
61+
Type: schema.TypeString,
62+
Required: true,
63+
ForceNew: true,
64+
},
65+
"route": &schema.Schema{
66+
Type: schema.TypeString,
67+
Required: true,
68+
ForceNew: true,
69+
},
70+
"json_params": &schema.Schema{
71+
Type: schema.TypeString,
72+
Optional: true,
73+
ForceNew: true,
74+
},
75+
},
5076
}
5177
}
5278

@@ -145,39 +171,29 @@ func resourceRouteServiceBindingDelete(ctx context.Context, d *schema.ResourceDa
145171
return diag.FromErr(err)
146172
}
147173

148-
func resourceRouteServiceBindingMigrateState(v int, inst *terraform.InstanceState, meta any) (*terraform.InstanceState, error) {
149-
switch v {
150-
case 0:
151-
log.Println("[INFO] Found Route Service Binding State v0; migrating to v1: change ID from routeID:serviceID to routeServiceBindingID.")
152-
return migrateRouteServiceBindingStateV0toV1ChangeID(inst, meta)
153-
default:
154-
return inst, fmt.Errorf("Unexpected schema version: %d", v)
155-
}
156-
}
157-
158-
func migrateRouteServiceBindingStateV0toV1ChangeID(inst *terraform.InstanceState, meta any) (*terraform.InstanceState, error) {
174+
func upgradeStateRouteServiceBindingStateV0toV1ChangeID(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
159175
session := meta.(*managers.Session)
160176

161-
if inst.Empty() {
177+
if len(rawState) == 0 {
162178
log.Println("[DEBUG] Empty RouteServiceBinding; nothing to migrate.")
163-
return inst, nil
179+
return rawState, nil
164180
}
165181

166-
log.Printf("[DEBUG] Attributes before migration: %#v", inst.Attributes)
182+
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)
167183
options := client.NewServiceRouteBindingListOptions()
168-
options.ServiceInstanceGUIDs = client.Filter{Values: []string{inst.Attributes["service_instance"]}}
169-
options.RouteGUIDs = client.Filter{Values: []string{inst.Attributes["route"]}}
184+
options.ServiceInstanceGUIDs = client.Filter{Values: []string{rawState["service_instance"].(string)}}
185+
options.RouteGUIDs = client.Filter{Values: []string{rawState["route"].(string)}}
170186

171187
routeBinding, err := session.ClientGo.ServiceRouteBindings.Single(context.Background(), options)
172188

173189
if err != nil {
174190
log.Println("[DEBUG] Failed to migrate RouteServiceBinding id: did not find the route service binding.")
175-
return inst, err
191+
return rawState, err
176192
}
177193

178-
inst.Attributes["id"] = routeBinding.GUID
194+
rawState["id"] = routeBinding.GUID
179195

180-
log.Printf("[DEBUG] Attributes after migration: %#v", inst.Attributes)
196+
log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
181197

182-
return inst, nil
198+
return rawState, nil
183199
}

0 commit comments

Comments
 (0)