Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
resourceIcon := map[string]string{}
resourceCost := map[string]int32{}

metadataTargetLabels := map[string]bool{}
for _, resources := range tfResourcesByLabel {
for _, resource := range resources {
if resource.Type != "coder_metadata" {
Expand All @@ -396,7 +397,6 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
if err != nil {
return nil, xerrors.Errorf("decode metadata attributes: %w", err)
}

resourceLabel := convertAddressToLabel(resource.Address)

var attachedNode *gographviz.Node
Expand Down Expand Up @@ -433,6 +433,11 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
}
targetLabel := attachedResource.Label

if metadataTargetLabels[targetLabel] {
return nil, xerrors.Errorf("duplicate metadata resource: %s", targetLabel)
}
metadataTargetLabels[targetLabel] = true

resourceHidden[targetLabel] = attrs.Hide
resourceIcon[targetLabel] = attrs.Icon
resourceCost[targetLabel] = attrs.DailyCost
Expand Down
19 changes: 19 additions & 0 deletions provisioner/terraform/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,25 @@ func TestAppSlugValidation(t *testing.T) {
require.ErrorContains(t, err, "duplicate app slug")
}

func TestMetadataResourceDuplicate(t *testing.T) {
t.Parallel()

// Load the multiple-apps state file and edit it.
dir := filepath.Join("testdata", "resource-metadata-duplicate")
tfPlanRaw, err := os.ReadFile(filepath.Join(dir, "resource-metadata-duplicate.tfplan.json"))
require.NoError(t, err)
var tfPlan tfjson.Plan
err = json.Unmarshal(tfPlanRaw, &tfPlan)
require.NoError(t, err)
tfPlanGraph, err := os.ReadFile(filepath.Join(dir, "resource-metadata-duplicate.tfplan.dot"))
require.NoError(t, err)

state, err := terraform.ConvertState([]*tfjson.StateModule{tfPlan.PlannedValues.RootModule}, string(tfPlanGraph))
require.Nil(t, state)
require.Error(t, err)
require.ErrorContains(t, err, "duplicate metadata resource: null_resource.about")
}

func TestParameterValidation(t *testing.T) {
t.Parallel()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.9.0"
}
}
}

resource "coder_agent" "main" {
os = "linux"
arch = "amd64"
metadata {
key = "process_count"
display_name = "Process Count"
script = "ps -ef | wc -l"
interval = 5
timeout = 1
}
}

resource "null_resource" "about" {
depends_on = [
coder_agent.main,
]
}

resource "coder_metadata" "about_info" {
resource_id = null_resource.about.id
hide = true
icon = "/icon/server.svg"
daily_cost = 29
item {
key = "hello"
value = "world"
}
item {
key = "null"
}
}

resource "coder_metadata" "other_info" {
resource_id = null_resource.about.id
hide = true
icon = "/icon/server.svg"
daily_cost = 20
item {
key = "hello"
value = "world"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading