File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import (
1111 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
14+ "golang.org/x/xerrors"
1415)
1516
1617func agentResource () * schema.Resource {
@@ -38,6 +39,19 @@ func agentResource() *schema.Resource {
3839 return diag .FromErr (err )
3940 }
4041 }
42+
43+ rawPlan := resourceData .GetRawPlan ()
44+ items := rawPlan .GetAttr ("metadata" ).AsValueSlice ()
45+ itemKeys := map [string ]struct {}{}
46+ for _ , item := range items {
47+ key := valueAsString (item .GetAttr ("key" ))
48+ _ , exists := itemKeys [key ]
49+ if exists {
50+ return diag .FromErr (xerrors .Errorf ("duplicate agent metadata key %q" , key ))
51+ }
52+ itemKeys [key ] = struct {}{}
53+ }
54+
4155 return updateInitScript (resourceData , i )
4256 },
4357 ReadWithoutTimeout : func (ctx context.Context , resourceData * schema.ResourceData , i interface {}) diag.Diagnostics {
Original file line number Diff line number Diff line change @@ -250,6 +250,42 @@ func TestAgent_Metadata(t *testing.T) {
250250 })
251251}
252252
253+ func TestAgent_MetadataDuplicateKeys (t * testing.T ) {
254+ t .Parallel ()
255+ resource .Test (t , resource.TestCase {
256+ Providers : map [string ]* schema.Provider {
257+ "coder" : provider .New (),
258+ },
259+ IsUnitTest : true ,
260+ Steps : []resource.TestStep {{
261+ Config : `
262+ provider "coder" {
263+ url = "https://example.com"
264+ }
265+ resource "coder_agent" "dev" {
266+ os = "linux"
267+ arch = "amd64"
268+ metadata {
269+ key = "process_count"
270+ display_name = "Process Count"
271+ script = "ps aux | wc -l"
272+ interval = 5
273+ timeout = 1
274+ }
275+ metadata {
276+ key = "process_count"
277+ display_name = "Process Count"
278+ script = "ps aux | wc -l"
279+ interval = 5
280+ timeout = 1
281+ }
282+ }
283+ ` ,
284+ ExpectError : regexp .MustCompile ("duplicate agent metadata key" ),
285+ }},
286+ })
287+ }
288+
253289func TestAgent_DisplayApps (t * testing.T ) {
254290 t .Parallel ()
255291 t .Run ("OK" , func (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments