- Notifications
You must be signed in to change notification settings - Fork925
feat: load terraform modules when using dynamic parameters#17714
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
+374 −32
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletionscoderd/files/overlay.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package files | ||
import ( | ||
"io/fs" | ||
"path" | ||
"strings" | ||
"golang.org/x/xerrors" | ||
) | ||
// overlayFS allows you to "join" together the template files tar file fs.FS | ||
// with the Terraform modules tar file fs.FS. We could potentially turn this | ||
// into something more parameterized/configurable, but the requirements here are | ||
// a _bit_ odd, because every file in the modulesFS includes the | ||
// .terraform/modules/ folder at the beginning of it's path. | ||
type overlayFS struct { | ||
baseFS fs.FS | ||
overlays []Overlay | ||
} | ||
type Overlay struct { | ||
Path string | ||
fs.FS | ||
} | ||
func NewOverlayFS(baseFS fs.FS, overlays []Overlay) (fs.FS, error) { | ||
if err := valid(baseFS); err != nil { | ||
return nil, xerrors.Errorf("baseFS: %w", err) | ||
} | ||
for _, overlay := range overlays { | ||
if err := valid(overlay.FS); err != nil { | ||
return nil, xerrors.Errorf("overlayFS: %w", err) | ||
} | ||
} | ||
return overlayFS{ | ||
baseFS: baseFS, | ||
overlays: overlays, | ||
}, nil | ||
} | ||
func (f overlayFS) Open(p string) (fs.File, error) { | ||
for _, overlay := range f.overlays { | ||
if strings.HasPrefix(path.Clean(p), overlay.Path) { | ||
return overlay.FS.Open(p) | ||
} | ||
} | ||
return f.baseFS.Open(p) | ||
} | ||
func (f overlayFS) ReadDir(p string) ([]fs.DirEntry, error) { | ||
for _, overlay := range f.overlays { | ||
if strings.HasPrefix(path.Clean(p), overlay.Path) { | ||
//nolint:forcetypeassert | ||
return overlay.FS.(fs.ReadDirFS).ReadDir(p) | ||
} | ||
} | ||
//nolint:forcetypeassert | ||
return f.baseFS.(fs.ReadDirFS).ReadDir(p) | ||
} | ||
func (f overlayFS) ReadFile(p string) ([]byte, error) { | ||
for _, overlay := range f.overlays { | ||
if strings.HasPrefix(path.Clean(p), overlay.Path) { | ||
//nolint:forcetypeassert | ||
return overlay.FS.(fs.ReadFileFS).ReadFile(p) | ||
} | ||
} | ||
//nolint:forcetypeassert | ||
return f.baseFS.(fs.ReadFileFS).ReadFile(p) | ||
} | ||
// valid checks that the fs.FS implements the required interfaces. | ||
// The fs.FS interface is not sufficient. | ||
func valid(fsys fs.FS) error { | ||
_, ok := fsys.(fs.ReadDirFS) | ||
if !ok { | ||
return xerrors.New("overlayFS does not implement ReadDirFS") | ||
} | ||
_, ok = fsys.(fs.ReadFileFS) | ||
if !ok { | ||
return xerrors.New("overlayFS does not implement ReadFileFS") | ||
} | ||
return nil | ||
} |
44 changes: 44 additions & 0 deletionscoderd/files/overlay_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package files_test | ||
import ( | ||
"io/fs" | ||
"testing" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/v2/coderd/files" | ||
) | ||
func TestOverlayFS(t *testing.T) { | ||
t.Parallel() | ||
a := afero.NewMemMapFs() | ||
afero.WriteFile(a, "main.tf", []byte("terraform {}"), 0o644) | ||
afero.WriteFile(a, ".terraform/modules/example_module/main.tf", []byte("inaccessible"), 0o644) | ||
afero.WriteFile(a, ".terraform/modules/other_module/main.tf", []byte("inaccessible"), 0o644) | ||
b := afero.NewMemMapFs() | ||
afero.WriteFile(b, ".terraform/modules/modules.json", []byte("{}"), 0o644) | ||
afero.WriteFile(b, ".terraform/modules/example_module/main.tf", []byte("terraform {}"), 0o644) | ||
it, err := files.NewOverlayFS(afero.NewIOFS(a), []files.Overlay{{ | ||
Path: ".terraform/modules", | ||
FS: afero.NewIOFS(b), | ||
}}) | ||
require.NoError(t, err) | ||
content, err := fs.ReadFile(it, "main.tf") | ||
require.NoError(t, err) | ||
require.Equal(t, "terraform {}", string(content)) | ||
_, err = fs.ReadFile(it, ".terraform/modules/other_module/main.tf") | ||
require.Error(t, err) | ||
content, err = fs.ReadFile(it, ".terraform/modules/modules.json") | ||
require.NoError(t, err) | ||
require.Equal(t, "{}", string(content)) | ||
content, err = fs.ReadFile(it, ".terraform/modules/example_module/main.tf") | ||
require.NoError(t, err) | ||
require.Equal(t, "terraform {}", string(content)) | ||
} |
27 changes: 24 additions & 3 deletionscoderd/parameters.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletionscoderd/parameters_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletionscoderd/testdata/parameters/modules/.terraform/modules/jetbrains_gateway/main.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
terraform { | ||
required_version = ">= 1.0" | ||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
version = ">= 0.17" | ||
} | ||
} | ||
} | ||
locals { | ||
jetbrains_ides = { | ||
"GO" = { | ||
icon = "/icon/goland.svg", | ||
name = "GoLand", | ||
identifier = "GO", | ||
}, | ||
"WS" = { | ||
icon = "/icon/webstorm.svg", | ||
name = "WebStorm", | ||
identifier = "WS", | ||
}, | ||
"IU" = { | ||
icon = "/icon/intellij.svg", | ||
name = "IntelliJ IDEA Ultimate", | ||
identifier = "IU", | ||
}, | ||
"PY" = { | ||
icon = "/icon/pycharm.svg", | ||
name = "PyCharm Professional", | ||
identifier = "PY", | ||
}, | ||
"CL" = { | ||
icon = "/icon/clion.svg", | ||
name = "CLion", | ||
identifier = "CL", | ||
}, | ||
"PS" = { | ||
icon = "/icon/phpstorm.svg", | ||
name = "PhpStorm", | ||
identifier = "PS", | ||
}, | ||
"RM" = { | ||
icon = "/icon/rubymine.svg", | ||
name = "RubyMine", | ||
identifier = "RM", | ||
}, | ||
"RD" = { | ||
icon = "/icon/rider.svg", | ||
name = "Rider", | ||
identifier = "RD", | ||
}, | ||
"RR" = { | ||
icon = "/icon/rustrover.svg", | ||
name = "RustRover", | ||
identifier = "RR" | ||
} | ||
} | ||
icon = local.jetbrains_ides[data.coder_parameter.jetbrains_ide.value].icon | ||
display_name = local.jetbrains_ides[data.coder_parameter.jetbrains_ide.value].name | ||
identifier = data.coder_parameter.jetbrains_ide.value | ||
} | ||
data "coder_parameter" "jetbrains_ide" { | ||
type = "string" | ||
name = "jetbrains_ide" | ||
display_name = "JetBrains IDE" | ||
icon = "/icon/gateway.svg" | ||
mutable = true | ||
default = sort(keys(local.jetbrains_ides))[0] | ||
dynamic "option" { | ||
for_each = local.jetbrains_ides | ||
content { | ||
icon = option.value.icon | ||
name = option.value.name | ||
value = option.key | ||
} | ||
} | ||
} | ||
output "identifier" { | ||
value = local.identifier | ||
} | ||
output "display_name" { | ||
value = local.display_name | ||
} | ||
output "icon" { | ||
value = local.icon | ||
} |
1 change: 1 addition & 0 deletionscoderd/testdata/parameters/modules/.terraform/modules/modules.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"jetbrains_gateway","Source":"jetbrains_gateway","Dir":".terraform/modules/jetbrains_gateway"}]} |
5 changes: 5 additions & 0 deletionscoderd/testdata/parameters/modules/main.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
terraform {} | ||
module "jetbrains_gateway" { | ||
source = "jetbrains_gateway" | ||
} |
2 changes: 1 addition & 1 deletionprovisioner/terraform/executor.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.