Skip to content

Commit 9e23196

Browse files
committed
planVars
1 parent c0ba41b commit 9e23196

File tree

9 files changed

+271
-243
lines changed

9 files changed

+271
-243
lines changed

coderd/autobuild/executor/lifecycle_executor.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"cdr.dev/slog"
1313
"github.com/coder/coder/coderd/autobuild/schedule"
1414
"github.com/coder/coder/coderd/database"
15+
"github.com/coder/coder/coderd/provisionerdserver"
1516
)
1617

1718
// Executor automatically starts or stops workspaces.
@@ -247,10 +248,8 @@ func build(ctx context.Context, store database.Store, workspace database.Workspa
247248
// This must happen in a transaction to ensure history can be inserted, and
248249
// the prior history can update it's "after" column to point at the new.
249250
workspaceBuildID := uuid.New()
250-
input, err := json.Marshal(struct {
251-
WorkspaceBuildID string `json:"workspace_build_id"`
252-
}{
253-
WorkspaceBuildID: workspaceBuildID.String(),
251+
input, err := json.Marshal(provisionerdserver.WorkspaceProvisionJob{
252+
WorkspaceBuildID: workspaceBuildID,
254253
})
255254
if err != nil {
256255
return xerrors.Errorf("marshal provision job: %w", err)

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (server *Server) AcquireJob(ctx context.Context, _ *proto.Empty) (*proto.Ac
142142
if err != nil {
143143
return nil, failJob(fmt.Sprintf("get template version: %s", err))
144144
}
145+
templateVariables, err := server.Database.GetTemplateVersionVariables(ctx, templateVersion.ID)
146+
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
147+
return nil, failJob(fmt.Sprintf("get template version variables: %s", err))
148+
}
145149
template, err := server.Database.GetTemplateByID(ctx, templateVersion.TemplateID.UUID)
146150
if err != nil {
147151
return nil, failJob(fmt.Sprintf("get template: %s", err))
@@ -193,6 +197,7 @@ func (server *Server) AcquireJob(ctx context.Context, _ *proto.Empty) (*proto.Ac
193197
State: workspaceBuild.ProvisionerState,
194198
ParameterValues: protoParameters,
195199
RichParameterValues: convertRichParameterValues(workspaceBuildParameters),
200+
VariableValues: asVariableValues(templateVariables),
196201
Metadata: &sdkproto.Provision_Metadata{
197202
CoderUrl: server.AccessURL.String(),
198203
WorkspaceTransition: transition,
@@ -1284,3 +1289,21 @@ type ProvisionerJobLogsNotifyMessage struct {
12841289
func ProvisionerJobLogsNotifyChannel(jobID uuid.UUID) string {
12851290
return fmt.Sprintf("provisioner-log-logs:%s", jobID)
12861291
}
1292+
1293+
func asVariableValues(templateVariables []database.TemplateVersionVariable) []*sdkproto.VariableValue {
1294+
var apiVariableValues []*sdkproto.VariableValue
1295+
for _, v := range templateVariables {
1296+
var value = v.Value
1297+
if value == "" && v.DefaultValue != "" {
1298+
value = v.DefaultValue
1299+
}
1300+
1301+
if value != "" || v.Required {
1302+
apiVariableValues = append(apiVariableValues, &sdkproto.VariableValue{
1303+
Name: v.Name,
1304+
Value: v.Value,
1305+
})
1306+
}
1307+
}
1308+
return apiVariableValues
1309+
}

coderd/templateversions.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,7 @@ func (api *API) templateVersionVariables(rw http.ResponseWriter, r *http.Request
282282
return
283283
}
284284

285-
templateVersionVariables, err := convertTemplateVersionVariables(dbTemplateVersionVariables)
286-
if err != nil {
287-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
288-
Message: "Internal error converting template version parameter.",
289-
Detail: err.Error(),
290-
})
291-
return
292-
}
293-
httpapi.Write(ctx, rw, http.StatusOK, templateVersionVariables)
285+
httpapi.Write(ctx, rw, http.StatusOK, convertTemplateVersionVariables(dbTemplateVersionVariables))
294286
}
295287

296288
// @Summary Get parameters by template version
@@ -1531,19 +1523,15 @@ func convertTemplateVersionParameter(param database.TemplateVersionParameter) (c
15311523
}, nil
15321524
}
15331525

1534-
func convertTemplateVersionVariables(dbVariables []database.TemplateVersionVariable) ([]codersdk.TemplateVersionVariable, error) {
1526+
func convertTemplateVersionVariables(dbVariables []database.TemplateVersionVariable) []codersdk.TemplateVersionVariable {
15351527
variables := make([]codersdk.TemplateVersionVariable, 0)
15361528
for _, dbVariable := range dbVariables {
1537-
param, err := convertTemplateVersionVariable(dbVariable)
1538-
if err != nil {
1539-
return nil, err
1540-
}
1541-
variables = append(variables, param)
1529+
variables = append(variables, convertTemplateVersionVariable(dbVariable))
15421530
}
1543-
return variables, nil
1531+
return variables
15441532
}
15451533

1546-
func convertTemplateVersionVariable(variable database.TemplateVersionVariable) (codersdk.TemplateVersionVariable, error) {
1534+
func convertTemplateVersionVariable(variable database.TemplateVersionVariable) codersdk.TemplateVersionVariable {
15471535
return codersdk.TemplateVersionVariable{
15481536
Name: variable.Name,
15491537
Description: variable.Description,
@@ -1552,7 +1540,7 @@ func convertTemplateVersionVariable(variable database.TemplateVersionVariable) (
15521540
DefaultValue: variable.DefaultValue,
15531541
Required: variable.Required,
15541542
Sensitive: variable.Sensitive,
1555-
}, nil
1543+
}
15561544
}
15571545

15581546
func watchTemplateChannel(id uuid.UUID) string {

coderd/workspaces.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
458458
}
459459

460460
tags := provisionerdserver.MutateTags(user.ID, templateVersionJob.Tags)
461-
462461
var (
463462
provisionerJob database.ProvisionerJob
464463
workspaceBuild database.WorkspaceBuild

docs/cli/coder_templates_push.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ coder templates push [template] [flags]
1515
--name string Specify a name for the new template version. It will be automatically generated if not provided.
1616
--parameter-file string Specify a file path with parameter values.
1717
--provisioner-tag stringArray Specify a set of tags to target provisioner daemons.
18+
--values-file string Specify a file path with values for managed variables.
1819
-y, --yes Bypass prompts
1920
```
2021

provisioner/terraform/provision.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ func (s *server) Provision(stream proto.DRPCProvisioner_ProvisionStream) error {
138138
return xerrors.Errorf("initialize terraform: %w", err)
139139
}
140140
s.logger.Debug(ctx, "ran initialization")
141-
142-
env, err := provisionEnv(config, request.GetPlan().GetParameterValues(), request.GetPlan().GetVariableValues(), request.GetPlan().GetRichParameterValues())
141+
env, err := provisionEnv(config, request.GetPlan().GetParameterValues(), request.GetPlan().GetRichParameterValues())
143142
if err != nil {
144143
return err
145144
}
@@ -202,10 +201,13 @@ func planVars(plan *proto.Provision_Plan) ([]string, error) {
202201
return nil, xerrors.Errorf("unsupported parameter type %q for %q", param.DestinationScheme, param.Name)
203202
}
204203
}
204+
for _, variable := range plan.VariableValues {
205+
vars = append(vars, fmt.Sprintf("%s=%s", variable.Name, variable.Value))
206+
}
205207
return vars, nil
206208
}
207209

208-
func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue, variableValues []*proto.VariableValue, richParams []*proto.RichParameterValue) ([]string, error) {
210+
func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue, richParams []*proto.RichParameterValue) ([]string, error) {
209211
env := safeEnviron()
210212
env = append(env,
211213
"CODER_AGENT_URL="+config.Metadata.CoderUrl,
@@ -229,9 +231,6 @@ func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue
229231
return nil, xerrors.Errorf("unsupported parameter type %q for %q", param.DestinationScheme, param.Name)
230232
}
231233
}
232-
for _, variable := range variableValues {
233-
env = append(env, fmt.Sprintf("%s=%s", variable.Name, variable.Value))
234-
}
235234
for _, param := range richParams {
236235
env = append(env, provider.ParameterEnvironmentVariable(param.Name)+"="+param.Value)
237236
}

0 commit comments

Comments
 (0)