Skip to content

Commit e51bf1c

Browse files
committed
simplify function
1 parent 5a2fd97 commit e51bf1c

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/docker/docker v26.1.5+incompatible
77
github.com/google/uuid v1.6.0
88
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
9+
github.com/hashicorp/terraform-plugin-log v0.9.0
910
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1
1011
github.com/masterminds/semver v1.5.0
1112
github.com/mitchellh/mapstructure v1.5.0
@@ -50,7 +51,6 @@ require (
5051
github.com/hashicorp/terraform-exec v0.22.0 // indirect
5152
github.com/hashicorp/terraform-json v0.24.0 // indirect
5253
github.com/hashicorp/terraform-plugin-go v0.26.0 // indirect
53-
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
5454
github.com/hashicorp/terraform-registry-address v0.2.4 // indirect
5555
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
5656
github.com/hashicorp/yamux v0.1.1 // indirect

provider/agent.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -478,27 +478,22 @@ func updateInitScript(resourceData *schema.ResourceData, i interface{}) diag.Dia
478478
}
479479

480480
func agentAuthToken(ctx context.Context, agentID string) string {
481-
// Most of the time, we will generate a new token for the agent.
482-
// In the case of a prebuilt workspace being claimed, we will override with
483-
// an existing token provided below.
484-
token := uuid.NewString()
485-
486481
existingToken := helpers.OptionalEnv(RunningAgentTokenEnvironmentVariable(agentID))
487-
logFields := map[string]interface{}{
488-
"agent_id": agentID,
489-
"token_provided": existingToken != "",
490-
}
491-
if existingToken != "" {
492-
// An existing token was provided for this agent. That means that this
493-
// is a prebuilt workspace in the process of being claimed.
494-
// We should reuse the token.
495-
tflog.Info(ctx, "using provided agent token for prebuild", logFields)
496-
token = existingToken
497-
} else {
498-
tflog.Info(ctx, "using a new agent token", logFields)
482+
if existingToken == "" {
483+
// Most of the time, we will generate a new token for the agent.
484+
// In the case of a prebuilt workspace being claimed, we will override with
485+
// an existing token provided below.
486+
token := uuid.NewString()
487+
return token
499488
}
500489

501-
return token
490+
// An existing token was provided for this agent. That means that this
491+
// is a prebuilt workspace in the process of being claimed.
492+
// We should reuse the token.
493+
tflog.Info(ctx, "using provided agent token for prebuild", map[string]interface{}{
494+
"agent_id": agentID,
495+
})
496+
return existingToken
502497
}
503498

504499
// RunningAgentTokenEnvironmentVariable returns the name of an environment variable

provider/workspace.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ func workspaceDataSource() *schema.Resource {
2727
}
2828
_ = rd.Set("start_count", count)
2929

30-
isPrebuild := isPrebuiltWorkspace()
31-
prebuildCount := 0
32-
if isPrebuild {
33-
prebuildCount = 1
30+
if isPrebuiltWorkspace() {
31+
_ = rd.Set("prebuild_count", 1)
3432
_ = rd.Set("is_prebuild", true)
33+
} else {
34+
_ = rd.Set("prebuild_count", 0)
35+
_ = rd.Set("is_prebuild", false)
3536
}
36-
_ = rd.Set("prebuild_count", prebuildCount)
3737

3838
name := helpers.OptionalEnvOrDefault("CODER_WORKSPACE_NAME", "default")
3939
rd.Set("name", name)

0 commit comments

Comments
 (0)