Skip to content

Commit 23b8031

Browse files
committed
WIP
Signed-off-by: Danny Kopping <danny@coder.com> # Conflicts: # provider/agent.go
1 parent 72bed80 commit 23b8031

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ to setup your local Terraform to use your local version rather than the registry
4747
}
4848
```
4949
2. Run `terraform init` and observe a warning like `Warning: Provider development overrides are in effect`
50-
4. Run `go build -o terraform-provider-coder` to build the provider binary, which Terraform will try locate and execute
50+
4. Run `make build` to build the provider binary, which Terraform will try locate and execute
5151
5. All local Terraform runs will now use your local provider!
5252
6. _**NOTE**: we vendor in this provider into `github.com/coder/coder`, so if you're testing with a local clone then you should also run `go mod edit -replace github.com/coder/terraform-provider-coder=/path/to/terraform-provider-coder` in your clone._
5353

provider/agent.go

+53-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"path/filepath"
78
"reflect"
89
"strings"
910

11+
"github.com/hashicorp/terraform-plugin-log/tflog"
12+
1013
"github.com/google/uuid"
1114
"github.com/hashicorp/go-cty/cty"
1215
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -22,10 +25,54 @@ func agentResource() *schema.Resource {
2225
SchemaVersion: 1,
2326

2427
Description: "Use this resource to associate an agent.",
25-
CreateContext: func(_ context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
28+
CreateContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
2629
// This should be a real authentication token!
2730
resourceData.SetId(uuid.NewString())
28-
err := resourceData.Set("token", uuid.NewString())
31+
32+
// CODER_RUNNING_WORKSPACE_AGENT_TOKEN is *only* used for prebuilds. We pass it down when we want to rebuild a prebuilt workspace
33+
// but not generate a new agent token. The provisionerdserver will retrieve this token and push it down to
34+
// here where it will be reused.
35+
// Context: the agent token is often used in immutable attributes of workspace resource (e.g. VM/container)
36+
// to initialize the agent, so if that value changes it will necessitate a replacement of that resource, thus
37+
// obviating the whole point of the prebuild.
38+
//
39+
// The default path is for a new token to be generated on each new resource creation.
40+
// TODO: add logging when the running token is actually used.
41+
var token string
42+
43+
isPrebuild := helpers.OptionalEnv(IsPrebuildEnvironmentVariable()) == "true"
44+
if !isPrebuild {
45+
token = os.Getenv(RunningAgentTokenEnvironmentVariable())
46+
}
47+
48+
allEnv := make(map[string]interface{})
49+
for _, v := range os.Environ() {
50+
split := strings.Split(v, "=")
51+
var key, val string
52+
if len(split) > 0 {
53+
key = split[0]
54+
}
55+
if len(split) > 1 {
56+
val = split[1]
57+
}
58+
59+
allEnv[key] = val
60+
}
61+
62+
allEnv["is_prebuild"] = fmt.Sprintf("%v", isPrebuild)
63+
64+
if token == "" {
65+
token = uuid.NewString()
66+
if !isPrebuild {
67+
tflog.Warn(ctx, "NOT USING EXISTING AGENT TOKEN", allEnv)
68+
}
69+
} else {
70+
if !isPrebuild {
71+
tflog.Info(ctx, "IS USING EXISTING AGENT TOKEN", allEnv)
72+
}
73+
}
74+
75+
err := resourceData.Set("token", token)
2976
if err != nil {
3077
return diag.FromErr(err)
3178
}
@@ -469,3 +516,7 @@ func updateInitScript(resourceData *schema.ResourceData, i interface{}) diag.Dia
469516
}
470517
return nil
471518
}
519+
520+
func RunningAgentTokenEnvironmentVariable() string {
521+
return "CODER_RUNNING_WORKSPACE_AGENT_TOKEN"
522+
}

provider/workspace.go

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

30+
prebuild := helpers.OptionalEnv(IsPrebuildEnvironmentVariable())
31+
prebuildCount := 0
32+
if prebuild == "true" {
33+
prebuildCount = 1
34+
_ = rd.Set("is_prebuild", true)
35+
}
36+
_ = rd.Set("prebuild_count", prebuildCount)
37+
3038
name := helpers.OptionalEnvOrDefault("CODER_WORKSPACE_NAME", "default")
3139
rd.Set("name", name)
3240

@@ -88,6 +96,16 @@ func workspaceDataSource() *schema.Resource {
8896
Computed: true,
8997
Description: "A computed count based on `transition` state. If `start`, count will equal 1.",
9098
},
99+
"prebuild_count": {
100+
Type: schema.TypeInt,
101+
Computed: true,
102+
Description: "TODO",
103+
},
104+
"is_prebuild": {
105+
Type: schema.TypeBool,
106+
Computed: true,
107+
Description: "TODO",
108+
},
91109
"transition": {
92110
Type: schema.TypeString,
93111
Computed: true,
@@ -121,3 +139,7 @@ func workspaceDataSource() *schema.Resource {
121139
},
122140
}
123141
}
142+
143+
func IsPrebuildEnvironmentVariable() string {
144+
return "CODER_WORKSPACE_IS_PREBUILD"
145+
}

0 commit comments

Comments
 (0)