Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: validate agent & resource metadata keys during plan#277

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
38 changes: 26 additions & 12 deletionsprovider/agent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,18 +43,6 @@ func agentResource() *schema.Resource {
}
}

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 DownExpand Up@@ -272,6 +260,32 @@ func agentResource() *schema.Resource {
Optional: true,
},
},
CustomizeDiff: func(ctx context.Context, rd *schema.ResourceDiff, i any) error {
if !rd.HasChange("metadata") {
return nil
}

keys := map[string]bool{}
metadata, ok := rd.Get("metadata").([]any)
if !ok {
return xerrors.Errorf("unexpected type %T for metadata, expected []any", rd.Get("metadata"))
}
for _, t := range metadata {
obj, ok := t.(map[string]any)
if !ok {
return xerrors.Errorf("unexpected type %T for metadata, expected map[string]any", t)
}
key, ok := obj["key"].(string)
if !ok {
return xerrors.Errorf("unexpected type %T for metadata key, expected string", obj["key"])
}
if keys[key] {
return xerrors.Errorf("duplicate agent metadata key %q", key)
}
keys[key] = true
}
return nil
},
}
}

Expand Down
7 changes: 4 additions & 3 deletionsprovider/agent_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -254,6 +254,7 @@ func TestAgent_MetadataDuplicateKeys(t *testing.T) {
}
`,
ExpectError: regexp.MustCompile("duplicate agent metadata key"),
PlanOnly: true,
}},
})
}
Expand DownExpand Up@@ -281,7 +282,7 @@ func TestAgent_DisplayApps(t *testing.T) {
web_terminal = false
port_forwarding_helper = false
ssh_helper = false
}
}
}
`,
Check: func(state *terraform.State) error {
Expand DownExpand Up@@ -331,7 +332,7 @@ func TestAgent_DisplayApps(t *testing.T) {
display_apps {
vscode_insiders = true
web_terminal = true
}
}
}
`,
Check: func(state *terraform.State) error {
Expand DownExpand Up@@ -426,7 +427,7 @@ func TestAgent_DisplayApps(t *testing.T) {
web_terminal = false
port_forwarding_helper = false
ssh_helper = false
}
}
}
`,
ExpectError: regexp.MustCompile(`An argument named "fake_app" is not expected here.`),
Expand Down
27 changes: 27 additions & 0 deletionsprovider/metadata.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/xerrors"
)

func metadataResource() *schema.Resource {
Expand DownExpand Up@@ -111,5 +112,31 @@ func metadataResource() *schema.Resource {
},
},
},
CustomizeDiff: func(ctx context.Context, rd *schema.ResourceDiff, i interface{}) error {
if !rd.HasChange("item") {
return nil
}

keys := map[string]bool{}
metadata, ok := rd.Get("item").([]any)
if !ok {
return xerrors.Errorf("unexpected type %T for items, expected []any", rd.Get("metadata"))
}
for _, t := range metadata {
obj, ok := t.(map[string]any)
if !ok {
return xerrors.Errorf("unexpected type %T for item, expected map[string]any", t)
}
key, ok := obj["key"].(string)
if !ok {
return xerrors.Errorf("unexpected type %T for items 'key' attribute, expected string", obj["key"])
}
if keys[key] {
return xerrors.Errorf("duplicate resource metadata key %q", key)
}
keys[key] = true
}
return nil
},
}
}
3 changes: 2 additions & 1 deletionprovider/metadata_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -123,7 +123,8 @@ func TestMetadataDuplicateKeys(t *testing.T) {
}
}
`,
ExpectError: regexp.MustCompile("duplicate metadata key"),
PlanOnly: true,
ExpectError: regexp.MustCompile("duplicate resource metadata key"),
}},
})
}
6 changes: 0 additions & 6 deletionsprovider/provider.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,14 +97,8 @@ func populateIsNull(resourceData *schema.ResourceData) (result interface{}, err
items := rawPlan.GetAttr("item").AsValueSlice()

var resultItems []interface{}
itemKeys := map[string]struct{}{}
for _, item := range items {
key := valueAsString(item.GetAttr("key"))
_, exists := itemKeys[key]
if exists {
return nil, xerrors.Errorf("duplicate metadata key %q", key)
}
itemKeys[key] = struct{}{}
resultItem := map[string]interface{}{
"key": key,
"value": valueAsString(item.GetAttr("value")),
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp