@@ -2,7 +2,6 @@ package provider
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "net/url"
87 "os"
@@ -16,6 +15,7 @@ import (
1615 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1716 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1817 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
18+ "golang.org/x/xerrors"
1919)
2020
2121type config struct {
@@ -509,17 +509,24 @@ func populateIsNull(resourceData *schema.ResourceData) (result interface{}, err
509509 // The cty package reports type mismatches by panicking
510510 defer func () {
511511 if r := recover (); r != nil {
512- err = errors . New ( fmt . Sprintf ( "panic while handling coder_metadata: %#v" , r ) )
512+ err = xerrors . Errorf ( "panic while handling coder_metadata: %#v" , r )
513513 }
514514 }()
515515
516516 rawPlan := resourceData .GetRawPlan ()
517517 items := rawPlan .GetAttr ("item" ).AsValueSlice ()
518518
519519 var resultItems []interface {}
520+ itemKeys := map [string ]struct {}{}
520521 for _ , item := range items {
522+ key := valueAsString (item .GetAttr ("key" ))
523+ _ , exists := itemKeys [key ]
524+ if exists {
525+ return nil , xerrors .Errorf ("duplicate metadata key %q" , key )
526+ }
527+ itemKeys [key ] = struct {}{}
521528 resultItem := map [string ]interface {}{
522- "key" : valueAsString ( item . GetAttr ( " key" )) ,
529+ "key" : key ,
523530 "value" : valueAsString (item .GetAttr ("value" )),
524531 "sensitive" : valueAsBool (item .GetAttr ("sensitive" )),
525532 }
@@ -532,9 +539,10 @@ func populateIsNull(resourceData *schema.ResourceData) (result interface{}, err
532539 return resultItems , nil
533540}
534541
535- // valueAsString takes a cty.Value that may be a string or null, and converts it to either a Go string
542+ // valueAsString takes a cty.Value that may be a string or null, and converts it to a Go string,
543+ // which will be empty if the input value was null.
536544// or a nil interface{}
537- func valueAsString (value cty.Value ) interface {} {
545+ func valueAsString (value cty.Value ) string {
538546 if value .IsNull () {
539547 return ""
540548 }
0 commit comments