File tree 2 files changed +50
-0
lines changed
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import (
11
11
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
14
+ "golang.org/x/xerrors"
14
15
)
15
16
16
17
func agentResource () * schema.Resource {
@@ -38,6 +39,19 @@ func agentResource() *schema.Resource {
38
39
return diag .FromErr (err )
39
40
}
40
41
}
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
+
41
55
return updateInitScript (resourceData , i )
42
56
},
43
57
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) {
250
250
})
251
251
}
252
252
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
+
253
289
func TestAgent_DisplayApps (t * testing.T ) {
254
290
t .Parallel ()
255
291
t .Run ("OK" , func (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments