-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add "coder_metadata" resource #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
0d8385d
c5bc403
37daed4
d901870
73a59e4
73f0c3f
b10fb1c
b3755bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "coder_metadata Resource - terraform-provider-coder" | ||
subcategory: "" | ||
description: |- | ||
Use this resource to attach key/value pairs to a resource. They will be displayed in the Coder dashboard. | ||
--- | ||
|
||
# coder_metadata (Resource) | ||
|
||
Use this resource to attach key/value pairs to a resource. They will be displayed in the Coder dashboard. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "coder_workspace" "me" { | ||
} | ||
|
||
resource "kubernetes_pod" "dev" { | ||
count = data.coder_workspace.me.start_count | ||
} | ||
|
||
resource "coder_metadata" "pod_info" { | ||
count = data.coder_workspace.me.start_count | ||
resource_id = kubernetes_pod.dev[0].id | ||
pair { | ||
key = "pod_uid" | ||
value = kubernetes_pod.dev[0].uid | ||
} | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `pair` (Block List, Min: 1) Each "pair" block defines a single key/value metadata pair. (see [below for nested schema](#nestedblock--pair)) | ||
- `resource_id` (String) The "id" property of another resource that metadata should be attached to. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
<a id="nestedblock--pair"></a> | ||
### Nested Schema for `pair` | ||
|
||
Required: | ||
|
||
- `key` (String) The key of this metadata item. | ||
|
||
Optional: | ||
|
||
- `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. | ||
|
||
Read-Only: | ||
|
||
- `is_null` (Boolean) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
data "coder_workspace" "me" { | ||
} | ||
|
||
resource "kubernetes_pod" "dev" { | ||
count = data.coder_workspace.me.start_count | ||
} | ||
|
||
resource "coder_metadata" "pod_info" { | ||
count = data.coder_workspace.me.start_count | ||
resource_id = kubernetes_pod.dev[0].id | ||
pair { | ||
key = "pod_uid" | ||
value = kubernetes_pod.dev[0].uid | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ package provider | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
|
@@ -318,6 +320,75 @@ func New() *schema.Provider { | |
}, | ||
}, | ||
}, | ||
"coder_metadata": { | ||
Description: "Use this resource to attach key/value pairs to a resource. They will be " + | ||
"displayed in the Coder dashboard.", | ||
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
resourceData.SetId(uuid.NewString()) | ||
|
||
pairs, err := populateIsNull(resourceData) | ||
if err != nil { | ||
return errorAsDiagnostics(err) | ||
} | ||
err = resourceData.Set("pair", pairs) | ||
if err != nil { | ||
return errorAsDiagnostics(err) | ||
} | ||
|
||
return nil | ||
}, | ||
ReadContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
return nil | ||
}, | ||
DeleteContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
return nil | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"resource_id": { | ||
Type: schema.TypeString, | ||
Description: "The \"id\" property of another resource that metadata should be attached to.", | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
"pair": { | ||
Type: schema.TypeList, | ||
Description: "Each \"pair\" block defines a single key/value metadata pair.", | ||
ForceNew: true, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"key": { | ||
Type: schema.TypeString, | ||
Description: "The key of this metadata item.", | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Description: "The value of this metadata item.", | ||
ForceNew: true, | ||
Optional: true, | ||
}, | ||
"sensitive": { | ||
Type: schema.TypeBool, | ||
Description: "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.", | ||
ForceNew: true, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"is_null": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we just assume an empty string is null? Maybe that's too lossy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly! I implemented it this way because the original ticket asked for null support, and I figured somebody who's used to Terraform's semantics wouldn't necessarily be expecting us to conflate null and "" the way Go does. But if we're OK with that behavior, I definitely wouldn't mind getting rid of this hack. |
||
Type: schema.TypeBool, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -356,3 +427,62 @@ func updateInitScript(resourceData *schema.ResourceData, i interface{}) diag.Dia | |
} | ||
return nil | ||
} | ||
|
||
// populateIsNull reads the raw plan for a coder_metadata resource being created, | ||
// figures out which items have null "value"s, and augments them by setting the | ||
// "is_null" field to true. This ugly hack is necessary because terraform-plugin-sdk | ||
// is designed around a old version of Terraform that didn't support nullable fields, | ||
// and it doesn't correctly propagate null values for primitive types. | ||
// Returns an interface{} representing the new value of the "pair" field, or an error. | ||
func populateIsNull(resourceData *schema.ResourceData) (result interface{}, err error) { | ||
// The cty package reports type mismatches by panicking | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = errors.New(fmt.Sprintf("panic while handling coder_metadata: %#v", r)) | ||
} | ||
}() | ||
|
||
rawPlan := resourceData.GetRawPlan() | ||
pairs := rawPlan.GetAttr("pair").AsValueSlice() | ||
|
||
var resultPairs []interface{} | ||
for _, pair := range pairs { | ||
resultPair := map[string]interface{}{ | ||
"key": valueAsString(pair.GetAttr("key")), | ||
"value": valueAsString(pair.GetAttr("value")), | ||
"sensitive": valueAsBool(pair.GetAttr("sensitive")), | ||
} | ||
if pair.GetAttr("value").IsNull() { | ||
resultPair["is_null"] = true | ||
} | ||
resultPairs = append(resultPairs, resultPair) | ||
} | ||
|
||
return resultPairs, nil | ||
} | ||
|
||
// valueAsString takes a cty.Value that may be a string or null, and converts it to either a Go string | ||
// or a nil interface{} | ||
func valueAsString(value cty.Value) interface{} { | ||
if value.IsNull() { | ||
return "" | ||
} | ||
return value.AsString() | ||
} | ||
|
||
// valueAsString takes a cty.Value that may be a boolean or null, and converts it to either a Go bool | ||
// or a nil interface{} | ||
func valueAsBool(value cty.Value) interface{} { | ||
if value.IsNull() { | ||
return nil | ||
} | ||
return value.True() | ||
} | ||
|
||
// errorAsDiagnostic transforms a Go error to a diag.Diagnostics object representing a fatal error. | ||
func errorAsDiagnostics(err error) diag.Diagnostics { | ||
return []diag.Diagnostic{{ | ||
Severity: diag.Error, | ||
Summary: err.Error(), | ||
}} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was confused about why
key
andvalue
were nested until @kylecarbs explained to me that HCL allows multiple pair blocks. We should demonstrate that here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I updated the example to use multiple blocks, and also to demonstrate the
sensitive
attribute.