Skip to content

Commit 89f8435

Browse files
committed
feat: add use_classic_parameter_flow to coderd_template resource
1 parent e5680f3 commit 89f8435

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

internal/provider/template_resource.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/hashicorp/terraform-plugin-framework/resource"
2525
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2626
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
27+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
2728
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
2829
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectdefault"
2930
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -72,6 +73,7 @@ type TemplateResourceModel struct {
7273
RequireActiveVersion types.Bool `tfsdk:"require_active_version"`
7374
DeprecationMessage types.String `tfsdk:"deprecation_message"`
7475
MaxPortShareLevel types.String `tfsdk:"max_port_share_level"`
76+
UseClassicParameterFlow types.Bool `tfsdk:"use_classic_parameter_flow"`
7577

7678
// If null, we are not managing ACL via Terraform (such as for AGPL).
7779
ACL types.Object `tfsdk:"acl"`
@@ -97,7 +99,8 @@ func (m *TemplateResourceModel) EqualTemplateMetadata(other *TemplateResourceMod
9799
m.TimeTilDormantAutoDeleteMillis.Equal(other.TimeTilDormantAutoDeleteMillis) &&
98100
m.RequireActiveVersion.Equal(other.RequireActiveVersion) &&
99101
m.DeprecationMessage.Equal(other.DeprecationMessage) &&
100-
m.MaxPortShareLevel.Equal(other.MaxPortShareLevel)
102+
m.MaxPortShareLevel.Equal(other.MaxPortShareLevel) &&
103+
m.UseClassicParameterFlow.Equal(other.UseClassicParameterFlow)
101104
}
102105

103106
func (m *TemplateResourceModel) CheckEntitlements(ctx context.Context, features map[codersdk.FeatureName]codersdk.Feature) (diags diag.Diagnostics) {
@@ -385,13 +388,24 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
385388
Validators: []validator.String{
386389
stringvalidator.OneOfCaseInsensitive(string(codersdk.WorkspaceAgentPortShareLevelAuthenticated), string(codersdk.WorkspaceAgentPortShareLevelOwner), string(codersdk.WorkspaceAgentPortShareLevelPublic)),
387390
},
391+
PlanModifiers: []planmodifier.String{
392+
stringplanmodifier.UseStateForUnknown(),
393+
},
388394
},
389395
"deprecation_message": schema.StringAttribute{
390396
MarkdownDescription: "If set, the template will be marked as deprecated with the provided message and users will be blocked from creating new workspaces from it. Does nothing if set when the resource is created.",
391397
Optional: true,
392398
Computed: true,
393399
Default: stringdefault.StaticString(""),
394400
},
401+
"use_classic_parameter_flow": schema.BoolAttribute{
402+
MarkdownDescription: "If true, the classic parameter flow will be used when creating workspaces from this template. This only affects deployments with the experiment \"dynamic-parameters\" enabled. Defaults to false.",
403+
Optional: true,
404+
Computed: true,
405+
PlanModifiers: []planmodifier.Bool{
406+
boolplanmodifier.UseStateForUnknown(),
407+
},
408+
},
395409
"acl": schema.SingleNestedAttribute{
396410
MarkdownDescription: "(Enterprise) Access control list for the template. If null, ACL policies will not be added, removed, or read by Terraform.",
397411
Optional: true,
@@ -588,6 +602,25 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques
588602
data.MaxPortShareLevel = types.StringValue(string(mpslResp.MaxPortShareLevel))
589603
}
590604

605+
// TODO: Remove this update call (and the attribute) once the provider
606+
// requires a Coder version where this flag has been removed.
607+
if data.UseClassicParameterFlow.IsUnknown() {
608+
data.UseClassicParameterFlow = types.BoolValue(templateResp.UseClassicParameterFlow)
609+
} else if data.UseClassicParameterFlow.ValueBool() == templateResp.UseClassicParameterFlow {
610+
tflog.Info(ctx, "use classic parameter flow set to default, not updating")
611+
} else {
612+
ucpfReq := data.toUpdateRequest(ctx, &resp.Diagnostics)
613+
if resp.Diagnostics.HasError() {
614+
return
615+
}
616+
ucpfResp, err := client.UpdateTemplateMeta(ctx, data.ID.ValueUUID(), *ucpfReq)
617+
if err != nil {
618+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to set use classic parameter flow via update: %s", err))
619+
return
620+
}
621+
data.UseClassicParameterFlow = types.BoolValue(ucpfResp.UseClassicParameterFlow)
622+
}
623+
591624
resp.Diagnostics.Append(data.Versions.setPrivateState(ctx, resp.Private)...)
592625
if resp.Diagnostics.HasError() {
593626
return
@@ -627,6 +660,7 @@ func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, r
627660
return
628661
}
629662
data.MaxPortShareLevel = types.StringValue(string(template.MaxPortShareLevel))
663+
data.UseClassicParameterFlow = types.BoolValue(template.UseClassicParameterFlow)
630664

631665
if !data.ACL.IsNull() {
632666
tflog.Info(ctx, "reading template ACL")
@@ -701,11 +735,6 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
701735

702736
client := r.data.Client
703737

704-
// TODO(ethanndickson): Remove this once the provider requires a Coder
705-
// deployment running `v2.15.0` or later.
706-
if newState.MaxPortShareLevel.IsUnknown() {
707-
newState.MaxPortShareLevel = curState.MaxPortShareLevel
708-
}
709738
templateMetadataChanged := !newState.EqualTemplateMetadata(&curState)
710739
// This is required, as the API will reject no-diff updates.
711740
if templateMetadataChanged {
@@ -1290,6 +1319,7 @@ func (r *TemplateResourceModel) toUpdateRequest(ctx context.Context, diag *diag.
12901319
RequireActiveVersion: r.RequireActiveVersion.ValueBool(),
12911320
DeprecationMessage: r.DeprecationMessage.ValueStringPointer(),
12921321
MaxPortShareLevel: ptr.Ref(codersdk.WorkspaceAgentPortShareLevel(r.MaxPortShareLevel.ValueString())),
1322+
UseClassicParameterFlow: ptr.Ref(r.UseClassicParameterFlow.ValueBool()),
12931323
// If we're managing ACL, we want to delete the everyone group
12941324
DisableEveryoneGroupAccess: !r.ACL.IsNull(),
12951325
}
@@ -1334,6 +1364,7 @@ func (r *TemplateResourceModel) toCreateRequest(ctx context.Context, resp *resou
13341364
TimeTilDormantMillis: r.TimeTilDormantMillis.ValueInt64Pointer(),
13351365
TimeTilDormantAutoDeleteMillis: r.TimeTilDormantAutoDeleteMillis.ValueInt64Pointer(),
13361366
RequireActiveVersion: r.RequireActiveVersion.ValueBool(),
1367+
UseClassicParameterFlow: r.UseClassicParameterFlow.ValueBoolPointer(),
13371368
DisableEveryoneGroupAccess: !r.ACL.IsNull(),
13381369
}
13391370
}

internal/provider/template_resource_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func TestAccTemplateResource(t *testing.T) {
116116
resource.TestCheckResourceAttr("coderd_template.test", "time_til_dormant_autodelete_ms", "0"),
117117
resource.TestCheckResourceAttr("coderd_template.test", "require_active_version", "false"),
118118
resource.TestCheckResourceAttr("coderd_template.test", "max_port_share_level", "public"),
119+
resource.TestCheckResourceAttr("coderd_template.test", "use_classic_parameter_flow", "false"),
119120
resource.TestMatchTypeSetElemNestedAttrs("coderd_template.test", "versions.*", map[string]*regexp.Regexp{
120121
"name": regexp.MustCompile(".+"),
121122
"id": regexp.MustCompile(".+"),
@@ -991,6 +992,7 @@ type testAccTemplateResourceConfig struct {
991992
RequireActiveVersion *bool
992993
DeprecationMessage *string
993994
MaxPortShareLevel *string
995+
UseClassicParameterFlow *bool
994996

995997
Versions []testAccTemplateVersionConfig
996998
ACL testAccTemplateACLConfig
@@ -1098,6 +1100,7 @@ resource "coderd_template" "test" {
10981100
require_active_version = {{orNull .RequireActiveVersion}}
10991101
deprecation_message = {{orNull .DeprecationMessage}}
11001102
max_port_share_level = {{orNull .MaxPortShareLevel}}
1103+
use_classic_parameter_flow = {{orNull .UseClassicParameterFlow}}
11011104
11021105
acl = ` + c.ACL.String(t) + `
11031106

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy