Skip to content

Commit

Permalink
fix panic
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Dec 10, 2024
1 parent dcb2eb1 commit 89e62ea
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/tfshim/sdk-v2/provider2.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (s *v2InstanceState2) Type() string {
}

func (s *v2InstanceState2) ID() string {
if s.stateValue.IsNull() {
return ""
}
id := s.stateValue.GetAttr("id")
if !id.IsKnown() {
return ""
Expand Down Expand Up @@ -621,7 +624,7 @@ func (s *grpcServer) ApplyResourceChange(
config, priorState, plannedState cty.Value,
plannedMeta map[string]interface{},
providerMeta *cty.Value,
) (*v2InstanceState2, error) {
) (shim.InstanceState, error) {
configVal, err := msgpack.Marshal(config, ty)
if err != nil {
return nil, err
Expand Down Expand Up @@ -671,6 +674,9 @@ func (s *grpcServer) ApplyResourceChange(
}
}
returnErr := handleDiagnostics(ctx, resp.Diagnostics, applyErr)
if newState.IsNull() {
return nil, returnErr
}
return &v2InstanceState2{
resourceType: typeName,
stateValue: newState,
Expand Down
37 changes: 37 additions & 0 deletions pkg/tfshim/sdk-v2/provider2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,40 @@ func (l *testLogger) Error(msg string) {
func (*testLogger) StatusUntyped() any {
return "?"
}

func TestInstanceStateId(t *testing.T) {
t.Parallel()

state := v2InstanceState2{
stateValue: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("1"),
}),
}
assert.Equal(t, "1", state.ID())

state.stateValue = cty.ObjectVal(map[string]cty.Value{
"id": cty.UnknownVal(cty.String),
})
assert.Equal(t, "", state.ID())

state.stateValue = cty.Value{}
assert.Equal(t, "", state.ID())
}

func TestInstanceStateObject(t *testing.T) {
t.Parallel()

state := v2InstanceState2{
stateValue: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("1"),
}),
}
actual, err := state.Object(nil)
require.NoError(t, err)
assert.Equal(t, map[string]interface{}{"id": "1"}, actual)

state.stateValue = cty.Value{}
actual, err = state.Object(nil)
require.NoError(t, err)
assert.Equal(t, map[string]interface{}(nil), actual)
}

0 comments on commit 89e62ea

Please sign in to comment.