feat: tenants querying
All checks were successful
arcad/emissary/pipeline/pr-master This commit looks good
All checks were successful
arcad/emissary/pipeline/pr-master This commit looks good
This commit is contained in:
@ -38,6 +38,7 @@ func (m *Mount) Mount(r chi.Router) {
|
||||
})
|
||||
|
||||
r.Route("/tenants", func(r chi.Router) {
|
||||
r.With(assertQueryAccess).Get("/", m.queryTenants)
|
||||
r.With(assertAdminAccess).Post("/", m.createTenant)
|
||||
r.With(assertAdminOrTenantReadAccess).Get("/{tenantID}", m.getTenant)
|
||||
r.With(assertAdminOrTenantWriteAccess).Put("/{tenantID}", m.updateTenant)
|
||||
|
@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
userAuth "forge.cadoles.com/Cadoles/emissary/internal/auth/user"
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
@ -10,11 +11,21 @@ import (
|
||||
)
|
||||
|
||||
func (m *Mount) queryAgents(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := assertRequestUser(w, r)
|
||||
baseUser, ok := assertRequestUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
user, ok := baseUser.(*userAuth.User)
|
||||
if !ok {
|
||||
logger.Error(ctx, "unexpected user type", logger.F("user", baseUser))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
limit, ok := getIntQueryParam(w, r, "limit", 10)
|
||||
if !ok {
|
||||
return
|
||||
@ -28,7 +39,10 @@ func (m *Mount) queryAgents(w http.ResponseWriter, r *http.Request) {
|
||||
options := []datastore.AgentQueryOptionFunc{
|
||||
datastore.WithAgentQueryLimit(int(limit)),
|
||||
datastore.WithAgentQueryOffset(int(offset)),
|
||||
datastore.WithAgentQueryTenantID(user.Tenant()),
|
||||
}
|
||||
|
||||
if user.Role() != userAuth.RoleAdmin {
|
||||
options = append(options, datastore.WithAgentQueryTenantID(user.Tenant()))
|
||||
}
|
||||
|
||||
ids, ok := getIntSliceValues(w, r, "ids", nil)
|
||||
@ -76,8 +90,6 @@ func (m *Mount) queryAgents(w http.ResponseWriter, r *http.Request) {
|
||||
options = append(options, datastore.WithAgentQueryStatus(agentStatuses...))
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
agents, total, err := m.agentRepo.Query(
|
||||
ctx,
|
||||
options...,
|
||||
|
82
internal/server/api/query_tenants.go
Normal file
82
internal/server/api/query_tenants.go
Normal file
@ -0,0 +1,82 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
userAuth "forge.cadoles.com/Cadoles/emissary/internal/auth/user"
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
)
|
||||
|
||||
func (m *Mount) queryTenants(w http.ResponseWriter, r *http.Request) {
|
||||
baseUser, ok := assertRequestUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
user, ok := baseUser.(*userAuth.User)
|
||||
if !ok {
|
||||
logger.Error(ctx, "unexpected user type", logger.F("user", baseUser))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
limit, ok := getIntQueryParam(w, r, "limit", 10)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
offset, ok := getIntQueryParam(w, r, "offset", 0)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
options := []datastore.TenantQueryOptionFunc{
|
||||
datastore.WithTenantQueryLimit(int(limit)),
|
||||
datastore.WithTenantQueryOffset(int(offset)),
|
||||
}
|
||||
|
||||
ids, ok := getStringSliceValues(w, r, "ids", nil)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
tenantIDs := make([]datastore.TenantID, 0)
|
||||
|
||||
if user.Role() != userAuth.RoleAdmin {
|
||||
tenantIDs = append(tenantIDs, user.Tenant())
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
tenantIDs = append(tenantIDs, datastore.TenantID(id))
|
||||
}
|
||||
|
||||
if len(tenantIDs) > 0 {
|
||||
options = append(options, datastore.WithTenantQueryID(tenantIDs...))
|
||||
}
|
||||
|
||||
tenants, total, err := m.tenantRepo.Query(
|
||||
ctx,
|
||||
options...,
|
||||
)
|
||||
if err != nil {
|
||||
err = errors.WithStack(err)
|
||||
logger.Error(ctx, "could not list tenants", logger.CapturedE(err))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, struct {
|
||||
Tenants []*datastore.Tenant `json:"tenants"`
|
||||
Total int `json:"total"`
|
||||
}{
|
||||
Tenants: tenants,
|
||||
Total: total,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user