Skip to content

Commit

Permalink
add stub config to api be-1276 (#120)
Browse files Browse the repository at this point in the history
* add stub config to api

* remove openmeter api token and fix up stub usage
  • Loading branch information
jsun-m authored Apr 5, 2024
1 parent 034ed64 commit f8b7dfa
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/api/v1/stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package apiv1

import (
"log"
"net/http"

"github.com/beam-cloud/beta9/internal/auth"
"github.com/beam-cloud/beta9/internal/repository"
"github.com/beam-cloud/beta9/internal/types"
"github.com/labstack/echo/v4"
)

type StubGroup struct {
routerGroup *echo.Group
config types.AppConfig
backendRepo repository.BackendRepository
}

func NewStubGroup(g *echo.Group, backendRepo repository.BackendRepository, config types.AppConfig) *StubGroup {
group := &StubGroup{routerGroup: g,
backendRepo: backendRepo,
config: config,
}

g.GET("/", group.ListStubs)

return group
}

func (g *StubGroup) ListStubs(ctx echo.Context) error {
cc, _ := ctx.(*auth.HttpAuthContext)
if cc.AuthInfo.Token.TokenType != types.TokenTypeClusterAdmin {
return echo.NewHTTPError(http.StatusUnauthorized)
}

var filters types.StubFilter
if err := ctx.Bind(&filters); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to decode query parameters")
}

if stubs, err := g.backendRepo.ListStubs(ctx.Request().Context(), filters); err != nil {
log.Println(err)
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to list stubs")
} else {
return ctx.JSON(http.StatusOK, stubs)
}
}
3 changes: 3 additions & 0 deletions internal/common/config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ monitoring:
idleConnTimeout: 10s
dialTimeout: 2s
keepAlive: 30s
openmeter:
serverUrl: ""
apiKey: ""
1 change: 1 addition & 0 deletions internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (g *Gateway) initHttp() error {
apiv1.NewTokenGroup(g.baseRouteGroup.Group("/token", authMiddleware), g.BackendRepo, g.config)
apiv1.NewTaskGroup(g.baseRouteGroup.Group("/task", authMiddleware), g.BackendRepo, g.config)
apiv1.NewDeploymentGroup(g.baseRouteGroup.Group("/deployment", authMiddleware), g.BackendRepo, g.config)
apiv1.NewStubGroup(g.baseRouteGroup.Group("/stub", authMiddleware), g.BackendRepo, g.config)

return nil
}
Expand Down
16 changes: 16 additions & 0 deletions internal/repository/backend_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/beam-cloud/beta9/internal/types"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
_ "github.com/lib/pq"
"github.com/pressly/goose/v3"
)
Expand Down Expand Up @@ -691,3 +692,18 @@ func (c *PostgresBackendRepository) CreateDeployment(ctx context.Context, worksp

return &deployment, nil
}

func (c *PostgresBackendRepository) ListStubs(ctx context.Context, filters types.StubFilter) ([]types.StubWithRelated, error) {
query := `
SELECT s.*, w.external_id AS "workspace.external_id", w.name AS "workspace.name"
FROM stub s
JOIN workspace w ON s.workspace_id = w.id
WHERE s.external_id = ANY($1);`
var stubs []types.StubWithRelated
err := c.client.SelectContext(ctx, &stubs, query, pq.Array(filters.StubIds))
if err != nil {
return nil, err
}

return stubs, nil
}
1 change: 1 addition & 0 deletions internal/repository/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type BackendRepository interface {
GetDeploymentByExternalId(ctx context.Context, workspaceId uint, deploymentExternalId string) (*types.DeploymentWithRelated, error)
GetDeploymentByNameAndVersion(ctx context.Context, workspaceId uint, name string, version uint, stubType string) (*types.DeploymentWithRelated, error)
CreateDeployment(ctx context.Context, workspaceId uint, name string, version uint, stubId uint, stubType string) (*types.Deployment, error)
ListStubs(ctx context.Context, filters types.StubFilter) ([]types.StubWithRelated, error)
}

type WorkerPoolRepository interface {
Expand Down
16 changes: 16 additions & 0 deletions internal/types/filters.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package types

import "strings"

type BaseFilter struct {
Limit uint32 `query:"limit"`
Offset int `query:"offset"`
}

// Custom type for a slice of strings
type StringSlice []string

// UnmarshalParam implements the echo query parameter unmarshaler interface
func (s *StringSlice) UnmarshalParam(src string) error {
*s = strings.Split(src, ",")
return nil
}

type DeploymentFilter struct {
BaseFilter
WorkspaceID uint `query:"workspace_id"`
Expand All @@ -23,3 +34,8 @@ type TaskFilter struct {
CreatedAtEnd string `query:"created_at_end"`
Cursor string `query:"cursor"`
}

// Struct that includes the custom type
type StubFilter struct {
StubIds StringSlice `query:"stub_ids"` // The query parameter name is "values"
}

0 comments on commit f8b7dfa

Please sign in to comment.