agola/internal/services/configstore/api/org.go
Simone Gotti d2b09d854f *: use new errors handling library
Implement a new error handling library based on pkg/errors. It provides
stack saving on wrapping and exports some function to add stack saving
also to external errors.
It also implements custom zerolog error formatting without adding too
much verbosity by just printing the chain error file:line without a full
stack trace of every error.

* Add a --detailed-errors options to print error with they full chain
* Wrap all error returns. Use errors.WithStack to wrap without adding a
  new messsage and error.Wrap[f] to add a message.
* Add golangci-lint wrapcheck to check that external packages errors are
  wrapped. This won't check that internal packages error are wrapped.
  But we want also to ensure this case so we'll have to find something
  else to check also these.
2022-02-28 12:49:13 +01:00

281 lines
6.6 KiB
Go

// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"encoding/json"
"net/http"
"strconv"
"agola.io/agola/internal/db"
"agola.io/agola/internal/errors"
"agola.io/agola/internal/services/configstore/action"
"agola.io/agola/internal/services/configstore/readdb"
"agola.io/agola/internal/util"
csapitypes "agola.io/agola/services/configstore/api/types"
"agola.io/agola/services/configstore/types"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
)
type OrgHandler struct {
log zerolog.Logger
readDB *readdb.ReadDB
}
func NewOrgHandler(log zerolog.Logger, readDB *readdb.ReadDB) *OrgHandler {
return &OrgHandler{log: log, readDB: readDB}
}
func (h *OrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
orgRef := vars["orgref"]
var org *types.Organization
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
var err error
org, err = h.readDB.GetOrg(tx, orgRef)
return errors.WithStack(err)
})
if err != nil {
h.log.Err(err).Send()
util.HTTPError(w, err)
return
}
if org == nil {
util.HTTPError(w, util.NewAPIError(util.ErrNotExist, errors.Errorf("org %q doesn't exist", orgRef)))
return
}
if err := util.HTTPResponse(w, http.StatusOK, org); err != nil {
h.log.Err(err).Send()
}
}
type CreateOrgHandler struct {
log zerolog.Logger
ah *action.ActionHandler
}
func NewCreateOrgHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateOrgHandler {
return &CreateOrgHandler{log: log, ah: ah}
}
func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var req types.Organization
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
return
}
org, err := h.ah.CreateOrg(ctx, &req)
if util.HTTPError(w, err) {
h.log.Err(err).Send()
return
}
if err := util.HTTPResponse(w, http.StatusCreated, org); err != nil {
h.log.Err(err).Send()
}
}
type DeleteOrgHandler struct {
log zerolog.Logger
ah *action.ActionHandler
}
func NewDeleteOrgHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteOrgHandler {
return &DeleteOrgHandler{log: log, ah: ah}
}
func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
orgRef := vars["orgref"]
err := h.ah.DeleteOrg(ctx, orgRef)
if util.HTTPError(w, err) {
h.log.Err(err).Send()
return
}
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
h.log.Err(err).Send()
}
}
const (
DefaultOrgsLimit = 10
MaxOrgsLimit = 20
)
type OrgsHandler struct {
log zerolog.Logger
readDB *readdb.ReadDB
}
func NewOrgsHandler(log zerolog.Logger, readDB *readdb.ReadDB) *OrgsHandler {
return &OrgsHandler{log: log, readDB: readDB}
}
func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.URL.Query()
limitS := query.Get("limit")
limit := DefaultOrgsLimit
if limitS != "" {
var err error
limit, err = strconv.Atoi(limitS)
if err != nil {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Wrapf(err, "cannot parse limit")))
return
}
}
if limit < 0 {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("limit must be greater or equal than 0")))
return
}
if limit > MaxOrgsLimit {
limit = MaxOrgsLimit
}
asc := false
if _, ok := query["asc"]; ok {
asc = true
}
start := query.Get("start")
var orgs []*types.Organization
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
var err error
orgs, err = h.readDB.GetOrgs(tx, start, limit, asc)
return errors.WithStack(err)
})
if err != nil {
h.log.Err(err).Send()
util.HTTPError(w, err)
return
}
if err := util.HTTPResponse(w, http.StatusOK, orgs); err != nil {
h.log.Err(err).Send()
}
}
type AddOrgMemberHandler struct {
log zerolog.Logger
ah *action.ActionHandler
}
func NewAddOrgMemberHandler(log zerolog.Logger, ah *action.ActionHandler) *AddOrgMemberHandler {
return &AddOrgMemberHandler{log: log, ah: ah}
}
func (h *AddOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
orgRef := vars["orgref"]
userRef := vars["userref"]
var req csapitypes.AddOrgMemberRequest
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
return
}
org, err := h.ah.AddOrgMember(ctx, orgRef, userRef, req.Role)
if util.HTTPError(w, err) {
h.log.Err(err).Send()
return
}
if err := util.HTTPResponse(w, http.StatusCreated, org); err != nil {
h.log.Err(err).Send()
}
}
type RemoveOrgMemberHandler struct {
log zerolog.Logger
ah *action.ActionHandler
}
func NewRemoveOrgMemberHandler(log zerolog.Logger, ah *action.ActionHandler) *RemoveOrgMemberHandler {
return &RemoveOrgMemberHandler{log: log, ah: ah}
}
func (h *RemoveOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
orgRef := vars["orgref"]
userRef := vars["userref"]
err := h.ah.RemoveOrgMember(ctx, orgRef, userRef)
if util.HTTPError(w, err) {
h.log.Err(err).Send()
return
}
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
h.log.Err(err).Send()
}
}
func orgMemberResponse(orgUser *action.OrgMemberResponse) *csapitypes.OrgMemberResponse {
return &csapitypes.OrgMemberResponse{
User: orgUser.User,
Role: orgUser.Role,
}
}
type OrgMembersHandler struct {
log zerolog.Logger
ah *action.ActionHandler
}
func NewOrgMembersHandler(log zerolog.Logger, ah *action.ActionHandler) *OrgMembersHandler {
return &OrgMembersHandler{log: log, ah: ah}
}
func (h *OrgMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
orgRef := vars["orgref"]
orgUsers, err := h.ah.GetOrgMembers(ctx, orgRef)
if util.HTTPError(w, err) {
h.log.Err(err).Send()
return
}
res := make([]*csapitypes.OrgMemberResponse, len(orgUsers))
for i, orgUser := range orgUsers {
res[i] = orgMemberResponse(orgUser)
}
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
h.log.Err(err).Send()
}
}