Skip to content

Commit 9cc59a9

Browse files
committed
chore: add form_type_metadata parsing
1 parent 46a45dc commit 9cc59a9

File tree

7 files changed

+64
-32
lines changed

7 files changed

+64
-32
lines changed

extract/parameter.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package extract
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"slices"
67
"strings"
@@ -55,14 +56,21 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
5556
def = types.ToHCLString(block, defAttr)
5657
}
5758

59+
ftmeta := optionalString(block, "form_type_metadata")
60+
formTypeMeta := make(map[string]any)
61+
if ftmeta != "" {
62+
_ = json.Unmarshal([]byte(ftmeta), &formTypeMeta)
63+
}
64+
5865
p := types.Parameter{
5966
Value: pVal,
6067
ParameterData: types.ParameterData{
61-
Name: pName,
62-
Description: optionalString(block, "description"),
63-
Type: pType,
64-
FormType: formType,
65-
Mutable: optionalBoolean(block, "mutable"),
68+
Name: pName,
69+
Description: optionalString(block, "description"),
70+
Type: pType,
71+
FormType: formType,
72+
FormTypeMetadata: ftmeta,
73+
Mutable: optionalBoolean(block, "mutable"),
6674
// Default value is always written as a string, then converted
6775
// to the correct type.
6876
DefaultValue: def,

extract/state.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package extract
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67

@@ -52,22 +53,29 @@ func ParameterFromState(block *tfjson.StateResource) (types.Parameter, error) {
5253
return types.Parameter{}, fmt.Errorf("convert param validations: %w", err)
5354
}
5455

56+
ftmeta := st.optionalString("form_type_metadata")
57+
var formTypeMeta any
58+
if ftmeta != "" {
59+
_ = json.Unmarshal([]byte(ftmeta), &formTypeMeta)
60+
}
61+
5562
param := types.Parameter{
5663
Value: types.StringLiteral(st.string("value")),
5764
ParameterData: types.ParameterData{
58-
Name: st.string("name"),
59-
Description: st.optionalString("description"),
60-
Type: types.ParameterType(st.optionalString("type")),
61-
FormType: provider.ParameterFormType(st.optionalString("form_type")),
62-
Mutable: st.optionalBool("mutable"),
63-
DefaultValue: types.StringLiteral(st.optionalString("default")),
64-
Icon: st.optionalString("icon"),
65-
Options: options,
66-
Validations: validations,
67-
Required: st.optionalBool("required"),
68-
DisplayName: st.optionalString("display_name"),
69-
Order: st.optionalInteger("order"),
70-
Ephemeral: st.optionalBool("ephemeral"),
65+
Name: st.string("name"),
66+
Description: st.optionalString("description"),
67+
Type: types.ParameterType(st.optionalString("type")),
68+
FormType: provider.ParameterFormType(st.optionalString("form_type")),
69+
FormTypeMetadata: formTypeMeta,
70+
Mutable: st.optionalBool("mutable"),
71+
DefaultValue: types.StringLiteral(st.optionalString("default")),
72+
Icon: st.optionalString("icon"),
73+
Options: options,
74+
Validations: validations,
75+
Required: st.optionalBool("required"),
76+
DisplayName: st.optionalString("display_name"),
77+
Order: st.optionalInteger("order"),
78+
Ephemeral: st.optionalBool("ephemeral"),
7179
},
7280
}
7381

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/aquasecurity/trivy v0.58.2
1010
github.com/coder/guts v1.0.2-0.20250227211802-139809366a22
1111
github.com/coder/serpent v0.10.0
12-
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250313232118-21ff5ae2bf31
12+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250317152424-16ab5102775e
1313
github.com/coder/websocket v1.8.13
1414
github.com/go-chi/chi v4.1.2+incompatible
1515
github.com/hashicorp/go-version v1.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,8 @@ github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx
720720
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
721721
github.com/coder/serpent v0.10.0 h1:ofVk9FJXSek+SmL3yVE3GoArP83M+1tX+H7S4t8BSuM=
722722
github.com/coder/serpent v0.10.0/go.mod h1:cZFW6/fP+kE9nd/oRkEHJpG6sXCtQ+AX7WMMEHv0Y3Q=
723-
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250313232118-21ff5ae2bf31 h1:JpDe/YonsKNbpbfruoniS/N0ozBV8Ekd67auoGOi3lY=
724-
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250313232118-21ff5ae2bf31/go.mod h1:X28s3rz+aEM5PkBKvk3xcUrQFO2eNPjzRChUg9wb70U=
723+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250317152424-16ab5102775e h1:CW8AyXdqeGRk6wE1L9py5jFbJrlGBwrUNbyb+26NrFU=
724+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250317152424-16ab5102775e/go.mod h1:X28s3rz+aEM5PkBKvk3xcUrQFO2eNPjzRChUg9wb70U=
725725
github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE=
726726
github.com/coder/websocket v1.8.13/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
727727
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=

testdata/demo/parameters.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ data "coder_parameter" "cpu" {
6464
display_name = "CPU"
6565
description = "The number of CPU cores"
6666
type = "number"
67+
form_type = "slider"
6768
default = "2"
6869
icon = "/icon/memory.svg"
6970
mutable = true
7071
order = 20
7172

73+
form_type_metadata = jsonencode({
74+
"minumum" = 1
75+
"budget" = 2
76+
"performance" = module.base.security_level == "high" ? 4 : 8
77+
})
78+
79+
7280
validation {
7381
min = 1
7482
// Confidential instances are more expensive, or some justification like

testdata/demo_flat/parameters.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,19 @@ data "coder_parameter" "cpu" {
6969
name = "cpu"
7070
display_name = "CPU"
7171
description = "The number of CPU cores"
72+
form_type = "slider"
7273
type = "number"
7374
default = "2"
7475
icon = "/icon/memory.svg"
7576
mutable = true
7677
order = 20
7778

79+
form_type_metadata = jsonencode({
80+
"minumum" = 1
81+
"budget" = 2
82+
"performance" = local.secutity_level == "high" ? 4 : 8
83+
})
84+
7885
validation {
7986
min = 1
8087
// Confidential instances are more expensive, or some justification like

types/parameter.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ type Parameter struct {
4545
}
4646

4747
type ParameterData struct {
48-
Name string `json:"name"`
49-
DisplayName string `json:"display_name"`
50-
Description string `json:"description"`
51-
Type ParameterType `json:"type"`
52-
FormType provider.ParameterFormType `json:"form_type"`
53-
Mutable bool `json:"mutable"`
54-
DefaultValue HCLString `json:"default_value"`
55-
Icon string `json:"icon"`
56-
Options []*ParameterOption `json:"options"`
57-
Validations []*ParameterValidation `json:"validations"`
58-
Required bool `json:"required"`
48+
Name string `json:"name"`
49+
DisplayName string `json:"display_name"`
50+
Description string `json:"description"`
51+
Type ParameterType `json:"type"`
52+
FormType provider.ParameterFormType `json:"form_type"`
53+
FormTypeMetadata any `json:"form_type_metadata"`
54+
Mutable bool `json:"mutable"`
55+
DefaultValue HCLString `json:"default_value"`
56+
Icon string `json:"icon"`
57+
Options []*ParameterOption `json:"options"`
58+
Validations []*ParameterValidation `json:"validations"`
59+
Required bool `json:"required"`
5960
// legacy_variable_name was removed (= 14)
6061
Order int64 `json:"order"`
6162
Ephemeral bool `json:"ephemeral"`

0 commit comments

Comments
 (0)