Skip to content

Commit

Permalink
Initial implementation of partition_template
Browse files Browse the repository at this point in the history
  • Loading branch information
thulasirajkomminar committed Aug 20, 2024
1 parent a2295ca commit d65aaac
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 44 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ lint:
docs:
go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
tfplugindocs generate
@echo "Use this site to preview markdown rendering: https://registry.terraform.io/tools/doc-preview"
9 changes: 9 additions & 0 deletions docs/data-sources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ Retrieves a database. Use this data source to retrieve information for a specifi
- `cluster_id` (String) The ID of the cluster that you want to manage.
- `max_columns_per_table` (Number) The maximum number of columns per table for the cluster database.
- `max_tables` (Number) The maximum number of tables for the cluster database.
- `partition_template` (Attributes List) The template partitioning of the cluster database. (see [below for nested schema](#nestedatt--partition_template))
- `retention_period` (Number) The retention period of the cluster database in nanoseconds.

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

Read-Only:

- `type` (String) The type of template part.
- `value` (String) The value of template part.
9 changes: 9 additions & 0 deletions docs/data-sources/databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ Read-Only:
- `max_columns_per_table` (Number) The maximum number of columns per table for the cluster database.
- `max_tables` (Number) The maximum number of tables for the cluster database.
- `name` (String) The name of the cluster database.
- `partition_template` (Attributes List) The template partitioning of the cluster database. (see [below for nested schema](#nestedatt--databases--partition_template))
- `retention_period` (Number) The retention period of the cluster database in nanoseconds.

<a id="nestedatt--databases--partition_template"></a>
### Nested Schema for `databases.partition_template`

Read-Only:

- `type` (String) The type of template part.
- `value` (String) The value of template part.
40 changes: 38 additions & 2 deletions docs/resources/database.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "influxdb3_database Resource - terraform-provider-influxdb3"
subcategory: ""
description: |-
Expand All @@ -10,7 +8,36 @@ description: |-

Creates and manages a database.

## Example Usage

```terraform
resource "influxdb3_database" "signals" {
name = "signals"
retention_period = 604800
partition_template = [
{
type = "tag"
value = "line"
},
{
type = "tag"
value = "station"
},
{
type = "time"
value = "%Y-%m-%d"
},
{
type = "bucket"
value = jsonencode({
"tagName" : "temperature",
"numberOfBuckets" : 10
})
},
]
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand All @@ -23,9 +50,18 @@ Creates and manages a database.

- `max_columns_per_table` (Number) The maximum number of columns per table for the cluster database. The default is `200`
- `max_tables` (Number) The maximum number of tables for the cluster database. The default is `500`
- `partition_template` (Attributes List) A template for [partitioning](https://docs.influxdata.com/influxdb/cloud-dedicated/admin/custom-partitions/partition-templates/) a cluster database. **Note:** A partition template can include up to 7 total tag and tag bucket parts and only 1 time part. (see [below for nested schema](#nestedatt--partition_template))
- `retention_period` (Number) The retention period of the cluster database in nanoseconds. The default is `0`. If the retention period is not set or is set to `0`, the database will have infinite retention.

### Read-Only

- `account_id` (String) The ID of the account that the cluster belongs to.
- `cluster_id` (String) The ID of the cluster that you want to manage.

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

Required:

- `type` (String) The type of template part. Valid values are `bucket`, `tag` or `time`.
- `value` (String) The value of template part. **Note:** For `bucket` partition template type use `jsonencode()` function to encode the value to a string.
26 changes: 26 additions & 0 deletions examples/resources/database/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "influxdb3_database" "signals" {
name = "signals"
retention_period = 604800

partition_template = [
{
type = "tag"
value = "line"
},
{
type = "tag"
value = "station"
},
{
type = "time"
value = "%Y-%m-%d"
},
{
type = "bucket"
value = jsonencode({
"tagName" : "temperature",
"numberOfBuckets" : 10
})
},
]
}
3 changes: 3 additions & 0 deletions examples/resources/database/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "signals_database" {
value = influxdb3_database.signals
}
9 changes: 9 additions & 0 deletions examples/resources/database/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
influxdb3 = {
source = "komminarlabs/influxdb3"
}
}
}

provider "influxdb3" {}
18 changes: 0 additions & 18 deletions examples/resources/database/resource.tf

This file was deleted.

25 changes: 24 additions & 1 deletion internal/provider/database_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ func (d *DatabaseDataSource) Schema(ctx context.Context, req datasource.SchemaRe
Computed: true,
Description: "The retention period of the cluster database in nanoseconds.",
},
"partition_template": schema.ListNestedAttribute{
Computed: true,
Description: "The template partitioning of the cluster database.",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Computed: true,
Description: "The type of template part.",
},
"value": schema.StringAttribute{
Computed: true,
Description: "The value of template part.",
},
},
},
},
},
}
}
Expand Down Expand Up @@ -124,7 +140,14 @@ func (d *DatabaseDataSource) Read(ctx context.Context, req datasource.ReadReques
}

// Check if the database exists
readDatabase := getDatabaseByName(*readDatabasesResponse, databaseName.ValueString())
readDatabase, err := getDatabaseByName(*readDatabasesResponse, databaseName.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error getting database",
"Unexpected error: "+err.Error(),
)
return
}
if readDatabase == nil {
resp.Diagnostics.AddError(
"Database not found",
Expand Down
76 changes: 67 additions & 9 deletions internal/provider/database_model.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,91 @@
package provider

import (
"encoding/json"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/komminarlabs/influxdb3"
)

// DatabaseModel maps InfluxDB database schema data.
type DatabaseModel struct {
AccountId types.String `tfsdk:"account_id"`
ClusterId types.String `tfsdk:"cluster_id"`
Name types.String `tfsdk:"name"`
MaxTables types.Int64 `tfsdk:"max_tables"`
MaxColumnsPerTable types.Int64 `tfsdk:"max_columns_per_table"`
RetentionPeriod types.Int64 `tfsdk:"retention_period"`
AccountId types.String `tfsdk:"account_id"`
ClusterId types.String `tfsdk:"cluster_id"`
Name types.String `tfsdk:"name"`
MaxTables types.Int64 `tfsdk:"max_tables"`
MaxColumnsPerTable types.Int64 `tfsdk:"max_columns_per_table"`
RetentionPeriod types.Int64 `tfsdk:"retention_period"`
PartitionTemplate []DatabasePartitionTemplateModel `tfsdk:"partition_template"`
}

// DatabasePartitionTemplateModel maps InfluxDB database partition template schema data.
type DatabasePartitionTemplateModel struct {
Type types.String `json:"type" tfsdk:"type"`
Value types.String `json:"value" tfsdk:"value"`
}

// GetAttrType returns the attribute type for the DatabasePartitionTemplateModel.
func (d DatabasePartitionTemplateModel) GetAttrType() attr.Type {
return types.ObjectType{AttrTypes: map[string]attr.Type{
"type": types.StringType,
"value": types.StringType,
}}
}

func getDatabaseByName(databases influxdb3.GetClusterDatabasesResponse, name string) *DatabaseModel {
func getDatabaseByName(databases influxdb3.GetClusterDatabasesResponse, name string) (*DatabaseModel, error) {
for _, database := range *databases.JSON200 {
if database.Name == name {
partitionTemplate, err := getPartitionTemplate(database.PartitionTemplate)
if err != nil {
return nil, err
}

db := DatabaseModel{
AccountId: types.StringValue(database.AccountId.String()),
ClusterId: types.StringValue(database.ClusterId.String()),
Name: types.StringValue(database.Name),
MaxTables: types.Int64Value(int64(database.MaxTables)),
MaxColumnsPerTable: types.Int64Value(int64(database.MaxColumnsPerTable)),
PartitionTemplate: partitionTemplate,
RetentionPeriod: types.Int64Value(database.RetentionPeriod),
}
return &db
return &db, nil
}
}
return nil, nil
}

func getPartitionTemplate(partitionTemplates *influxdb3.ClusterDatabasePartitionTemplate) ([]DatabasePartitionTemplateModel, error) {
if partitionTemplates == nil {
return nil, nil
}

partitionTemplateModels := make([]DatabasePartitionTemplateModel, 0)
for _, v := range *partitionTemplates {
partitionTemplate := make(map[string]any)
b, err := v.MarshalJSON()
if err != nil {
return nil, err
}

json.Unmarshal(b, &partitionTemplate)
if partitionTemplate["type"] == "time" || partitionTemplate["type"] == "tag" {
partitionTemplateModels = append(partitionTemplateModels, DatabasePartitionTemplateModel{
Type: types.StringValue(partitionTemplate["type"].(string)),
Value: types.StringValue(partitionTemplate["value"].(string)),
})
} else if partitionTemplate["type"] == "bucket" {
jsonEncoded, err := json.Marshal(partitionTemplate["value"])
if err != nil {
return nil, err
}

partitionTemplateModels = append(partitionTemplateModels, DatabasePartitionTemplateModel{
Type: types.StringValue(partitionTemplate["type"].(string)),
Value: types.StringValue(string(jsonEncoded)),
})
}
}
return nil
return partitionTemplateModels, nil
}
Loading

0 comments on commit d65aaac

Please sign in to comment.