Skip to content

feat: Update CLI to handle managed variables #6220

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 63 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
63 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
3b54e52
CLI: create
mtojek Feb 15, 2023
a3cc673
unit tests: create templates with variables
mtojek Feb 15, 2023
0ff8cd5
Use last variables
mtojek Feb 15, 2023
a9a5262
Merge branch 'main' into 5980-cli
mtojek Feb 15, 2023
2c0d885
Fix
mtojek Feb 15, 2023
84a6048
Fix
mtojek Feb 15, 2023
c10a755
Fix
mtojek Feb 15, 2023
3662fae
Push tests
mtojek Feb 16, 2023
9d85768
fix: variable is required if Default is nil
mtojek Feb 16, 2023
3dd894a
WIP
mtojek Feb 16, 2023
6c10c98
Redact sensitive values
mtojek Feb 16, 2023
d107c83
Fixes
mtojek Feb 16, 2023
b466aab
Fixes
mtojek Feb 16, 2023
c82da7f
Fix: arg description
mtojek Feb 16, 2023
1803d53
Fix
mtojek Feb 16, 2023
f16103d
Variable param
mtojek Feb 16, 2023
8771ecb
Fix: gen
mtojek Feb 16, 2023
33a173a
Fix
mtojek Feb 16, 2023
c1076db
Fix: goldens
mtojek Feb 16, 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
Prev Previous commit
Next Next commit
More wiring
  • Loading branch information
mtojek committed Feb 10, 2023
commit e4ee7f93ccff9e96e56899823f49384fe2c6eaac
23 changes: 21 additions & 2 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,22 @@ func (server *Server) UpdateJob(ctx context.Context, request *proto.UpdateJobReq
return nil, xerrors.Errorf("get template version by job id: %w", err)
}

var variableValues []*sdkproto.VariableValue
for _, templateVariable := range request.TemplateVariables {
server.Logger.Info(ctx, "insert template variable", slog.F("template_version_id", templateVersion.ID), slog.F("template_variable", templateVariable))

var value = templateVariable.DefaultValue
for _, v := range request.VariableValues {
if v.Name == templateVariable.Name {
value = v.Value
variableValues = append(variableValues, &sdkproto.VariableValue{
Name: v.Name,
Value: v.Value,
})
break
}
}

_, err = server.Database.InsertTemplateVersionVariable(ctx, database.InsertTemplateVersionVariableParams{
TemplateVersionID: templateVersion.ID,
Name: templateVariable.Name,
Expand All @@ -396,12 +409,17 @@ func (server *Server) UpdateJob(ctx context.Context, request *proto.UpdateJobReq
DefaultValue: templateVariable.DefaultValue,
Required: templateVariable.Required,
Sensitive: templateVariable.Sensitive,
// FIXME value
Value: value,
})
if err != nil {
return nil, xerrors.Errorf("insert parameter schema: %w", err)
}
}

return &proto.UpdateJobResponse{
Canceled: job.CanceledAt.Valid,
VariableValues: variableValues,
}, nil
}

if len(request.ParameterSchemas) > 0 {
Expand Down Expand Up @@ -1216,7 +1234,8 @@ func auditActionFromTransition(transition database.WorkspaceTransition) database
}

type TemplateVersionImportJob struct {
TemplateVersionID uuid.UUID `json:"template_version_id"`
TemplateVersionID uuid.UUID `json:"template_version_id"`
VariableValues []codersdk.VariableValue `json:"variable_values"`
}

// WorkspaceProvisionJob is the payload for the "workspace_provision" job type.
Expand Down
1 change: 1 addition & 0 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,7 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
templateVersionID := uuid.New()
jobInput, err := json.Marshal(provisionerdserver.TemplateVersionImportJob{
TemplateVersionID: templateVersionID,
VariableValues: req.VariableValues,
})
if err != nil {
return xerrors.Errorf("marshal job input: %w", err)
Expand Down
7 changes: 7 additions & 0 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ type CreateTemplateVersionRequest struct {
// ParameterValues allows for additional parameters to be provided
// during the dry-run provision stage.
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`

VariableValues []VariableValue `json:"variable_values,omitempty"`
}

type VariableValue struct {
Name string `json:"name"`
Value string `json:"value"`
}

// CreateTemplateRequest provides options when creating a template.
Expand Down
7 changes: 5 additions & 2 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (s *server) Provision(stream proto.DRPCProvisioner_ProvisionStream) error {
}
s.logger.Debug(ctx, "ran initialization")

env, err := provisionEnv(config, request.GetPlan().GetParameterValues(), request.GetPlan().GetRichParameterValues())
env, err := provisionEnv(config, request.GetPlan().GetParameterValues(), request.GetPlan().GetVariableValues(), request.GetPlan().GetRichParameterValues())
if err != nil {
return err
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func planVars(plan *proto.Provision_Plan) ([]string, error) {
return vars, nil
}

func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue, richParams []*proto.RichParameterValue) ([]string, error) {
func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue, variableValues []*proto.VariableValue, richParams []*proto.RichParameterValue) ([]string, error) {
env := safeEnviron()
env = append(env,
"CODER_AGENT_URL="+config.Metadata.CoderUrl,
Expand All @@ -229,6 +229,9 @@ func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue
return nil, xerrors.Errorf("unsupported parameter type %q for %q", param.DestinationScheme, param.Name)
}
}
for _, variable := range variableValues {
env = append(env, fmt.Sprintf("%s=%s", variable.Name, variable.Value))
}
for _, param := range richParams {
env = append(env, provider.ParameterEnvironmentVariable(param.Name)+"="+param.Value)
}
Expand Down
448 changes: 243 additions & 205 deletions provisionerd/proto/provisionerd.pb.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion provisionerd/proto/provisionerd.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ message AcquiredJob {
}
message TemplateImport {
provisioner.Provision.Metadata metadata = 1;
repeated provisioner.VariableValue variable_values = 2;
}
message TemplateDryRun {
repeated provisioner.ParameterValue parameter_values = 1;
Expand Down Expand Up @@ -100,14 +101,16 @@ message UpdateJobRequest {
repeated Log logs = 2;
repeated provisioner.ParameterSchema parameter_schemas = 3;
repeated provisioner.TemplateVariable template_variables = 4;
bytes readme = 5;
repeated provisioner.VariableValue variable_values = 5;
bytes readme = 6;
}

message UpdateJobResponse {
bool canceled = 1;
// If parameter schemas are sent, the job will respond
// with resolved parameter values.
repeated provisioner.ParameterValue parameter_values = 2;
repeated provisioner.VariableValue variable_values = 5;
}

message CommitQuotaRequest {
Expand Down
2 changes: 1 addition & 1 deletion provisionerd/proto/provisionerd_drpc.pb.go

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

20 changes: 14 additions & 6 deletions provisionerd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ func (r *Runner) do(ctx context.Context) (*proto.CompletedJob, *proto.FailedJob)
}
switch jobType := r.job.Type.(type) {
case *proto.AcquiredJob_TemplateImport_:
r.logger.Debug(context.Background(), "acquired job is template import")
r.logger.Info(context.Background(), "acquired job is template import",
slog.F("variable_values", jobType.TemplateImport.VariableValues), // FIXME to be redacted
)

failedJob := r.runReadmeParse(ctx)
if failedJob != nil {
Expand Down Expand Up @@ -542,10 +544,14 @@ func (r *Runner) runTemplateImport(ctx context.Context) (*proto.CompletedJob, *p
if err != nil {
return nil, r.failedJobf("run parse: %s", err)
}

// Once Terraform template variables are parsed, the runner can pass variables
// to store in database and filter valid ones.
updateResponse, err := r.update(ctx, &proto.UpdateJobRequest{
JobId: r.job.JobId,
ParameterSchemas: parameterSchemas,
TemplateVariables: templateVariables,
VariableValues: r.job.GetTemplateImport().GetVariableValues(),
})
if err != nil {
return nil, r.failedJobf("update job: %s", err)
Expand All @@ -569,7 +575,7 @@ func (r *Runner) runTemplateImport(ctx context.Context) (*proto.CompletedJob, *p
Stage: "Detecting persistent resources",
CreatedAt: time.Now().UnixMilli(),
})
startResources, parameters, err := r.runTemplateImportProvision(ctx, updateResponse.ParameterValues, &sdkproto.Provision_Metadata{
startResources, parameters, err := r.runTemplateImportProvision(ctx, updateResponse.ParameterValues, updateResponse.VariableValues, &sdkproto.Provision_Metadata{
CoderUrl: r.job.GetTemplateImport().Metadata.CoderUrl,
WorkspaceTransition: sdkproto.WorkspaceTransition_START,
})
Expand All @@ -584,7 +590,7 @@ func (r *Runner) runTemplateImport(ctx context.Context) (*proto.CompletedJob, *p
Stage: "Detecting ephemeral resources",
CreatedAt: time.Now().UnixMilli(),
})
stopResources, _, err := r.runTemplateImportProvision(ctx, updateResponse.ParameterValues, &sdkproto.Provision_Metadata{
stopResources, _, err := r.runTemplateImportProvision(ctx, updateResponse.ParameterValues, updateResponse.VariableValues, &sdkproto.Provision_Metadata{
CoderUrl: r.job.GetTemplateImport().Metadata.CoderUrl,
WorkspaceTransition: sdkproto.WorkspaceTransition_STOP,
})
Expand Down Expand Up @@ -652,13 +658,13 @@ func (r *Runner) runTemplateImportParse(ctx context.Context) ([]*sdkproto.Parame
// Performs a dry-run provision when importing a template.
// This is used to detect resources that would be provisioned for a workspace in various states.
// It doesn't define values for rich parameters as they're unknown during template import.
func (r *Runner) runTemplateImportProvision(ctx context.Context, values []*sdkproto.ParameterValue, metadata *sdkproto.Provision_Metadata) ([]*sdkproto.Resource, []*sdkproto.RichParameter, error) {
return r.runTemplateImportProvisionWithRichParameters(ctx, values, nil, metadata)
func (r *Runner) runTemplateImportProvision(ctx context.Context, values []*sdkproto.ParameterValue, variableValues []*sdkproto.VariableValue, metadata *sdkproto.Provision_Metadata) ([]*sdkproto.Resource, []*sdkproto.RichParameter, error) {
return r.runTemplateImportProvisionWithRichParameters(ctx, values, variableValues, nil, metadata)
}

// Performs a dry-run provision with provided rich parameters.
// This is used to detect resources that would be provisioned for a workspace in various states.
func (r *Runner) runTemplateImportProvisionWithRichParameters(ctx context.Context, values []*sdkproto.ParameterValue, richParameterValues []*sdkproto.RichParameterValue, metadata *sdkproto.Provision_Metadata) ([]*sdkproto.Resource, []*sdkproto.RichParameter, error) {
func (r *Runner) runTemplateImportProvisionWithRichParameters(ctx context.Context, values []*sdkproto.ParameterValue, variableValues []*sdkproto.VariableValue, richParameterValues []*sdkproto.RichParameterValue, metadata *sdkproto.Provision_Metadata) ([]*sdkproto.Resource, []*sdkproto.RichParameter, error) {
ctx, span := r.startTrace(ctx, tracing.FuncName())
defer span.End()

Expand Down Expand Up @@ -697,6 +703,7 @@ func (r *Runner) runTemplateImportProvisionWithRichParameters(ctx context.Contex
},
ParameterValues: values,
RichParameterValues: richParameterValues,
VariableValues: variableValues,
},
},
})
Expand Down Expand Up @@ -785,6 +792,7 @@ func (r *Runner) runTemplateDryRun(ctx context.Context) (*proto.CompletedJob, *p
// Run the template import provision task since it's already a dry run.
resources, _, err := r.runTemplateImportProvisionWithRichParameters(ctx,
r.job.GetTemplateDryRun().GetParameterValues(),
nil, // FIXME variable values
r.job.GetTemplateDryRun().GetRichParameterValues(),
metadata,
)
Expand Down
Loading