Skip to content

Commit 00b8131

Browse files
committed
convert sentinel string to an error
1 parent 03be52e commit 00b8131

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

provisioner/terraform/resources.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ type State struct {
134134
ExternalAuthProviders []*proto.ExternalAuthProviderResource
135135
}
136136

137+
var ErrInvalidTerraformAddr = xerrors.New("invalid terraform address")
138+
137139
// ConvertState consumes Terraform state and a GraphViz representation
138140
// produced by `terraform graph` to produce resources consumable by Coder.
139141
// nolint:gocognit // This function makes more sense being large for now, until refactored.
@@ -609,7 +611,7 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
609611
// the database, a null value in WorkspaceResource's ModulePath
610612
// indicates "this resource was created before module paths
611613
// were tracked."
612-
modulePath = "FAILED_TO_PARSE_TERRAFORM_ADDRESS"
614+
modulePath = fmt.Sprintf("%s", ErrInvalidTerraformAddr)
613615
logger.Error(ctx, "failed to parse Terraform address", slog.F("address", resource.Address))
614616
}
615617

provisioner/terraform/resources_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,25 @@ func TestConvertResources(t *testing.T) {
816816
}
817817
}
818818

819+
func TestInvalidTerraformAddress(t *testing.T) {
820+
t.Parallel()
821+
ctx, logger := context.Background(), slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
822+
state, err := terraform.ConvertState(ctx, []*tfjson.StateModule{{
823+
Resources: []*tfjson.StateResource{{
824+
Address: "invalid",
825+
Type: "invalid",
826+
Name: "invalid",
827+
Mode: tfjson.ManagedResourceMode,
828+
AttributeValues: map[string]interface{}{},
829+
}},
830+
// This is manually created to join the edges.
831+
}}, `digraph {}`, logger)
832+
require.Nil(t, err)
833+
require.Len(t, state.Resources, 1)
834+
require.Equal(t, state.Resources[0].Name, "invalid")
835+
require.Equal(t, state.Resources[0].ModulePath, "invalid terraform address")
836+
}
837+
819838
func TestAppSlugValidation(t *testing.T) {
820839
t.Parallel()
821840
ctx, logger := ctxAndLogger(t)

0 commit comments

Comments
 (0)