Skip to content

Commit f8f4e4b

Browse files
feat: add coderd_user resource
Co-authored-by: Ethan Dickson <[email protected]>
1 parent 31e99a7 commit f8f4e4b

File tree

13 files changed

+596
-400
lines changed

13 files changed

+596
-400
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ website/vendor
3636
terraform-provider-coderd
3737

3838
# Needs to be written on each invocation
39-
integration/integration.tfrc
39+
integration/integration.tfrc
40+
41+
*.tfstate

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/docker/docker v26.1.4+incompatible
88
github.com/docker/go-connections v0.4.0
99
github.com/hashicorp/terraform-plugin-docs v0.19.4
10-
github.com/hashicorp/terraform-plugin-framework v1.9.0
10+
github.com/hashicorp/terraform-plugin-framework v1.10.0
1111
github.com/hashicorp/terraform-plugin-go v0.23.0
1212
github.com/hashicorp/terraform-plugin-log v0.9.0
1313
github.com/hashicorp/terraform-plugin-testing v1.8.0
@@ -78,6 +78,7 @@ require (
7878
github.com/hashicorp/logutils v1.0.0 // indirect
7979
github.com/hashicorp/terraform-exec v0.21.0 // indirect
8080
github.com/hashicorp/terraform-json v0.22.1 // indirect
81+
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 // indirect
8182
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 // indirect
8283
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
8384
github.com/hashicorp/terraform-svchost v0.1.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ github.com/hashicorp/terraform-plugin-docs v0.19.4 h1:G3Bgo7J22OMtegIgn8Cd/CaSey
237237
github.com/hashicorp/terraform-plugin-docs v0.19.4/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA=
238238
github.com/hashicorp/terraform-plugin-framework v1.9.0 h1:caLcDoxiRucNi2hk8+j3kJwkKfvHznubyFsJMWfZqKU=
239239
github.com/hashicorp/terraform-plugin-framework v1.9.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
240+
github.com/hashicorp/terraform-plugin-framework v1.10.0 h1:xXhICE2Fns1RYZxEQebwkB2+kXouLC932Li9qelozrc=
241+
github.com/hashicorp/terraform-plugin-framework v1.10.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
242+
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 h1:bxZfGo9DIUoLLtHMElsu+zwqI4IsMZQBRRy4iLzZJ8E=
243+
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0/go.mod h1:wGeI02gEhj9nPANU62F2jCaHjXulejm/X+af4PdZaNo=
240244
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
241245
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
242246
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=

integration/example-test/main.tf

Lines changed: 0 additions & 12 deletions
This file was deleted.

integration/integration_test.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"net"
89
"net/url"
910
"os"
@@ -15,6 +16,7 @@ import (
1516

1617
"github.com/coder/coder/v2/codersdk"
1718
"github.com/docker/docker/api/types/container"
19+
"github.com/docker/docker/api/types/image"
1820
"github.com/docker/docker/client"
1921
"github.com/docker/go-connections/nat"
2022
"github.com/stretchr/testify/assert"
@@ -50,11 +52,33 @@ func TestIntegration(t *testing.T) {
5052
assertF func(testing.TB, *codersdk.Client)
5153
}{
5254
{
53-
name: "example-test",
55+
name: "user-test",
5456
assertF: func(t testing.TB, c *codersdk.Client) {
55-
me, err := c.User(ctx, codersdk.Me)
57+
// Check user fields.
58+
user, err := c.User(ctx, "dean")
5659
assert.NoError(t, err)
57-
assert.NotEmpty(t, me)
60+
assert.Equal(t, "dean", user.Username)
61+
assert.Equal(t, "Dean Coolguy", user.Name)
62+
assert.Equal(t, "[email protected]", user.Email)
63+
roles := make([]string, len(user.Roles))
64+
for i, role := range user.Roles {
65+
roles[i] = role.Name
66+
}
67+
assert.ElementsMatch(t, []string{"owner", "template-admin"}, roles)
68+
assert.Equal(t, codersdk.LoginTypePassword, user.LoginType)
69+
assert.Contains(t, []codersdk.UserStatus{codersdk.UserStatusActive, codersdk.UserStatusDormant}, user.Status)
70+
71+
// Test password.
72+
newClient := codersdk.New(c.URL)
73+
res, err := newClient.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
74+
75+
Password: "SomeSecurePassword!",
76+
})
77+
assert.NoError(t, err)
78+
newClient.SetSessionToken(res.SessionToken)
79+
user, err = newClient.User(ctx, codersdk.Me)
80+
assert.NoError(t, err)
81+
assert.Equal(t, "dean", user.Username)
5882
},
5983
},
6084
} {
@@ -63,6 +87,14 @@ func TestIntegration(t *testing.T) {
6387
wd, err := os.Getwd()
6488
require.NoError(t, err)
6589
srcDir := filepath.Join(wd, tt.name)
90+
// Delete all .tfstate files
91+
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
92+
if filepath.Ext(path) == ".tfstate" {
93+
return os.Remove(path)
94+
}
95+
return nil
96+
})
97+
require.NoError(t, err)
6698
tfCmd := exec.CommandContext(ctx, "terraform", "-chdir="+srcDir, "apply", "-auto-approve")
6799
tfCmd.Env = append(tfCmd.Env, "TF_CLI_CONFIG_FILE="+tfrcPath)
68100
tfCmd.Env = append(tfCmd.Env, "CODER_URL="+client.URL.String())
@@ -124,6 +156,11 @@ func startCoder(ctx context.Context, t *testing.T, name string) *codersdk.Client
124156
p := randomPort(t)
125157
t.Logf("random port is %d", p)
126158
// Stand up a temporary Coder instance
159+
puller, err := cli.ImagePull(ctx, coderImg+":"+coderVersion, image.PullOptions{})
160+
require.NoError(t, err, "pull coder image")
161+
defer puller.Close()
162+
_, err = io.Copy(os.Stderr, puller)
163+
require.NoError(t, err, "pull coder image")
127164
ctr, err := cli.ContainerCreate(ctx, &container.Config{
128165
Image: coderImg + ":" + coderVersion,
129166
Env: []string{

integration/user-test/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_providers {
3+
coderd = {
4+
source = "coder/coderd"
5+
version = ">=0.0.0"
6+
}
7+
}
8+
}
9+
10+
resource "coderd_user" "dean" {
11+
username = "dean"
12+
name = "Dean Coolguy"
13+
14+
roles = ["owner", "template-admin"]
15+
login_type = "password"
16+
password = "SomeSecurePassword!"
17+
suspended = false
18+
}

internal/provider/example_function.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

internal/provider/example_function_test.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)