Skip to content

Commit

Permalink
fix: List and object decoding (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 authored Sep 12, 2024
1 parent e47b97b commit 38348e7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 69 deletions.
14 changes: 7 additions & 7 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ builds:
ldflags:
- "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}"
goos:
# - freebsd
# - windows
- freebsd
- windows
- linux
- darwin
goarch:
- amd64
- arm64
# - '386'
# - arm
# ignore:
# - goos: darwin
# goarch: '386'
- '386'
- arm
ignore:
- goos: darwin
goarch: '386'
binary: "{{ .ProjectName }}_v{{ .Version }}"
archives:
- format: zip
Expand Down
20 changes: 13 additions & 7 deletions internal/provider/datasource_database_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,20 @@ func (r *DatabaseInstancesDataSource) Read(ctx context.Context, req datasource.R
}
instances := make([]attr.Value, len(res.Instances))
for i, instance := range res.Instances {
instances[i] = datasource_database_instances.InstancesValue{
Hostname: basetypes.NewStringValue(instance.Hostname.Value),
Name: basetypes.NewStringValue(instance.Name.Value),
Region: basetypes.NewStringValue(instance.Region.Value),
InstancesType: basetypes.NewStringValue(string(instance.Type.Value)),
Uuid: basetypes.NewStringValue(instance.UUID.Value),
}
instanceVal, diags := datasource_database_instances.NewInstancesValue(datasource_database_instances.InstancesValue{}.AttributeTypes(ctx), map[string]attr.Value{
"hostname": basetypes.NewStringValue(instance.Hostname.Value),
"name": basetypes.NewStringValue(instance.Name.Value),
"region": basetypes.NewStringValue(instance.Region.Value),
"type": basetypes.NewStringValue(string(instance.Type.Value)),
"uuid": basetypes.NewStringValue(instance.UUID.Value),
})
resp.Diagnostics.Append(diags...)
instances[i] = instanceVal
}
if resp.Diagnostics.HasError() {
return
}

instancesTy := datasource_database_instances.InstancesValue{}.Type(ctx)
data.Instances = basetypes.NewListValueMust(instancesTy, instances)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
22 changes: 1 addition & 21 deletions internal/provider/datasource_database_instances_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -26,28 +25,9 @@ func TestAccDataSourceDatabaseInstances(t *testing.T) {
}`),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue("data.turso_database_instances.test", tfjsonpath.New("instances"), listNotEmpty{}),
statecheck.ExpectKnownValue("data.turso_database_instances.test", tfjsonpath.New("instances"), listOfNonNulls{}),
},
},
},
})
}

type listNotEmpty struct{}

func (listNotEmpty) CheckValue(v any) error {
val, ok := v.([]any)

if !ok {
return fmt.Errorf("expected []any value for ListNotEmpty check, got: %T", v)
}

if len(val) == 0 {
return fmt.Errorf("expected non-empty list for ListNotEmpty check, but list was empty")
}

return nil
}

func (listNotEmpty) String() string {
return "non-empty list"
}
70 changes: 36 additions & 34 deletions internal/provider/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/celest-dev/terraform-provider-turso/internal/resource_database"
"github.com/celest-dev/terraform-provider-turso/internal/tursoclient"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -53,34 +54,34 @@ func (r *DatabaseResource) Configure(ctx context.Context, req resource.Configure
}

func (r *DatabaseResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan resource_database.DatabaseModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
var data resource_database.DatabaseModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

tflog.Trace(ctx, "create database plan", map[string]interface{}{
"plan": plan,
"plan": data,
})
fmt.Printf("create database plan: %+v\n", plan)
fmt.Printf("create database plan: %+v\n", data)

var dbSeed tursoclient.OptCreateDatabaseInputSeed
if !plan.Seed.IsNull() && !plan.Seed.IsUnknown() {
if !data.Seed.IsNull() && !data.Seed.IsUnknown() {
dbSeed = tursoclient.NewOptCreateDatabaseInputSeed(tursoclient.CreateDatabaseInputSeed{
Type: tursoclient.NewOptCreateDatabaseInputSeedType(tursoclient.CreateDatabaseInputSeedType(plan.Seed.SeedType.ValueString())),
Name: optString(plan.Seed.Name),
URL: optString(plan.Seed.Url),
Timestamp: optString(plan.Seed.Timestamp),
Type: tursoclient.NewOptCreateDatabaseInputSeedType(tursoclient.CreateDatabaseInputSeedType(data.Seed.SeedType.ValueString())),
Name: optString(data.Seed.Name),
URL: optString(data.Seed.Url),
Timestamp: optString(data.Seed.Timestamp),
})
}

createReq := tursoclient.CreateDatabaseInput{
Name: plan.Name.ValueString(),
Group: plan.Group.ValueString(),
Name: data.Name.ValueString(),
Group: data.Group.ValueString(),
Seed: dbSeed,
SizeLimit: optString(plan.SizeLimit),
IsSchema: optBool(plan.IsSchema),
Schema: optString(plan.Schema),
SizeLimit: optString(data.SizeLimit),
IsSchema: optBool(data.IsSchema),
Schema: optString(data.Schema),
}
tflog.Trace(ctx, "creating database", map[string]interface{}{
"request": createReq,
Expand All @@ -100,13 +101,13 @@ func (r *DatabaseResource) Create(ctx context.Context, req resource.CreateReques
}

dbName := string(db.Database.Value.Name.Value)
resp.Diagnostics.Append(r.readDatabase(ctx, dbName, &plan)...)
resp.Diagnostics.Append(r.readDatabase(ctx, dbName, &data)...)
if resp.Diagnostics.HasError() {
return
}

tflog.Trace(ctx, "created database resource")
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *DatabaseResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand All @@ -132,10 +133,7 @@ func (r *DatabaseResource) Update(ctx context.Context, req resource.UpdateReques

func (r *DatabaseResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data resource_database.DatabaseModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -189,22 +187,26 @@ func (r *DatabaseResource) readDatabase(ctx context.Context, name string, data *
if data.Seed.IsUnknown() {
data.Seed = resource_database.NewSeedValueNull()
}
data.Database = resource_database.DatabaseValue{
DbId: types.StringValue(db.Database.Value.DbId.Value),
Name: types.StringValue(db.Database.Value.Name.Value),
Group: types.StringValue(db.Database.Value.Group.Value),
Hostname: types.StringValue(db.Database.Value.Hostname.Value),
Regions: encodeStringList(db.Database.Value.Regions),
PrimaryRegion: types.StringValue(db.Database.Value.PrimaryRegion.Value),
Schema: types.StringValue(db.Database.Value.Schema.Value),
IsSchema: types.BoolValue(db.Database.Value.IsSchema.Value),
DatabaseType: types.StringValue(db.Database.Value.Type.Value),
Archived: types.BoolValue(db.Database.Value.Archived.Value),
Version: types.StringValue(db.Database.Value.Version.Value),
AllowAttach: types.BoolValue(db.Database.Value.AllowAttach.Value),
BlockReads: types.BoolValue(db.Database.Value.BlockReads.Value),
BlockWrites: types.BoolValue(db.Database.Value.BlockWrites.Value),
dbVal, diags := resource_database.NewDatabaseValue(resource_database.DatabaseValue{}.AttributeTypes(ctx), map[string]attr.Value{
"db_id": types.StringValue(db.Database.Value.DbId.Value),
"name": types.StringValue(db.Database.Value.Name.Value),
"group": types.StringValue(db.Database.Value.Group.Value),
"hostname": types.StringValue(db.Database.Value.Hostname.Value),
"regions": encodeStringList(db.Database.Value.Regions),
"primary_region": types.StringValue(db.Database.Value.PrimaryRegion.Value),
"schema": types.StringValue(db.Database.Value.Schema.Value),
"is_schema": types.BoolValue(db.Database.Value.IsSchema.Value),
"type": types.StringValue(db.Database.Value.Type.Value),
"archived": types.BoolValue(db.Database.Value.Archived.Value),
"version": types.StringValue(db.Database.Value.Version.Value),
"allow_attach": types.BoolValue(db.Database.Value.AllowAttach.Value),
"block_reads": types.BoolValue(db.Database.Value.BlockReads.Value),
"block_writes": types.BoolValue(db.Database.Value.BlockWrites.Value),
})
if diags.HasError() {
return diags
}
data.Database = dbVal

return nil
}
1 change: 1 addition & 0 deletions internal/provider/resource_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestAccResourceDatabase(t *testing.T) {
statecheck.ExpectKnownValue("turso_database.test", tfjsonpath.New("id"), knownvalue.StringExact(name)),
statecheck.ExpectKnownValue("turso_database.test", tfjsonpath.New("name"), knownvalue.StringExact(name)),
statecheck.ExpectKnownValue("turso_database.test", tfjsonpath.New("group"), knownvalue.StringExact("test")),
statecheck.ExpectKnownValue("turso_database.test", tfjsonpath.New("database"), knownvalue.NotNull()),
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("turso_database.test", "name", name),
Expand Down
45 changes: 45 additions & 0 deletions internal/provider/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package provider

import "fmt"

type listNotEmpty struct{}

func (listNotEmpty) CheckValue(v any) error {
val, ok := v.([]any)

if !ok {
return fmt.Errorf("expected []any value for ListNotEmpty check, got: %T", v)
}

if len(val) == 0 {
return fmt.Errorf("expected non-empty list for ListNotEmpty check, but list was empty")
}

return nil
}

func (listNotEmpty) String() string {
return "non-empty list"
}

type listOfNonNulls struct{}

func (listOfNonNulls) CheckValue(v any) error {
val, ok := v.([]any)

if !ok {
return fmt.Errorf("expected []any value for ListOfNonNulls check, got: %T", v)
}

for i, item := range val {
if item == nil {
return fmt.Errorf("expected non-nil value at index %d for ListOfNonNulls check, but value was nil", i)
}
}

return nil
}

func (listOfNonNulls) String() string {
return "list of non-nil values"
}

0 comments on commit 38348e7

Please sign in to comment.