diff --git a/cloudfoundry/provider/fixtures/resource_service_instance_sharing.yaml b/cloudfoundry/provider/fixtures/resource_service_instance_sharing.yaml index 8a06970..d7c8a0b 100644 --- a/cloudfoundry/provider/fixtures/resource_service_instance_sharing.yaml +++ b/cloudfoundry/provider/fixtures/resource_service_instance_sharing.yaml @@ -268,3 +268,70 @@ interactions: status: 204 No Content code: 204 duration: 204.222897ms + - id: 4 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [ ] + trailer: { } + host: api.x.x.x.x.com + remote_addr: "" + request_uri: "" + body: "" + form: { } + headers: + Authorization: + - Bearer redacted + User-Agent: + - Go-CF-Client/3.0 + url: https://api.x.x.x.x.com/v3/service_instances/5e2976bb-332e-41e1-8be3-53baafea9296/shared_spaces + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [ ] + trailer: { } + content_length: -1 + uncompressed: false + body: '{"data":[{"guid":"02c0cc92-6ecc-44b1-b7b2-096ca19ee143"}, {"guid":"121c3a95-0f82-45a6-8ff2-1920b2067edb"}]}' + headers: + Content-Type: + - application/json; charset=utf-8 + Date: + - Mon, 25 Mar 2024 10:39:34 GMT + Referrer-Policy: + - strict-origin-when-cross-origin + Server: + - nginx + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload; + X-B3-Spanid: + - 7031327687a56f81 + X-B3-Traceid: + - de8d237a1f2045057031327687a56f81 + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Frame-Options: + - SAMEORIGIN + X-Permitted-Cross-Domain-Policies: + - none + X-Ratelimit-Limit: + - "20000" + X-Ratelimit-Remaining: + - "18000" + X-Ratelimit-Reset: + - "1711364588" + X-Runtime: + - "0.058984" + X-Vcap-Request-Id: + - de8d237a-1f20-4505-7031-327687a56f81::f3bea4eb-8c5a-432a-8d19-f256a2758e80 + X-Xss-Protection: + - 1; mode=block + status: 200 OK + code: 200 + duration: 225.980057ms diff --git a/cloudfoundry/provider/resource_service_instance_sharing.go b/cloudfoundry/provider/resource_service_instance_sharing.go index ddc0c1c..78a1989 100644 --- a/cloudfoundry/provider/resource_service_instance_sharing.go +++ b/cloudfoundry/provider/resource_service_instance_sharing.go @@ -3,6 +3,7 @@ package provider import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/cloudfoundry/terraform-provider-cloudfoundry/internal/validation" "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" @@ -45,6 +46,7 @@ func (r *serviceInstanceSharingResource) Schema(ctx context.Context, req resourc MarkdownDescription: "Provides a resource for managing service instance sharing in Cloud Foundry.", Attributes: map[string]schema.Attribute{ + idKey: guidSchema(), "service_instance": schema.StringAttribute{ MarkdownDescription: "The ID of the service instance to share.", Required: true, @@ -108,8 +110,10 @@ func (r *serviceInstanceSharingResource) Create(ctx context.Context, req resourc resp.Diagnostics.AddError("Error sharing service instance with spaces", err.Error()) return } + tflog.Trace(ctx, "created a service instance sharing resource") newState := ServiceInstanceSharingType{ + Id: plan.ServiceInstance, ServiceInstance: plan.ServiceInstance, Spaces: plan.Spaces, } @@ -126,13 +130,19 @@ func (r *serviceInstanceSharingResource) Read(ctx context.Context, req resource. return } - relationship, err := r.cfClient.ServiceInstances.GetSharedSpaceRelationships(ctx, data.ServiceInstance.ValueString()) + serviceInstanceID := data.Id.ValueString() + + if serviceInstanceID == "" { + serviceInstanceID = data.ServiceInstance.ValueString() + } + + relationship, err := r.cfClient.ServiceInstances.GetSharedSpaceRelationships(ctx, serviceInstanceID) if err != nil { resp.Diagnostics.AddError("Error when getting shared spaces for service instance", err.Error()) return } - data = mapSharedSpacesValuesToType(relationship, data.ServiceInstance.ValueString()) + data = mapSharedSpacesValuesToType(relationship, serviceInstanceID) tflog.Trace(ctx, "read a service instance sharing resource") @@ -145,7 +155,6 @@ func (r *serviceInstanceSharingResource) Update(ctx context.Context, req resourc } func (r *serviceInstanceSharingResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var state ServiceInstanceSharingType diags := req.State.Get(ctx, &state) @@ -162,7 +171,13 @@ func (r *serviceInstanceSharingResource) Delete(ctx context.Context, req resourc return } - err := r.cfClient.ServiceInstances.UnShareWithSpaces(ctx, state.ServiceInstance.ValueString(), spaces) + serviceInstanceID := state.Id.ValueString() + + if serviceInstanceID == "" { + serviceInstanceID = state.ServiceInstance.ValueString() + } + + err := r.cfClient.ServiceInstances.UnShareWithSpaces(ctx, serviceInstanceID, spaces) if err != nil { resp.Diagnostics.AddError("Error unsharing service instance with spaces", err.Error()) @@ -179,7 +194,12 @@ func mapSharedSpacesValuesToType(relationship *cfv3resource.ServiceInstanceShare } s := types.SetValueMust(types.StringType, sharedSpaces) return ServiceInstanceSharingType{ + Id: types.StringValue(serviceInstance), ServiceInstance: types.StringValue(serviceInstance), Spaces: s, } } + +func (r *serviceInstanceSharingResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +} diff --git a/cloudfoundry/provider/resource_service_instance_sharing_test.go b/cloudfoundry/provider/resource_service_instance_sharing_test.go index 68765ca..3ddb77f 100644 --- a/cloudfoundry/provider/resource_service_instance_sharing_test.go +++ b/cloudfoundry/provider/resource_service_instance_sharing_test.go @@ -135,6 +135,7 @@ func TestMapSharedSpacesValuesToType(t *testing.T) { } spaces := types.SetValueMust(types.StringType, sharedSpaces) expected := ServiceInstanceSharingType{ + Id: types.StringValue(serviceInstance), ServiceInstance: types.StringValue(serviceInstance), Spaces: spaces, } @@ -143,3 +144,42 @@ func TestMapSharedSpacesValuesToType(t *testing.T) { assert.Equal(t, expected, result) } + +func TestServiceInstanceSharingResource_Import(t *testing.T) { + var ( + testUserProvidedServiceInstanceGUID = "5e2976bb-332e-41e1-8be3-53baafea9296" + testSpaces = `["02c0cc92-6ecc-44b1-b7b2-096ca19ee143", "121c3a95-0f82-45a6-8ff2-1920b2067edb"]` + ) + t.Parallel() + + // setup + resourceName := "cloudfoundry_service_instance_sharing.rs" + cfg := getCFHomeConf() + rec := cfg.SetupVCR(t, "fixtures/resource_service_instance_sharing") + defer stopQuietly(rec) + + // actual test + resource.Test(t, resource.TestCase{ + IsUnitTest: true, + ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()), + Steps: []resource.TestStep{ + { + Config: hclProvider(nil) + hclResourceServiceInstanceSharing(&ServiceInstanceSharingResourceModelPtr{ + HclType: hclObjectResource, + HclObjectName: "rs", + ServiceInstance: strtostrptr(testUserProvidedServiceInstanceGUID), + Spaces: &testSpaces, + }), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestMatchResourceAttr(resourceName, "service_instance", regexpValidUUID), + resource.TestMatchResourceAttr(resourceName, "spaces.0", regexpValidUUID), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/cloudfoundry/provider/types_service_instance.go b/cloudfoundry/provider/types_service_instance.go index 1b10995..3840798 100644 --- a/cloudfoundry/provider/types_service_instance.go +++ b/cloudfoundry/provider/types_service_instance.go @@ -94,6 +94,7 @@ type maintenanceInfoType struct { } type ServiceInstanceSharingType struct { + Id types.String `tfsdk:"id"` ServiceInstance types.String `tfsdk:"service_instance"` Spaces types.Set `tfsdk:"spaces"` } diff --git a/docs/resources/service_instance_sharing.md b/docs/resources/service_instance_sharing.md index 2965fe4..dbf794b 100644 --- a/docs/resources/service_instance_sharing.md +++ b/docs/resources/service_instance_sharing.md @@ -51,3 +51,25 @@ resource "cloudfoundry_service_instance_sharing" "instance_sharing" { - `service_instance` (String) The ID of the service instance to share. - `spaces` (Set of String) The IDs of the spaces to share the service instance with. +### Read-Only + +- `id` (String) The GUID of the object. This will be the same as the service_instance GUID. + +## Import + +Import is supported using the following syntax: + +```terraform +# terraform import cloudfoundry_service_instance_sharing. + +terraform import cloudfoundry_service_instance_sharing.my_instance_sharing a1b2c3d4-5678-90ab-cdef-12345678abcd +``` + +For Terraform 1.5+ users, you can also use the newer import blocks syntax: + +```terraform +import { + to = cloudfoundry_service_instance_sharing.my_instance_sharing + id = "a1b2c3d4-5678-90ab-cdef-12345678abcd" +} +``` \ No newline at end of file