- Notifications
You must be signed in to change notification settings - Fork928
feat(provisioner): add support for presets to coder provisioners#16574
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
39a33ec
bump our dependency on the coder terraform provider to v2
SasSwart414bacf
add support for presets to coder provisioners
SasSwart75a5d49
upgrade to v2
SasSwart272eb51
add e2e tests
SasSwartb58e0b9
remove comments
SasSwart3f5e05e
Merge remote-tracking branch 'origin/main' into jjs/presets-provisioner
SasSwart7a1f91a
revert unwanted testdata generation
SasSwart7c4c7e5
review notes
SasSwartFile 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
2 changes: 0 additions & 2 deletionscoderd/presets_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 |
---|---|---|
@@ -15,8 +15,6 @@ import ( | ||
) | ||
func TestTemplateVersionPresets(t *testing.T) { | ||
t.Parallel() | ||
givenPreset := codersdk.Preset{ | ||
51 changes: 51 additions & 0 deletionscoderd/provisionerdserver/provisionerdserver.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
150 changes: 150 additions & 0 deletionscoderd/provisionerdserver/provisionerdserver_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
1 change: 1 addition & 0 deletionsprovisioner/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
79 changes: 78 additions & 1 deletionprovisioner/terraform/resources.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 |
---|---|---|
@@ -149,6 +149,7 @@ type resourceMetadataItem struct { | ||
type State struct { | ||
Resources []*proto.Resource | ||
Parameters []*proto.RichParameter | ||
Presets []*proto.Preset | ||
ExternalAuthProviders []*proto.ExternalAuthProviderResource | ||
} | ||
@@ -176,7 +177,7 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s | ||
// Extra array to preserve the order of rich parameters. | ||
tfResourcesRichParameters := make([]*tfjson.StateResource, 0) | ||
tfResourcesPresets := make([]*tfjson.StateResource, 0) | ||
var findTerraformResources func(mod *tfjson.StateModule) | ||
findTerraformResources = func(mod *tfjson.StateModule) { | ||
for _, module := range mod.ChildModules { | ||
@@ -186,6 +187,9 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s | ||
if resource.Type == "coder_parameter" { | ||
tfResourcesRichParameters = append(tfResourcesRichParameters, resource) | ||
} | ||
if resource.Type == "coder_workspace_preset" { | ||
tfResourcesPresets = append(tfResourcesPresets, resource) | ||
} | ||
label := convertAddressToLabel(resource.Address) | ||
if tfResourcesByLabel[label] == nil { | ||
@@ -775,6 +779,78 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s | ||
) | ||
} | ||
var duplicatedPresetNames []string | ||
presets := make([]*proto.Preset, 0) | ||
for _, resource := range tfResourcesPresets { | ||
var preset provider.WorkspacePreset | ||
err = mapstructure.Decode(resource.AttributeValues, &preset) | ||
if err != nil { | ||
return nil, xerrors.Errorf("decode preset attributes: %w", err) | ||
} | ||
var duplicatedPresetParameterNames []string | ||
var nonExistentParameters []string | ||
var presetParameters []*proto.PresetParameter | ||
for name, value := range preset.Parameters { | ||
presetParameter := &proto.PresetParameter{ | ||
Name: name, | ||
Value: value, | ||
} | ||
formattedName := fmt.Sprintf("%q", name) | ||
if !slice.Contains(duplicatedPresetParameterNames, formattedName) && | ||
slice.ContainsCompare(presetParameters, presetParameter, func(a, b *proto.PresetParameter) bool { | ||
return a.Name == b.Name | ||
}) { | ||
duplicatedPresetParameterNames = append(duplicatedPresetParameterNames, formattedName) | ||
} | ||
if !slice.ContainsCompare(parameters, &proto.RichParameter{Name: name}, func(a, b *proto.RichParameter) bool { | ||
return a.Name == b.Name | ||
}) { | ||
nonExistentParameters = append(nonExistentParameters, name) | ||
} | ||
presetParameters = append(presetParameters, presetParameter) | ||
} | ||
if len(duplicatedPresetParameterNames) > 0 { | ||
s := "" | ||
if len(duplicatedPresetParameterNames) == 1 { | ||
SasSwart marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
s = "s" | ||
} | ||
return nil, xerrors.Errorf( | ||
"coder_workspace_preset parameters must be unique but %s appear%s multiple times", stringutil.JoinWithConjunction(duplicatedPresetParameterNames), s, | ||
) | ||
} | ||
if len(nonExistentParameters) > 0 { | ||
logger.Warn( | ||
ctx, | ||
"coder_workspace_preset defines preset values for at least one parameter that is not defined by the template", | ||
slog.F("parameters", stringutil.JoinWithConjunction(nonExistentParameters)), | ||
) | ||
} | ||
protoPreset := &proto.Preset{ | ||
Name: preset.Name, | ||
Parameters: presetParameters, | ||
} | ||
if slice.Contains(duplicatedPresetNames, preset.Name) { | ||
duplicatedPresetNames = append(duplicatedPresetNames, preset.Name) | ||
} | ||
presets = append(presets, protoPreset) | ||
} | ||
if len(duplicatedPresetNames) > 0 { | ||
s := "" | ||
if len(duplicatedPresetNames) == 1 { | ||
s = "s" | ||
} | ||
return nil, xerrors.Errorf( | ||
"coder_workspace_preset names must be unique but %s appear%s multiple times", | ||
stringutil.JoinWithConjunction(duplicatedPresetNames), s, | ||
) | ||
} | ||
// A map is used to ensure we don't have duplicates! | ||
externalAuthProvidersMap := map[string]*proto.ExternalAuthProviderResource{} | ||
for _, tfResources := range tfResourcesByLabel { | ||
@@ -808,6 +884,7 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s | ||
return &State{ | ||
Resources: resources, | ||
Parameters: parameters, | ||
Presets: presets, | ||
ExternalAuthProviders: externalAuthProviders, | ||
}, nil | ||
} | ||
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.