Skip to content

Commit 6cb646d

Browse files
committed
Add BYOK resource
1 parent 202c28e commit 6cb646d

15 files changed

+561
-7
lines changed

docs/data-sources/cloud_accounts.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_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+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
21+
- `region` (String) Cloud provider region
22+
23+
### Read-Only
24+
25+
- `id` (String) The ID of this resource.
26+
- `results` (List of Object) The list of Cloud Accounts for the given Organization. (see [below for nested schema](#nestedatt--results))
27+
28+
<a id="nestedatt--results"></a>
29+
### Nested Schema for `results`
30+
31+
Read-Only:
32+
33+
- `organization_id` (String)
34+
- `provider` (String)
35+
- `provider_id` (String)

docs/data-sources/customer_key.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
21+
- `region` (String) Cloud provider region
22+
23+
### Read-Only
24+
25+
- `id` (String) The ID of this resource.
26+
- `key_id` (String) The Customer Key ID
27+
- `organization_id` (String) Organization ID

docs/data-sources/customer_keys.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Read-Only
19+
20+
- `id` (String) The ID of this resource.
21+
- `results` (List of Object) The list of Customer Keys for the given Organization. (see [below for nested schema](#nestedatt--results))
22+
23+
<a id="nestedatt--results"></a>
24+
### Nested Schema for `results`
25+
26+
Read-Only:
27+
28+
- `cloud_provider` (String)
29+
- `key_id` (String)
30+
- `organization_id` (String)
31+
- `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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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).
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).
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `cloud_provider` (String) The cloud provider where the Customer Key exists (Currently supported: aws, gcp)
21+
- `key_id` (String) Customer Key ID.
22+
- `region` (String) Region in which the Customer Key exists.
23+
24+
### Read-Only
25+
26+
- `id` (String) The ID of this resource.
27+
- `organization_id` (String) The Astra organization ID (this is derived from the token used to create the Customer Key).

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/datastax/terraform-provider-astra/v2
33
go 1.18
44

55
require (
6-
github.com/datastax/astra-client-go/v2 v2.2.51
6+
github.com/datastax/astra-client-go/v2 v2.2.52
77
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587
88
github.com/google/uuid v1.3.1
99
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
@@ -97,8 +97,8 @@ github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEM
9797
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
9898
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
9999
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
100-
github.com/datastax/astra-client-go/v2 v2.2.51 h1:Eptn/UNQiHT6uT9Tzg1iZUOmkyzdZpeXPJBIuzTIvZA=
101-
github.com/datastax/astra-client-go/v2 v2.2.51/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
100+
github.com/datastax/astra-client-go/v2 v2.2.52 h1:gpW7tRA3FvTX55SNeKbgOz+67T9XYm5OIvjLguhQzJw=
101+
github.com/datastax/astra-client-go/v2 v2.2.52/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
102102
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587 h1:3jv+O0hWcz3oj3sZ9/Ov9/m1Vaqx8Ql8jp5ZeA13O5A=
103103
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587/go.mod h1:guL8YZ5gJINN+h5Kmja1AnuzhxLU3sHQL8o/8HYLtqk=
104104
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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
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/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12+
)
13+
14+
func dataSourceCustomerKey() *schema.Resource {
15+
return &schema.Resource{
16+
Description: "Retrieve a Customer Key for a given cloud provider and region",
17+
18+
ReadContext: dataSourceCustomerKeyRead,
19+
20+
Schema: map[string]*schema.Schema{
21+
// Required inputs
22+
"cloud_provider": {
23+
Description: "The cloud provider where the Customer Key exists (Currently supported: aws, gcp)",
24+
Type: schema.TypeString,
25+
Required: true,
26+
ForceNew: true,
27+
ValidateFunc: validation.StringInSlice(availableBYOKCloudProviders, true),
28+
DiffSuppressFunc: ignoreCase,
29+
},
30+
"region": {
31+
Description: "Cloud provider region",
32+
Type: schema.TypeString,
33+
Required: true,
34+
},
35+
// Computed outputs
36+
"organization_id": {
37+
Description: "Organization ID",
38+
Type: schema.TypeString,
39+
Computed: true,
40+
},
41+
"key_id": {
42+
Description: "The Customer Key ID",
43+
Type: schema.TypeString,
44+
Computed: true,
45+
},
46+
},
47+
}
48+
}
49+
50+
func dataSourceCustomerKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
51+
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
52+
cloudProvider := d.Get("cloud_provider").(string)
53+
region := d.Get("region").(string)
54+
55+
customerKeys, err := listCustomerKeys(ctx, client)
56+
if err != nil {
57+
return diag.FromErr(err)
58+
}
59+
for _, key := range customerKeys {
60+
if strings.EqualFold(cloudProvider, key["cloud_provider"].(string)) &&
61+
region == key["region].(string)"] {
62+
orgId := key["organization_id"].(string)
63+
keyId := key["key_id"].(string)
64+
d.Set("organization_id", orgId)
65+
d.Set("key_id", keyId)
66+
d.SetId(fmt.Sprintf("%s/%s/%s", orgId, cloudProvider, region))
67+
return nil
68+
}
69+
}
70+
// key not found
71+
return diag.Errorf("No Customer Key found for provider: %s, region: %s", cloudProvider, region)
72+
}

0 commit comments

Comments
 (0)