Skip to content

Commit

Permalink
Add HTTP handlers for plans, quotas, usages, and users
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wregglesworth committed May 30, 2024
1 parent a420cfd commit 5e634b5
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 11 deletions.
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ func New(client *natscl.Client, db *sqlx.DB, userSuffix string) *App {
app.Router.PUT("/subscriptions/:sub_uuid/addons/:addon_uuid", app.AddSubscriptionAddonHTTPHandler)
app.Router.DELETE("/subscriptions/:sub_uuid/addons/:addon_uuid", app.DeleteSubscriptionAddonHTTPHandler)
app.Router.POST("/subscriptions/:sub_uuid/addons/:addon_uuid", app.UpdateSubscriptionAddonHTTPHandler)
app.Router.PUT("/users", app.AddUserHTTPHandler)
app.Router.GET("/users/:username/updates", app.GetUserUpdatesHTTPHandler)
app.Router.PUT("/user/:username/updates", app.AddUserUpdateHTTPHandler)
app.Router.GET("/users/:username/overages", app.GetUserOveragesHTTPHandler)
app.Router.GET("/users/:username/overages/:resource_name", app.CheckUserOveragesHTTPHandler)
app.Router.GET("/users/:username/usages", app.GetUsagesHTTPHandler)
app.Router.PUT("/users/:username/usages", app.AddUsageHTTPHandler)
app.Router.GET("/plans", app.ListPlansHTTPHandler)
app.Router.PUT("/plans", app.AddPlanHTTPHandler)
app.Router.GET("/plans/:plan_id", app.GetPlanHTTPHandler)
app.Router.POST("/quotas/defaults", app.UpsertQuotaDefaultsHTTPHandler)
app.Router.PUT("/quotas", app.AddQuotaHTTPHandler)

return app
}
Expand Down
87 changes: 76 additions & 11 deletions app/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@ package app
import (
"context"
"fmt"
"net/http"

"github.com/cyverse-de/go-mod/pbinit"
"github.com/cyverse-de/p/go/qms"
"github.com/cyverse-de/subscriptions/db"
"github.com/cyverse-de/subscriptions/errors"
"github.com/sirupsen/logrus"
"github.com/labstack/echo/v4"
)

func (a *App) sendPlanResponseError(reply string, log *logrus.Entry) func(context.Context, *qms.PlanResponse, error) {
return func(ctx context.Context, response *qms.PlanResponse, err error) {
log.Error(err)
response.Error = errors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}
}

func (a *App) listPlans(ctx context.Context) *qms.PlanList {
response := pbinit.NewPlanList()

Expand Down Expand Up @@ -75,6 +66,18 @@ func (a *App) ListPlansHandler(subject, reply string, request *qms.NoParamsReque
}
}

func (a *App) ListPlansHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

response := a.listPlans(ctx)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) addPlan(ctx context.Context, request *qms.AddPlanRequest) *qms.PlanResponse {
response := pbinit.NewPlanResponse()

Expand Down Expand Up @@ -139,6 +142,29 @@ func (a *App) AddPlanHandler(subject, reply string, request *qms.AddPlanRequest)
}
}

func (a *App) AddPlanHTTPHandler(c echo.Context) error {
var (
err error
request qms.AddPlanRequest
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "bad request",
})
}

response := a.addPlan(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) getPlan(ctx context.Context, request *qms.PlanRequest) *qms.PlanResponse {
response := pbinit.NewPlanResponse()

Expand Down Expand Up @@ -192,6 +218,22 @@ func (a *App) GetPlanHandler(subject, reply string, request *qms.PlanRequest) {
}
}

func (a *App) GetPlanHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

request := &qms.PlanRequest{
PlanId: c.Param("plan_id"),
}

response := a.getPlan(ctx, request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) upsertQuotaDefault(ctx context.Context, _ *qms.AddPlanQuotaDefaultRequest) *qms.QuotaDefaultResponse {
response := pbinit.NewQuotaDefaultResponse()
response.Error = errors.NatsError(ctx, fmt.Errorf("not implemented"))
Expand All @@ -213,3 +255,26 @@ func (a *App) UpsertQuotaDefaultsHandler(subject, reply string, request *qms.Add
log.Error(err)
}
}

func (a *App) UpsertQuotaDefaultsHTTPHandler(c echo.Context) error {
var (
err error
request qms.AddPlanQuotaDefaultRequest
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "bad request",
})
}

response := a.upsertQuotaDefault(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}
25 changes: 25 additions & 0 deletions app/quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package app

import (
"context"
"net/http"

"github.com/cyverse-de/go-mod/pbinit"
"github.com/cyverse-de/p/go/qms"
"github.com/cyverse-de/subscriptions/db"
"github.com/cyverse-de/subscriptions/errors"
"github.com/labstack/echo/v4"
)

func (a *App) addQuota(ctx context.Context, request *qms.AddQuotaRequest) *qms.QuotaResponse {
Expand Down Expand Up @@ -55,3 +57,26 @@ func (a *App) AddQuotaHandler(subject, reply string, request *qms.AddQuotaReques
log.Error(err)
}
}

func (a *App) AddQuotaHTTPHandler(c echo.Context) error {
var (
err error
request qms.AddQuotaRequest
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "bad request",
})
}

response := a.addQuota(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}
43 changes: 43 additions & 0 deletions app/usages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package app

import (
"context"
"net/http"

"github.com/cyverse-de/go-mod/pbinit"
"github.com/cyverse-de/p/go/qms"
"github.com/cyverse-de/subscriptions/db"
"github.com/cyverse-de/subscriptions/errors"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -73,6 +75,22 @@ func (a *App) GetUsagesHandler(subject, reply string, request *qms.GetUsages) {
}
}

func (a *App) GetUsagesHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

request := &qms.GetUsages{
Username: c.Param("username"),
}

response := a.getUsages(ctx, request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) addUsage(ctx context.Context, request *qms.AddUsage) *qms.UsageResponse {
var (
err error
Expand Down Expand Up @@ -156,3 +174,28 @@ func (a *App) AddUsageHandler(subject, reply string, request *qms.AddUsage) {
log.Error(err)
}
}

func (a *App) AddUsageHTTPHandler(c echo.Context) error {
var (
err error
request qms.AddUsage
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "bad request",
})
}

request.Username = c.Param("username")

response := a.addUsage(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}
27 changes: 27 additions & 0 deletions app/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package app

import (
"context"
"net/http"

"github.com/cyverse-de/go-mod/pbinit"
"github.com/cyverse-de/p/go/qms"
"github.com/cyverse-de/subscriptions/db"
"github.com/cyverse-de/subscriptions/errors"
"github.com/cyverse-de/subscriptions/utils"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -141,3 +143,28 @@ func (a *App) AddUserHandler(subject, reply string, request *qms.AddUserRequest)
log.Error(err)
}
}

func (a *App) AddUserHTTPHandler(c echo.Context) error {
var (
err error
request qms.AddUserRequest
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "bad request",
})
}

request.Username = c.Param("username")

response := a.addUser(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

0 comments on commit 5e634b5

Please sign in to comment.