Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Dec 11, 2024
1 parent 5da3a69 commit ecd7d29
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 49 deletions.
79 changes: 37 additions & 42 deletions internal/providers/pluginfw/products/app/app_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
}
Expand Down
16 changes: 9 additions & 7 deletions internal/providers/pluginfw/products/app/resource_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"context"
"time"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/apps"
Expand All @@ -19,9 +18,7 @@ import (
)

const (
resourceName = "app"
defaultAppProvisionTimeout = 10 * time.Minute
deleteCallTimeout = 10 * time.Second
resourceName = "app"
)

func ResourceApp() resource.Resource {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions internal/providers/pluginfw/tfschema/customizable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions internal/providers/pluginfw/tfschema/customizable_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ecd7d29

Please sign in to comment.