Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add coderd_user resource #18

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 0 additions & 26 deletions docs/functions/example.md

This file was deleted.

3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ provider "coderd" {

### Optional

- `endpoint` (String) Example provider attribute
- `token` (String) API token for communicating with the deployment. Most resource types require elevated permissions. Defaults to $CODER_SESSION_TOKEN.
- `url` (String) URL to the Coder deployment. Defaults to $CODER_URL.
31 changes: 0 additions & 31 deletions docs/resources/example.md

This file was deleted.

33 changes: 33 additions & 0 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "coderd_user Resource - coderd"
subcategory: ""
description: |-
A user on the Coder deployment.
---

# coderd_user (Resource)

A user on the Coder deployment.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `email` (String) Email address of the user.
- `username` (String) Username of the user.

### Optional

- `login_type` (String) Type of login for the user. Valid types are 'none', 'password', 'github', and 'oidc'.
- `name` (String) Display name of the user. Defaults to username.
- `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.
- `roles` (Set of String) Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'.
- `suspended` (Boolean) Whether the user is suspended.

### Read-Only

- `id` (String) User ID
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, it just seems like these integration tests accomplish the same thing as the acceptance test in user_resource_test.go, yet they're way more complicated. I think the integration tests probably make more sense for chaining resources together or something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear from your comment what you want to happen. Do you want us to use the integration tests or the resource tests? The resource tests will take effort to get working (most likely some mocking stuff, and we might not be able to use the hashi test framework)

Copy link
Member

@coadler coadler Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like you could easily start a coderd pod in the resource tests similar to whats going on here and accomplish the same thing without having to mock anything. It also doesn't seem like we're testing read/update/delete/import here either, which the resource test just has natively. We're also not able to assert any of the terraform state here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can we do that in a follow up though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shore

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to start a coderd pod for acceptance tests, why not just import coderdtest?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not much of a difference, but running a container is better because it auto updates for us lol

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah plus we'll need to copy the gomod replaces and keeping that in sync seems like more of a headache

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah agreed that's super annoying, let's not do that

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
Loading