@@ -43,18 +43,6 @@ func agentResource() *schema.Resource {
4343 }
4444 }
4545
46- rawPlan := resourceData .GetRawPlan ()
47- items := rawPlan .GetAttr ("metadata" ).AsValueSlice ()
48- itemKeys := map [string ]struct {}{}
49- for _ , item := range items {
50- key := valueAsString (item .GetAttr ("key" ))
51- _ , exists := itemKeys [key ]
52- if exists {
53- return diag .FromErr (xerrors .Errorf ("duplicate agent metadata key %q" , key ))
54- }
55- itemKeys [key ] = struct {}{}
56- }
57-
5846 return updateInitScript (resourceData , i )
5947 },
6048 ReadWithoutTimeout : func (ctx context.Context , resourceData * schema.ResourceData , i interface {}) diag.Diagnostics {
@@ -272,6 +260,32 @@ func agentResource() *schema.Resource {
272260 Optional : true ,
273261 },
274262 },
263+ CustomizeDiff : func (ctx context.Context , rd * schema.ResourceDiff , i any ) error {
264+ if ! rd .HasChange ("metadata" ) {
265+ return nil
266+ }
267+
268+ keys := map [string ]bool {}
269+ metadata , ok := rd .Get ("metadata" ).([]any )
270+ if ! ok {
271+ return xerrors .Errorf ("unexpected type %T for metadata, expected []any" , rd .Get ("metadata" ))
272+ }
273+ for _ , t := range metadata {
274+ obj , ok := t .(map [string ]any )
275+ if ! ok {
276+ return xerrors .Errorf ("unexpected type %T for metadata, expected map[string]any" , t )
277+ }
278+ key , ok := obj ["key" ].(string )
279+ if ! ok {
280+ return xerrors .Errorf ("unexpected type %T for metadata key, expected string" , obj ["key" ])
281+ }
282+ if keys [key ] {
283+ return xerrors .Errorf ("duplicate agent metadata key %q" , key )
284+ }
285+ keys [key ] = true
286+ }
287+ return nil
288+ },
275289 }
276290}
277291
0 commit comments