ParameterTerraform will create a Terraform data block for the provided parameter.
(param *proto.RichParameter)
| 640 | |
| 641 | // ParameterTerraform will create a Terraform data block for the provided parameter. |
| 642 | func ParameterTerraform(param *proto.RichParameter) (string, error) { |
| 643 | tmpl := template.Must(template.New("parameter").Funcs(map[string]any{ |
| 644 | "showValidation": func(v *proto.RichParameter) bool { |
| 645 | return v != nil && (v.ValidationMax != nil || v.ValidationMin != nil || |
| 646 | v.ValidationError != "" || v.ValidationRegex != "" || |
| 647 | v.ValidationMonotonic != "") |
| 648 | }, |
| 649 | "formType": func(v *proto.RichParameter) string { |
| 650 | s, _ := proto.ProviderFormType(v.FormType) |
| 651 | return string(s) |
| 652 | }, |
| 653 | "hasDefault": func(v *proto.RichParameter) bool { |
| 654 | // Emit default when the value is explicitly non-empty, |
| 655 | // or when the parameter is ephemeral (ephemeral params |
| 656 | // always need a default, even if it's an empty string). |
| 657 | return v.DefaultValue != "" || v.Ephemeral |
| 658 | }, |
| 659 | }).Parse(` |
| 660 | data "coder_parameter" "{{ .Name }}" { |
| 661 | name = "{{ .Name }}" |
| 662 | display_name = "{{ .DisplayName }}" |
| 663 | description = "{{ .Description }}" |
| 664 | icon = "{{ .Icon }}" |
| 665 | mutable = {{ .Mutable }} |
| 666 | ephemeral = {{ .Ephemeral }} |
| 667 | order = {{ .Order }} |
| 668 | {{- if hasDefault . }} |
| 669 | {{- if eq .Type "list(string)" }} |
| 670 | default = jsonencode({{ .DefaultValue }}) |
| 671 | {{else if eq .Type "bool"}} |
| 672 | default = {{ .DefaultValue }} |
| 673 | {{else if eq .Type "number"}} |
| 674 | default = {{ .DefaultValue }} |
| 675 | {{else}} |
| 676 | default = "{{ .DefaultValue }}" |
| 677 | {{- end }} |
| 678 | {{- end }} |
| 679 | {{- if .Type }} |
| 680 | type = "{{ .Type }}" |
| 681 | {{- end }} |
| 682 | {{- if .FormType }} |
| 683 | form_type = "{{ formType . }}" |
| 684 | {{- end }} |
| 685 | {{- range .Options }} |
| 686 | option { |
| 687 | name = "{{ .Name }}" |
| 688 | value = "{{ .Value }}" |
| 689 | } |
| 690 | {{- end }} |
| 691 | {{- if showValidation .}} |
| 692 | validation { |
| 693 | {{- if .ValidationRegex }} |
| 694 | regex = "{{ .ValidationRegex }}" |
| 695 | {{- end }} |
| 696 | {{- if .ValidationError }} |
| 697 | error = "{{ .ValidationError }}" |
| 698 | {{- end }} |
| 699 | {{- if .ValidationMin }} |
no test coverage detected