Skip to content

Commit a533bc8

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

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

internal/provider/template_resource.go

Lines changed: 18 additions & 13 deletions
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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,62 @@ 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 "PRIOR_GIT_COMMIT_SHA" {
663+
default = "abcdef"
664+
}
665+
666+
variable "CURRENT_GIT_COMMIT_SHA" {
667+
default = "ghijkl"
668+
}
669+
670+
variable "ACTIVE" {
671+
default = true
672+
}
673+
674+
resource "coderd_template" "sample" {
675+
name = "example-template"
676+
versions = [
677+
{
678+
name = "${var.PRIOR_GIT_COMMIT_SHA}"
679+
directory = "../../integration/template-test/example-template"
680+
active = var.ACTIVE
681+
},
682+
{
683+
name = "${var.CURRENT_GIT_COMMIT_SHA}"
684+
directory = "../../integration/template-test/example-template"
685+
active = false
686+
}
687+
]
688+
}`
689+
690+
ctx := context.Background()
691+
client := integration.StartCoder(ctx, t, "template_acc", false)
692+
693+
cfg = fmt.Sprintf(cfg, client.URL.String(), client.SessionToken())
694+
695+
resource.Test(t, resource.TestCase{
696+
PreCheck: func() { testAccPreCheck(t) },
697+
IsUnitTest: true,
698+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
699+
Steps: []resource.TestStep{
700+
{
701+
Config: cfg,
702+
},
703+
},
704+
})
705+
}
706+
651707
type testAccTemplateResourceConfig struct {
652708
URL string
653709
Token string

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy