Skip to content

Commit

Permalink
CORE-2016: removed some unused endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
slr71 committed Oct 30, 2024
1 parent 6b9d053 commit fa43dc9
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 224 deletions.
14 changes: 0 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,14 @@ require (
require (
github.com/cyverse-de/go-mod/cfg v0.0.2
github.com/cyverse-de/go-mod/logging v0.0.3
github.com/cyverse-de/go-mod/pbinit v0.1.12
github.com/cyverse-de/go-mod/protobufjson v0.0.4
github.com/cyverse-de/p/go/qms v0.1.13
github.com/go-playground/validator/v10 v10.22.0
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/labstack/echo/v4 v4.12.0
github.com/spirosoik/echo-logrus v1.0.0
)

require (
github.com/cyverse-de/go-mod/gotelnats v0.0.12 // indirect
github.com/cyverse-de/p v0.0.0-20240820230617-9e063fe10de8 // indirect
github.com/cyverse-de/p/go/analysis v0.0.16 // indirect
github.com/cyverse-de/p/go/containers v0.0.2 // indirect
github.com/cyverse-de/p/go/header v0.0.4 // indirect
github.com/cyverse-de/p/go/monitoring v0.0.5 // indirect
github.com/cyverse-de/p/go/svcerror v0.0.8 // indirect
github.com/cyverse-de/p/go/user v0.0.11 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand All @@ -66,17 +56,13 @@ require (
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/nats-io/nats.go v1.37.0 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/uptrace/opentelemetry-go-extra/otelsql v0.3.1 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
Expand Down
28 changes: 0 additions & 28 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,10 @@ github.com/cyverse-de/echo-middleware/v2 v2.0.3 h1:zu7t40uic5cLBCBt6LTH+04DLj3CZ
github.com/cyverse-de/echo-middleware/v2 v2.0.3/go.mod h1:cqXIRR0GOYcVFzU6yyRfv+QhMcFjoESiktswiulOGHk=
github.com/cyverse-de/go-mod/cfg v0.0.2 h1:evHNKqLwOPWHhxxzF498/Rtac7LZb1zxnHAjZSuqiEo=
github.com/cyverse-de/go-mod/cfg v0.0.2/go.mod h1:jjn1fZJRwqKiYgiS5AcXg9Dzxp2QOiLyrWVWCcq9Dw0=
github.com/cyverse-de/go-mod/gotelnats v0.0.12 h1:oq24rRC2Tsc2z/5d0cso9QqrSRq6ce+e0vfHlk+wtPA=
github.com/cyverse-de/go-mod/gotelnats v0.0.12/go.mod h1:n9+Qw8jV5z+w476xk3RRPBQzJQbA/fehv4UiXtL+Zqk=
github.com/cyverse-de/go-mod/logging v0.0.3 h1:sbehQImEP5cYusWVN1sDvo3lqwEfaZSCZLmrhFaI8xI=
github.com/cyverse-de/go-mod/logging v0.0.3/go.mod h1:KRRtiAvsBZlnR9qxXLFYaFdvJAJZKGYAIhRnjuORpIQ=
github.com/cyverse-de/go-mod/pbinit v0.1.12 h1:HA9q0ffLAzAbcEYJkhQigZXXl06juaAhuft/baIKbOg=
github.com/cyverse-de/go-mod/pbinit v0.1.12/go.mod h1:M5zde8HkoyViaCD1sT/8PNNiYCheCfwhktfaoI6lobA=
github.com/cyverse-de/go-mod/protobufjson v0.0.4 h1:UPZCpdg6zm30tNf/c8JCI+F/SewiqeF42Uwi13X8diU=
github.com/cyverse-de/go-mod/protobufjson v0.0.4/go.mod h1:SJuTgEbjy56L3QtVKiON/kAHM298Xd8F4Uq7kyU8nFo=
github.com/cyverse-de/p v0.0.0-20240820230617-9e063fe10de8 h1:7vJEIY4rqwf1S4LRvOioCWAKIDsz5dtNjG3kumBFB5c=
github.com/cyverse-de/p v0.0.0-20240820230617-9e063fe10de8/go.mod h1:JM04cBlOajBkOVfQOEbBD6QwmWyupji9J84wfUA+LTc=
github.com/cyverse-de/p/go/analysis v0.0.16 h1:E+SE/v3RE1znwnIzk4ugXiM0knVsBnTLmuv4fcvietc=
github.com/cyverse-de/p/go/analysis v0.0.16/go.mod h1:SJi4jTyxhqOe2waLcHKbDIN6+CZdV6WAJI2JWIrpFIQ=
github.com/cyverse-de/p/go/containers v0.0.2 h1:PIpw5KHC6A85KeloHms86zG5ADVAQXjsP05n8KEWeXE=
github.com/cyverse-de/p/go/containers v0.0.2/go.mod h1:Yzjby5JYINnpPkLadB0G+zFvxAzCx6V2vqyL170E33U=
github.com/cyverse-de/p/go/header v0.0.4 h1:asY/uixi9rCqbDLq2G21CT7WJRaendBfr1z2YTuK2hk=
github.com/cyverse-de/p/go/header v0.0.4/go.mod h1:1j79PqvJyMRmXXKyW4KbZWGQNontm0B9smsaq5t5Ct8=
github.com/cyverse-de/p/go/monitoring v0.0.5 h1:n+GdL9a3R1zM7L+18SIufrtmLxXU8L/cKX6DhOJ7IhY=
github.com/cyverse-de/p/go/monitoring v0.0.5/go.mod h1:CB0aMpTRJvCFlGeL/75uX4Snj/OXj9wzKF5KQX4tASg=
github.com/cyverse-de/p/go/qms v0.1.13 h1:4pcBCETTKwCYo9L3zfbQy92TXbaH/wv/VVyc3LGMl9E=
github.com/cyverse-de/p/go/qms v0.1.13/go.mod h1:Uv07x99AFaFmFwIwa3xTrN6bRxr76J7wK6R3Fgt/UG4=
github.com/cyverse-de/p/go/svcerror v0.0.8 h1:UE06rE31kk7OcciTQlZfrJTFlC9AMTfNf6JKll41u0U=
github.com/cyverse-de/p/go/svcerror v0.0.8/go.mod h1:0h/BngUR9cTV86LYqHMkdREFcxJuMyOJV6f3u6Un4LQ=
github.com/cyverse-de/p/go/user v0.0.11 h1:VM2WGrdvcmDLwwjUJpCbrhC5te8xFY8f71ALFO2N7LI=
github.com/cyverse-de/p/go/user v0.0.11/go.mod h1:g6Gl6+Zv0PlZMoydO2cBmLFTLwQs39gYauboxp769KE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -227,8 +207,6 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/knadh/koanf v1.5.0 h1:q2TSd/3Pyc/5yP9ldIrSdIz26MCcyNQzW0pEAugLPNs=
github.com/knadh/koanf v1.5.0/go.mod h1:Hgyjp4y8v44hpZtPzs7JZfRAW5AhN7KfZcwv1RYggDs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -300,12 +278,6 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down
84 changes: 0 additions & 84 deletions internal/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

"gorm.io/gorm"

"github.com/cyverse-de/go-mod/pbinit"
"github.com/cyverse-de/p/go/qms"
"github.com/cyverse/qms/internal/db"
"github.com/cyverse/qms/internal/httpmodel"
"github.com/cyverse/qms/internal/model"
Expand Down Expand Up @@ -190,88 +188,6 @@ func (s Server) UpdateCurrentSubscriptionQuota(c echo.Context) error {
})
}

// GetUserOverages is the echo handler for listing the resources that a user is
// in overage for.
func (s Server) GetUserOverages(ctx echo.Context) error {
log := log.WithFields(logrus.Fields{"context": "getting any overages for the user"})

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

responseList := pbinit.NewOverageList()

// Skip the remaining logic because qms is configured to not report overages.
if !s.ReportOverages {
return model.ProtobufJSON(ctx, responseList, http.StatusOK)
}

username := strings.TrimSuffix(ctx.Param("username"), s.UsernameSuffix)
if username == "" {
return model.Error(ctx, "missing username", http.StatusBadRequest)
}

log.WithFields(logrus.Fields{"user": username})

log.Info("looking up any overages")

log.Debug("before calling db.GetUserOverages()")
results, err := db.GetUserOverages(context, s.GORMDB, username)
if err != nil {
return model.Error(ctx, err.Error(), http.StatusInternalServerError)
}
log.Debug("after calling db.GetUserOverages()")

for _, r := range results {
responseList.Overages = append(responseList.Overages, &qms.Overage{
ResourceName: r["resource_type_name"].(string),
Quota: r["quota"].(float64),
Usage: r["usage"].(float64),
})
}

return model.ProtobufJSON(ctx, responseList, http.StatusOK)
}

// InOverage is the echo handler for checking if a user is in overage for a
// resource.
func (s Server) InOverage(ctx echo.Context) error {
log := log.WithFields(logrus.Fields{"context": "checking if a user's usage is an overage"})

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

response := pbinit.NewIsOverage()

// Skip the rest of the logic because qms is configured to not report overages
if !s.ReportOverages {
response.IsOverage = false
return model.ProtobufJSON(ctx, response, http.StatusOK)
}

username := strings.TrimSuffix(ctx.Param("username"), s.UsernameSuffix)
if username == "" {
return model.Error(ctx, "missing username", http.StatusBadRequest)
}

resource := ctx.Param("resource-name")
if resource == "" {
return model.Error(ctx, "missing resource name", http.StatusBadRequest)
}

log.WithFields(logrus.Fields{"user": username, "resource": resource})

log.Info("checking if the usage is an overage")

results, err := db.IsOverage(context, s.GORMDB, username, resource)
if err != nil {
return model.Error(ctx, err.Error(), http.StatusInternalServerError)
}

if results != nil {
response.IsOverage = results["overage"].(bool)
}

return model.ProtobufJSON(ctx, response, http.StatusOK)
}

// AddUser adds a new user to the database. This is a no-op if the user already
// exists.
func (s Server) AddUser(ctx echo.Context) error {
Expand Down
92 changes: 0 additions & 92 deletions internal/db/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,95 +310,3 @@ func UpsertQuota(ctx context.Context, db *gorm.DB, quota *model.Quota) error {

return nil
}

func GetUserOverages(ctx context.Context, db *gorm.DB, username string) ([]map[string]interface{}, error) {
var err error

retval := make([]map[string]interface{}, 0)

err = db.WithContext(ctx).
Table("subscriptions").
Select(
"subscriptions.id as subscription_id",
"users.username",
"plans.name as plan_name",
"resource_types.name as resource_type_name",
"quotas.quota",
"usages.usage",
).
Joins("JOIN users ON subscriptions.user_id = users.id").
Joins("JOIN plans ON subscriptions.plan_id = plans.id").
Joins("JOIN quotas ON subscriptions.id = quotas.subscription_id").
Joins("JOIN usages ON subscriptions.id = usages.subscription_id").
Joins("JOIN resource_types ON usages.resource_type_id = resource_types.id").
Where("users.username = ?", username).
Where(
db.Where("CURRENT_TIMESTAMP BETWEEN subscriptions.effective_start_date AND subscriptions.effective_end_date").
Or("CURRENT_TIMESTAMP > subscriptions.effective_start_date AND subscriptions.effective_end_date IS NULL"),
).
Where("usages.resource_type_id = quotas.resource_type_id").
Where("usages.usage >= quotas.quota").
Find(&retval).Error

if err != nil {
return nil, errors.Wrap(err, "failed to look up overages")
}

return retval, nil
}

func IsOverage(ctx context.Context, db *gorm.DB, username string, resourceName string) (map[string]interface{}, error) {
var err error

rsc, err := GetResourceTypeByName(ctx, db, resourceName)
if err != nil {
return nil, err
}
if rsc == nil {
return nil, fmt.Errorf("resource type %s does not exist", resourceName)
}

result := make([]map[string]interface{}, 0)
retval := make(map[string]interface{})

err = db.WithContext(ctx).
Table("subscriptions").
Select(
"subscriptions.id as subscription_id",
"users.username",
"plans.name as plan_name",
"resource_types.name as resource_type_name",
"quotas.quota",
"usages.usage",
).
Joins("JOIN users ON subscriptions.user_id = users.id").
Joins("JOIN plans ON subscriptions.plan_id = plans.id").
Joins("JOIN quotas ON subscriptions.id = quotas.subscription_id").
Joins("JOIN usages ON subscriptions.id = usages.subscription_id").
Joins("JOIN resource_types ON usages.resource_type_id = resource_types.id").
Where("users.username = ?", username).
Where("resource_types.name = ?", resourceName).
Where(
db.Where("CURRENT_TIMESTAMP BETWEEN subscriptions.effective_start_date AND subscriptions.effective_end_date").
Or("CURRENT_TIMESTAMP > subscriptions.effective_start_date AND subscriptions.effective_end_date IS NULL"),
).
Where("usages.resource_type_id = quotas.resource_type_id").
Where("usages.usage >= quotas.quota").
Find(&result).Error
if err != nil {
return nil, errors.Wrap(err, "failed to check for overage")
}

k := make([]string, 0)
for key := range retval {
k = append(k, key)
}

if len(k) > 0 {
retval["overage"] = true
} else {
retval["overage"] = false
}

return retval, nil
}
6 changes: 0 additions & 6 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ func registerUserEndpoints(users *echo.Group, s *controllers.Server) {
// Updates a quota in the user's current subscription plan.
users.POST("/:username/plan/:resource-type/quota", s.UpdateCurrentSubscriptionQuota)

// GET /:username/resources/overages returns summaries of any usages that exceed the quota for the corresponding resource.
users.GET("/:username/resources/overages", s.GetUserOverages)

// GET /:username/resources/:resource-name/overage returns whether the usage exceeds the quota for the resource.
users.GET("/:username/resources/:resource-name/in-overage", s.InOverage)

// Changes the user's current plan to one corresponding to plan name.
users.PUT("/:username/:plan_name", s.UpdateSubscription)
}
Expand Down

0 comments on commit fa43dc9

Please sign in to comment.