Skip to content

Commit

Permalink
feat: add coderd_user resource
Browse files Browse the repository at this point in the history
Co-authored-by: Ethan Dickson <[email protected]>
  • Loading branch information
deansheather and ethanndickson committed Jul 11, 2024
1 parent 31e99a7 commit f8f4e4b
Show file tree
Hide file tree
Showing 13 changed files with 596 additions and 400 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ website/vendor
terraform-provider-coderd

# Needs to be written on each invocation
integration/integration.tfrc
integration/integration.tfrc

*.tfstate
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/docker/docker v26.1.4+incompatible
github.com/docker/go-connections v0.4.0
github.com/hashicorp/terraform-plugin-docs v0.19.4
github.com/hashicorp/terraform-plugin-framework v1.9.0
github.com/hashicorp/terraform-plugin-framework v1.10.0
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.8.0
Expand Down Expand Up @@ -78,6 +78,7 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ github.com/hashicorp/terraform-plugin-docs v0.19.4 h1:G3Bgo7J22OMtegIgn8Cd/CaSey
github.com/hashicorp/terraform-plugin-docs v0.19.4/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA=
github.com/hashicorp/terraform-plugin-framework v1.9.0 h1:caLcDoxiRucNi2hk8+j3kJwkKfvHznubyFsJMWfZqKU=
github.com/hashicorp/terraform-plugin-framework v1.9.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
github.com/hashicorp/terraform-plugin-framework v1.10.0 h1:xXhICE2Fns1RYZxEQebwkB2+kXouLC932Li9qelozrc=
github.com/hashicorp/terraform-plugin-framework v1.10.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 h1:bxZfGo9DIUoLLtHMElsu+zwqI4IsMZQBRRy4iLzZJ8E=
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0/go.mod h1:wGeI02gEhj9nPANU62F2jCaHjXulejm/X+af4PdZaNo=
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
12 changes: 0 additions & 12 deletions integration/example-test/main.tf

This file was deleted.

43 changes: 40 additions & 3 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/url"
"os"
Expand All @@ -15,6 +16,7 @@ import (

"github.com/coder/coder/v2/codersdk"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -50,11 +52,33 @@ func TestIntegration(t *testing.T) {
assertF func(testing.TB, *codersdk.Client)
}{
{
name: "example-test",
name: "user-test",
assertF: func(t testing.TB, c *codersdk.Client) {
me, err := c.User(ctx, codersdk.Me)
// Check user fields.
user, err := c.User(ctx, "dean")
assert.NoError(t, err)
assert.NotEmpty(t, me)
assert.Equal(t, "dean", user.Username)
assert.Equal(t, "Dean Coolguy", user.Name)
assert.Equal(t, "[email protected]", user.Email)
roles := make([]string, len(user.Roles))
for i, role := range user.Roles {
roles[i] = role.Name
}
assert.ElementsMatch(t, []string{"owner", "template-admin"}, roles)
assert.Equal(t, codersdk.LoginTypePassword, user.LoginType)
assert.Contains(t, []codersdk.UserStatus{codersdk.UserStatusActive, codersdk.UserStatusDormant}, user.Status)

// Test password.
newClient := codersdk.New(c.URL)
res, err := newClient.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
Email: "[email protected]",
Password: "SomeSecurePassword!",
})
assert.NoError(t, err)
newClient.SetSessionToken(res.SessionToken)
user, err = newClient.User(ctx, codersdk.Me)
assert.NoError(t, err)
assert.Equal(t, "dean", user.Username)
},
},
} {
Expand All @@ -63,6 +87,14 @@ func TestIntegration(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
srcDir := filepath.Join(wd, tt.name)
// Delete all .tfstate files
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".tfstate" {
return os.Remove(path)
}
return nil
})
require.NoError(t, err)
tfCmd := exec.CommandContext(ctx, "terraform", "-chdir="+srcDir, "apply", "-auto-approve")
tfCmd.Env = append(tfCmd.Env, "TF_CLI_CONFIG_FILE="+tfrcPath)
tfCmd.Env = append(tfCmd.Env, "CODER_URL="+client.URL.String())
Expand Down Expand Up @@ -124,6 +156,11 @@ func startCoder(ctx context.Context, t *testing.T, name string) *codersdk.Client
p := randomPort(t)
t.Logf("random port is %d", p)
// Stand up a temporary Coder instance
puller, err := cli.ImagePull(ctx, coderImg+":"+coderVersion, image.PullOptions{})
require.NoError(t, err, "pull coder image")
defer puller.Close()
_, err = io.Copy(os.Stderr, puller)
require.NoError(t, err, "pull coder image")
ctr, err := cli.ContainerCreate(ctx, &container.Config{
Image: coderImg + ":" + coderVersion,
Env: []string{
Expand Down
18 changes: 18 additions & 0 deletions integration/user-test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
coderd = {
source = "coder/coderd"
version = ">=0.0.0"
}
}
}

resource "coderd_user" "dean" {
username = "dean"
name = "Dean Coolguy"
email = "[email protected]"
roles = ["owner", "template-admin"]
login_type = "password"
password = "SomeSecurePassword!"
suspended = false
}
50 changes: 0 additions & 50 deletions internal/provider/example_function.go

This file was deleted.

78 changes: 0 additions & 78 deletions internal/provider/example_function_test.go

This file was deleted.

Loading

0 comments on commit f8f4e4b

Please sign in to comment.