Skip to content

fix: add error detail for duplicate agent metadata key #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions provider/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"golang.org/x/xerrors"
)

func agentResource() *schema.Resource {
Expand Down Expand Up @@ -38,6 +39,19 @@ func agentResource() *schema.Resource {
return diag.FromErr(err)
}
}

rawPlan := resourceData.GetRawPlan()
items := rawPlan.GetAttr("metadata").AsValueSlice()
itemKeys := map[string]struct{}{}
for _, item := range items {
key := valueAsString(item.GetAttr("key"))
_, exists := itemKeys[key]
if exists {
return diag.FromErr(xerrors.Errorf("duplicate agent metadata key %q", key))
}
itemKeys[key] = struct{}{}
}

return updateInitScript(resourceData, i)
},
ReadWithoutTimeout: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down
36 changes: 36 additions & 0 deletions provider/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,42 @@ func TestAgent_Metadata(t *testing.T) {
})
}

func TestAgent_MetadataDuplicateKeys(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
url = "https://example.com"
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
metadata {
key = "process_count"
display_name = "Process Count"
script = "ps aux | wc -l"
interval = 5
timeout = 1
}
metadata {
key = "process_count"
display_name = "Process Count"
script = "ps aux | wc -l"
interval = 5
timeout = 1
}
}
`,
ExpectError: regexp.MustCompile("duplicate agent metadata key"),
}},
})
}

func TestAgent_DisplayApps(t *testing.T) {
t.Parallel()
t.Run("OK", func(t *testing.T) {
Expand Down