@@ -2,6 +2,11 @@ package cloudfoundry
2
2
3
3
import (
4
4
"context"
5
+ "log"
6
+ "strings"
7
+
8
+ "github.com/cloudfoundry/go-cfclient/v3/client"
9
+ "github.com/cloudfoundry/go-cfclient/v3/resource"
5
10
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
6
11
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
7
12
@@ -21,18 +26,32 @@ func resourceRouteServiceBinding() *schema.Resource {
21
26
StateContext : resourceRouteServiceBindingImport ,
22
27
},
23
28
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 {
24
43
Schema : map [string ]* schema.Schema {
25
- "service_instance" : & schema. Schema {
44
+ "service_instance" : {
26
45
Type : schema .TypeString ,
27
46
Required : true ,
28
47
ForceNew : true ,
29
48
},
30
- "route" : & schema. Schema {
49
+ "route" : {
31
50
Type : schema .TypeString ,
32
51
Required : true ,
33
52
ForceNew : true ,
34
53
},
35
- "json_params" : & schema. Schema {
54
+ "json_params" : {
36
55
Type : schema .TypeString ,
37
56
Optional : true ,
38
57
ForceNew : true ,
@@ -41,15 +60,11 @@ func resourceRouteServiceBinding() *schema.Resource {
41
60
}
42
61
}
43
62
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 ) {
49
64
return ImportReadContext (resourceRouteServiceBindingRead )(ctx , d , meta )
50
65
}
51
66
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 {
53
68
session := meta .(* managers.Session )
54
69
55
70
var data map [string ]interface {}
@@ -63,48 +78,110 @@ func resourceRouteServiceBindingCreate(ctx context.Context, d *schema.ResourceDa
63
78
return diag .FromErr (err )
64
79
}
65
80
}
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
+
67
100
if err != nil {
68
101
return diag .FromErr (err )
69
102
}
70
103
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
+ }
79
107
if err != nil {
80
108
return diag .FromErr (err )
81
109
}
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
+
83
117
if err != nil {
84
118
return diag .FromErr (err )
85
119
}
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
91
134
}
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 )
96
136
}
97
137
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
+
100
141
return nil
101
142
}
102
143
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 {
104
145
session := meta .(* managers.Session )
105
146
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
+ }
109
155
return diag .FromErr (err )
110
156
}
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