10000 feat: Allow inheriting parameters from previous template_versions when updating a template by Emyrk · Pull Request #2397 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: Allow inheriting parameters from previous template_versions when updating a template #2397

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 18 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Working implementation of inherited params
  • Loading branch information
Emyrk committed Jun 15, 2022
commit aceca623a9b8604ea27c009bf399962edd781e43
6 changes: 3 additions & 3 deletions cli/parameterslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func parameterList() *cobra.Command {
}
scopeID = template.ID

case codersdk.ParameterScopeImportJob, "template_version":
scope = string(codersdk.ParameterScopeImportJob)
case codersdk.ParameterImportJob, "template_version":
scope = string(codersdk.ParameterImportJob)
scopeID, err = uuid.Parse(name)
if err != nil {
return xerrors.Errorf("%q must be a uuid for this scope type", name)
}
default:
return xerrors.Errorf("%q is an unsupported scope, use %v", scope, []codersdk.ParameterScope{
codersdk.ParameterWorkspace, codersdk.ParameterTemplate, codersdk.ParameterScopeImportJob,
codersdk.ParameterWorkspace, codersdk.ParameterTemplate, codersdk.ParameterImportJob,
})
}

Expand Down
32 changes: 32 additions & 0 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,37 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
return nil, nil, err
}

// lastParameterValues are pulled from the current active template version if
// templateID is provided. This allows pulling params from the last
// version if we are updating template versions.
lastParameterValues := make(map[string]codersdk.Parameter)
if args.TemplateID != uuid.Nil {
template, err := client.Template(cmd.Context(), args.TemplateID)
if err != nil {
return nil, nil, xerrors.Errorf("Fetch template: %w", err)
}

activeVersion, err := client.TemplateVersion(cmd.Context(), template.ActiveVersionID)
if err != nil {
return nil, nil, xerrors.Errorf("Fetch current active template version: %w", err)
}

// We don't want to compute the params, we only want to copy from this scope
values, err := client.Parameters(cmd.Context(), codersdk.ParameterImportJob, activeVersion.Job.ID)
if err != nil {
return nil, nil, xerrors.Errorf("Fetch previous version parameters: %w", err)
}
for _, value := range values {
lastParameterValues[value.Name] = value
}
}

if provisionerd.IsMissingParameterError(version.Job.Error) {
valuesBySchemaID := map[string]codersdk.TemplateVersionParameter{}
for _, parameterValue := range parameterValues {
valuesBySchemaID[parameterValue.SchemaID.String()] = parameterValue
}

sort.Slice(parameterSchemas, func(i, j int) bool {
return parameterSchemas[i].Name < parameterSchemas[j].Name
})
Expand All @@ -224,6 +250,12 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
}
}
for _, parameterSchema := range missingSchemas {
if inherit, ok := lastParameterValues[parameterSchema.Name]; ok {
parameters = append(parameters, codersdk.CreateParameterRequest{
CopyFromParameter: inherit.ID,
})
continue
}
parameterValue, err := getParameterValueFromMapOrInput(cmd, parameterMapFromFile, parameterSchema)
if err != nil {
return nil, nil, err
Expand Down
7 changes: 2 additions & 5 deletions cli/templateupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func templateUpdate() *cobra.Command {
}
spin.Stop()

//before := time.Now()

job, parameters, err := createValidTemplateVersion(cmd, createValidTemplateVersionArgs{
job, _, err := createValidTemplateVersion(cmd, createValidTemplateVersionArgs{
Client: client,
Organization: organization,
Provisioner: database.ProvisionerType(provisioner),
Expand All @@ -95,8 +93,7 @@ func templateUpdate() *cobra.Command {
}

err = client.UpdateActiveTemplateVersion(cmd.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
ID: job.ID,
ParameterValues: parameters,
ID: job.ID,
})
if err != nil {
return err
Expand Down
40 changes: 36 additions & 4 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ func (q *fakeQuerier) AcquireProvisionerJob(_ context.Context, arg database.Acqu
return database.ProvisionerJob{}, sql.ErrNoRows
}

func (q *fakeQuerier) ParameterValue(_ context.Context, id uuid.UUID) (database.ParameterValue, error) {
q.mutex.Lock()
defer q.mutex.Unlock()

for _, parameterValue := range q.parameterValues {
if parameterValue.ID.String() != id.String() {
continue
}
return parameterValue, nil

}
return database.ParameterValue{}, sql.ErrNoRows
}

func (q *fakeQuerier) DeleteParameterValueByID(_ context.Context, id uuid.UUID) error {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down Expand Up @@ -716,17 +730,26 @@ func (q *fakeQuerier) GetOrganizationsByUserID(_ context.Context, userID uuid.UU
return organizations, nil
}

func (q *fakeQuerier) GetParameterValuesByScope(_ context.Context, arg database.GetParameterValuesByScopeParams) ([]database.ParameterValue, error) {
func (q *fakeQuerier) ParameterValues(_ context.Context, arg database.ParameterValuesParams) ([]database.ParameterValue, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made 1 query with filter options rather than adding new specific queries

q.mutex.RLock()
defer q.mutex.RUnlock()

parameterValues := make([]database.ParameterValue, 0)
for _, parameterValue := range q.parameterValues {
if parameterValue.Scope != arg.Scope {
if arg.Scope != "" && parameterValue.Scope != arg.Scope {
continue
}
if parameterValue.ScopeID != arg.ScopeID {
continue

if len(arg.ScopeIds) > 0 {
if !contains(arg.ScopeIds, parameterValue.ScopeID) {
continue
}
}

if len(arg.Ids) > 0 {
if !contains(arg.Ids, parameterValue.ID) {
continue
}
}
parameterValues = append(parameterValues, parameterValue)
}
Expand Down Expand Up @@ -1980,3 +2003,12 @@ func (q *fakeQuerier) InsertAuditLog(_ context.Context, arg database.InsertAudit

return alog, nil
}

func contains[T comparable](haystack []T, needle T) bool {
for _, hay := range haystack {
if needle == hay {
return true
}
}
return false
}
3 changes: 2 additions & 1 deletion coderd/database/querier.go

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

145 changes: 97 additions & 48 deletions coderd/database/queries.sql.go

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

32 changes: 29 additions & 3 deletions coderd/database/queries/parametervalues.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
-- name: ParameterValue :one
SELECT * FROM
parameter_values
WHERE
id = $1;


-- name: DeleteParameterValueByID :exec
DELETE FROM
parameter_values
WHERE
id = $1;

-- name: GetParameterValuesByScope :many
-- name: ParameterValues :many
SELECT
*
FROM
parameter_values
WHERE
scope = $1
AND scope_id = $2;
CASE
WHEN @scope :: parameter_scope != '' THEN
scope = @scope
ELSE true
END
AND CASE
WHEN cardinality(@scope_ids :: uuid[]) > 0 THEN
scope_id = ANY(@scope_ids :: uuid[])
ELSE true
END
AND CASE
WHEN cardinality(@ids :: uuid[]) > 0 THEN
id = ANY(@ids :: uuid[])
ELSE true
END
AND CASE
WHEN cardinality(@names :: text[]) > 0 THEN
"name" = ANY(@names :: text[])
ELSE true
END
;

-- name: GetParameterValueByScopeAndName :one
SELECT
Expand Down
Loading
0