Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/resources/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ resource "coder_metadata" "pod_info" {
item {
key = "description"
value = "This description will show up in the Coder dashboard."
order = 1
}
item {
key = "pod_uid"
value = kubernetes_pod.dev[0].uid
order = 2
}
item {
key = "public_key"
value = tls_private_key.example_key_pair.public_key_openssh
# The value of this item will be hidden from view by default
sensitive = true
order = 3
}
}
```
Expand Down Expand Up @@ -84,6 +87,7 @@ Required:

Optional:

- `order` (Number) The order determines the position of item in the UI presentation. The lowest order is shown first and items with equal order are sorted by key (ascending order).
- `sensitive` (Boolean) Set to `true` to for items such as API keys whose values should be hidden from view by default. Note that this does not prevent metadata from being retrieved using the API, so it is not suitable for secrets that should not be exposed to workspace users.
- `value` (String) The value of this metadata item. Supports basic Markdown, including hyperlinks.

Expand Down
3 changes: 3 additions & 0 deletions examples/resources/coder_metadata/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ resource "coder_metadata" "pod_info" {
item {
key = "description"
value = "This description will show up in the Coder dashboard."
order = 1
}
item {
key = "pod_uid"
value = kubernetes_pod.dev[0].uid
order = 2
}
item {
key = "public_key"
value = tls_private_key.example_key_pair.public_key_openssh
# The value of this item will be hidden from view by default
sensitive = true
order = 3
}
}
6 changes: 6 additions & 0 deletions provider/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ func metadataResource() *schema.Resource {
ForceNew: true,
Computed: true,
},
"order": {
Type: schema.TypeInt,
Description: "The order determines the position of item in the UI presentation. The lowest order is shown first and items with equal order are sorted by key (ascending order).",
ForceNew: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... is ForceNew necessary @johnstcn ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure, but it is already used in agent.metadata.order so it may be no harm to be safe and stick to the status quo.

Optional: true,
},
},
},
},
Expand Down
9 changes: 9 additions & 0 deletions provider/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ func TestMetadata(t *testing.T) {
key = "secret"
value = "squirrel"
sensitive = true
order = 1
}
item {
key = "implicit_null"
order = 2
}
item {
key = "explicit_null"
value = null
order = 3
}
item {
key = "empty"
value = ""
order = 4
}
}
`,
Expand All @@ -66,19 +70,24 @@ func TestMetadata(t *testing.T) {
"item.0.key": "foo",
"item.0.value": "bar",
"item.0.sensitive": "false",
"item.0.order": "0",
"item.1.key": "secret",
"item.1.value": "squirrel",
"item.1.sensitive": "true",
"item.1.order": "1",
"item.2.key": "implicit_null",
"item.2.is_null": "true",
"item.2.sensitive": "false",
"item.2.order": "2",
"item.3.key": "explicit_null",
"item.3.is_null": "true",
"item.3.sensitive": "false",
"item.3.order": "3",
"item.4.key": "empty",
"item.4.value": "",
"item.4.is_null": "false",
"item.4.sensitive": "false",
"item.4.order": "4",
} {
require.Equal(t, expected, metadata.Primary.Attributes[key])
}
Expand Down
16 changes: 16 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/go-cty/cty/gocty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -99,10 +100,16 @@ func populateIsNull(resourceData *schema.ResourceData) (result interface{}, err
var resultItems []interface{}
for _, item := range items {
key := valueAsString(item.GetAttr("key"))
order, err := valueAsInt(item.GetAttr("order"))
if err != nil {
return nil, xerrors.Errorf("unable to parse order for coder_metadata item %q: %w", key, err)
}

resultItem := map[string]interface{}{
"key": key,
"value": valueAsString(item.GetAttr("value")),
"sensitive": valueAsBool(item.GetAttr("sensitive")),
"order": order,
}
if item.GetAttr("value").IsNull() {
resultItem["is_null"] = true
Expand Down Expand Up @@ -132,6 +139,15 @@ func valueAsBool(value cty.Value) interface{} {
return value.True()
}

func valueAsInt(value cty.Value) (interface{}, error) {
if value.IsNull() {
return nil, nil
}
var valueAsInt int64
err := gocty.FromCtyValue(value, &valueAsInt)
return valueAsInt, err
}
Comment on lines +142 to +149
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, it would be good to have a test for a non-numeric value for order.


// errorAsDiagnostic transforms a Go error to a diag.Diagnostics object representing a fatal error.
func errorAsDiagnostics(err error) diag.Diagnostics {
return []diag.Diagnostic{{
Expand Down