Skip to content

Commit 66f639d

Browse files
Add list users API endpoint and UI page
1 parent 1054264 commit 66f639d

105 files changed

Lines changed: 399 additions & 109 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apiserver/controllers/users.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2022 Cloudbase Solutions SRL
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package controllers
16+
17+
import (
18+
"encoding/json"
19+
"log/slog"
20+
"net/http"
21+
)
22+
23+
// swagger:route GET /users users ListUsers
24+
//
25+
// List all users.
26+
//
27+
// Responses:
28+
// 200: Users
29+
// default: APIErrorResponse
30+
func (a *APIController) ListUsersHandler(w http.ResponseWriter, r *http.Request) {
31+
ctx := r.Context()
32+
33+
users, err := a.r.ListUsers(ctx)
34+
if err != nil {
35+
slog.With(slog.Any("error", err)).ErrorContext(ctx, "listing users")
36+
handleError(ctx, w, err)
37+
return
38+
}
39+
40+
w.Header().Set("Content-Type", "application/json")
41+
if err := json.NewEncoder(w).Encode(users); err != nil {
42+
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
43+
}
44+
}
45+

apiserver/routers/routers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ func NewAPIRouter(han *controllers.APIController, authMiddleware, initMiddleware
251251
apiRouter.Handle("/metrics-token/", http.HandlerFunc(han.MetricsTokenHandler)).Methods("GET", "OPTIONS")
252252
apiRouter.Handle("/metrics-token", http.HandlerFunc(han.MetricsTokenHandler)).Methods("GET", "OPTIONS")
253253

254+
///////////
255+
// Users //
256+
///////////
257+
// List users
258+
apiRouter.Handle("/users/", http.HandlerFunc(han.ListUsersHandler)).Methods("GET", "OPTIONS")
259+
apiRouter.Handle("/users", http.HandlerFunc(han.ListUsersHandler)).Methods("GET", "OPTIONS")
260+
254261
/////////////
255262
// Objects //
256263
/////////////

database/common/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type UserStore interface {
8585
GetUser(ctx context.Context, user string) (params.User, error)
8686
GetUserByID(ctx context.Context, userID string) (params.User, error)
8787
GetAdminUser(ctx context.Context) (params.User, error)
88+
ListUsers(ctx context.Context) ([]params.User, error)
8889

8990
CreateUser(ctx context.Context, user params.NewUserParams) (params.User, error)
9091
UpdateUser(ctx context.Context, user string, param params.UpdateUserParams) (params.User, error)

database/sql/users.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,17 @@ func (s *sqlDatabase) GetAdminUser(_ context.Context) (params.User, error) {
163163
}
164164
return s.sqlToParamsUser(user), nil
165165
}
166+
167+
func (s *sqlDatabase) ListUsers(_ context.Context) ([]params.User, error) {
168+
var users []User
169+
q := s.conn.Model(&User{}).Find(&users)
170+
if q.Error != nil {
171+
return nil, fmt.Errorf("error fetching users: %w", q.Error)
172+
}
173+
174+
ret := make([]params.User, len(users))
175+
for idx, user := range users {
176+
ret[idx] = s.sqlToParamsUser(user)
177+
}
178+
return ret, nil
179+
}

runner/runner.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,3 +977,11 @@ func (r *Runner) getGHCliFromInstance(ctx context.Context, instance params.Insta
977977
}
978978
return ghCli, scaleSetCli, nil
979979
}
980+
981+
func (r *Runner) ListUsers(ctx context.Context) ([]params.User, error) {
982+
users, err := r.store.ListUsers(ctx)
983+
if err != nil {
984+
return nil, fmt.Errorf("error fetching users: %w", err)
985+
}
986+
return users, nil
987+
}

webapp/assets/_app/immutable/assets/0.DHAitIu8.css renamed to webapp/assets/_app/immutable/assets/0.Xtu_mOgF.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/assets/_app/immutable/chunks/DeWAiRx4.js renamed to webapp/assets/_app/immutable/chunks/-FPg0SOX.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/assets/_app/immutable/chunks/CbpptuHW.js renamed to webapp/assets/_app/immutable/chunks/8ZO9Ka4R.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/assets/_app/immutable/chunks/B-bv0ihJ.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

webapp/assets/_app/immutable/chunks/Z0yBlujf.js renamed to webapp/assets/_app/immutable/chunks/B87zT258.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)