@@ -2,6 +2,11 @@ package cloudfoundry
22
33import (
44 "context"
5+ "log"
6+ "strings"
7+
8+ "github.com/cloudfoundry/go-cfclient/v3/client"
9+ "github.com/cloudfoundry/go-cfclient/v3/resource"
510 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
611 "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
712
@@ -21,18 +26,32 @@ func resourceRouteServiceBinding() *schema.Resource {
2126 StateContext : resourceRouteServiceBindingImport ,
2227 },
2328
29+ Schema : resourceRouteServiceBindingSchema ().Schema ,
30+ SchemaVersion : 1 ,
31+ StateUpgraders : []schema.StateUpgrader {
32+ {
33+ Type : resourceRouteServiceBindingSchema ().CoreConfigSchema ().ImpliedType (),
34+ Upgrade : upgradeStateRouteServiceBindingStateV0toV1ChangeID ,
35+ Version : 0 ,
36+ },
37+ },
38+ }
39+ }
40+
41+ func resourceRouteServiceBindingSchema () * schema.Resource {
42+ return & schema.Resource {
2443 Schema : map [string ]* schema.Schema {
25- "service_instance" : & schema. Schema {
44+ "service_instance" : {
2645 Type : schema .TypeString ,
2746 Required : true ,
2847 ForceNew : true ,
2948 },
30- "route" : & schema. Schema {
49+ "route" : {
3150 Type : schema .TypeString ,
3251 Required : true ,
3352 ForceNew : true ,
3453 },
35- "json_params" : & schema. Schema {
54+ "json_params" : {
3655 Type : schema .TypeString ,
3756 Optional : true ,
3857 ForceNew : true ,
@@ -41,15 +60,11 @@ func resourceRouteServiceBinding() *schema.Resource {
4160 }
4261}
4362
44- func resourceRouteServiceBindingImport (ctx context.Context , d * schema.ResourceData , meta interface {}) (res []* schema.ResourceData , err error ) {
45- id := d .Id ()
46- if _ , _ , err = parseID (id ); err != nil {
47- return
48- }
63+ func resourceRouteServiceBindingImport (ctx context.Context , d * schema.ResourceData , meta any ) (res []* schema.ResourceData , err error ) {
4964 return ImportReadContext (resourceRouteServiceBindingRead )(ctx , d , meta )
5065}
5166
52- func resourceRouteServiceBindingCreate (ctx context.Context , d * schema.ResourceData , meta interface {} ) diag.Diagnostics {
67+ func resourceRouteServiceBindingCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
5368 session := meta .(* managers.Session )
5469
5570 var data map [string ]interface {}
@@ -63,48 +78,110 @@ func resourceRouteServiceBindingCreate(ctx context.Context, d *schema.ResourceDa
6378 return diag .FromErr (err )
6479 }
6580 }
66- _ , err := session .ClientV2 .CreateServiceBindingRoute (serviceID , routeID , data )
81+
82+ jobGUID , _ , err := session .ClientGo .ServiceRouteBindings .Create (context .Background (), & resource.ServiceRouteBindingCreate {
83+ Relationships : resource.ServiceRouteBindingRelationships {
84+ // ServiceInstance ToOneRelationship `json:"service_instance"`
85+ // // The route that the service instance is bound to
86+ // Route ToOneRelationship `json:"route"`
87+ ServiceInstance : resource.ToOneRelationship {
88+ Data : & resource.Relationship {
89+ GUID : serviceID ,
90+ },
91+ },
92+ Route : resource.ToOneRelationship {
93+ Data : & resource.Relationship {
94+ GUID : routeID ,
95+ },
96+ },
97+ },
98+ })
99+
67100 if err != nil {
68101 return diag .FromErr (err )
69102 }
70103
71- d .SetId (computeID (serviceID , routeID ))
72- return nil
73- }
74-
75- func resourceRouteServiceBindingRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
76- session := meta .(* managers.Session )
77-
78- serviceID , routeID , err := parseID (d .Id ())
104+ if jobGUID != "" {
105+ err = session .ClientGo .Jobs .PollComplete (context .Background (), jobGUID , nil )
106+ }
79107 if err != nil {
80108 return diag .FromErr (err )
81109 }
82- routes , _ , err := session .ClientV2 .GetServiceBindingRoutes (serviceID )
110+
111+ options := client .NewServiceRouteBindingListOptions ()
112+ options .ServiceInstanceGUIDs = client.Filter {Values : []string {serviceID }}
113+ options .RouteGUIDs = client.Filter {Values : []string {routeID }}
114+
115+ routeBinding , err := session .ClientGo .ServiceRouteBindings .Single (context .Background (), options )
116+
83117 if err != nil {
84118 return diag .FromErr (err )
85119 }
86- found := false
87- for _ , route := range routes {
88- if route .GUID == routeID {
89- found = true
90- break
120+
121+ d .SetId (routeBinding .GUID )
122+ return nil
123+ }
124+
125+ func resourceRouteServiceBindingRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
126+ session := meta .(* managers.Session )
127+
128+ routeServiceBinding , err := session .ClientGo .ServiceRouteBindings .Get (context .Background (), d .Id ())
129+
130+ if err != nil {
131+ if strings .Contains (err .Error (), "CF-ResourceNotFound" ) {
132+ d .SetId ("" )
133+ return nil
91134 }
92- }
93- if ! found {
94- d .SetId ("" )
95- return diag .Errorf ("Route '%s' not found in service instance '%s'" , routeID , serviceID )
135+ return diag .Errorf ("Error when reading routeServiceBinding with id '%s': %s" , d .Id (), err )
96136 }
97137
98- d .Set ("service_instance" , serviceID )
99- d .Set ("route" , routeID )
138+ d .Set ("service_instance" , routeServiceBinding .Relationships .ServiceInstance .Data .GUID )
139+ d .Set ("route" , routeServiceBinding .Relationships .Route .Data .GUID )
140+
100141 return nil
101142}
102143
103- func resourceRouteServiceBindingDelete (ctx context.Context , d * schema.ResourceData , meta interface {} ) diag.Diagnostics {
144+ func resourceRouteServiceBindingDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
104145 session := meta .(* managers.Session )
105146
106- serviceID := d .Get ("service_instance" ).(string )
107- routeID := d .Get ("route" ).(string )
108- _ , err := session .ClientV2 .DeleteServiceBindingRoute (serviceID , routeID )
147+ jobGUID , err := session .ClientGo .ServiceRouteBindings .Delete (context .Background (), d .Id ())
148+
149+ if err != nil {
150+ return diag .FromErr (err )
151+ }
152+ if jobGUID != "" {
153+ err = session .ClientGo .Jobs .PollComplete (context .Background (), jobGUID , nil )
154+ }
109155 return diag .FromErr (err )
110156}
157+
158+ func upgradeStateRouteServiceBindingStateV0toV1ChangeID (ctx context.Context , rawState map [string ]any , meta any ) (map [string ]any , error ) {
159+ session := meta .(* managers.Session )
160+
161+ if len (rawState ) == 0 {
162+ log .Println ("[DEBUG] Empty RouteServiceBinding; nothing to migrate." )
163+ return rawState , nil
164+ }
165+
166+ log .Printf ("[DEBUG] Attributes before migration: %#v" , rawState )
167+ options := client .NewServiceRouteBindingListOptions ()
168+ options .ServiceInstanceGUIDs = client.Filter {Values : []string {rawState ["service_instance" ].(string )}}
169+ options .RouteGUIDs = client.Filter {Values : []string {rawState ["route" ].(string )}}
170+
171+ routeBinding , err := session .ClientGo .ServiceRouteBindings .Single (context .Background (), options )
172+
173+ if err != nil {
174+ if err == client .ErrExactlyOneResultNotReturned {
175+ rawState ["id" ] = ""
176+ return rawState , nil
177+ }
178+ log .Println ("[DEBUG] Failed to migrate RouteServiceBinding id: error while searching for the route service binding." )
179+ return rawState , err
180+ }
181+
182+ rawState ["id" ] = routeBinding .GUID
183+
184+ log .Printf ("[DEBUG] Attributes after migration: %#v" , rawState )
185+
186+ return rawState , nil
187+ }
0 commit comments