Skip to content

Commit 78edac1

Browse files
authored
Merge pull request #265 from datev/feature/import-service-instance-sharing
Feature/import service instance sharing
2 parents 8bafafc + d81624c commit 78edac1

File tree

6 files changed

+162
-4
lines changed

6 files changed

+162
-4
lines changed

cloudfoundry/provider/fixtures/resource_service_instance_sharing.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,70 @@ interactions:
268268
status: 204 No Content
269269
code: 204
270270
duration: 204.222897ms
271+
- id: 4
272+
request:
273+
proto: HTTP/1.1
274+
proto_major: 1
275+
proto_minor: 1
276+
content_length: 0
277+
transfer_encoding: [ ]
278+
trailer: { }
279+
host: api.x.x.x.x.com
280+
remote_addr: ""
281+
request_uri: ""
282+
body: ""
283+
form: { }
284+
headers:
285+
Authorization:
286+
- Bearer redacted
287+
User-Agent:
288+
- Go-CF-Client/3.0
289+
url: https://api.x.x.x.x.com/v3/service_instances/5e2976bb-332e-41e1-8be3-53baafea9296/shared_spaces
290+
method: GET
291+
response:
292+
proto: HTTP/2.0
293+
proto_major: 2
294+
proto_minor: 0
295+
transfer_encoding: [ ]
296+
trailer: { }
297+
content_length: -1
298+
uncompressed: false
299+
body: '{"data":[{"guid":"02c0cc92-6ecc-44b1-b7b2-096ca19ee143"}, {"guid":"121c3a95-0f82-45a6-8ff2-1920b2067edb"}]}'
300+
headers:
301+
Content-Type:
302+
- application/json; charset=utf-8
303+
Date:
304+
- Mon, 25 Mar 2024 10:39:34 GMT
305+
Referrer-Policy:
306+
- strict-origin-when-cross-origin
307+
Server:
308+
- nginx
309+
Strict-Transport-Security:
310+
- max-age=31536000; includeSubDomains; preload;
311+
X-B3-Spanid:
312+
- 7031327687a56f81
313+
X-B3-Traceid:
314+
- de8d237a1f2045057031327687a56f81
315+
X-Content-Type-Options:
316+
- nosniff
317+
X-Download-Options:
318+
- noopen
319+
X-Frame-Options:
320+
- SAMEORIGIN
321+
X-Permitted-Cross-Domain-Policies:
322+
- none
323+
X-Ratelimit-Limit:
324+
- "20000"
325+
X-Ratelimit-Remaining:
326+
- "18000"
327+
X-Ratelimit-Reset:
328+
- "1711364588"
329+
X-Runtime:
330+
- "0.058984"
331+
X-Vcap-Request-Id:
332+
- de8d237a-1f20-4505-7031-327687a56f81::f3bea4eb-8c5a-432a-8d19-f256a2758e80
333+
X-Xss-Protection:
334+
- 1; mode=block
335+
status: 200 OK
336+
code: 200
337+
duration: 225.980057ms

cloudfoundry/provider/resource_service_instance_sharing.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"github.com/hashicorp/terraform-plugin-framework/path"
67

78
"github.com/cloudfoundry/terraform-provider-cloudfoundry/internal/validation"
89
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
@@ -45,6 +46,7 @@ func (r *serviceInstanceSharingResource) Schema(ctx context.Context, req resourc
4546
MarkdownDescription: "Provides a resource for managing service instance sharing in Cloud Foundry.",
4647

4748
Attributes: map[string]schema.Attribute{
49+
idKey: guidSchema(),
4850
"service_instance": schema.StringAttribute{
4951
MarkdownDescription: "The ID of the service instance to share.",
5052
Required: true,
@@ -108,8 +110,10 @@ func (r *serviceInstanceSharingResource) Create(ctx context.Context, req resourc
108110
resp.Diagnostics.AddError("Error sharing service instance with spaces", err.Error())
109111
return
110112
}
113+
111114
tflog.Trace(ctx, "created a service instance sharing resource")
112115
newState := ServiceInstanceSharingType{
116+
Id: plan.ServiceInstance,
113117
ServiceInstance: plan.ServiceInstance,
114118
Spaces: plan.Spaces,
115119
}
@@ -126,13 +130,19 @@ func (r *serviceInstanceSharingResource) Read(ctx context.Context, req resource.
126130
return
127131
}
128132

129-
relationship, err := r.cfClient.ServiceInstances.GetSharedSpaceRelationships(ctx, data.ServiceInstance.ValueString())
133+
serviceInstanceID := data.Id.ValueString()
134+
135+
if serviceInstanceID == "" {
136+
serviceInstanceID = data.ServiceInstance.ValueString()
137+
}
138+
139+
relationship, err := r.cfClient.ServiceInstances.GetSharedSpaceRelationships(ctx, serviceInstanceID)
130140
if err != nil {
131141
resp.Diagnostics.AddError("Error when getting shared spaces for service instance", err.Error())
132142
return
133143
}
134144

135-
data = mapSharedSpacesValuesToType(relationship, data.ServiceInstance.ValueString())
145+
data = mapSharedSpacesValuesToType(relationship, serviceInstanceID)
136146

137147
tflog.Trace(ctx, "read a service instance sharing resource")
138148

@@ -145,7 +155,6 @@ func (r *serviceInstanceSharingResource) Update(ctx context.Context, req resourc
145155
}
146156

147157
func (r *serviceInstanceSharingResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
148-
149158
var state ServiceInstanceSharingType
150159

151160
diags := req.State.Get(ctx, &state)
@@ -162,7 +171,13 @@ func (r *serviceInstanceSharingResource) Delete(ctx context.Context, req resourc
162171
return
163172
}
164173

165-
err := r.cfClient.ServiceInstances.UnShareWithSpaces(ctx, state.ServiceInstance.ValueString(), spaces)
174+
serviceInstanceID := state.Id.ValueString()
175+
176+
if serviceInstanceID == "" {
177+
serviceInstanceID = state.ServiceInstance.ValueString()
178+
}
179+
180+
err := r.cfClient.ServiceInstances.UnShareWithSpaces(ctx, serviceInstanceID, spaces)
166181

167182
if err != nil {
168183
resp.Diagnostics.AddError("Error unsharing service instance with spaces", err.Error())
@@ -179,7 +194,12 @@ func mapSharedSpacesValuesToType(relationship *cfv3resource.ServiceInstanceShare
179194
}
180195
s := types.SetValueMust(types.StringType, sharedSpaces)
181196
return ServiceInstanceSharingType{
197+
Id: types.StringValue(serviceInstance),
182198
ServiceInstance: types.StringValue(serviceInstance),
183199
Spaces: s,
184200
}
185201
}
202+
203+
func (r *serviceInstanceSharingResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
204+
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
205+
}

cloudfoundry/provider/resource_service_instance_sharing_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func TestMapSharedSpacesValuesToType(t *testing.T) {
135135
}
136136
spaces := types.SetValueMust(types.StringType, sharedSpaces)
137137
expected := ServiceInstanceSharingType{
138+
Id: types.StringValue(serviceInstance),
138139
ServiceInstance: types.StringValue(serviceInstance),
139140
Spaces: spaces,
140141
}
@@ -143,3 +144,42 @@ func TestMapSharedSpacesValuesToType(t *testing.T) {
143144

144145
assert.Equal(t, expected, result)
145146
}
147+
148+
func TestServiceInstanceSharingResource_Import(t *testing.T) {
149+
var (
150+
testUserProvidedServiceInstanceGUID = "5e2976bb-332e-41e1-8be3-53baafea9296"
151+
testSpaces = `["02c0cc92-6ecc-44b1-b7b2-096ca19ee143", "121c3a95-0f82-45a6-8ff2-1920b2067edb"]`
152+
)
153+
t.Parallel()
154+
155+
// setup
156+
resourceName := "cloudfoundry_service_instance_sharing.rs"
157+
cfg := getCFHomeConf()
158+
rec := cfg.SetupVCR(t, "fixtures/resource_service_instance_sharing")
159+
defer stopQuietly(rec)
160+
161+
// actual test
162+
resource.Test(t, resource.TestCase{
163+
IsUnitTest: true,
164+
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
165+
Steps: []resource.TestStep{
166+
{
167+
Config: hclProvider(nil) + hclResourceServiceInstanceSharing(&ServiceInstanceSharingResourceModelPtr{
168+
HclType: hclObjectResource,
169+
HclObjectName: "rs",
170+
ServiceInstance: strtostrptr(testUserProvidedServiceInstanceGUID),
171+
Spaces: &testSpaces,
172+
}),
173+
Check: resource.ComposeAggregateTestCheckFunc(
174+
resource.TestMatchResourceAttr(resourceName, "service_instance", regexpValidUUID),
175+
resource.TestMatchResourceAttr(resourceName, "spaces.0", regexpValidUUID),
176+
),
177+
},
178+
{
179+
ResourceName: resourceName,
180+
ImportState: true,
181+
ImportStateVerify: true,
182+
},
183+
},
184+
})
185+
}

cloudfoundry/provider/types_service_instance.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type maintenanceInfoType struct {
9494
}
9595

9696
type ServiceInstanceSharingType struct {
97+
Id types.String `tfsdk:"id"`
9798
ServiceInstance types.String `tfsdk:"service_instance"`
9899
Spaces types.Set `tfsdk:"spaces"`
99100
}

docs/resources/service_instance_sharing.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,23 @@ resource "cloudfoundry_service_instance_sharing" "instance_sharing" {
5151
- `service_instance` (String) The ID of the service instance to share.
5252
- `spaces` (Set of String) The IDs of the spaces to share the service instance with.
5353

54+
### Read-Only
55+
56+
- `id` (String) The GUID of the object.
57+
58+
## Import
59+
60+
Import is supported using the following syntax:
61+
62+
```terraform
63+
# terraform import cloudfoundry_service_instance_sharing.<resource_name> <service_instance_guid>
64+
65+
terraform import cloudfoundry_service_instance_sharing.my_instance_sharing a1b2c3d4-5678-90ab-cdef-12345678abcd
66+
67+
# For Terraform 1.5+ users, you can also use the newer import blocks syntax:
68+
69+
import {
70+
to = cloudfoundry_service_instance_sharing.my_instance_sharing
71+
id = "a1b2c3d4-5678-90ab-cdef-12345678abcd"
72+
}
73+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# terraform import cloudfoundry_service_instance_sharing.<resource_name> <service_instance_guid>
2+
3+
terraform import cloudfoundry_service_instance_sharing.my_instance_sharing a1b2c3d4-5678-90ab-cdef-12345678abcd
4+
5+
# For Terraform 1.5+ users, you can also use the newer import blocks syntax:
6+
7+
import {
8+
to = cloudfoundry_service_instance_sharing.my_instance_sharing
9+
id = "a1b2c3d4-5678-90ab-cdef-12345678abcd"
10+
}

0 commit comments

Comments
 (0)