@@ -2,14 +2,12 @@ package cloudfoundry
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"log"
7
6
"strings"
8
7
9
8
"github.com/cloudfoundry/go-cfclient/v3/client"
10
9
"github.com/cloudfoundry/go-cfclient/v3/resource"
11
10
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12
- "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
13
11
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
14
12
15
13
"encoding/json"
@@ -46,7 +44,35 @@ func resourceRouteServiceBinding() *schema.Resource {
46
44
},
47
45
},
48
46
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
+ },
50
76
}
51
77
}
52
78
@@ -145,39 +171,29 @@ func resourceRouteServiceBindingDelete(ctx context.Context, d *schema.ResourceDa
145
171
return diag .FromErr (err )
146
172
}
147
173
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 ) {
159
175
session := meta .(* managers.Session )
160
176
161
- if inst . Empty () {
177
+ if len ( rawState ) == 0 {
162
178
log .Println ("[DEBUG] Empty RouteServiceBinding; nothing to migrate." )
163
- return inst , nil
179
+ return rawState , nil
164
180
}
165
181
166
- log .Printf ("[DEBUG] Attributes before migration: %#v" , inst . Attributes )
182
+ log .Printf ("[DEBUG] Attributes before migration: %#v" , rawState )
167
183
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 ) }}
170
186
171
187
routeBinding , err := session .ClientGo .ServiceRouteBindings .Single (context .Background (), options )
172
188
173
189
if err != nil {
174
190
log .Println ("[DEBUG] Failed to migrate RouteServiceBinding id: did not find the route service binding." )
175
- return inst , err
191
+ return rawState , err
176
192
}
177
193
178
- inst . Attributes ["id" ] = routeBinding .GUID
194
+ rawState ["id" ] = routeBinding .GUID
179
195
180
- log .Printf ("[DEBUG] Attributes after migration: %#v" , inst . Attributes )
196
+ log .Printf ("[DEBUG] Attributes after migration: %#v" , rawState )
181
197
182
- return inst , nil
198
+ return rawState , nil
183
199
}
0 commit comments