Skip to content

Commit 858a644

Browse files
authored
Add BYOK resource (#330)
* Adds astra_customer_key resource * Adds astra_customer_key datasource * Adds astra_customer_keys datasource * Adds astra_cloud_accounts datasource
1 parent 1b0f56a commit 858a644

File tree

21 files changed

+693
-7
lines changed

21 files changed

+693
-7
lines changed

docs/data-sources/cloud_accounts.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "astra_cloud_accounts Data Source - terraform-provider-astra"
4+
subcategory: ""
5+
description: |-
6+
Retrieve a list of Cloud Accounts within an Organization
7+
---
8+
9+
# astra_cloud_accounts (Data Source)
10+
11+
Retrieve a list of Cloud Accounts within an Organization
12+
13+
## Example Usage
14+
15+
```terraform
16+
# AWS example
17+
data "astra_cloud_accounts" "awsaccounts" {
18+
cloud_provider = "aws"
19+
region = "us-east-1"
20+
}
21+
22+
# GCP example
23+
data "astra_cloud_accounts" "gcpaccounts" {
24+
cloud_provider = "gcp"
25+
region = "us-east1"
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Required
33+
34+
- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
35+
- `region` (String) Cloud provider region
36+
37+
### Read-Only
38+
39+
- `id` (String) The ID of this resource.
40+
- `results` (List of Object) The list of Cloud Accounts for the given Organization. (see [below for nested schema](#nestedatt--results))
41+
42+
<a id="nestedatt--results"></a>
43+
### Nested Schema for `results`
44+
45+
Read-Only:
46+
47+
- `organization_id` (String)
48+
- `provider` (String)
49+
- `provider_id` (String)

docs/data-sources/customer_key.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "astra_customer_key Data Source - terraform-provider-astra"
4+
subcategory: ""
5+
description: |-
6+
Retrieve a Customer Key for a given cloud provider and region
7+
---
8+
9+
# astra_customer_key (Data Source)
10+
11+
Retrieve a Customer Key for a given cloud provider and region
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Read in a customer key for a given cloud provider and region
17+
data "astra_customer_key" "key" {
18+
cloud_provider = "aws"
19+
region = "us-east-1"
20+
}
21+
```
22+
23+
<!-- schema generated by tfplugindocs -->
24+
## Schema
25+
26+
### Required
27+
28+
- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
29+
- `region` (String) Cloud provider region
30+
31+
### Read-Only
32+
33+
- `id` (String) The ID of this resource.
34+
- `key_id` (String) The Customer Key ID
35+
- `organization_id` (String) Organization ID

docs/data-sources/customer_keys.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "astra_customer_keys Data Source - terraform-provider-astra"
4+
subcategory: ""
5+
description: |-
6+
Retrieve a list of Customer Keys within an Organization
7+
---
8+
9+
# astra_customer_keys (Data Source)
10+
11+
Retrieve a list of Customer Keys within an Organization
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Read in all customer keys for the given Organization
17+
data "astra_customer_keys" "keys" {
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Read-Only
25+
26+
- `id` (String) The ID of this resource.
27+
- `results` (List of Object) The list of Customer Keys for the given Organization. (see [below for nested schema](#nestedatt--results))
28+
29+
<a id="nestedatt--results"></a>
30+
### Nested Schema for `results`
31+
32+
Read-Only:
33+
34+
- `cloud_provider` (String)
35+
- `key_id` (String)
36+
- `organization_id` (String)
37+
- `region` (String)

docs/data-sources/database.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ data "astra_database" "db" {
3939
- `keyspace` (String) Initial keyspace
4040
- `name` (String) Database name (user provided)
4141
- `node_count` (Number) Node count (not relevant for serverless databases)
42-
- `organization_id` (String) Ordg id (system generated)
42+
- `organization_id` (String) Organization id (system generated)
4343
- `owner_id` (String) Owner id (system generated)
4444
- `regions` (List of String) Cloud provider region. Get list of supported regions from regions data-source
4545
- `replication_factor` (Number) Replication Factor (not relevant for serverless databases)

docs/data-sources/databases.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ output "existing_dbs" {
2828
### Optional
2929

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

3333
### Read-Only
3434

docs/resources/customer_key.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "astra_customer_key Resource - terraform-provider-astra"
4+
subcategory: ""
5+
description: |-
6+
astra_customer_key provides a Customer Key resource for Astra's Bring Your Own Key (BYOK). Note that DELETE is not supported through Terraform currently. A support ticket must be created to delete Customer Keys in Astra. WARNING: Deleting a key from Astra will result in an outage. Please see https://docs.datastax.com/en/astra-db-serverless/administration/delete-customer-keys.html for more information.
7+
---
8+
9+
# astra_customer_key (Resource)
10+
11+
`astra_customer_key` provides a Customer Key resource for Astra's Bring Your Own Key (BYOK). Note that DELETE is not supported through Terraform currently. A support ticket must be created to delete Customer Keys in Astra. WARNING: Deleting a key from Astra will result in an outage. Please see https://docs.datastax.com/en/astra-db-serverless/administration/delete-customer-keys.html for more information.
12+
13+
## Example Usage
14+
15+
```terraform
16+
# AWS example
17+
resource "astra_customer_key" "customerkey" {
18+
cloud_provider = "aws"
19+
region = "us-east-1"
20+
key_id = "arn:aws:kms:us-east-1:123456789012:key/1a2b3c4d-5e6f-1a2b-3c4d-5e6f1a2b3c4d"
21+
}
22+
23+
# GCP example
24+
resource "astra_customer_key" "customerKey" {
25+
cloud_provider = "gcp"
26+
region = "us-east1"
27+
key_id = "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key"
28+
}
29+
```
30+
31+
<!-- schema generated by tfplugindocs -->
32+
## Schema
33+
34+
### Required
35+
36+
- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
37+
- `key_id` (String) Customer Key ID. This is cloud provider specific.
38+
- `region` (String) Region in which the Customer Key exists.
39+
40+
### Read-Only
41+
42+
- `id` (String) The ID of this resource.
43+
- `organization_id` (String) The Astra organization ID (this is derived from the token used to create the Customer Key).
44+
45+
## Import
46+
47+
Import is supported using the following syntax:
48+
49+
```shell
50+
# the import id is in the fomrat of <orgId>/cloudProvider/<cloud_provider>/region/<region>/keyId/<key_id>
51+
terraform import astra_customer_key.customerkey 4d3c2b1a-5e6f-1a2b-3c4d-5e6f1a2b3c4d/cloudProvider/aws/region/us-east-1/keyId/arn:aws:kms:us-east-1:123456789012:key/1a2b3c4d-5e6f-1a2b-3c4d-5e6f1a2b3c4d
52+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# AWS example
2+
data "astra_cloud_accounts" "awsaccounts" {
3+
cloud_provider = "aws"
4+
region = "us-east-1"
5+
}
6+
7+
# GCP example
8+
data "astra_cloud_accounts" "gcpaccounts" {
9+
cloud_provider = "gcp"
10+
region = "us-east1"
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Read in a customer key for a given cloud provider and region
2+
data "astra_customer_key" "key" {
3+
cloud_provider = "aws"
4+
region = "us-east-1"
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Read in all customer keys for the given Organization
2+
data "astra_customer_keys" "keys" {
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# the import id is in the fomrat of <orgId>/cloudProvider/<cloud_provider>/region/<region>/keyId/<key_id>
2+
terraform import astra_customer_key.customerkey 4d3c2b1a-5e6f-1a2b-3c4d-5e6f1a2b3c4d/cloudProvider/aws/region/us-east-1/keyId/arn:aws:kms:us-east-1:123456789012:key/1a2b3c4d-5e6f-1a2b-3c4d-5e6f1a2b3c4d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# AWS example
2+
resource "astra_customer_key" "customerkey" {
3+
cloud_provider = "aws"
4+
region = "us-east-1"
5+
key_id = "arn:aws:kms:us-east-1:123456789012:key/1a2b3c4d-5e6f-1a2b-3c4d-5e6f1a2b3c4d"
6+
}
7+
8+
# GCP example
9+
resource "astra_customer_key" "customerKey" {
10+
cloud_provider = "gcp"
11+
region = "us-east1"
12+
key_id = "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key"
13+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.21
55
toolchain go1.22.0
66

77
require (
8-
github.com/datastax/astra-client-go/v2 v2.2.53
8+
github.com/datastax/astra-client-go/v2 v2.2.54
99
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587
1010
github.com/google/uuid v1.6.0
1111
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
101101
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
102102
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
103103
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
104-
github.com/datastax/astra-client-go/v2 v2.2.53 h1:qWCBksV9rWi9WmSBW71IGhy3mL/QwkEw1BMG42ph540=
105-
github.com/datastax/astra-client-go/v2 v2.2.53/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
104+
github.com/datastax/astra-client-go/v2 v2.2.54 h1:R2k9ek9zaU15cLD96np5gsj12oZhK3Z5/tSytjQagO8=
105+
github.com/datastax/astra-client-go/v2 v2.2.54/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
106106
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587 h1:3jv+O0hWcz3oj3sZ9/Ov9/m1Vaqx8Ql8jp5ZeA13O5A=
107107
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587/go.mod h1:guL8YZ5gJINN+h5Kmja1AnuzhxLU3sHQL8o/8HYLtqk=
108108
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/datastax/astra-client-go/v2/astra"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
)
14+
15+
func dataSourceCloudAccounts() *schema.Resource {
16+
return &schema.Resource{
17+
Description: "Retrieve a list of Cloud Accounts within an Organization",
18+
19+
ReadContext: dataSourceCloudAccountsRead,
20+
21+
Schema: map[string]*schema.Schema{
22+
// Required inputs
23+
"cloud_provider": {
24+
Description: "The cloud provider where the Customer Key exists (Currently supported: aws, gcp)",
25+
Type: schema.TypeString,
26+
Required: true,
27+
ForceNew: true,
28+
ValidateFunc: validation.StringInSlice(availableBYOKCloudProviders, true),
29+
DiffSuppressFunc: ignoreCase,
30+
},
31+
"region": {
32+
Description: "Cloud provider region",
33+
Type: schema.TypeString,
34+
Required: true,
35+
},
36+
// Computed outputs
37+
"results": {
38+
Type: schema.TypeList,
39+
Description: "The list of Cloud Accounts for the given Organization.",
40+
Computed: true,
41+
Elem: &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"organization_id": {
44+
Description: "Organization ID",
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"provider": {
49+
Description: "The cloud provider",
50+
Type: schema.TypeString,
51+
Required: true,
52+
},
53+
"provider_id": {
54+
Description: "The provider account ID",
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
},
59+
},
60+
},
61+
},
62+
}
63+
}
64+
65+
func dataSourceCloudAccountsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
66+
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
67+
provider := d.Get("cloud_provider").(string)
68+
region := d.Get("region").(string)
69+
70+
cloudAccounts, err := listCloudAccounts(ctx, client, provider, region)
71+
if err != nil {
72+
return diag.FromErr(err)
73+
}
74+
75+
if err := d.Set("results", cloudAccounts); err != nil {
76+
return diag.FromErr(err)
77+
}
78+
79+
d.SetId(id.UniqueId())
80+
return nil
81+
}
82+
83+
func listCloudAccounts(ctx context.Context, client *astra.ClientWithResponses, cloudProvider, region string) ([]map[string]interface{}, error) {
84+
resp, err := client.GetCloudAccountsWithResponse(ctx, cloudProvider, region)
85+
if err != nil {
86+
return nil, err
87+
}
88+
if resp.StatusCode() != http.StatusOK {
89+
return nil, fmt.Errorf("Error fetching Customer Keys. Status: %d, Message: %s", resp.StatusCode(), (resp.Body))
90+
}
91+
cloudAccounts := resp.JSON200
92+
result := make([]map[string]interface{}, 0, len(*cloudAccounts))
93+
for _, account := range *cloudAccounts {
94+
result = append(result, map[string]interface{}{
95+
"organization_id" : account.OrganizationId,
96+
"provider" : account.Provider,
97+
"provider_id" : account.ProviderId,
98+
})
99+
}
100+
return result, nil
101+
}

0 commit comments

Comments
 (0)