Skip to content

Commit

Permalink
feat: Add coder_provisioner resource for attributes from the host (#36
Browse files Browse the repository at this point in the history
)

* 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.

* Update internal/provider/provider.go

Co-authored-by: Ben Potter <me@bpmct.net>

Co-authored-by: Ben Potter <me@bpmct.net>
  • Loading branch information
kylecarbs and bpmct authored Aug 9, 2022
1 parent 6ce0af0 commit 5e9c0be
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/data-sources/provisioner.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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).


23 changes: 23 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"reflect"
"runtime"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -150,6 +151,28 @@ func New() *schema.Provider {
},
},
},
"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)
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. This exposes `runtime.GOOS` (see https://pkg.go.dev/runtime#pkg-constants).",
},
"arch": {
Type: schema.TypeString,
Computed: true,
Description: "The architecture of the host. This exposes `runtime.GOARCH` (see https://pkg.go.dev/runtime#pkg-constants).",
},
},
},
},
ResourcesMap: map[string]*schema.Resource{
"coder_agent": {
Expand Down
28 changes: 28 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider_test

import (
"runtime"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -52,6 +53,33 @@ func TestWorkspace(t *testing.T) {
})
}

func TestProvisioner(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_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_provisioner.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{
Expand Down

0 comments on commit 5e9c0be

Please sign in to comment.