Skip to content

Commit d565cb5

Browse files
committed
chore: pass previous values into the terraform apply
remove custom monotonic check
1 parent df0c6ed commit d565cb5

File tree

14 files changed

+519
-466
lines changed

14 files changed

+519
-466
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,28 @@ func (s *server) acquireProtoJob(ctx context.Context, job database.ProvisionerJo
543543
return nil, failJob(fmt.Sprintf("convert workspace transition: %s", err))
544544
}
545545

546+
// A previous workspace build exists
547+
var lastWorkspaceBuildParameters []database.WorkspaceBuildParameter
548+
if workspaceBuild.BuildNumber > 1 {
549+
// TODO: Should we fetch the last build that succeeded? This fetches the
550+
// previous build regardless of the status of the build.
551+
buildNum := workspaceBuild.BuildNumber - 1
552+
previous, err := s.Database.GetWorkspaceBuildByWorkspaceIDAndBuildNumber(ctx, database.GetWorkspaceBuildByWorkspaceIDAndBuildNumberParams{
553+
WorkspaceID: workspaceBuild.WorkspaceID,
554+
BuildNumber: buildNum,
555+
})
556+
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
557+
return nil, xerrors.Errorf("get last build with number=%d: %w", buildNum, err)
558+
}
559+
560+
if err == nil {
561+
lastWorkspaceBuildParameters, err = s.Database.GetWorkspaceBuildParameters(ctx, previous.ID)
562+
if err != nil {
563+
return nil, xerrors.Errorf("get last build parameters %q: %w", previous.ID, err)
564+
}
565+
}
566+
}
567+
546568
workspaceBuildParameters, err := s.Database.GetWorkspaceBuildParameters(ctx, workspaceBuild.ID)
547569
if err != nil {
548570
return nil, failJob(fmt.Sprintf("get workspace build parameters: %s", err))
@@ -619,12 +641,13 @@ func (s *server) acquireProtoJob(ctx context.Context, job database.ProvisionerJo
619641

620642
protoJob.Type = &proto.AcquiredJob_WorkspaceBuild_{
621643
WorkspaceBuild: &proto.AcquiredJob_WorkspaceBuild{
622-
WorkspaceBuildId: workspaceBuild.ID.String(),
623-
WorkspaceName: workspace.Name,
624-
State: workspaceBuild.ProvisionerState,
625-
RichParameterValues: convertRichParameterValues(workspaceBuildParameters),
626-
VariableValues: asVariableValues(templateVariables),
627-
ExternalAuthProviders: externalAuthProviders,
644+
WorkspaceBuildId: workspaceBuild.ID.String(),
645+
WorkspaceName: workspace.Name,
646+
State: workspaceBuild.ProvisionerState,
647+
RichParameterValues: convertRichParameterValues(workspaceBuildParameters),
648+
PreviousParameterValues: convertRichParameterValues(lastWorkspaceBuildParameters),
649+
VariableValues: asVariableValues(templateVariables),
650+
ExternalAuthProviders: externalAuthProviders,
628651
Metadata: &sdkproto.Metadata{
629652
CoderUrl: s.AccessURL.String(),
630653
WorkspaceTransition: transition,

codersdk/richparameters.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package codersdk
22

33
import (
4-
"strconv"
5-
64
"golang.org/x/xerrors"
75

86
"github.com/coder/terraform-provider-coder/v2/provider"
@@ -60,29 +58,6 @@ func validateBuildParameter(richParameter TemplateVersionParameter, buildParamet
6058
value = richParameter.DefaultValue
6159
}
6260

63-
if lastBuildParameter != nil && lastBuildParameter.Value != "" && richParameter.Type == "number" && len(richParameter.ValidationMonotonic) > 0 {
64-
prev, err := strconv.Atoi(lastBuildParameter.Value)
65-
if err != nil {
66-
return xerrors.Errorf("previous parameter value is not a number: %s", lastBuildParameter.Value)
67-
}
68-
69-
current, err := strconv.Atoi(buildParameter.Value)
70-
if err != nil {
71-
return xerrors.Errorf("current parameter value is not a number: %s", buildParameter.Value)
72-
}
73-
74-
switch richParameter.ValidationMonotonic {
75-
case MonotonicOrderIncreasing:
76-
if prev > current {
77-
return xerrors.Errorf("parameter value must be equal or greater than previous value: %d", prev)
78-
}
79-
case MonotonicOrderDecreasing:
80-
if prev < current {
81-
return xerrors.Errorf("parameter value must be equal or lower than previous value: %d", prev)
82-
}
83-
}
84-
}
85-
8661
if len(richParameter.Options) > 0 {
8762
var matched bool
8863
for _, opt := range richParameter.Options {
@@ -119,7 +94,13 @@ func validateBuildParameter(richParameter TemplateVersionParameter, buildParamet
11994
Error: richParameter.ValidationError,
12095
Monotonic: string(richParameter.ValidationMonotonic),
12196
}
122-
return validation.Valid(richParameter.Type, value)
97+
var prev *string
98+
// Empty strings should be rejected, however the previous behavior was to
99+
// accept the empty string ("") as a `nil` previous value.
100+
if lastBuildParameter != nil && lastBuildParameter.Value != "" {
101+
prev = &lastBuildParameter.Value
102+
}
103+
return validation.Valid(richParameter.Type, value, prev)
123104
}
124105

125106
func findBuildParameter(params []WorkspaceBuildParameter, parameterName string) (*WorkspaceBuildParameter, bool) {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ require (
101101
github.com/coder/quartz v0.1.2
102102
github.com/coder/retry v1.5.1
103103
github.com/coder/serpent v0.10.0
104-
github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250417100258-c86bb5c3ddcd
104+
github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250506184715-e011f733bf27
105105
github.com/coder/websocket v1.8.13
106106
github.com/coder/wgtunnel v0.1.13-0.20240522110300-ade90dfb2da0
107107
github.com/coreos/go-oidc/v3 v3.14.1
@@ -488,7 +488,7 @@ require (
488488

489489
require (
490490
github.com/anthropics/anthropic-sdk-go v0.2.0-beta.3
491-
github.com/coder/preview v0.0.1
491+
github.com/coder/preview v0.0.2-0.20250506195323-154d86b5a92a
492492
github.com/fsnotify/fsnotify v1.9.0
493493
github.com/kylecarbs/aisdk-go v0.0.8
494494
github.com/mark3labs/mcp-go v0.25.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,8 @@ github.com/coder/pq v1.10.5-0.20240813183442-0c420cb5a048 h1:3jzYUlGH7ZELIH4XggX
907907
github.com/coder/pq v1.10.5-0.20240813183442-0c420cb5a048/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
908908
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
909909
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
910-
github.com/coder/preview v0.0.1 h1:2X5McKdMOZJILTIDf7qRplXKupT+91qTJBN67XUh5cA=
911-
github.com/coder/preview v0.0.1/go.mod h1:eInDmOdSDF8cxCvapIvYkGRzmzvcvGAFL1HYqcA4g+E=
910+
github.com/coder/preview v0.0.2-0.20250506195323-154d86b5a92a h1:tmtq3YgYE69PKA7n10WRFZPQnLp45N3jPOcpr4Ki6z4=
911+
github.com/coder/preview v0.0.2-0.20250506195323-154d86b5a92a/go.mod h1:j2JOd9aN+pGLxBxOawtm+1pF3kgWdbn5xGvnDSqER+8=
912912
github.com/coder/quartz v0.1.2 h1:PVhc9sJimTdKd3VbygXtS4826EOCpB1fXoRlLnCrE+s=
913913
github.com/coder/quartz v0.1.2/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA=
914914
github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=
@@ -921,8 +921,8 @@ github.com/coder/tailscale v1.1.1-0.20250422090654-5090e715905e h1:nope/SZfoLB9M
921921
github.com/coder/tailscale v1.1.1-0.20250422090654-5090e715905e/go.mod h1:1ggFFdHTRjPRu9Yc1yA7nVHBYB50w9Ce7VIXNqcW6Ko=
922922
github.com/coder/terraform-config-inspect v0.0.0-20250107175719-6d06d90c630e h1:JNLPDi2P73laR1oAclY6jWzAbucf70ASAvf5mh2cME0=
923923
github.com/coder/terraform-config-inspect v0.0.0-20250107175719-6d06d90c630e/go.mod h1:Gz/z9Hbn+4KSp8A2FBtNszfLSdT2Tn/uAKGuVqqWmDI=
924-
github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250417100258-c86bb5c3ddcd h1:FsIG6Fd0YOEK7D0Hl/CJywRA+Y6Gd5RQbSIa2L+/BmE=
925-
github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250417100258-c86bb5c3ddcd/go.mod h1:56/KdGYaA+VbwXJbTI8CA57XPfnuTxN8rjxbR34PbZw=
924+
github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250506184715-e011f733bf27 h1:CLJwMqst39+wfFehYQzVOiG5uXUtC5fbAZ3/EpxOWos=
925+
github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250506184715-e011f733bf27/go.mod h1:2kaBpn5k9ZWtgKq5k4JbkVZG9DzEqR4mJSmpdshcO+s=
926926
github.com/coder/trivy v0.0.0-20250409153844-e6b004bc465a h1:yryP7e+IQUAArlycH4hQrjXQ64eRNbxsV5/wuVXHgME=
927927
github.com/coder/trivy v0.0.0-20250409153844-e6b004bc465a/go.mod h1:dDvq9axp3kZsT63gY2Znd1iwzfqDq3kXbQnccIrjRYY=
928928
github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE=

provisioner/terraform/provision.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (s *server) Plan(
152152

153153
s.logger.Debug(ctx, "ran initialization")
154154

155-
env, err := provisionEnv(sess.Config, request.Metadata, request.RichParameterValues, request.ExternalAuthProviders)
155+
env, err := provisionEnv(sess.Config, request.Metadata, request.PreviousParameterValues, request.RichParameterValues, request.ExternalAuthProviders)
156156
if err != nil {
157157
return provisionersdk.PlanErrorf("setup env: %s", err)
158158
}
@@ -205,7 +205,7 @@ func (s *server) Apply(
205205

206206
// Earlier in the session, Plan() will have written the state file and the plan file.
207207
statefilePath := getStateFilePath(sess.WorkDirectory)
208-
env, err := provisionEnv(sess.Config, request.Metadata, nil, nil)
208+
env, err := provisionEnv(sess.Config, request.Metadata, nil, nil, nil)
209209
if err != nil {
210210
return provisionersdk.ApplyErrorf("provision env: %s", err)
211211
}
@@ -236,7 +236,7 @@ func planVars(plan *proto.PlanRequest) ([]string, error) {
236236

237237
func provisionEnv(
238238
config *proto.Config, metadata *proto.Metadata,
239-
richParams []*proto.RichParameterValue, externalAuth []*proto.ExternalAuthProvider,
239+
previousParams, richParams []*proto.RichParameterValue, externalAuth []*proto.ExternalAuthProvider,
240240
) ([]string, error) {
241241
env := safeEnviron()
242242
ownerGroups, err := json.Marshal(metadata.GetWorkspaceOwnerGroups())
@@ -277,6 +277,9 @@ func provisionEnv(
277277
for key, value := range provisionersdk.AgentScriptEnv() {
278278
env = append(env, key+"="+value)
279279
}
280+
for _, param := range previousParams {
281+
env = append(env, provider.ParameterEnvironmentVariablePrevious(param.Name)+"="+param.Value)
282+
}
280283
for _, param := range richParams {
281284
env = append(env, provider.ParameterEnvironmentVariable(param.Name)+"="+param.Value)
282285
}

provisioner/terraform/resources.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,13 +749,17 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
749749
if err != nil {
750750
return nil, xerrors.Errorf("decode map values for coder_parameter.%s: %w", resource.Name, err)
751751
}
752+
def := ""
753+
if param.Default != nil {
754+
def = *param.Default
755+
}
752756
protoParam := &proto.RichParameter{
753757
Name: param.Name,
754758
DisplayName: param.DisplayName,
755759
Description: param.Description,
756760
Type: param.Type,
757761
Mutable: param.Mutable,
758-
DefaultValue: param.Default,
762+
DefaultValue: def,
759763
Icon: param.Icon,
760764
Required: !param.Optional,
761765
// #nosec G115 - Safe conversion as parameter order value is expected to be within int32 range

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