Skip to content

Commit 5e9c0be

Browse files
kylecarbsbpmct
andauthored
feat: Add coder_provisioner resource for attributes from the host (#36)
* 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>
1 parent 6ce0af0 commit 5e9c0be

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

docs/data-sources/provisioner.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coder_provisioner Data Source - terraform-provider-coder"
4+
subcategory: ""
5+
description: |-
6+
Use this data source to get information about the Coder provisioner.
7+
---
8+
9+
# coder_provisioner (Data Source)
10+
11+
Use this data source to get information about the Coder provisioner.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Read-Only
19+
20+
- `arch` (String) The architecture of the host. This exposes `runtime.GOARCH` (see https://pkg.go.dev/runtime#pkg-constants).
21+
- `id` (String) The ID of this resource.
22+
- `os` (String) The operating system of the host. This exposes `runtime.GOOS` (see https://pkg.go.dev/runtime#pkg-constants).
23+
24+

internal/provider/provider.go

+23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/url"
88
"os"
99
"reflect"
10+
"runtime"
1011
"strings"
1112

1213
"github.com/google/uuid"
@@ -150,6 +151,28 @@ func New() *schema.Provider {
150151
},
151152
},
152153
},
154+
"coder_provisioner": {
155+
Description: "Use this data source to get information about the Coder provisioner.",
156+
ReadContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
157+
rd.SetId(uuid.NewString())
158+
rd.Set("os", runtime.GOOS)
159+
rd.Set("arch", runtime.GOARCH)
160+
161+
return nil
162+
},
163+
Schema: map[string]*schema.Schema{
164+
"os": {
165+
Type: schema.TypeString,
166+
Computed: true,
167+
Description: "The operating system of the host. This exposes `runtime.GOOS` (see https://pkg.go.dev/runtime#pkg-constants).",
168+
},
169+
"arch": {
170+
Type: schema.TypeString,
171+
Computed: true,
172+
Description: "The architecture of the host. This exposes `runtime.GOARCH` (see https://pkg.go.dev/runtime#pkg-constants).",
173+
},
174+
},
175+
},
153176
},
154177
ResourcesMap: map[string]*schema.Resource{
155178
"coder_agent": {

internal/provider/provider_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider_test
22

33
import (
4+
"runtime"
45
"testing"
56

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

56+
func TestProvisioner(t *testing.T) {
57+
resource.Test(t, resource.TestCase{
58+
Providers: map[string]*schema.Provider{
59+
"coder": provider.New(),
60+
},
61+
IsUnitTest: true,
62+
Steps: []resource.TestStep{{
63+
Config: `
64+
provider "coder" {
65+
}
66+
data "coder_provisioner" "me" {
67+
}`,
68+
Check: func(state *terraform.State) error {
69+
require.Len(t, state.Modules, 1)
70+
require.Len(t, state.Modules[0].Resources, 1)
71+
resource := state.Modules[0].Resources["data.coder_provisioner.me"]
72+
require.NotNil(t, resource)
73+
74+
attribs := resource.Primary.Attributes
75+
require.Equal(t, runtime.GOOS, attribs["os"])
76+
require.Equal(t, runtime.GOARCH, attribs["arch"])
77+
return nil
78+
},
79+
}},
80+
})
81+
}
82+
5583
func TestAgent(t *testing.T) {
5684
t.Parallel()
5785
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)