Skip to content

Commit 352de2d

Browse files
committed
fix: skip validating unknown version names
1 parent 39ce21c commit 352de2d

File tree

2 files changed

+86
-13
lines changed

2 files changed

+86
-13
lines changed

internal/provider/template_resource.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,27 @@ func (a *activeVersionValidator) ValidateList(ctx context.Context, req validator
896896
return
897897
}
898898

899+
// Check all versions have unique names
900+
uniqueNames := make(map[string]struct{})
901+
for _, version := range data {
902+
if version.Name.IsNull() || version.Name.IsUnknown() {
903+
continue
904+
}
905+
if _, ok := uniqueNames[version.Name.ValueString()]; ok {
906+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Template version names must be unique. `%s` appears twice.", version.Name.ValueString()))
907+
return
908+
}
909+
uniqueNames[version.Name.ValueString()] = struct{}{}
910+
}
911+
899912
// Check if only one item in Version has active set to true
900913
active := false
901914
for _, version := range data {
915+
// `active` is required, so if it's null or unknown, this is Terraform
916+
// requesting an early validation.
917+
if version.Active.IsNull() || version.Active.IsUnknown() {
918+
return
919+
}
902920
if version.Active.ValueBool() {
903921
if active {
904922
resp.Diagnostics.AddError("Client Error", "Only one template version can be active at a time.")
@@ -910,19 +928,6 @@ func (a *activeVersionValidator) ValidateList(ctx context.Context, req validator
910928
if !active {
911929
resp.Diagnostics.AddError("Client Error", "At least one template version must be active.")
912930
}
913-
914-
// Check all versions have unique names
915-
uniqueNames := make(map[string]struct{})
916-
for _, version := range data {
917-
if version.Name.IsNull() {
918-
continue
919-
}
920-
if _, ok := uniqueNames[version.Name.ValueString()]; ok {
921-
resp.Diagnostics.AddError("Client Error", "Template version names must be unique.")
922-
return
923-
}
924-
uniqueNames[version.Name.ValueString()] = struct{}{}
925-
}
926931
}
927932

928933
var _ validator.List = &activeVersionValidator{}

internal/provider/template_resource_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,74 @@ func TestAccTemplateResourceAGPL(t *testing.T) {
648648
})
649649
}
650650

651+
func TestAccTemplateResourceVariables(t *testing.T) {
652+
cfg := `
653+
provider coderd {
654+
url = "%s"
655+
token = "%s"
656+
}
657+
658+
data "coderd_organization" "default" {
659+
is_default = true
660+
}
661+
662+
variable "CURRENT_WEEK" {
663+
default = "1"
664+
}
665+
666+
variable "PRIOR_WEEK" {
667+
default = "0"
668+
}
669+
670+
variable "YEAR" {
671+
default = "2021"
672+
}
673+
674+
variable "PRIOR_GIT_COMMIT_SHA" {
675+
default = "abcdef"
676+
}
677+
678+
variable "CURRENT_GIT_COMMIT_SHA" {
679+
default = "ghijkl"
680+
}
681+
682+
variable "ACTIVE" {
683+
default = true
684+
}
685+
686+
resource "coderd_template" "sample" {
687+
name = "example-template"
688+
versions = [
689+
{
690+
name = "${var.YEAR}_${var.PRIOR_WEEK}_${var.PRIOR_GIT_COMMIT_SHA}"
691+
directory = "../../integration/template-test/example-template"
692+
active = var.ACTIVE
693+
},
694+
{
695+
name = "${var.YEAR}_${var.CURRENT_WEEK}_${var.CURRENT_GIT_COMMIT_SHA}"
696+
directory = "../../integration/template-test/example-template"
697+
active = false
698+
}
699+
]
700+
}`
701+
702+
ctx := context.Background()
703+
client := integration.StartCoder(ctx, t, "template_acc", false)
704+
705+
cfg = fmt.Sprintf(cfg, client.URL.String(), client.SessionToken())
706+
707+
resource.Test(t, resource.TestCase{
708+
PreCheck: func() { testAccPreCheck(t) },
709+
IsUnitTest: true,
710+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
711+
Steps: []resource.TestStep{
712+
{
713+
Config: cfg,
714+
},
715+
},
716+
})
717+
}
718+
651719
type testAccTemplateResourceConfig struct {
652720
URL string
653721
Token string

0 commit comments

Comments
 (0)