-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of
partition_template
- Loading branch information
1 parent
a2295ca
commit d65aaac
Showing
13 changed files
with
337 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "signals_database" { | ||
value = influxdb3_database.signals | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
terraform { | ||
required_providers { | ||
influxdb3 = { | ||
source = "komminarlabs/influxdb3" | ||
} | ||
} | ||
} | ||
|
||
provider "influxdb3" {} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.