Skip to content

Commit

Permalink
feat: add service binding rsources and data sources (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
lechnerc77 authored Jul 19, 2023
1 parent 1dab6bf commit 1025f63
Show file tree
Hide file tree
Showing 15 changed files with 6,523 additions and 14 deletions.
57 changes: 57 additions & 0 deletions docs/data-sources/subaccount_service_binding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
page_title: "btp_subaccount_service_binding Data Source - terraform-provider-btp"
subcategory: ""
description: |-
Gets details about a specific service binding, such as its access details. They are included in its 'credentials' property, and typically include access URLs and credentials.
---

# btp_subaccount_service_binding (Data Source)

Gets details about a specific service binding, such as its access details. They are included in its 'credentials' property, and typically include access URLs and credentials.

## Example Usage

```terraform
# look up service binding by id
data "btp_subaccount_service_binding" "by_id" {
subaccount_id = "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
id = "c2d02852-1678-4c1e-b546-74d5274f1522"
}
# look up service binding by name
data "btp_subaccount_service_binding" "by_name" {
subaccount_id = "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
name = "hyperspace-2022-10"
}
```

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

### Required

- `subaccount_id` (String) The ID of the subaccount.

### Optional

- `id` (String) The ID of the service binding.
- `name` (String) The name of the service binding.

### Read-Only

- `bind_resource` (Map of String) Contains the resources associated with the binding.
- `context` (Map of String) Contextual data for the resource.
- `created_date` (String) The date and time when the resource was created in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `credentials` (String, Sensitive) The credentials to access the binding.
- `labels` (Map of Set of String) Set of words or phrases assigned to the binding.
- `last_modified` (String) The date and time when the resource was last modified in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `parameters` (String) The parameters of the service binding as a valid JSON object.
- `ready` (Boolean) Shows whether the service binding is ready.
- `service_instance_id` (String) The ID of the service instance associated with the binding.
- `state` (String) The current state of the service binding. Possible values are:

| state | description |
| --- | --- |
| in progress | The operation or processing is in progress |
| failed | The operation or processing failed |
| succeeded | The operation or processing succeeded |
61 changes: 61 additions & 0 deletions docs/data-sources/subaccount_service_bindings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
page_title: "btp_subaccount_service_bindings Data Source - terraform-provider-btp"
subcategory: ""
description: |-
Lists all service bindings in a subaccount.
---

# btp_subaccount_service_bindings (Data Source)

Lists all service bindings in a subaccount.

## Example Usage

```terraform
# look up all service bindings of a given subaccount
data "btp_subaccount_service_bindings" "all" {
subaccount_id = "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
}
# look up service bindings of a given subaccount wich have certain label assigned
data "btp_subaccount_service_bindings" "labeled" {
subaccount_id = "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
labels_filter = "subaccount_id eq '6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f'"
}
```

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

### Required

- `subaccount_id` (String) The ID of the subaccount.

### Optional

- `fields_filter` (String) Filters the service bindings based on the field query.
- `labels_filter` (String) Filters the service binding based on the label query.

### Read-Only

- `id` (String, Deprecated) The ID of the subaccount.
- `values` (Attributes List) (see [below for nested schema](#nestedatt--values))

<a id="nestedatt--values"></a>
### Nested Schema for `values`

Optional:

- `id` (String) The ID of the service binding.
- `name` (String) The name of the service binding.

Read-Only:

- `bind_resource` (Map of String) Contains the resources associated with the binding.
- `context` (Map of String) Contextual data for the resource.
- `created_date` (String) The date and time when the resource was created in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `credentials` (String, Sensitive) The credentials to access the binding.
- `labels` (Map of Set of String) Set of words or phrases assigned to the binding.
- `last_modified` (String) The date and time when the resource was last modified in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `ready` (Boolean) Shows whether the service binding is ready.
- `service_instance_id` (String) The ID of the service instance associated with the binding.
73 changes: 73 additions & 0 deletions docs/resources/subaccount_service_binding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
page_title: "btp_subaccount_service_binding Resource - terraform-provider-btp"
subcategory: ""
description: |-
Creates a service binding between a service instance and an application.
---

# btp_subaccount_service_binding (Resource)

Creates a service binding between a service instance and an application.

## Example Usage

```terraform
# create a service binding in a subaccount
resource "btp_subaccount_service_binding" "my_binding" {
subaccount_id = "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
service_instance_id = "8911491d-0e1d-425d-a233-785512602d6f"
name = "my binding"
}
# create a parameterized service binding in a subaccount
resource "btp_subaccount_service_binding" "my_parameterized_binding" {
subaccount_id = "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
service_instance_id = "8911491d-0e1d-425d-a233-785512602d6f"
name = "my parameterized binding"
parameters = jsonencode({
param_a = ""
param_b = ""
})
}
```

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

### Required

- `name` (String) The name of the service binding.
- `service_instance_id` (String) The ID of the service instance associated with the binding.
- `subaccount_id` (String) The ID of the subaccount.

### Optional

- `parameters` (String) The parameters of the service binding as a valid JSON object.

### Read-Only

- `bind_resource` (Map of String) Contains the resources associated with the binding.
- `context` (Map of String) The contextual data for the resource.
- `created_date` (String) The date and time when the resource was created in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `credentials` (String, Sensitive) The credentials to access the binding.
- `id` (String) The ID of the service binding.
- `labels` (Map of Set of String) Set of words or phrases assigned to service binding.
- `last_modified` (String) The date and time when the resource was last modified in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format.
- `ready` (Boolean) Shows whether the service binding is ready.
- `state` (String) The current state of the service binding. Possible values are:

| state | description |
| --- | --- |
| in progress | The operation or processing is in progress |
| failed | The operation or processing failed |
| succeeded | The operation or processing succeeded |

## Import

Import is supported using the following syntax:

```terraform
# terraform import btp_subaccount_service_binding.<resource_name> <subaccount_id>,<service_binding_id>
terraform import btp_subaccount_service_binding.my_binding 6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f,910e9a7d-0fb4-4428-a813-56550e683579
```
128 changes: 128 additions & 0 deletions internal/provider/datasource_subaccount_service_binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package provider

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestDataSourceSubaccountServiceBinding(t *testing.T) {

t.Parallel()
t.Run("happy path - service bindings by id", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_subaccount_service_binding_by_id")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceSubaccountServiceBindingbyId("uut", "59cd458e-e66e-4b60-b6d8-8f219379f9a5", "b02e4b22-906b-40c5-9c5e-dbb6a9068444"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "subaccount_id", "59cd458e-e66e-4b60-b6d8-8f219379f9a5"),
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "id", "b02e4b22-906b-40c5-9c5e-dbb6a9068444"),
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "name", "test-service-binding-iban"),
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "ready", "true"),
resource.TestMatchResourceAttr("data.btp_subaccount_service_binding.uut", "created_date", regexpValidRFC3999Format),
resource.TestMatchResourceAttr("data.btp_subaccount_service_binding.uut", "last_modified", regexpValidRFC3999Format),
),
},
},
})

})

t.Run("happy path - service bindings by name", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_subaccount_service_binding_by_name")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceSubaccountServiceBindingbyName("uut", "59cd458e-e66e-4b60-b6d8-8f219379f9a5", "test-service-binding-iban"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "subaccount_id", "59cd458e-e66e-4b60-b6d8-8f219379f9a5"),
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "id", "b02e4b22-906b-40c5-9c5e-dbb6a9068444"),
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "name", "test-service-binding-iban"),
resource.TestCheckResourceAttr("data.btp_subaccount_service_binding.uut", "ready", "true"),
resource.TestMatchResourceAttr("data.btp_subaccount_service_binding.uut", "created_date", regexpValidRFC3999Format),
resource.TestMatchResourceAttr("data.btp_subaccount_service_binding.uut", "last_modified", regexpValidRFC3999Format),
),
},
},
})

})
t.Run("error path - subaccount_id mandatory", func(t *testing.T) {
resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceSubaccountServiceBindingNoSubaccount("uut", "test-service-binding-iban"),
ExpectError: regexp.MustCompile(`The argument "subaccount_id" is required, but no definition was found`),
},
},
})
})

t.Run("error path - no ID or name", func(t *testing.T) {
resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceSubaccountServiceBindingNoIdOrName("uut", "59cd458e-e66e-4b60-b6d8-8f219379f9a5"),
ExpectError: regexp.MustCompile(`Error: Invalid Attribute Combination`),
},
},
})
})
t.Run("error path - subaccount_id not a valid UUID", func(t *testing.T) {
resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceSubaccountServiceBindingbyName("uut", "this-is-not-a-uuid", "test-service-binding-iban"),
ExpectError: regexp.MustCompile(`Attribute subaccount_id value must be a valid UUID, got: this-is-not-a-uuid`),
},
},
})
})
}

func hclDatasourceSubaccountServiceBindingbyId(resourceName string, subaccountId string, bindingId string) string {
template := `data "btp_subaccount_service_binding" "%s" {
subaccount_id = "%s"
id = "%s"
}`
return fmt.Sprintf(template, resourceName, subaccountId, bindingId)
}

func hclDatasourceSubaccountServiceBindingbyName(resourceName string, subaccountId string, bindingName string) string {
template := `data "btp_subaccount_service_binding" "%s" {
subaccount_id = "%s"
name = "%s"
}`
return fmt.Sprintf(template, resourceName, subaccountId, bindingName)
}

func hclDatasourceSubaccountServiceBindingNoSubaccount(resourceName string, bindingName string) string {
template := `data "btp_subaccount_service_binding" "%s" {
name = "%s"
}`
return fmt.Sprintf(template, resourceName, bindingName)
}

func hclDatasourceSubaccountServiceBindingNoIdOrName(resourceName string, subaccountId string) string {
template := `data "btp_subaccount_service_binding" "%s" {
subaccount_id = "%s"
}`
return fmt.Sprintf(template, resourceName, subaccountId)
}
7 changes: 7 additions & 0 deletions internal/provider/datasource_subaccount_service_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type subaccountServiceBindingValue struct {
type subaccountServiceBindingsDataSourceConfig struct {
/* INPUT */
SubaccountId types.String `tfsdk:"subaccount_id"`
Id types.String `tfsdk:"id"`
FieldsFilter types.String `tfsdk:"fields_filter"`
LabelsFilter types.String `tfsdk:"labels_filter"`
/* OUTPUT */
Expand Down Expand Up @@ -67,6 +68,11 @@ func (ds *subaccountServiceBindingsDataSource) Schema(_ context.Context, _ datas
uuidvalidator.ValidUUID(),
},
},
"id": schema.StringAttribute{ // required hashicorps terraform plugin testing framework
DeprecationMessage: "Use the `subaccount_id` attribute instead",
MarkdownDescription: "The ID of the subaccount.",
Computed: true,
},
"fields_filter": schema.StringAttribute{
MarkdownDescription: "Filters the service bindings based on the field query.",
Optional: true,
Expand Down Expand Up @@ -164,6 +170,7 @@ func (ds *subaccountServiceBindingsDataSource) Read(ctx context.Context, req dat
return
}

data.Id = data.SubaccountId
data.Values = []subaccountServiceBindingValue{}

for _, binding := range cliRes {
Expand Down
Loading

0 comments on commit 1025f63

Please sign in to comment.