Skip to content

Commit

Permalink
Add metadata block to the agent
Browse files Browse the repository at this point in the history
  • Loading branch information
ammario committed Mar 15, 2023
1 parent 23a23a2 commit 6a06f8f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/resources/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ resource "kubernetes_pod" "dev" {
- `dir` (String) The starting directory when a user creates a shell session. Defaults to $HOME.
- `env` (Map of String) A mapping of environment variables to set inside the workspace.
- `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.
- `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))
- `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.
- `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.
- `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.
Expand All @@ -64,4 +65,17 @@ resource "kubernetes_pod" "dev" {
- `init_script` (String) Run this script on startup of an instance to initialize the agent.
- `token` (String, Sensitive) Set the environment variable "CODER_AGENT_TOKEN" with this token to authenticate an agent.

<a id="nestedblock--metadata"></a>
### Nested Schema for `metadata`

Required:

- `cmd` (String) The command that retrieves the value of this metadata item.
- `interval` (Number) The interval in seconds at which to refresh this metadata item.
- `key` (String) The key of this metadata item.

Optional:

- `display_name` (String) The user-facing name of this value.


34 changes: 34 additions & 0 deletions provider/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,40 @@ func agentResource() *schema.Resource {
Optional: true,
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.",
},
"metadata": {
Type: schema.TypeList,
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.",
ForceNew: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Description: "The key of this metadata item.",
ForceNew: true,
Required: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The user-facing name of this value.",
ForceNew: true,
Optional: true,
},
"cmd": {
Type: schema.TypeString,
Description: "The command that retrieves the value of this metadata item.",
ForceNew: true,
Required: true,
},
"interval": {
Type: schema.TypeInt,
Description: "The interval in seconds at which to refresh this metadata item. ",
ForceNew: true,
Required: true,
},
},
},
},
},
}
}
Expand Down
46 changes: 45 additions & 1 deletion provider/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestAgent(t *testing.T) {
})
}

func TestAgentInstance(t *testing.T) {
func TestAgent_Instance(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
Expand Down Expand Up @@ -111,3 +111,47 @@ func TestAgentInstance(t *testing.T) {
}},
})
}

func TestAgent_Metadata(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
url = "https://example.com"
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
metadata {
key = "process_count"
display_name = "Process Count"
cmd = "ps aux | wc -l"
interval = 5
}
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)

resource := state.Modules[0].Resources["coder_agent.dev"]
require.NotNil(t, resource)

t.Logf("resource: %v", resource.Primary.Attributes)

attr := resource.Primary.Attributes
require.Equal(t, "1", attr["metadata.#"])
require.Equal(t, "process_count", attr["metadata.0.key"])
require.Equal(t, "Process Count", attr["metadata.0.display_name"])
require.Equal(t, "ps aux | wc -l", attr["metadata.0.cmd"])
require.Equal(t, "5", attr["metadata.0.interval"])
return nil
},
}},
})
}

0 comments on commit 6a06f8f

Please sign in to comment.