Files
signoz/pkg/subscription/handler.go
Vikrant Gupta 0f36cb9334 feat(subscription): add subscription endpoints with resource authz (#12767)
#### Description

- Adds a `subscription` domain: `POST`, `PUT`, and `GET
/api/v1/subscriptions`, wired with `CheckResources` + `ResourceDef`s on
the `subscription` metaresource (`create`, `list` + `update`, `read`).
Community gets a noop implementation; enterprise talks to Zeus.
- Migration `125_add_subscription_tuples` backfills the admin
subscription tuples for existing organizations.
- The legacy `/api/v1/checkout`, `/api/v1/billing`, and `/api/v1/portal`
routes are untouched; they are deleted once the frontend has moved.

#### Additional Information

Part of SigNoz/platform-pod#3091.
2026-09-04 10:15:17 +00:00

86 lines
1.9 KiB
Go

package subscription
import (
"net/http"
"github.com/SigNoz/signoz/pkg/http/binding"
"github.com/SigNoz/signoz/pkg/http/render"
"github.com/SigNoz/signoz/pkg/types/authtypes"
"github.com/SigNoz/signoz/pkg/types/subscriptiontypes"
"github.com/SigNoz/signoz/pkg/valuer"
)
type handler struct {
subscription Subscription
}
func NewHandler(subscription Subscription) Handler {
return &handler{subscription: subscription}
}
func (handler *handler) Create(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(rw, err)
return
}
req := new(subscriptiontypes.PostableSubscription)
if err := binding.JSON.BindBody(r.Body, req); err != nil {
render.Error(rw, err)
return
}
gettableSubscription, err := handler.subscription.Create(ctx, valuer.MustNewUUID(claims.OrgID), req)
if err != nil {
render.Error(rw, err)
return
}
render.Success(rw, http.StatusCreated, gettableSubscription)
}
func (handler *handler) Update(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(rw, err)
return
}
req := new(subscriptiontypes.PostableSubscription)
if err := binding.JSON.BindBody(r.Body, req); err != nil {
render.Error(rw, err)
return
}
gettableSubscription, err := handler.subscription.Update(ctx, valuer.MustNewUUID(claims.OrgID), req)
if err != nil {
render.Error(rw, err)
return
}
render.Success(rw, http.StatusOK, gettableSubscription)
}
func (handler *handler) Get(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(rw, err)
return
}
usage, err := handler.subscription.Get(ctx, valuer.MustNewUUID(claims.OrgID))
if err != nil {
render.Error(rw, err)
return
}
render.Success(rw, http.StatusOK, usage)
}