Skip to content

feat: Expose managed variables via API #6134

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 44 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
58fc371
WIP
mtojek Feb 9, 2023
84f6fe5
hcl
mtojek Feb 9, 2023
b29ab11
useManagedVariables
mtojek Feb 9, 2023
2124c54
fix
mtojek Feb 9, 2023
9f89e17
Fix
mtojek Feb 9, 2023
fc583ae
Fix
mtojek Feb 9, 2023
86a1c1c
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 9, 2023
183fdb7
fix
mtojek Feb 9, 2023
3e9dd7e
go:build
mtojek Feb 9, 2023
42f8d41
Fix
mtojek Feb 9, 2023
c194087
fix: bool flag
mtojek Feb 10, 2023
f84fbf6
Insert template variables
mtojek Feb 10, 2023
33e75d9
API
mtojek Feb 10, 2023
85bc72f
fix
mtojek Feb 10, 2023
99c5fde
Expose via API
mtojek Feb 10, 2023
e4ee7f9
More wiring
mtojek Feb 10, 2023
2a60174
CLI for testing purposes
mtojek Feb 10, 2023
ffb0b94
WIP
mtojek Feb 13, 2023
c0ba41b
Delete FIXME
mtojek Feb 13, 2023
9e23196
planVars
mtojek Feb 13, 2023
80fdf07
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 13, 2023
3a510ea
WIP
mtojek Feb 13, 2023
72f76b1
WIP
mtojek Feb 13, 2023
78d7252
UserVariableValues
mtojek Feb 13, 2023
845dd92
no dry run
mtojek Feb 13, 2023
e021e52
Dry run
mtojek Feb 13, 2023
54dc685
Done FIXME
mtojek Feb 13, 2023
81490a2
Fix
mtojek Feb 14, 2023
6aa97ab
Fix: CLI
mtojek Feb 14, 2023
920bf7c
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 14, 2023
56f08f9
Fix: migration
mtojek Feb 14, 2023
2c57c96
API tests
mtojek Feb 14, 2023
bc18624
Test info
mtojek Feb 14, 2023
ae6f072
Tests
mtojek Feb 14, 2023
f9b4349
More tests
mtojek Feb 14, 2023
af4adec
fix: lint
mtojek Feb 14, 2023
864052c
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 14, 2023
4bac517
Fix: authz
mtojek Feb 14, 2023
930624a
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 14, 2023
23226fa
Address PR comments
mtojek Feb 15, 2023
ca23476
Fix
mtojek Feb 15, 2023
953b9a7
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 15, 2023
bb8b500
fix
mtojek Feb 15, 2023
78ac8f5
fix
mtojek Feb 15, 2023
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
21 changes: 15 additions & 6 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type createValidTemplateVersionArgs struct {
Provisioner database.ProvisionerType
FileID uuid.UUID
ParameterFile string
ValuesFile string
// Template is only required if updating a template's active version.
Template *codersdk.Template
// ReuseParameters will attempt to reuse params from the Template field
Expand All @@ -145,13 +146,21 @@ type createValidTemplateVersionArgs struct {
func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVersionArgs, parameters ...codersdk.CreateParameterRequest) (*codersdk.TemplateVersion, []codersdk.CreateParameterRequest, error) {
client := args.Client

// FIXME(mtojek): I will iterate on CLI experience in the follow-up.
// see: https://github.com/coder/coder/issues/5980
variableValues, err := loadVariableValues(args.ValuesFile)
if err != nil {
return nil, nil, err
}

req := codersdk.CreateTemplateVersionRequest{
Name: args.Name,
StorageMethod: codersdk.ProvisionerStorageMethodFile,
FileID: args.FileID,
Provisioner: codersdk.ProvisionerType(args.Provisioner),
ParameterValues: parameters,
ProvisionerTags: args.ProvisionerTags,
Name: args.Name,
StorageMethod: codersdk.ProvisionerStorageMethodFile,
FileID: args.FileID,
Provisioner: codersdk.ProvisionerType(args.Provisioner),
ParameterValues: parameters,
ProvisionerTags: args.ProvisionerTags,
UserVariableValues: variableValues,
}
if args.Template != nil {
req.TemplateID = args.Template.ID
Expand Down
3 changes: 3 additions & 0 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func templatePush() *cobra.Command {
versionName string
provisioner string
parameterFile string
valuesFile string
alwaysPrompt bool
provisionerTags []string
uploadFlags templateUploadFlags
Expand Down Expand Up @@ -139,6 +140,7 @@ func templatePush() *cobra.Command {
Provisioner: database.ProvisionerType(provisioner),
FileID: resp.ID,
ParameterFile: parameterFile,
ValuesFile: valuesFile,
Template: &template,
ReuseParameters: !alwaysPrompt,
ProvisionerTags: tags,
Expand All @@ -165,6 +167,7 @@ func templatePush() *cobra.Command {

cmd.Flags().StringVarP(&provisioner, "test.provisioner", "", "terraform", "Customize the provisioner backend")
cmd.Flags().StringVarP(&parameterFile, "parameter-file", "", "", "Specify a file path with parameter values.")
cmd.Flags().StringVarP(&valuesFile, "values-file", "", "", "Specify a file path with values for managed variables.")
cmd.Flags().StringVarP(&versionName, "name", "", "", "Specify a name for the new template version. It will be automatically generated if not provided.")
cmd.Flags().StringArrayVarP(&provisionerTags, "provisioner-tag", "", []string{}, "Specify a set of tags to target provisioner daemons.")
cmd.Flags().BoolVar(&alwaysPrompt, "always-prompt", false, "Always prompt all parameters. Does not pull parameter values from active template version")
Expand Down
50 changes: 50 additions & 0 deletions cli/templatevariables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli

import (
"os"

"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

"github.com/coder/coder/codersdk"
)

func loadVariableValues(variablesFile string) ([]codersdk.VariableValue, error) {
var values []codersdk.VariableValue
if variablesFile == "" {
return values, nil
}

variablesMap, err := createVariablesMapFromFile(variablesFile)
if err != nil {
return nil, err
}

for name, value := range variablesMap {
values = append(values, codersdk.VariableValue{
Name: name,
Value: value,
})
}
return values, nil
}

// Reads a YAML file and populates a string -> string map.
// Throws an error if the file name is empty.
func createVariablesMapFromFile(variablesFile string) (map[string]string, error) {
if variablesFile == "" {
return nil, xerrors.Errorf("variable file name is not specified")
}

variablesMap := make(map[string]string)
variablesFileContents, err := os.ReadFile(variablesFile)
if err != nil {
return nil, err
}

err = yaml.Unmarshal(variablesFileContents, &variablesMap)
if err != nil {
return nil, err
}
return variablesMap, nil
}
1 change: 1 addition & 0 deletions cli/testdata/coder_templates_push_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Flags:
automatically generated if not provided.
--parameter-file string Specify a file path with parameter values.
--provisioner-tag stringArray Specify a set of tags to target provisioner daemons.
--values-file string Specify a file path with values for managed variables.
-y, --yes Bypass prompts

Global Flags:
Expand Down
86 changes: 86 additions & 0 deletions coderd/apidoc/docs.go

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

78 changes: 78 additions & 0 deletions coderd/apidoc/swagger.json

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

7 changes: 3 additions & 4 deletions coderd/autobuild/executor/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/coder/coder/coderd/autobuild/schedule"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/provisionerdserver"
)

// Executor automatically starts or stops workspaces.
Expand Down Expand Up @@ -249,10 +250,8 @@ func build(ctx context.Context, store database.Store, workspace database.Workspa
// This must happen in a transaction to ensure history can be inserted, and
// the prior history can update it's "after" column to point at the new.
workspaceBuildID := uuid.New()
input, err := json.Marshal(struct {
WorkspaceBuildID string `json:"workspace_build_id"`
}{
WorkspaceBuildID: workspaceBuildID.String(),
input, err := json.Marshal(provisionerdserver.WorkspaceProvisionJob{
WorkspaceBuildID: workspaceBuildID,
})
if err != nil {
return xerrors.Errorf("marshal provision job: %w", err)
Expand Down
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ func New(options *Options) *API {
r.Get("/schema", api.templateVersionSchema)
r.Get("/parameters", api.templateVersionParameters)
r.Get("/rich-parameters", api.templateVersionRichParameters)
r.Get("/variables", api.templateVersionVariables)
r.Get("/resources", api.templateVersionResources)
r.Get("/logs", api.templateVersionLogs)
r.Route("/dry-run", func(r chi.Router) {
Expand Down
12 changes: 9 additions & 3 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,17 +522,23 @@ func createAnotherUserRetry(t *testing.T, client *codersdk.Client, organizationI
// CreateTemplateVersion creates a template import provisioner job
// with the responses provided. It uses the "echo" provisioner for compatibility
// with testing.
func CreateTemplateVersion(t *testing.T, client *codersdk.Client, organizationID uuid.UUID, res *echo.Responses) codersdk.TemplateVersion {
func CreateTemplateVersion(t *testing.T, client *codersdk.Client, organizationID uuid.UUID, res *echo.Responses, mutators ...func(*codersdk.CreateTemplateVersionRequest)) codersdk.TemplateVersion {
t.Helper()
data, err := echo.Tar(res)
require.NoError(t, err)
file, err := client.Upload(context.Background(), codersdk.ContentTypeTar, bytes.NewReader(data))
require.NoError(t, err)
templateVersion, err := client.CreateTemplateVersion(context.Background(), organizationID, codersdk.CreateTemplateVersionRequest{

req := codersdk.CreateTemplateVersionRequest{
FileID: file.ID,
StorageMethod: codersdk.ProvisionerStorageMethodFile,
Provisioner: codersdk.ProvisionerTypeEcho,
})
}
for _, mut := range mutators {
mut(&req)
}

templateVersion, err := client.CreateTemplateVersion(context.Background(), organizationID, req)
require.NoError(t, err)
return templateVersion
}
Expand Down
Loading