From ecd7d2902b884bd7de70e53ac51d46d8dd292bcd Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Wed, 11 Dec 2024 14:44:21 +0100 Subject: [PATCH] tests --- .../pluginfw/products/app/app_acc_test.go | 79 +++++++++---------- .../pluginfw/products/app/resource_app.go | 16 ++-- .../pluginfw/tfschema/customizable_schema.go | 2 + .../tfschema/customizable_schema_test.go | 22 ++++++ 4 files changed, 70 insertions(+), 49 deletions(-) diff --git a/internal/providers/pluginfw/products/app/app_acc_test.go b/internal/providers/pluginfw/products/app/app_acc_test.go index 8fc8c5950..10da74154 100644 --- a/internal/providers/pluginfw/products/app/app_acc_test.go +++ b/internal/providers/pluginfw/products/app/app_acc_test.go @@ -8,7 +8,17 @@ import ( "github.com/databricks/terraform-provider-databricks/internal/acceptance" ) -/* +const baseResources = ` + resource "databricks_secret_scope" "this" { + name = "tf-{var.STICKY_RANDOM}" + } + + resource "databricks_secret" "this" { + scope = databricks_secret_scope.this.name + key = "tf-{var.STICKY_RANDOM}" + string_value = "secret" + } + resource "databricks_sql_endpoint" "this" { name = "tf-{var.STICKY_RANDOM}" cluster_size = "2X-Small" @@ -38,49 +48,8 @@ import ( } } } -*/ -const baseResources = ` - - resource "databricks_secret_scope" "this" { - name = "tf-{var.STICKY_RANDOM}" - } - - resource "databricks_secret" "this" { - scope = databricks_secret_scope.this.name - key = "tf-{var.STICKY_RANDOM}" - string_value = "secret" - } - ` -/* - resources { - name = "warehouse" - description = "warehouse for app" - job { - id = databricks_job.this.id - permission = "CAN_MANAGE" - } - } - - resources { - name = "serving endpoint" - description = "serving endpoint for app" - serving_endpoint { - name = databricks_model_serving.this.name - permission = "CAN_MANAGE" - } - } - - resources { - name = "sql warehouse" - description = "sql warehouse for app" - sql_warehouse { - id = databricks_sql_endpoint.this.id - permission = "CAN_MANAGE" - } - } -*/ func makeTemplate(description string) string { appTemplate := baseResources + ` resource "databricks_app" "this" { @@ -95,6 +64,32 @@ func makeTemplate(description string) string { permission = "MANAGE" } } + resources { + name = "warehouse" + description = "warehouse for app" + job { + id = databricks_job.this.id + permission = "CAN_MANAGE" + } + } + + resources { + name = "serving endpoint" + description = "serving endpoint for app" + serving_endpoint { + name = databricks_model_serving.this.name + permission = "CAN_MANAGE" + } + } + + resources { + name = "sql warehouse" + description = "sql warehouse for app" + sql_warehouse { + id = databricks_sql_endpoint.this.id + permission = "CAN_MANAGE" + } + } }` return fmt.Sprintf(appTemplate, description) } diff --git a/internal/providers/pluginfw/products/app/resource_app.go b/internal/providers/pluginfw/products/app/resource_app.go index 928f2d0d3..1042c184f 100644 --- a/internal/providers/pluginfw/products/app/resource_app.go +++ b/internal/providers/pluginfw/products/app/resource_app.go @@ -2,7 +2,6 @@ package app import ( "context" - "time" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/apps" @@ -19,9 +18,7 @@ import ( ) const ( - resourceName = "app" - defaultAppProvisionTimeout = 10 * time.Minute - deleteCallTimeout = 10 * time.Second + resourceName = "app" ) func ResourceApp() resource.Resource { @@ -120,7 +117,7 @@ func (a *resourceApp) Create(ctx context.Context, req resource.CreateRequest, re // Wait for the app to be created finalApp, err := waiter.Get() if err != nil { - resp.Diagnostics.AddError("failed to create app", err.Error()) + resp.Diagnostics.AddError("error waiting for app to be ready", err.Error()) return } @@ -151,7 +148,7 @@ func (a *resourceApp) Read(ctx context.Context, req resource.ReadRequest, resp * appGoSdk, err := w.Apps.GetByName(ctx, app.Name.ValueString()) if err != nil { - resp.Diagnostics.AddError("failed to get app", err.Error()) + resp.Diagnostics.AddError("failed to read app", err.Error()) return } @@ -160,8 +157,10 @@ func (a *resourceApp) Read(ctx context.Context, req resource.ReadRequest, resp * if resp.Diagnostics.HasError() { return } - resp.Diagnostics.Append(resp.State.Set(ctx, newApp)...) + if resp.Diagnostics.HasError() { + return + } } func (a *resourceApp) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { @@ -197,6 +196,9 @@ func (a *resourceApp) Update(ctx context.Context, req resource.UpdateRequest, re return } resp.Diagnostics.Append(resp.State.Set(ctx, newApp)...) + if resp.Diagnostics.HasError() { + return + } } func (a *resourceApp) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { diff --git a/internal/providers/pluginfw/tfschema/customizable_schema.go b/internal/providers/pluginfw/tfschema/customizable_schema.go index b7051b450..b2837371a 100644 --- a/internal/providers/pluginfw/tfschema/customizable_schema.go +++ b/internal/providers/pluginfw/tfschema/customizable_schema.go @@ -61,6 +61,8 @@ func (s *CustomizableSchema) AddValidator(v any, path ...string) *CustomizableSc return a.AddValidator(v.(validator.List)) case ListNestedAttributeBuilder: return a.AddValidator(v.(validator.List)) + case ListNestedBlockBuilder: + return a.AddValidator(v.(validator.List)) case MapAttributeBuilder: return a.AddValidator(v.(validator.Map)) case MapNestedAttributeBuilder: diff --git a/internal/providers/pluginfw/tfschema/customizable_schema_test.go b/internal/providers/pluginfw/tfschema/customizable_schema_test.go index 6de3c0002..0b6fc49eb 100644 --- a/internal/providers/pluginfw/tfschema/customizable_schema_test.go +++ b/internal/providers/pluginfw/tfschema/customizable_schema_test.go @@ -115,6 +115,28 @@ func TestCustomizeSchemaSetReadOnly(t *testing.T) { assert.True(t, scm.Attributes["map"].IsComputed()) } +type testTfSdkListNestedAttribute struct { + List types.List `tfsdk:"list"` +} + +func (testTfSdkListNestedAttribute) GetComplexFieldTypes(context.Context) map[string]reflect.Type { + return map[string]reflect.Type{ + "list": reflect.TypeOf(NestedTfSdk{}), + } +} + +func TestCustomizeSchemaSetReadOnly_RecursivelySetsFieldsOfListNestedAttributes(t *testing.T) { + scm := ResourceStructToSchema(context.Background(), testTfSdkListNestedAttribute{}, func(c CustomizableSchema) CustomizableSchema { + c.ConvertToAttribute("list").SetReadOnly("list") + return c + }) + for _, field := range []string{"name", "enabled"} { + assert.True(t, !scm.Attributes["list"].(schema.ListNestedAttribute).NestedObject.Attributes[field].IsOptional()) + assert.True(t, !scm.Attributes["list"].(schema.ListNestedAttribute).NestedObject.Attributes[field].IsRequired()) + assert.True(t, scm.Attributes["list"].(schema.ListNestedAttribute).NestedObject.Attributes[field].IsComputed()) + } +} + func TestCustomizeSchemaAddValidator(t *testing.T) { scm := ResourceStructToSchema(context.Background(), TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema { c.AddValidator(stringLengthBetweenValidator{}, "description")