Skip to content

Commit

Permalink
Add dbType to database creation fro Vector support (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
emerkle826 authored Oct 4, 2023
1 parent d3332f1 commit 253f3f6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ data "astra_database" "db" {
- `cqlsh_url` (String) URL for cqlsh web
- `data_endpoint_url` (String) REST API URL
- `datacenters` (Map of String) Map of Datacenter IDs. The map key is "cloud_provider.region". Example: "GCP.us-east4".
- `db_type` (String) Database Type. Only 'vector' is supported. For serverless, this field will be omitted
- `grafana_url` (String) URL for the grafana dashboard for this database
- `graphql_url` (String) Graphql URL
- `id` (String) The ID of this resource.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ output "cqlsh_url" {

### Optional

- `db_type` (String) Database type. Currently only `vector` is supported. Omit this optional field if you want a regular severless database.
- `deletion_protection` (Boolean) Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/datastax/terraform-provider-astra/v2
go 1.18

require (
github.com/datastax/astra-client-go/v2 v2.2.50-0.20230626192203-43de11466bf8
github.com/datastax/astra-client-go/v2 v2.2.50
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587
github.com/google/uuid v1.3.1
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEM
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/datastax/astra-client-go/v2 v2.2.50-0.20230626192203-43de11466bf8 h1:lcjdcx7OmSmsXhuD7400+P8WYXGM4Q5y9gw61POOq58=
github.com/datastax/astra-client-go/v2 v2.2.50-0.20230626192203-43de11466bf8/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
github.com/datastax/astra-client-go/v2 v2.2.50 h1:GEKSQTphbEz0Qe29Uyl2yxs1FuN9N0ID4gXLsy4HuH0=
github.com/datastax/astra-client-go/v2 v2.2.50/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587 h1:3jv+O0hWcz3oj3sZ9/Ov9/m1Vaqx8Ql8jp5ZeA13O5A=
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587/go.mod h1:guL8YZ5gJINN+h5Kmja1AnuzhxLU3sHQL8o/8HYLtqk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/data_source_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func dataSourceDatabase() *schema.Resource {
Type: schema.TypeString,
},
},
"db_type": {
Description: "Database Type. Only 'vector' is supported. For serverless, this field will be omitted",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
24 changes: 22 additions & 2 deletions internal/provider/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var availableCloudProviders = []string{
"azure",
}

var availableDbTypes = []string{
"vector",
}

var databaseCreateTimeout = time.Minute * 40
var databaseReadTimeout = time.Minute * 5
var databaseDeleteTimeout = time.Minute * 20
Expand Down Expand Up @@ -86,6 +90,13 @@ func resourceDatabase() *schema.Resource {
Optional: true,
Default: true,
},
"db_type": {
Description: "Database type. Currently only `vector` is supported. Omit this optional field if you want a regular severless database.",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(availableDbTypes, false),
},
// Computed
"owner_id": {
Description: "The owner id.",
Expand Down Expand Up @@ -164,6 +175,7 @@ func resourceDatabaseCreate(ctx context.Context, resourceData *schema.ResourceDa
keyspace := resourceData.Get("keyspace").(string)
cloudProvider := resourceData.Get("cloud_provider").(string)
regions := resourceData.Get("regions").([]interface{})
dbType := resourceData.Get("db_type").(string)

if len(regions) < 1 {
return diag.Errorf("\"region\" array must have at least 1 region specified")
Expand All @@ -184,14 +196,19 @@ func resourceDatabaseCreate(ctx context.Context, resourceData *schema.ResourceDa
}
}

resp, err := client.CreateDatabaseWithResponse(ctx, astra.CreateDatabaseJSONRequestBody{
createDbRequest := astra.CreateDatabaseJSONRequestBody{
Name: name,
Keyspace: keyspace,
CloudProvider: astra.CloudProvider(cloudProvider),
CapacityUnits: 1,
Region: region,
Tier: astra.Tier("serverless"),
})
}
// if Vector DB was requested, add that to the request
if len(dbType) > 0 {
createDbRequest.DbType = (*astra.DatabaseInfoCreateDbType)(&dbType)
}
resp, err := client.CreateDatabaseWithResponse(ctx, createDbRequest)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -564,6 +581,9 @@ func flattenDatabase(db *astra.Database) map[string]interface{} {
flatDB["regions"] = regions
flatDB["datacenters"] = datacenters
}
if db.Info.DbType != nil {
flatDB["db_type"] = *db.Info.DbType
}
return flatDB
}

Expand Down

0 comments on commit 253f3f6

Please sign in to comment.