Skip to content

feat: add "display_order" column to coder_parameter to keep parameters sorted in UI #8227

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 25 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions coderd/database/dbfake/dbfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,12 @@ func (q *fakeQuerier) GetTemplateVersionParameters(_ context.Context, templateVe
}
parameters = append(parameters, param)
}
sort.Slice(parameters, func(i, j int) bool {
if parameters[i].DisplayOrder != parameters[j].DisplayOrder {
return parameters[i].DisplayOrder < parameters[j].DisplayOrder
}
return strings.ToLower(parameters[i].Name) < strings.ToLower(parameters[j].Name)
})
return parameters, nil
}

Expand Down Expand Up @@ -3934,6 +3940,7 @@ func (q *fakeQuerier) InsertTemplateVersionParameter(_ context.Context, arg data
ValidationMax: arg.ValidationMax,
ValidationMonotonic: arg.ValidationMonotonic,
Required: arg.Required,
DisplayOrder: arg.DisplayOrder,
LegacyVariableName: arg.LegacyVariableName,
}
q.templateVersionParameters = append(q.templateVersionParameters, param)
Expand Down
3 changes: 3 additions & 0 deletions coderd/database/dump.sql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE template_version_parameters DROP COLUMN display_order;
4 changes: 4 additions & 0 deletions coderd/database/migrations/000132_parameters_order.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE template_version_parameters ADD COLUMN display_order integer NOT NULL DEFAULT 0;
Copy link
Member

Choose a reason for hiding this comment

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

It's possible to use a reserved word as a name via quoting, e.g. ADD COLUMN "order", but I prefer this approach 👍🏻.


COMMENT ON COLUMN template_version_parameters.display_order
IS 'Specifies the order in which to display parameters in user interfaces.';
2 changes: 2 additions & 0 deletions coderd/database/models.go

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

14 changes: 10 additions & 4 deletions coderd/database/queries.sql.go

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

8 changes: 5 additions & 3 deletions coderd/database/queries/templateversionparameters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ INSERT INTO
validation_monotonic,
required,
legacy_variable_name,
display_name
display_name,
display_order
)
VALUES
(
Expand All @@ -35,8 +36,9 @@ VALUES
$13,
$14,
$15,
$16
$16,
$17
) RETURNING *;

-- name: GetTemplateVersionParameters :many
SELECT * FROM template_version_parameters WHERE template_version_id = $1;
SELECT * FROM template_version_parameters WHERE template_version_id = $1 ORDER BY display_order ASC, LOWER(name) ASC;
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for using explicit ASC, helps to convey the intent. ☺️

1 change: 1 addition & 0 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ func (server *Server) CompleteJob(ctx context.Context, completed *proto.Complete
ValidationMax: validationMax,
ValidationMonotonic: richParameter.ValidationMonotonic,
Required: richParameter.Required,
DisplayOrder: richParameter.Order,
LegacyVariableName: richParameter.LegacyVariableName,
})
if err != nil {
Expand Down
90 changes: 90 additions & 0 deletions coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,3 +1291,93 @@ func TestTemplateVersionPatch(t *testing.T) {
require.Error(t, err)
})
}

func TestTemplateVersionParameters_Order(t *testing.T) {
t.Parallel()

const (
firstParameterName = "first_parameter"
firstParameterType = "string"
firstParameterValue = "aaa"
// no order

secondParameterName = "Second_parameter"
secondParameterType = "number"
secondParameterValue = "2"
secondParameterOrder = 3

thirdParameterName = "third_parameter"
thirdParameterType = "number"
thirdParameterValue = "3"
thirdParameterOrder = 3

fourthParameterName = "Fourth_parameter"
fourthParameterType = "number"
fourthParameterValue = "3"
fourthParameterOrder = 2

fifthParameterName = "Fifth_parameter"
fifthParameterType = "string"
fifthParameterValue = "aaa"
// no order
)

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
Parse: echo.ParseComplete,
ProvisionPlan: []*proto.Provision_Response{
{
Type: &proto.Provision_Response_Complete{
Complete: &proto.Provision_Complete{
Parameters: []*proto.RichParameter{
{
Name: firstParameterName,
Type: firstParameterType,
// No order
},
{
Name: secondParameterName,
Type: secondParameterType,
Order: secondParameterOrder,
},
{
Name: thirdParameterName,
Type: thirdParameterType,
Order: thirdParameterOrder,
},
{
Name: fourthParameterName,
Type: fourthParameterType,
Order: fourthParameterOrder,
},
{
Name: fifthParameterName,
Type: fifthParameterType,
// No order
},
},
},
},
},
},
ProvisionApply: []*proto.Provision_Response{{
Type: &proto.Provision_Response_Complete{
Complete: &proto.Provision_Complete{},
},
}},
})
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

templateRichParameters, err := client.TemplateVersionRichParameters(ctx, version.ID)
require.NoError(t, err)
require.Len(t, templateRichParameters, 5)
require.Equal(t, fifthParameterName, templateRichParameters[0].Name)
require.Equal(t, firstParameterName, templateRichParameters[1].Name)
require.Equal(t, fourthParameterName, templateRichParameters[2].Name)
require.Equal(t, secondParameterName, templateRichParameters[3].Name)
require.Equal(t, thirdParameterName, templateRichParameters[4].Name)
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ require (
github.com/codeclysm/extract v2.2.0+incompatible
github.com/coder/flog v1.1.0
github.com/coder/retry v1.4.0
github.com/coder/terraform-provider-coder v0.8.2
github.com/coder/terraform-provider-coder v0.9.0
github.com/coder/wgtunnel v0.1.5
github.com/coreos/go-oidc/v3 v3.6.0
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
Expand Down Expand Up @@ -126,7 +126,7 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1
github.com/mitchellh/mapstructure v1.5.0
github.com/moby/moby v24.0.1+incompatible
github.com/muesli/reflow v0.3.0
github.com/muesli/reflow v0.3.0 // indirect
github.com/open-policy-agent/opa v0.51.0
github.com/ory/dockertest/v3 v3.10.0
github.com/pion/udp v0.1.2
Expand Down Expand Up @@ -218,7 +218,6 @@ require (
github.com/docker/docker v23.0.3+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1
github.com/elastic/go-windows v1.0.0 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ github.com/coder/ssh v0.0.0-20230621095435-9a7e23486f1c h1:TI7TzdFI0UvQmwgyQhtI1
github.com/coder/ssh v0.0.0-20230621095435-9a7e23486f1c/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ=
github.com/coder/tailscale v0.0.0-20230522123520-74712221d00f h1:F0Xr1d8h8dAHn7tab1HXuzYFkcjmCydnEfdMbkOhlVk=
github.com/coder/tailscale v0.0.0-20230522123520-74712221d00f/go.mod h1:jpg+77g19FpXL43U1VoIqoSg1K/Vh5CVxycGldQ8KhA=
github.com/coder/terraform-provider-coder v0.8.2 h1:EPhkdpsNd8fcg6eqpAQr+W1eRrEAMtugoqujoTK4O6o=
github.com/coder/terraform-provider-coder v0.8.2/go.mod h1:UIfU3bYNeSzJJvHyJ30tEKjD6Z9utloI+HUM/7n94CY=
github.com/coder/terraform-provider-coder v0.9.0 h1:OJazaeBz6O2h7tDDUyUWgNW71lTPbvbI2raX1CGuBzI=
github.com/coder/terraform-provider-coder v0.9.0/go.mod h1:UIfU3bYNeSzJJvHyJ30tEKjD6Z9utloI+HUM/7n94CY=
github.com/coder/wgtunnel v0.1.5 h1:WP3sCj/3iJ34eKvpMQEp1oJHvm24RYh0NHbj1kfUKfs=
github.com/coder/wgtunnel v0.1.5/go.mod h1:bokoUrHnUFY4lu9KOeSYiIcHTI2MO1KwqumU4DPDyJI=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
Expand Down Expand Up @@ -238,7 +238,6 @@ github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/elastic/go-sysinfo v1.11.0 h1:QW+6BF1oxBoAprH3w2yephF7xLkrrSXj7gl2xC2BM4w=
github.com/elastic/go-sysinfo v1.11.0/go.mod h1:6KQb31j0QeWBDF88jIdWSxE8cwoOB9tO4Y4osN7Q70E=
github.com/elastic/go-windows v1.0.0 h1:qLURgZFkkrYyTTkvYpsZIgf83AUsdIHfvlJaqaZ7aSY=
Expand Down
26 changes: 9 additions & 17 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,7 @@ func (e *executor) planResources(ctx, killCtx context.Context, planfilePath stri
}
modules = append(modules, plan.PlannedValues.RootModule)

rawParameterNames, err := rawRichParameterNames(e.workdir)
if err != nil {
return nil, xerrors.Errorf("raw rich parameter names: %w", err)
}

state, err := ConvertState(modules, rawGraph, rawParameterNames)
state, err := ConvertState(modules, rawGraph)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -410,18 +405,15 @@ func (e *executor) stateResources(ctx, killCtx context.Context) (*State, error)
return nil, xerrors.Errorf("get terraform graph: %w", err)
}
converted := &State{}
if state.Values != nil {
rawParameterNames, err := rawRichParameterNames(e.workdir)
if err != nil {
return nil, xerrors.Errorf("raw rich parameter names: %w", err)
}
if state.Values == nil {
return converted, nil
}

converted, err = ConvertState([]*tfjson.StateModule{
state.Values.RootModule,
}, rawGraph, rawParameterNames)
if err != nil {
return nil, err
}
converted, err = ConvertState([]*tfjson.StateModule{
state.Values.RootModule,
}, rawGraph)
if err != nil {
return nil, err
}
return converted, nil
}
Expand Down
70 changes: 0 additions & 70 deletions provisioner/terraform/parameters.go

This file was deleted.

Loading