99"golang.org/x/xerrors"
1010
1111"github.com/coder/coder/v2/coderd/database"
12+ "github.com/coder/coder/v2/coderd/database/db2sdk"
1213"github.com/coder/coder/v2/coderd/util/ptr"
1314sdkproto"github.com/coder/coder/v2/provisionersdk/proto"
1415"github.com/coder/preview"
@@ -26,89 +27,7 @@ func (r *loader) staticRender(ctx context.Context, db database.Store) (*staticRe
2627return nil ,xerrors .Errorf ("template version parameters: %w" ,err )
2728}
2829
29- params := make ([]previewtypes.Parameter ,0 ,len (dbTemplateVersionParameters ))
30- for _ ,it := range dbTemplateVersionParameters {
31- param := previewtypes.Parameter {
32- ParameterData : previewtypes.ParameterData {
33- Name :it .Name ,
34- DisplayName :it .DisplayName ,
35- Description :it .Description ,
36- Type :previewtypes .ParameterType (it .Type ),
37- FormType :provider .ParameterFormType (it .FormType ),
38- Styling : previewtypes.ParameterStyling {},
39- Mutable :it .Mutable ,
40- DefaultValue :previewtypes .StringLiteral (it .DefaultValue ),
41- Icon :it .Icon ,
42- Options :make ([]* previewtypes.ParameterOption ,0 ),
43- Validations :make ([]* previewtypes.ParameterValidation ,0 ),
44- Required :it .Required ,
45- Order :int64 (it .DisplayOrder ),
46- Ephemeral :it .Ephemeral ,
47- Source :nil ,
48- },
49- // Always use the default, since we used to assume the empty string
50- Value :previewtypes .StringLiteral (it .DefaultValue ),
51- Diagnostics :make (previewtypes.Diagnostics ,0 ),
52- }
53-
54- if it .ValidationError != "" || it .ValidationRegex != "" || it .ValidationMonotonic != "" {
55- var reg * string
56- if it .ValidationRegex != "" {
57- reg = ptr .Ref (it .ValidationRegex )
58- }
59-
60- var vMin * int64
61- if it .ValidationMin .Valid {
62- vMin = ptr .Ref (int64 (it .ValidationMin .Int32 ))
63- }
64-
65- var vMax * int64
66- if it .ValidationMax .Valid {
67- vMax = ptr .Ref (int64 (it .ValidationMax .Int32 ))
68- }
69-
70- var monotonic * string
71- if it .ValidationMonotonic != "" {
72- monotonic = ptr .Ref (it .ValidationMonotonic )
73- }
74-
75- param .Validations = append (param .Validations ,& previewtypes.ParameterValidation {
76- Error :it .ValidationError ,
77- Regex :reg ,
78- Min :vMin ,
79- Max :vMax ,
80- Monotonic :monotonic ,
81- })
82- }
83-
84- var protoOptions []* sdkproto.RichParameterOption
85- err := json .Unmarshal (it .Options ,& protoOptions )
86- if err != nil {
87- param .Diagnostics = append (param .Diagnostics ,& hcl.Diagnostic {
88- Severity :hcl .DiagError ,
89- Summary :"Failed to parse json parameter options" ,
90- Detail :err .Error (),
91- })
92- }
93-
94- for _ ,opt := range protoOptions {
95- param .Options = append (param .Options ,& previewtypes.ParameterOption {
96- Name :opt .Name ,
97- Description :opt .Description ,
98- Value :previewtypes .StringLiteral (opt .Value ),
99- Icon :opt .Icon ,
100- })
101- }
102-
103- // Take the form type from the ValidateFormType function. This is a bit
104- // unfortunate we have to do this, but it will return the default form_type
105- // for a given set of conditions.
106- _ ,param .FormType ,_ = provider .ValidateFormType (provider .OptionType (param .Type ),len (param .Options ),param .FormType )
107-
108- param .Diagnostics = append (param .Diagnostics ,previewtypes .Diagnostics (param .Valid (param .Value ))... )
109- params = append (params ,param )
110- }
111-
30+ params := db2sdk .List (dbTemplateVersionParameters ,TemplateVersionParameter )
11231return & staticRender {
11332staticParams :params ,
11433},nil
@@ -140,3 +59,85 @@ func (r *staticRender) Render(_ context.Context, _ uuid.UUID, values map[string]
14059}
14160
14261func (* staticRender )Close () {}
62+
63+ func TemplateVersionParameter (it database.TemplateVersionParameter ) previewtypes.Parameter {
64+ param := previewtypes.Parameter {
65+ ParameterData : previewtypes.ParameterData {
66+ Name :it .Name ,
67+ DisplayName :it .DisplayName ,
68+ Description :it .Description ,
69+ Type :previewtypes .ParameterType (it .Type ),
70+ FormType :provider .ParameterFormType (it .FormType ),
71+ Styling : previewtypes.ParameterStyling {},
72+ Mutable :it .Mutable ,
73+ DefaultValue :previewtypes .StringLiteral (it .DefaultValue ),
74+ Icon :it .Icon ,
75+ Options :make ([]* previewtypes.ParameterOption ,0 ),
76+ Validations :make ([]* previewtypes.ParameterValidation ,0 ),
77+ Required :it .Required ,
78+ Order :int64 (it .DisplayOrder ),
79+ Ephemeral :it .Ephemeral ,
80+ Source :nil ,
81+ },
82+ // Always use the default, since we used to assume the empty string
83+ Value :previewtypes .StringLiteral (it .DefaultValue ),
84+ Diagnostics :make (previewtypes.Diagnostics ,0 ),
85+ }
86+
87+ if it .ValidationError != "" || it .ValidationRegex != "" || it .ValidationMonotonic != "" {
88+ var reg * string
89+ if it .ValidationRegex != "" {
90+ reg = ptr .Ref (it .ValidationRegex )
91+ }
92+
93+ var vMin * int64
94+ if it .ValidationMin .Valid {
95+ vMin = ptr .Ref (int64 (it .ValidationMin .Int32 ))
96+ }
97+
98+ var vMax * int64
99+ if it .ValidationMax .Valid {
100+ vMax = ptr .Ref (int64 (it .ValidationMax .Int32 ))
101+ }
102+
103+ var monotonic * string
104+ if it .ValidationMonotonic != "" {
105+ monotonic = ptr .Ref (it .ValidationMonotonic )
106+ }
107+
108+ param .Validations = append (param .Validations ,& previewtypes.ParameterValidation {
109+ Error :it .ValidationError ,
110+ Regex :reg ,
111+ Min :vMin ,
112+ Max :vMax ,
113+ Monotonic :monotonic ,
114+ })
115+ }
116+
117+ var protoOptions []* sdkproto.RichParameterOption
118+ err := json .Unmarshal (it .Options ,& protoOptions )
119+ if err != nil {
120+ param .Diagnostics = append (param .Diagnostics ,& hcl.Diagnostic {
121+ Severity :hcl .DiagError ,
122+ Summary :"Failed to parse json parameter options" ,
123+ Detail :err .Error (),
124+ })
125+ }
126+
127+ for _ ,opt := range protoOptions {
128+ param .Options = append (param .Options ,& previewtypes.ParameterOption {
129+ Name :opt .Name ,
130+ Description :opt .Description ,
131+ Value :previewtypes .StringLiteral (opt .Value ),
132+ Icon :opt .Icon ,
133+ })
134+ }
135+
136+ // Take the form type from the ValidateFormType function. This is a bit
137+ // unfortunate we have to do this, but it will return the default form_type
138+ // for a given set of conditions.
139+ _ ,param .FormType ,_ = provider .ValidateFormType (provider .OptionType (param .Type ),len (param .Options ),param .FormType )
140+
141+ param .Diagnostics = append (param .Diagnostics ,previewtypes .Diagnostics (param .Valid (param .Value ))... )
142+ return param
143+ }