2019-02-28 14:52:35 +00:00
|
|
|
// 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"
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-02-28 14:52:35 +00:00
|
|
|
"github.com/sorintlab/agola/internal/db"
|
2019-05-03 21:35:25 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/configstore/action"
|
2019-02-28 14:52:35 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/configstore/readdb"
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-02-28 14:52:35 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrgHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgHandler(logger *zap.Logger, readDB *readdb.ReadDB) *OrgHandler {
|
|
|
|
return &OrgHandler{log: logger.Sugar(), readDB: readDB}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
orgRef := vars["orgref"]
|
2019-02-28 14:52:35 +00:00
|
|
|
|
|
|
|
var org *types.Organization
|
|
|
|
err := h.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
2019-05-03 09:07:53 +00:00
|
|
|
org, err = h.readDB.GetOrg(tx, orgRef)
|
2019-02-28 14:52:35 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if org == nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
httpError(w, util.NewErrNotFound(errors.Errorf("org %q doesn't exist", orgRef)))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, org); err != nil {
|
2019-02-28 14:52:35 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateOrgHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateOrgHandler {
|
|
|
|
return &CreateOrgHandler{log: logger.Sugar(), ah: ah}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
org, err := h.ah.CreateOrg(ctx, &req)
|
2019-03-14 08:34:05 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-28 14:52:35 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, org); err != nil {
|
2019-02-28 14:52:35 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteOrgHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewDeleteOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteOrgHandler {
|
|
|
|
return &DeleteOrgHandler{log: logger.Sugar(), ah: ah}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.log.Infof("deleteorghandler")
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
orgRef := vars["orgref"]
|
2019-02-28 14:52:35 +00:00
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
err := h.ah.DeleteOrg(ctx, orgRef)
|
2019-03-14 08:34:05 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-28 14:52:35 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultOrgsLimit = 10
|
|
|
|
MaxOrgsLimit = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrgsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgsHandler(logger *zap.Logger, readDB *readdb.ReadDB) *OrgsHandler {
|
|
|
|
return &OrgsHandler{log: logger.Sugar(), readDB: readDB}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultOrgsLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Wrapf(err, "cannot parse limit")))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("limit must be greater or equal than 0")))
|
2019-02-28 14:52:35 +00:00
|
|
|
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(func(tx *db.Tx) error {
|
|
|
|
var err error
|
|
|
|
orgs, err = h.readDB.GetOrgs(tx, start, limit, asc)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, orgs); err != nil {
|
2019-02-28 14:52:35 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:22 +00:00
|
|
|
|
|
|
|
type AddOrgMemberRequest struct {
|
|
|
|
Role types.MemberRole
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddOrgMemberHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAddOrgMemberHandler(logger *zap.Logger, ah *action.ActionHandler) *AddOrgMemberHandler {
|
|
|
|
return &AddOrgMemberHandler{log: logger.Sugar(), 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 AddOrgMemberRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
org, err := h.ah.AddOrgMember(ctx, orgRef, userRef, req.Role)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, org); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|