From 29a323118246f2e94069295f736a626fd9b810df Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 9 Aug 2022 16:26:47 +0000 Subject: [PATCH 1/2] feat: Add `coder_host` resource for attributes from the host Our [Docker template](https://github.com/coder/coder/blob/main/examples/templates/docker/main.tf#L16-L60) exposes variables which are easily obtainable from the host. By adding this resource, we allow simple setups that depend on host architecture to work without input from the user. --- docs/data-sources/host.md | 24 ++++++++++++++++++++++++ internal/provider/provider.go | 23 +++++++++++++++++++++++ internal/provider/provider_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 docs/data-sources/host.md diff --git a/docs/data-sources/host.md b/docs/data-sources/host.md new file mode 100644 index 00000000..a4c5e285 --- /dev/null +++ b/docs/data-sources/host.md @@ -0,0 +1,24 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "coder_host Data Source - terraform-provider-coder" +subcategory: "" +description: |- + Use this data source to get information about the host. +--- + +# coder_host (Data Source) + +Use this data source to get information about the host. + + + + +## Schema + +### Read-Only + +- `arch` (String) The architecture of the host. +- `id` (String) The ID of this resource. +- `os` (String) The operating system of the host. + + diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ef6dc1fe..330106f7 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -7,6 +7,7 @@ import ( "net/url" "os" "reflect" + "runtime" "strings" "github.com/google/uuid" @@ -150,6 +151,28 @@ func New() *schema.Provider { }, }, }, + "coder_host": { + Description: "Use this data source to get information about the host.", + ReadContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { + rd.SetId(uuid.NewString()) + rd.Set("os", runtime.GOOS) + rd.Set("arch", runtime.GOARCH) + + return nil + }, + Schema: map[string]*schema.Schema{ + "os": { + Type: schema.TypeString, + Computed: true, + Description: "The operating system of the host.", + }, + "arch": { + Type: schema.TypeString, + Computed: true, + Description: "The architecture of the host.", + }, + }, + }, }, ResourcesMap: map[string]*schema.Resource{ "coder_agent": { diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index e82c5ad7..7e19d7d7 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -1,6 +1,7 @@ package provider_test import ( + "runtime" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -52,6 +53,33 @@ func TestWorkspace(t *testing.T) { }) } +func TestHost(t *testing.T) { + resource.Test(t, resource.TestCase{ + Providers: map[string]*schema.Provider{ + "coder": provider.New(), + }, + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + provider "coder" { + } + data "coder_host" "me" { + }`, + 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["data.coder_host.me"] + require.NotNil(t, resource) + + attribs := resource.Primary.Attributes + require.Equal(t, runtime.GOOS, attribs["os"]) + require.Equal(t, runtime.GOARCH, attribs["arch"]) + return nil + }, + }}, + }) +} + func TestAgent(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ From 6db84bf348268756e30f711d2cc0ec6251e3c4d7 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 9 Aug 2022 11:57:29 -0500 Subject: [PATCH 2/2] Update internal/provider/provider.go Co-authored-by: Ben Potter --- docs/data-sources/host.md | 24 ------------------------ docs/data-sources/provisioner.md | 24 ++++++++++++++++++++++++ internal/provider/provider.go | 8 ++++---- internal/provider/provider_test.go | 6 +++--- 4 files changed, 31 insertions(+), 31 deletions(-) delete mode 100644 docs/data-sources/host.md create mode 100644 docs/data-sources/provisioner.md diff --git a/docs/data-sources/host.md b/docs/data-sources/host.md deleted file mode 100644 index a4c5e285..00000000 --- a/docs/data-sources/host.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -# generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "coder_host Data Source - terraform-provider-coder" -subcategory: "" -description: |- - Use this data source to get information about the host. ---- - -# coder_host (Data Source) - -Use this data source to get information about the host. - - - - -## Schema - -### Read-Only - -- `arch` (String) The architecture of the host. -- `id` (String) The ID of this resource. -- `os` (String) The operating system of the host. - - diff --git a/docs/data-sources/provisioner.md b/docs/data-sources/provisioner.md new file mode 100644 index 00000000..47bdaf04 --- /dev/null +++ b/docs/data-sources/provisioner.md @@ -0,0 +1,24 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "coder_provisioner Data Source - terraform-provider-coder" +subcategory: "" +description: |- + Use this data source to get information about the Coder provisioner. +--- + +# coder_provisioner (Data Source) + +Use this data source to get information about the Coder provisioner. + + + + +## Schema + +### Read-Only + +- `arch` (String) The architecture of the host. This exposes `runtime.GOARCH` (see https://pkg.go.dev/runtime#pkg-constants). +- `id` (String) The ID of this resource. +- `os` (String) The operating system of the host. This exposes `runtime.GOOS` (see https://pkg.go.dev/runtime#pkg-constants). + + diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 330106f7..21227214 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -151,8 +151,8 @@ func New() *schema.Provider { }, }, }, - "coder_host": { - Description: "Use this data source to get information about the host.", + "coder_provisioner": { + Description: "Use this data source to get information about the Coder provisioner.", ReadContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { rd.SetId(uuid.NewString()) rd.Set("os", runtime.GOOS) @@ -164,12 +164,12 @@ func New() *schema.Provider { "os": { Type: schema.TypeString, Computed: true, - Description: "The operating system of the host.", + Description: "The operating system of the host. This exposes `runtime.GOOS` (see https://pkg.go.dev/runtime#pkg-constants).", }, "arch": { Type: schema.TypeString, Computed: true, - Description: "The architecture of the host.", + Description: "The architecture of the host. This exposes `runtime.GOARCH` (see https://pkg.go.dev/runtime#pkg-constants).", }, }, }, diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 7e19d7d7..40c659c8 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -53,7 +53,7 @@ func TestWorkspace(t *testing.T) { }) } -func TestHost(t *testing.T) { +func TestProvisioner(t *testing.T) { resource.Test(t, resource.TestCase{ Providers: map[string]*schema.Provider{ "coder": provider.New(), @@ -63,12 +63,12 @@ func TestHost(t *testing.T) { Config: ` provider "coder" { } - data "coder_host" "me" { + data "coder_provisioner" "me" { }`, 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["data.coder_host.me"] + resource := state.Modules[0].Resources["data.coder_provisioner.me"] require.NotNil(t, resource) attribs := resource.Primary.Attributes