Skip to content

Commit

Permalink
adding cf_service_instance_sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
sleungcy-sap committed Feb 16, 2024
1 parent 4a0bf96 commit ee7cfa7
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions cloudfoundry/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func Provider() *schema.Provider {
"cloudfoundry_service_instance": resourceServiceInstance(),
"cloudfoundry_service_key": resourceServiceKey(),
"cloudfoundry_user_provided_service": resourceUserProvidedService(),
"cloudfoundry_service_instance_sharing": resourceServiceInstanceSharing(),
"cloudfoundry_buildpack": resourceBuildpack(),
"cloudfoundry_route": ResourceRoute(),
"cloudfoundry_route_service_binding": resourceRouteServiceBinding(),
Expand Down
92 changes: 92 additions & 0 deletions cloudfoundry/resource_cf_service_instance_sharing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cloudfoundry

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
)

func resourceServiceInstanceSharing() *schema.Resource {
return &schema.Resource{
CreateContext: resourceServiceInstanceSharingCreate,
ReadContext: resourceServiceInstanceSharingRead,
DeleteContext: resourceServiceInstanceSharingDelete,

Schema: map[string]*schema.Schema{
"service_instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the service instance to share",
},
"space_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the space to share the service instance with, the space can be in the same or different org",
},
},
}
}

func resourceServiceInstanceSharingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
session := meta.(*managers.Session)

serviceID, spaceID, err := parseID(d.Id())
if err != nil {
return diag.FromErr(err)
}

spaceWithOrganizationList, _, err := session.ClientV3.GetServiceInstanceSharedSpaces(serviceID)

if err != nil {
return diag.FromErr(err)
}

found := false
for _, spaceWithOrganization := range spaceWithOrganizationList {
if spaceWithOrganization.SpaceGUID == spaceID {
found = true
break
}
}

if !found {
d.SetId("")
return nil
}

d.Set("service_instance_id", serviceID)
d.Set("space_id", spaceID)
return nil
}

func resourceServiceInstanceSharingCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
session := meta.(*managers.Session)
serviceID := d.Get("service_instance_id").(string)
spaceID := d.Get("space_id").(string)

spacesGUIDList, _, err := session.ClientV3.ShareServiceInstanceToSpaces(serviceID, []string{spaceID})

if err != nil {
return diag.FromErr(err)
}

if len(spacesGUIDList.GUIDs) < 1 {
return diag.Errorf("failed to share service instance %s to space %s", serviceID, spaceID)
}

d.SetId(computeID(serviceID, spaceID))
return nil
}

func resourceServiceInstanceSharingDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
session := meta.(*managers.Session)
serviceID := d.Get("service_instance_id").(string)
spaceID := d.Get("space_id").(string)
_, err := session.ClientV3.UnshareServiceInstanceFromSpace(serviceID, spaceID)

return diag.FromErr(err)
}
74 changes: 74 additions & 0 deletions cloudfoundry/resource_cf_service_instance_sharing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cloudfoundry

import (
"fmt"
"testing"

guuid "github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

const serviceInstanceSharing = `
data "cloudfoundry_service" "test-service" {
name = "%s"
}
resource "cloudfoundry_space" "test-space-1" {
name = "space-1-%s"
org = "%s"
}
resource "cloudfoundry_space" "test-space-2" {
name = "space-2-%s"
org = "%s"
}
resource "cloudfoundry_service_instance" "test-service-instance" {
name = "test-service-instance-sharing-%s"
space = resource.cloudfoundry_space.test-space-1.id
service_plan = data.cloudfoundry_service.test-service.service_plans["%s"]
}
resource "cloudfoundry_service_instance_sharing" "test-service-instance-sharing" {
service_instance_id = resource.cloudfoundry_service_instance.test-service-instance.id
space_id = resource.cloudfoundry_space.test-space-2.id
}
`

func TestAccResServiceInstanceSharing_normal(t *testing.T) {
t.Parallel()
orgId, _ := defaultTestOrg(t)
// spaceId, _ := defaultTestSpace(t)
serviceName, _, servicePlan := getTestServiceBrokers(t)
testId := guuid.New().String()

ref := "cloudfoundry_service_instance.test-service-instance"

resource.Test(t,
resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProvidersFactories,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckServiceInstanceDestroyed([]string{serviceName}, ref),
testAccCheckSpaceDestroyed(fmt.Sprintf("space-1-%s", testId)),
testAccCheckSpaceDestroyed(fmt.Sprintf("space-2-%s", testId)),
),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(serviceInstanceSharing, serviceName, testId, orgId, testId, orgId, testId, servicePlan),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceInstanceExists(ref),
func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type == "cloudfoundry_service_instance_sharing" {
return nil
}
}
return fmt.Errorf("resource 'cloudfoundry_service_instance_sharing' not found in terraform state")
},
),
},
},
})
}

0 comments on commit ee7cfa7

Please sign in to comment.