Skip to content

Commit bfa0c5a

Browse files
authored
Add metadata block to the agent (#112)
1 parent d4e356f commit bfa0c5a

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

docs/resources/agent.md

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ resource "kubernetes_pod" "dev" {
5151
- `dir` (String) The starting directory when a user creates a shell session. Defaults to $HOME.
5252
- `env` (Map of String) A mapping of environment variables to set inside the workspace.
5353
- `login_before_ready` (Boolean) This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.
54+
- `metadata` (Block List) Each "metadata" block defines a single item consisting of a key/value pair. This feature is in alpha and may break in future releases. (see [below for nested schema](#nestedblock--metadata))
5455
- `motd_file` (String) The path to a file within the workspace containing a message to display to users when they login via SSH. A typical value would be /etc/motd.
5556
- `shutdown_script` (String) A script to run before the agent is stopped. The script should exit when it is done to signal that the workspace can be stopped.
5657
- `shutdown_script_timeout` (Number) Time in seconds until the agent lifecycle status is marked as timed out during shutdown, this happens when the shutdown script has not completed (exited) in the given time.
@@ -64,4 +65,18 @@ resource "kubernetes_pod" "dev" {
6465
- `init_script` (String) Run this script on startup of an instance to initialize the agent.
6566
- `token` (String, Sensitive) Set the environment variable "CODER_AGENT_TOKEN" with this token to authenticate an agent.
6667

68+
<a id="nestedblock--metadata"></a>
69+
### Nested Schema for `metadata`
70+
71+
Required:
72+
73+
- `interval` (Number) The interval in seconds at which to refresh this metadata item.
74+
- `key` (String) The key of this metadata item.
75+
- `script` (String) The script that retrieves the value of this metadata item.
76+
77+
Optional:
78+
79+
- `display_name` (String) The user-facing name of this value.
80+
- `timeout` (Number) The maximum time the command is allowed to run in seconds.
81+
6782

provider/agent.go

+43
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,49 @@ func agentResource() *schema.Resource {
137137
Optional: true,
138138
Description: "This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.",
139139
},
140+
"metadata": {
141+
Type: schema.TypeList,
142+
Description: "Each \"metadata\" block defines a single item consisting of a key/value pair. This feature is in alpha and may break in future releases.",
143+
ForceNew: true,
144+
Optional: true,
145+
Elem: &schema.Resource{
146+
Schema: map[string]*schema.Schema{
147+
"key": {
148+
Type: schema.TypeString,
149+
Description: "The key of this metadata item.",
150+
ForceNew: true,
151+
Required: true,
152+
},
153+
"display_name": {
154+
Type: schema.TypeString,
155+
Description: "The user-facing name of this value.",
156+
ForceNew: true,
157+
Optional: true,
158+
},
159+
"script": {
160+
Type: schema.TypeString,
161+
Description: "The script that retrieves the value of this metadata item.",
162+
ForceNew: true,
163+
Required: true,
164+
Elem: &schema.Schema{
165+
Type: schema.TypeString,
166+
},
167+
},
168+
"timeout": {
169+
Type: schema.TypeInt,
170+
Description: "The maximum time the command is allowed to run in seconds.",
171+
ForceNew: true,
172+
Optional: true,
173+
},
174+
"interval": {
175+
Type: schema.TypeInt,
176+
Description: "The interval in seconds at which to refresh this metadata item. ",
177+
ForceNew: true,
178+
Required: true,
179+
},
180+
},
181+
},
182+
},
140183
},
141184
}
142185
}

provider/agent_test.go

+47-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestAgent(t *testing.T) {
7171
})
7272
}
7373

74-
func TestAgentInstance(t *testing.T) {
74+
func TestAgent_Instance(t *testing.T) {
7575
t.Parallel()
7676
resource.Test(t, resource.TestCase{
7777
Providers: map[string]*schema.Provider{
@@ -111,3 +111,49 @@ func TestAgentInstance(t *testing.T) {
111111
}},
112112
})
113113
}
114+
115+
func TestAgent_Metadata(t *testing.T) {
116+
t.Parallel()
117+
resource.Test(t, resource.TestCase{
118+
Providers: map[string]*schema.Provider{
119+
"coder": provider.New(),
120+
},
121+
IsUnitTest: true,
122+
Steps: []resource.TestStep{{
123+
Config: `
124+
provider "coder" {
125+
url = "https://example.com"
126+
}
127+
resource "coder_agent" "dev" {
128+
os = "linux"
129+
arch = "amd64"
130+
metadata {
131+
key = "process_count"
132+
display_name = "Process Count"
133+
script = "ps aux | wc -l"
134+
interval = 5
135+
timeout = 1
136+
}
137+
}
138+
`,
139+
Check: func(state *terraform.State) error {
140+
require.Len(t, state.Modules, 1)
141+
require.Len(t, state.Modules[0].Resources, 1)
142+
143+
resource := state.Modules[0].Resources["coder_agent.dev"]
144+
require.NotNil(t, resource)
145+
146+
t.Logf("resource: %v", resource.Primary.Attributes)
147+
148+
attr := resource.Primary.Attributes
149+
require.Equal(t, "1", attr["metadata.#"])
150+
require.Equal(t, "process_count", attr["metadata.0.key"])
151+
require.Equal(t, "Process Count", attr["metadata.0.display_name"])
152+
require.Equal(t, "ps aux | wc -l", attr["metadata.0.script"])
153+
require.Equal(t, "5", attr["metadata.0.interval"])
154+
require.Equal(t, "1", attr["metadata.0.timeout"])
155+
return nil
156+
},
157+
}},
158+
})
159+
}

0 commit comments

Comments
 (0)