-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sleungcy-sap
committed
Feb 16, 2024
1 parent
4a0bf96
commit ee7cfa7
Showing
3 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
), | ||
}, | ||
}, | ||
}) | ||
} |