Skip to content

Commit 8c23074

Browse files
feat: add coderd_user resource (#18)
--------- Co-authored-by: Ethan Dickson <ethan@coder.com>
1 parent 31e99a7 commit 8c23074

File tree

17 files changed

+673
-460
lines changed

17 files changed

+673
-460
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

docs/functions/example.md

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

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ provider "coderd" {
2323

2424
### Optional
2525

26-
- `endpoint` (String) Example provider attribute
26+
- `token` (String) API token for communicating with the deployment. Most resource types require elevated permissions. Defaults to $CODER_SESSION_TOKEN.
27+
- `url` (String) URL to the Coder deployment. Defaults to $CODER_URL.

docs/resources/example.md

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

docs/resources/user.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coderd_user Resource - coderd"
4+
subcategory: ""
5+
description: |-
6+
A user on the Coder deployment.
7+
---
8+
9+
# coderd_user (Resource)
10+
11+
A user on the Coder deployment.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `email` (String) Email address of the user.
21+
- `username` (String) Username of the user.
22+
23+
### Optional
24+
25+
- `login_type` (String) Type of login for the user. Valid types are 'none', 'password', 'github', and 'oidc'.
26+
- `name` (String) Display name of the user. Defaults to username.
27+
- `password` (String, Sensitive) Password for the user. Required when login_type is 'password'. Passwords are saved into the state as plain text and should only be used for testing purposes.
28+
- `roles` (Set of String) Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'.
29+
- `suspended` (Boolean) Whether the user is suspended.
30+
31+
### Read-Only
32+
33+
- `id` (String) User ID

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, "test@coder.com", 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+
Email: "test@coder.com",
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+
email = "test@coder.com"
14+
roles = ["owner", "template-admin"]
15+
login_type = "password"
16+
password = "SomeSecurePassword!"
17+
suspended = false
18+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy