Skip to content

Commit

Permalink
test nested unknown value setting
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Sep 28, 2021
1 parent 6eba869 commit 10b41f5
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions tfsdk/serve_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ func (t *testServeProvider) GetResources(_ context.Context) (map[string]Resource
return map[string]ResourceType{
"test_one": testServeResourceTypeOne{},
"test_two": testServeResourceTypeTwo{},
"test_three": testServeResourceTypeThree{},
"test_attribute_plan_modifiers": testServeResourceTypeAttributePlanModifiers{},
"test_config_validators": testServeResourceTypeConfigValidators{},
"test_import_state": testServeResourceTypeImportState{},
Expand Down
142 changes: 142 additions & 0 deletions tfsdk/serve_resource_three_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package tfsdk

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func (rt testServeResourceTypeThree) GetSchema(_ context.Context) (Schema, diag.Diagnostics) {
return Schema{
Version: 1,
Attributes: map[string]Attribute{
"name": {
Required: true,
Type: types.StringType,
},
"last_updated": {
Computed: true,
Type: types.StringType,
},
"first_updated": {
Computed: true,
Type: types.StringType,
},
"map_nested": {
Required: true,
Attributes: MapNestedAttributes(map[string]Attribute{
"computed_string": {
Computed: true,
Type: types.StringType,
},
"string": {
Optional: true,
Type: types.StringType,
},
}, MapNestedAttributesOptions{}),
},
},
}, nil
}

func (rt testServeResourceTypeThree) NewResource(_ context.Context, p Provider) (Resource, diag.Diagnostics) {
provider, ok := p.(*testServeProvider)
if !ok {
prov, ok := p.(*testServeProviderWithMetaSchema)
if !ok {
panic(fmt.Sprintf("unexpected provider type %T", p))
}
provider = prov.testServeProvider
}
return testServeResourceThree{
provider: provider,
}, nil
}

var testServeResourceTypeThreeSchema = &tfprotov6.Schema{
Version: 1,
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "first_updated",
Computed: true,
Type: tftypes.String,
},
{
Name: "last_updated",
Computed: true,
Type: tftypes.String,
},
{
Name: "map_nested",
Required: true,
NestedType: &tfprotov6.SchemaObject{
Nesting: tfprotov6.SchemaObjectNestingModeMap,
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "computed_string",
Computed: true,
Type: tftypes.String,
},
{
Name: "string",
Optional: true,
Type: tftypes.String,
},
},
},
},
{
Name: "name",
Required: true,
Type: tftypes.String,
},
},
},
}

var testServeResourceTypeThreeType = tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
"last_updated": tftypes.String,
"first_updated": tftypes.String,
"map_nested": tftypes.Map{
AttributeType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
},
},
},
}

type testServeResourceThree struct {
provider *testServeProvider
}

type testServeResourceTypeThree struct{}

func (r testServeResourceThree) Create(ctx context.Context, req CreateResourceRequest, resp *CreateResourceResponse) {
// Intentionally blank. Not expected to be called during testing.
}

func (r testServeResourceThree) Read(ctx context.Context, req ReadResourceRequest, resp *ReadResourceResponse) {
// Intentionally blank. Not expected to be called during testing.
}

func (r testServeResourceThree) Update(ctx context.Context, req UpdateResourceRequest, resp *UpdateResourceResponse) {
// Intentionally blank. Not expected to be called during testing.
}

func (r testServeResourceThree) Delete(ctx context.Context, req DeleteResourceRequest, resp *DeleteResourceResponse) {
// Intentionally blank. Not expected to be called during testing.
}

func (r testServeResourceThree) ImportState(ctx context.Context, req ImportResourceStateRequest, resp *ImportResourceStateResponse) {
ResourceImportStateNotImplemented(ctx, "Not expected to be called during testing.", resp)
}
98 changes: 98 additions & 0 deletions tfsdk/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func TestServerGetProviderSchema(t *testing.T) {
ResourceSchemas: map[string]*tfprotov6.Schema{
"test_one": testServeResourceTypeOneSchema,
"test_two": testServeResourceTypeTwoSchema,
"test_three": testServeResourceTypeThreeSchema,
"test_attribute_plan_modifiers": testServeResourceTypeAttributePlanModifiersSchema,
"test_config_validators": testServeResourceTypeConfigValidatorsSchema,
"test_import_state": testServeResourceTypeImportStateSchema,
Expand Down Expand Up @@ -309,6 +310,7 @@ func TestServerGetProviderSchemaWithProviderMeta(t *testing.T) {
ResourceSchemas: map[string]*tfprotov6.Schema{
"test_one": testServeResourceTypeOneSchema,
"test_two": testServeResourceTypeTwoSchema,
"test_three": testServeResourceTypeThreeSchema,
"test_attribute_plan_modifiers": testServeResourceTypeAttributePlanModifiersSchema,
"test_config_validators": testServeResourceTypeConfigValidatorsSchema,
"test_import_state": testServeResourceTypeImportStateSchema,
Expand Down Expand Up @@ -1775,6 +1777,102 @@ func TestServerPlanResourceChange(t *testing.T) {
resourceType: testServeResourceTypeTwoType,
expectedPlannedState: tftypes.NewValue(testServeResourceTypeTwoType, nil),
},
"three_nested_computed_unknown": {
resource: "test_three",
resourceType: testServeResourceTypeThreeType,
priorState: tftypes.NewValue(testServeResourceTypeThreeType, map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "myname"),
"last_updated": tftypes.NewValue(tftypes.String, "yesterday"),
"first_updated": tftypes.NewValue(tftypes.String, "last year"),
"map_nested": tftypes.NewValue(tftypes.Map{
AttributeType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
},
}, map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
}, map[string]tftypes.Value{
"computed_string": tftypes.NewValue(tftypes.String, "mycompstring"),
"string": tftypes.NewValue(tftypes.String, "mystring"),
}),
}),
}),
config: tftypes.NewValue(testServeResourceTypeThreeType, map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "myname"),
"last_updated": tftypes.NewValue(tftypes.String, nil),
"first_updated": tftypes.NewValue(tftypes.String, nil),
"map_nested": tftypes.NewValue(tftypes.Map{
AttributeType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
},
}, map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
}, map[string]tftypes.Value{
"computed_string": tftypes.NewValue(tftypes.String, nil),
"string": tftypes.NewValue(tftypes.String, nil),
}),
}),
}),
proposedNewState: tftypes.NewValue(testServeResourceTypeThreeType, map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "myname"),
"last_updated": tftypes.NewValue(tftypes.String, nil),
"first_updated": tftypes.NewValue(tftypes.String, nil),
"map_nested": tftypes.NewValue(tftypes.Map{
AttributeType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
},
}, map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
}, map[string]tftypes.Value{
"computed_string": tftypes.NewValue(tftypes.String, nil),
"string": tftypes.NewValue(tftypes.String, nil),
}),
}),
}),
expectedPlannedState: tftypes.NewValue(testServeResourceTypeThreeType, map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "myname"),
"last_updated": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
"first_updated": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
"map_nested": tftypes.NewValue(tftypes.Map{
AttributeType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
},
}, map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"computed_string": tftypes.String,
"string": tftypes.String,
},
}, map[string]tftypes.Value{
"computed_string": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
"string": tftypes.NewValue(tftypes.String, nil),
}),
}),
}),
},
"one_add": {
priorState: tftypes.NewValue(testServeResourceTypeOneType, nil),
proposedNewState: tftypes.NewValue(testServeResourceTypeOneType, map[string]tftypes.Value{
Expand Down

0 comments on commit 10b41f5

Please sign in to comment.