Skip to content

Commit 6a06f8f

Browse files
committed
Add metadata block to the agent
1 parent 23a23a2 commit 6a06f8f

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

docs/resources/agent.md

+14
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,17 @@ 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+
- `cmd` (String) The command that retrieves the value of this metadata item.
74+
- `interval` (Number) The interval in seconds at which to refresh this metadata item.
75+
- `key` (String) The key of this metadata item.
76+
77+
Optional:
78+
79+
- `display_name` (String) The user-facing name of this value.
80+
6781

provider/agent.go

+34
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,40 @@ 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+
"cmd": {
160+
Type: schema.TypeString,
161+
Description: "The command that retrieves the value of this metadata item.",
162+
ForceNew: true,
163+
Required: true,
164+
},
165+
"interval": {
166+
Type: schema.TypeInt,
167+
Description: "The interval in seconds at which to refresh this metadata item. ",
168+
ForceNew: true,
169+
Required: true,
170+
},
171+
},
172+
},
173+
},
140174
},
141175
}
142176
}

provider/agent_test.go

+45-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,47 @@ 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+
cmd = "ps aux | wc -l"
134+
interval = 5
135+
}
136+
}
137+
`,
138+
Check: func(state *terraform.State) error {
139+
require.Len(t, state.Modules, 1)
140+
require.Len(t, state.Modules[0].Resources, 1)
141+
142+
resource := state.Modules[0].Resources["coder_agent.dev"]
143+
require.NotNil(t, resource)
144+
145+
t.Logf("resource: %v", resource.Primary.Attributes)
146+
147+
attr := resource.Primary.Attributes
148+
require.Equal(t, "1", attr["metadata.#"])
149+
require.Equal(t, "process_count", attr["metadata.0.key"])
150+
require.Equal(t, "Process Count", attr["metadata.0.display_name"])
151+
require.Equal(t, "ps aux | wc -l", attr["metadata.0.cmd"])
152+
require.Equal(t, "5", attr["metadata.0.interval"])
153+
return nil
154+
},
155+
}},
156+
})
157+
}

0 commit comments

Comments
 (0)