Skip to content

Commit

Permalink
feat: add GET /api/v2/users (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoQuaresma committed Apr 18, 2022
1 parent af67280 commit 1df750b
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func New(options *Options) (http.Handler, func()) {
r.Group(func(r chi.Router) {
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
r.Post("/", api.postUsers)
r.Get("/", api.users)
r.Route("/{user}", func(r chi.Router) {
r.Use(httpmw.ExtractUserParam(options.Database))
r.Get("/", api.userByName)
Expand Down
7 changes: 7 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ func (q *fakeQuerier) GetUserCount(_ context.Context) (int64, error) {
return int64(len(q.users)), nil
}

func (q *fakeQuerier) GetUsers(_ context.Context) ([]database.User, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

return q.users, nil
}

func (q *fakeQuerier) GetWorkspacesByTemplateID(_ context.Context, arg database.GetWorkspacesByTemplateIDParams) ([]database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
1 change: 1 addition & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ SET
updated_at = $5
WHERE
id = $1 RETURNING *;

-- name: GetUsers :many
SELECT
*
FROM
users;
19 changes: 19 additions & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ import (
"github.com/coder/coder/cryptorand"
)

// Lists all the users
func (api *api) users(rw http.ResponseWriter, r *http.Request) {
users, err := api.Database.GetUsers(r.Context())

if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get users: %s", err.Error()),
})
return
}

var res []codersdk.User
for _, user := range users {
res = append(res, convertUser(user))
}

httpapi.Write(rw, http.StatusOK, res)
}

// Returns whether the initial user has been created or not.
func (api *api) firstUser(rw http.ResponseWriter, r *http.Request) {
userCount, err := api.Database.GetUserCount(r.Context())
Expand Down
15 changes: 15 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,21 @@ func TestUserByName(t *testing.T) {
require.NoError(t, err)
}

func TestGetUsers(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
client.CreateUser(context.Background(), codersdk.CreateUserRequest{
Email: "bruno@coder.com",
Username: "bruno",
Password: "password",
OrganizationID: user.OrganizationID,
})
users, err := client.GetUsers(context.Background())
require.NoError(t, err)
require.Len(t, users, 2)
}

func TestOrganizationsByUser(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
13 changes: 13 additions & 0 deletions codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ func (c *Client) UpdateUserProfile(ctx context.Context, userID uuid.UUID, req Up
return user, json.NewDecoder(res.Body).Decode(&user)
}

func (c *Client) GetUsers(ctx context.Context) ([]User, error) {
res, err := c.request(ctx, http.MethodGet, "/api/v2/users", nil)
if err != nil {
return []User{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return []User{}, readBodyAsError(res)
}
var users []User
return users, json.NewDecoder(res.Body).Decode(&users)
}

// CreateAPIKey generates an API key for the user ID provided.
func (c *Client) CreateAPIKey(ctx context.Context, userID uuid.UUID) (*GenerateAPIKeyResponse, error) {
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/users/%s/keys", uuidOrMe(userID)), nil)
Expand Down

0 comments on commit 1df750b

Please sign in to comment.