Skip to content

Commit

Permalink
Add BYOK resource
Browse files Browse the repository at this point in the history
  • Loading branch information
emerkle826 committed May 22, 2024
1 parent 1b0f56a commit b986502
Show file tree
Hide file tree
Showing 13 changed files with 558 additions and 4 deletions.
35 changes: 35 additions & 0 deletions docs/data-sources/cloud_accounts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astra_cloud_accounts Data Source - terraform-provider-astra"
subcategory: ""
description: |-
Retrieve a list of Cloud Accounts within an Organization
---

# astra_cloud_accounts (Data Source)

Retrieve a list of Cloud Accounts within an Organization



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

### Required

- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
- `region` (String) Cloud provider region

### Read-Only

- `id` (String) The ID of this resource.
- `results` (List of Object) The list of Cloud Accounts for the given Organization. (see [below for nested schema](#nestedatt--results))

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

Read-Only:

- `organization_id` (String)
- `provider` (String)
- `provider_id` (String)
27 changes: 27 additions & 0 deletions docs/data-sources/customer_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astra_customer_key Data Source - terraform-provider-astra"
subcategory: ""
description: |-
Retrieve a Customer Key for a given cloud provider and region
---

# astra_customer_key (Data Source)

Retrieve a Customer Key for a given cloud provider and region



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

### Required

- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
- `region` (String) Cloud provider region

### Read-Only

- `id` (String) The ID of this resource.
- `key_id` (String) The Customer Key ID
- `organization_id` (String) Organization ID
31 changes: 31 additions & 0 deletions docs/data-sources/customer_keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astra_customer_keys Data Source - terraform-provider-astra"
subcategory: ""
description: |-
Retrieve a list of Customer Keys within an Organization
---

# astra_customer_keys (Data Source)

Retrieve a list of Customer Keys within an Organization



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

### Read-Only

- `id` (String) The ID of this resource.
- `results` (List of Object) The list of Customer Keys for the given Organization. (see [below for nested schema](#nestedatt--results))

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

Read-Only:

- `cloud_provider` (String)
- `key_id` (String)
- `organization_id` (String)
- `region` (String)
2 changes: 1 addition & 1 deletion docs/data-sources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data "astra_database" "db" {
- `keyspace` (String) Initial keyspace
- `name` (String) Database name (user provided)
- `node_count` (Number) Node count (not relevant for serverless databases)
- `organization_id` (String) Ordg id (system generated)
- `organization_id` (String) Organization id (system generated)
- `owner_id` (String) Owner id (system generated)
- `regions` (List of String) Cloud provider region. Get list of supported regions from regions data-source
- `replication_factor` (Number) Replication Factor (not relevant for serverless databases)
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ output "existing_dbs" {
### Optional

- `cloud_provider` (String) The cloud provider
- `status` (String) Status flter. Only return databases with matching status, if supplied. Otherwise return all databases matching other requirements
- `status` (String) Status filter. Only return databases with matching status, if supplied. Otherwise return all databases matching other requirements

### Read-Only

Expand Down
27 changes: 27 additions & 0 deletions docs/resources/customer_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astra_customer_key Resource - terraform-provider-astra"
subcategory: ""
description: |-
astra_customer_key provides a Customer Key resource for Astra's Bring Your Own Key (BYOK).
---

# astra_customer_key (Resource)

`astra_customer_key` provides a Customer Key resource for Astra's Bring Your Own Key (BYOK).



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

### Required

- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
- `key_id` (String) Customer Key ID.
- `region` (String) Region in which the Customer Key exists.

### Read-Only

- `id` (String) The ID of this resource.
- `organization_id` (String) The Astra organization ID (this is derived from the token used to create the Customer Key).
101 changes: 101 additions & 0 deletions internal/provider/data_source_cloud_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package provider

import (
"context"
"fmt"
"net/http"

"github.com/datastax/astra-client-go/v2/astra"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceCloudAccounts() *schema.Resource {
return &schema.Resource{
Description: "Retrieve a list of Cloud Accounts within an Organization",

ReadContext: dataSourceCloudAccountsRead,

Schema: map[string]*schema.Schema{
// Required inputs
"cloud_provider": {
Description: "The cloud provider where the Customer Key exists (Currently supported: aws, gcp)",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(availableBYOKCloudProviders, true),
DiffSuppressFunc: ignoreCase,
},
"region": {
Description: "Cloud provider region",
Type: schema.TypeString,
Required: true,
},
// Computed outputs
"results": {
Type: schema.TypeList,
Description: "The list of Cloud Accounts for the given Organization.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"organization_id": {
Description: "Organization ID",
Type: schema.TypeString,
Computed: true,
},
"provider": {
Description: "The cloud provider",
Type: schema.TypeString,
Required: true,
},
"provider_id": {
Description: "The provider account ID",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceCloudAccountsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
provider := d.Get("cloud_provider").(string)
region := d.Get("region").(string)

cloudAccounts, err := listCloudAccounts(ctx, client, provider, region)
if err != nil {
return diag.FromErr(err)
}

if err := d.Set("results", cloudAccounts); err != nil {
return diag.FromErr(err)
}

d.SetId(id.UniqueId())
return nil
}

func listCloudAccounts(ctx context.Context, client *astra.ClientWithResponses, cloudProvider, region string) ([]map[string]interface{}, error) {
resp, err := client.GetCloudAccountsWithResponse(ctx, cloudProvider, region)
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("Error fetching Customer Keys. Status: %d, Message: %s", resp.StatusCode(), (resp.Body))
}
cloudAccounts := resp.JSON200
result := make([]map[string]interface{}, 0, len(*cloudAccounts))
for _, account := range *cloudAccounts {
result = append(result, map[string]interface{}{
"organization_id" : account.OrganizationId,
"provider" : account.Provider,
"provider_id" : account.ProviderId,
})
}
return result, nil
}
72 changes: 72 additions & 0 deletions internal/provider/data_source_customer_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package provider

import (
"context"
"fmt"
"strings"

"github.com/datastax/astra-client-go/v2/astra"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceCustomerKey() *schema.Resource {
return &schema.Resource{
Description: "Retrieve a Customer Key for a given cloud provider and region",

ReadContext: dataSourceCustomerKeyRead,

Schema: map[string]*schema.Schema{
// Required inputs
"cloud_provider": {
Description: "The cloud provider where the Customer Key exists (Currently supported: aws, gcp)",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(availableBYOKCloudProviders, true),
DiffSuppressFunc: ignoreCase,
},
"region": {
Description: "Cloud provider region",
Type: schema.TypeString,
Required: true,
},
// Computed outputs
"organization_id": {
Description: "Organization ID",
Type: schema.TypeString,
Computed: true,
},
"key_id": {
Description: "The Customer Key ID",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceCustomerKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
cloudProvider := d.Get("cloud_provider").(string)
region := d.Get("region").(string)

customerKeys, err := listCustomerKeys(ctx, client)
if err != nil {
return diag.FromErr(err)
}
for _, key := range customerKeys {
if strings.EqualFold(cloudProvider, key["cloud_provider"].(string)) &&
region == key["region].(string)"] {
orgId := key["organization_id"].(string)
keyId := key["key_id"].(string)
d.Set("organization_id", orgId)
d.Set("key_id", keyId)
d.SetId(fmt.Sprintf("%s/%s/%s", orgId, cloudProvider, region))
return nil
}
}
// key not found
return diag.Errorf("No Customer Key found for provider: %s, region: %s", cloudProvider, region)
}
Loading

0 comments on commit b986502

Please sign in to comment.