Skip to content

Commit

Permalink
feat : Add isolation segment resource and datasource (#141)
Browse files Browse the repository at this point in the history
Signed-off-by: Ray <[email protected]>
Co-authored-by: Ray <[email protected]>
  • Loading branch information
Dray56 and debTheRay authored Jun 17, 2024
1 parent 94bbb79 commit 5cf4960
Show file tree
Hide file tree
Showing 40 changed files with 3,672 additions and 33 deletions.
33 changes: 33 additions & 0 deletions docs/data-sources/isolation_segment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
page_title: "cloudfoundry_isolation_segment Data Source - terraform-provider-cloudfoundry"
subcategory: ""
description: |-
Gets information on a Cloud Foundry Isolation Segment.
---

# cloudfoundry_isolation_segment (Data Source)

Gets information on a Cloud Foundry Isolation Segment.

## Example Usage

```terraform
data "cloudfoundry_isolation_segment" "isosegment" {
name = "hifij"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the isolation segment

### Read-Only

- `annotations` (Map of String) The annotations associated with Cloud Foundry resources.
- `created_at` (String) The date and time when the resource was created in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `id` (String) The GUID of the object.
- `labels` (Map of String) The labels associated with Cloud Foundry resources.
- `updated_at` (String) The date and time when the resource was updated in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
29 changes: 29 additions & 0 deletions docs/data-sources/isolation_segment_entitlement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
page_title: "cloudfoundry_isolation_segment_entitlement Data Source - terraform-provider-cloudfoundry"
subcategory: ""
description: |-
Fetches organizations entitled with a Cloud Foundry Isolation Segment.
---

# cloudfoundry_isolation_segment_entitlement (Data Source)

Fetches organizations entitled with a Cloud Foundry Isolation Segment.

## Example Usage

```terraform
data "cloudfoundry_isolation_segment_entitlement" "isosegment" {
segment = "63ae51b9-9073-4409-81b0-3704b8de85dd"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `segment` (String) GUID of the isolation segment

### Read-Only

- `orgs` (Set of String) GUID's of organizations the segment is entitled with.
47 changes: 47 additions & 0 deletions docs/resources/isolation_segment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
page_title: "cloudfoundry_isolation_segment Resource - terraform-provider-cloudfoundry"
subcategory: ""
description: |-
Provides an isolation segment resource for Cloud Foundry.
---

# cloudfoundry_isolation_segment (Resource)

Provides an isolation segment resource for Cloud Foundry.

## Example Usage

```terraform
resource "cloudfoundry_isolation_segment" "isosegment" {
name = "hifi"
labels = { "purpose" : "testing" }
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the isolation segment

### Optional

- `annotations` (Map of String) The annotations associated with Cloud Foundry resources. Add as described [here](https://docs.cloudfoundry.org/adminguide/metadata.html#-view-metadata-for-an-object).
- `labels` (Map of String) The labels associated with Cloud Foundry resources. Add as described [here](https://docs.cloudfoundry.org/adminguide/metadata.html#-view-metadata-for-an-object).

### Read-Only

- `created_at` (String) The date and time when the resource was created in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `id` (String) The GUID of the object.
- `updated_at` (String) The date and time when the resource was updated in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.

## Import

Import is supported using the following syntax:

```terraform
# terraform import cloudfoundry_isolation_segment.<resource_name> <segment_guid>
terraform import cloudfoundry_isolation_segment.my_segment e3cef997-9ba5-4cb4-b25b-c79faa81a33f
```
33 changes: 33 additions & 0 deletions docs/resources/isolation_segment_entitlement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
page_title: "cloudfoundry_isolation_segment_entitlement Resource - terraform-provider-cloudfoundry"
subcategory: ""
description: |-
Provides a Cloud Foundry resource for entitling and revoking isolation segment from organizations. Only entitles and revokes the segment from the orgs managed through this resource and does not touch existing entitlements. Revoking isolation segment entitlement from an organization will fail if it is the default for the organization. On deleting the resource, the isolation segment will be revoked from the mentioned orgs.
---

# cloudfoundry_isolation_segment_entitlement (Resource)

Provides a Cloud Foundry resource for entitling and revoking isolation segment from organizations. Only entitles and revokes the segment from the orgs managed through this resource and does not touch existing entitlements. Revoking isolation segment entitlement from an organization will fail if it is the default for the organization. On deleting the resource, the isolation segment will be revoked from the mentioned orgs.

## Example Usage

```terraform
resource "cloudfoundry_isolation_segment_entitlement" "isosegment" {
segment = "63ae51b9-9073-4409-81b0-3704b8de85dd"
orgs = ["c8e454cc-7a24-4d71-b146-51d69538acfb"]
default = true
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `orgs` (Set of String) GUID's of organizations to entitle the segment to.
- `segment` (String) GUID of the isolation segment

### Optional

- `default` (Boolean) Set isolation segment as default for the organizations. Defaults to false.

2 changes: 1 addition & 1 deletion docs/resources/org.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Creates a Cloud Foundry Organization

```terraform
resource "cloudfoundry_org" "org" {
name = "tf-test"
name = "tf-test-iso"
suspended = false
labels = {
env = "test"
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/space.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Provides a Cloud Foundry resource for managing Cloud Foundry spaces within organ
```terraform
resource "cloudfoundry_space" "space" {
name = "space"
org = "ca721b24-e24d-4171-83e1-1ef6bd836b38"
org = "c8e454cc-7a24-4d71-b146-51d69538acfb"
allow_ssh = "true"
labels = { test : "pass", purpose : "prod" }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudfoundry_isolation_segment" "isosegment" {
name = "hifij"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudfoundry_isolation_segment_entitlement" "isosegment" {
segment = "63ae51b9-9073-4409-81b0-3704b8de85dd"
}
3 changes: 3 additions & 0 deletions examples/resources/cloudfoundry_isolation_segment/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# terraform import cloudfoundry_isolation_segment.<resource_name> <segment_guid>

terraform import cloudfoundry_isolation_segment.my_segment e3cef997-9ba5-4cb4-b25b-c79faa81a33f
4 changes: 4 additions & 0 deletions examples/resources/cloudfoundry_isolation_segment/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "cloudfoundry_isolation_segment" "isosegment" {
name = "hifi"
labels = { "purpose" : "testing" }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "cloudfoundry_isolation_segment_entitlement" "isosegment" {
segment = "63ae51b9-9073-4409-81b0-3704b8de85dd"
orgs = ["c8e454cc-7a24-4d71-b146-51d69538acfb"]
default = true
}
2 changes: 1 addition & 1 deletion examples/resources/cloudfoundry_org/resource.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "cloudfoundry_org" "org" {
name = "tf-test"
name = "tf-test-iso"
suspended = false
labels = {
env = "test"
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/cloudfoundry_space/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "cloudfoundry_space" "space" {
name = "space"
org = "ca721b24-e24d-4171-83e1-1ef6bd836b38"
org = "c8e454cc-7a24-4d71-b146-51d69538acfb"
allow_ssh = "true"
labels = { test : "pass", purpose : "prod" }
}
106 changes: 106 additions & 0 deletions internal/provider/datasource_isolation_segment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package provider

import (
"context"
"fmt"

"github.com/SAP/terraform-provider-cloudfoundry/internal/provider/managers"
cfv3client "github.com/cloudfoundry/go-cfclient/v3/client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces.
var (
_ datasource.DataSource = &IsolationSegmentDataSource{}
_ datasource.DataSourceWithConfigure = &IsolationSegmentDataSource{}
)

func NewIsolationSegmentDataSource() datasource.DataSource {
return &IsolationSegmentDataSource{}
}

type IsolationSegmentDataSource struct {
cfClient *cfv3client.Client
}

func (d *IsolationSegmentDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_isolation_segment"
}

func (d *IsolationSegmentDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Gets information on a Cloud Foundry Isolation Segment.",

Attributes: map[string]schema.Attribute{
idKey: guidSchema(),
"name": schema.StringAttribute{
MarkdownDescription: "Name of the isolation segment",
Required: true,
},
labelsKey: datasourceLabelsSchema(),
annotationsKey: datasourceAnnotationsSchema(),
createdAtKey: createdAtSchema(),
updatedAtKey: updatedAtSchema(),
},
}
}

func (d *IsolationSegmentDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
session, ok := req.ProviderData.(*managers.Session)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *managers.Session, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.cfClient = session.CFClient
}

func (d *IsolationSegmentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {

var data IsolationSegmentType

diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

getOptions := cfv3client.IsolationSegmentListOptions{
Names: cfv3client.Filter{
Values: []string{
data.Name.ValueString(),
},
},
}

isolationSegments, err := d.cfClient.IsolationSegments.ListAll(ctx, &getOptions)
if err != nil {
resp.Diagnostics.AddError(
"API Error Fetching Isolation Segment.",
fmt.Sprintf("Request failed with %s.", err.Error()),
)
return
}

if len(isolationSegments) == 0 {
resp.Diagnostics.AddError(
"Unable to find any Isolation Segment.",
fmt.Sprintf("Given name %s not in the list of isolation segments.", data.Name.ValueString()),
)
return
}

data, diags = mapIsolationSegmentValuesToType(ctx, isolationSegments[0])
resp.Diagnostics.Append(diags...)

tflog.Trace(ctx, "read an isolation segment data source")
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading

0 comments on commit 5cf4960

Please sign in to comment.