Skip to content

feat: include coder_parameters from external modules #8195

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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: 25 additions & 13 deletions provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package terraform

import (
"fmt"
"sort"
"strings"

"github.com/awalterschulze/gographviz"
Expand Down Expand Up @@ -723,29 +724,40 @@ func orderedRichParametersResources(tfResourcesRichParameters []*tfjson.StateRes
return tfResourcesRichParameters
}

ordered := make([]*tfjson.StateResource, len(orderedNames))
for i, name := range orderedNames {
var ordered []*tfjson.StateResource
for _, name := range orderedNames {
for _, resource := range tfResourcesRichParameters {
if resource.Name == name {
ordered[i] = resource
ordered = append(ordered, resource)
}
}
}

// There's an edge case possible for us to have a parameter name that isn't
// present in the state, since the ordered names come statically from
// parsing the Terraform file. We need to filter out the nil values if there
// are any present.
if len(tfResourcesRichParameters) != len(orderedNames) {
nonNil := make([]*tfjson.StateResource, 0, len(ordered))
for _, resource := range ordered {
if resource != nil {
nonNil = append(nonNil, resource)
// Edge case: a parameter is present in an external module (Git repository, static files, etc.),
// which can't be easily parsed to check the parameter order.
// Those parameters will be prepended to the "ordered" list.
var external []*tfjson.StateResource
for _, resource := range tfResourcesRichParameters {
for _, o := range ordered {
if resource.Name == o.Name {
goto next
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have anything in particular against the goto here, but continue outerLoop is likely to raise fewer eyebrows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hear you. I replaced goto with a simple boolean check.

}

ordered = nonNil
// This is external parameter.
external = append(external, resource)
next:
}

if len(external) > 0 {
sort.Slice(external, func(i, j int) bool {
return external[i].Name < external[j].Name
})

withExternal := make([]*tfjson.StateResource, 0, len(ordered)+len(external))
withExternal = append(withExternal, external...)
withExternal = append(withExternal, ordered...)
ordered = withExternal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified:

Suggested change
withExternal := make([]*tfjson.StateResource, 0, len(ordered)+len(external))
withExternal = append(withExternal, external...)
withExternal = append(withExternal, ordered...)
ordered = withExternal
ordered = append(external, ordered...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

}
return ordered
}
24 changes: 24 additions & 0 deletions provisioner/terraform/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,30 @@ func TestConvertResources(t *testing.T) {
}},
}},
parameters: []*proto.RichParameter{{
Name: "First parameter from child module",
Type: "string",
Description: "First parameter from child module",
Mutable: true,
DefaultValue: "abcdef",
}, {
Name: "Second parameter from child module",
Type: "string",
Description: "Second parameter from child module",
Mutable: true,
DefaultValue: "ghijkl",
}, {
Name: "First parameter from module",
Type: "string",
Description: "First parameter from module",
Mutable: true,
DefaultValue: "abcdef",
}, {
Name: "Second parameter from module",
Type: "string",
Description: "Second parameter from module",
Mutable: true,
DefaultValue: "ghijkl",
}, {
Name: "Example",
Type: "string",
Options: []*proto.RichParameterOption{{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.7.0"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.22"
}
}
}

data "coder_parameter" "child_first_parameter_from_module" {
name = "First parameter from child module"
mutable = true
type = "string"
description = "First parameter from child module"
default = "abcdef"
}

data "coder_parameter" "child_second_parameter_from_module" {
name = "Second parameter from child module"
mutable = true
type = "string"
description = "Second parameter from child module"
default = "ghijkl"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.7.0"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.22"
}
}
}

module "this_is_external_child_module" {
source = "./child-external-module"
}

data "coder_parameter" "first_parameter_from_module" {
name = "First parameter from module"
mutable = true
type = "string"
description = "First parameter from module"
default = "abcdef"
}

data "coder_parameter" "second_parameter_from_module" {
name = "Second parameter from module"
mutable = true
type = "string"
description = "Second parameter from module"
default = "ghijkl"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ terraform {
}
}

module "this_is_external_module" {
source = "./external-module"
}

data "coder_parameter" "sample" {
name = "Sample"
type = "string"
Expand Down

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

Loading