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-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/action"
|
2022-02-24 10:18:29 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/common"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
|
|
|
gwapitypes "agola.io/agola/services/gateway/api/types"
|
2019-02-28 14:52:35 +00:00
|
|
|
"go.uber.org/zap"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-28 14:52:35 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateOrgHandler struct {
|
2019-04-08 09:35:45 +00:00
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +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()
|
|
|
|
|
2022-02-24 10:18:29 +00:00
|
|
|
userID := common.CurrentUserID(ctx)
|
2019-05-03 12:24:18 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.CreateOrgRequest
|
2019-02-28 14:52:35 +00:00
|
|
|
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:48:49 +00:00
|
|
|
creq := &action.CreateOrgRequest{
|
2019-05-03 12:24:18 +00:00
|
|
|
Name: req.Name,
|
2019-07-31 13:39:07 +00:00
|
|
|
Visibility: cstypes.Visibility(req.Visibility),
|
2019-05-03 12:24:18 +00:00
|
|
|
CreatorUserID: userID,
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
org, err := h.ah.CreateOrg(ctx, creq)
|
2019-04-08 09:35:45 +00:00
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res := createOrgResponse(org)
|
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteOrgHandler struct {
|
2019-05-05 12:36:14 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:36:14 +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) {
|
|
|
|
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-05 12:36:14 +00:00
|
|
|
err := h.ah.DeleteOrg(ctx, orgRef)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgHandler struct {
|
2019-05-05 12:36:14 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:36:14 +00:00
|
|
|
func NewOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgHandler {
|
|
|
|
return &OrgHandler{log: logger.Sugar(), ah: ah}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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-05 12:36:14 +00:00
|
|
|
org, err := h.ah.GetOrg(ctx, orgRef)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createOrgResponse(org)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createOrgResponse(o *cstypes.Organization) *gwapitypes.OrgResponse {
|
|
|
|
org := &gwapitypes.OrgResponse{
|
2019-05-14 08:56:17 +00:00
|
|
|
ID: o.ID,
|
|
|
|
Name: o.Name,
|
2019-07-31 13:39:07 +00:00
|
|
|
Visibility: gwapitypes.Visibility(o.Visibility),
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
return org
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgsHandler struct {
|
2019-05-05 12:36:14 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:36:14 +00:00
|
|
|
func NewOrgsHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgsHandler {
|
|
|
|
return &OrgsHandler{log: logger.Sugar(), ah: ah}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultRunsLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("cannot parse limit: %w", err)))
|
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 > MaxRunsLimit {
|
|
|
|
limit = MaxRunsLimit
|
|
|
|
}
|
|
|
|
asc := false
|
|
|
|
if _, ok := query["asc"]; ok {
|
|
|
|
asc = true
|
|
|
|
}
|
|
|
|
|
|
|
|
start := query.Get("start")
|
|
|
|
|
2019-05-05 12:36:14 +00:00
|
|
|
areq := &action.GetOrgsRequest{
|
|
|
|
Start: start,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
}
|
|
|
|
csorgs, err := h.ah.GetOrgs(ctx, areq)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
orgs := make([]*gwapitypes.OrgResponse, len(csorgs))
|
2019-02-28 14:52:35 +00:00
|
|
|
for i, p := range csorgs {
|
|
|
|
orgs[i] = createOrgResponse(p)
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, orgs); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:22 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createOrgMemberResponse(user *cstypes.User, role cstypes.MemberRole) *gwapitypes.OrgMemberResponse {
|
|
|
|
return &gwapitypes.OrgMemberResponse{
|
2019-05-14 09:25:12 +00:00
|
|
|
User: createUserResponse(user),
|
2019-07-31 13:39:07 +00:00
|
|
|
Role: gwapitypes.MemberRole(role),
|
2019-05-14 09:25:12 +00:00
|
|
|
}
|
2019-05-14 10:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type OrgMembersHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgMembersHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgMembersHandler {
|
|
|
|
return &OrgMembersHandler{log: logger.Sugar(), ah: ah}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
orgRef := vars["orgref"]
|
|
|
|
|
|
|
|
ares, err := h.ah.GetOrgMembers(ctx, orgRef)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
res := &gwapitypes.OrgMembersResponse{
|
2019-05-14 10:58:29 +00:00
|
|
|
Organization: createOrgResponse(ares.Organization),
|
2019-07-31 13:39:07 +00:00
|
|
|
Members: make([]*gwapitypes.OrgMemberResponse, len(ares.Members)),
|
2019-05-14 10:58:29 +00:00
|
|
|
}
|
|
|
|
for i, m := range ares.Members {
|
|
|
|
res.Members[i] = createOrgMemberResponse(m.User, m.Role)
|
|
|
|
}
|
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-05-14 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createAddOrgMemberResponse(org *cstypes.Organization, user *cstypes.User, role cstypes.MemberRole) *gwapitypes.AddOrgMemberResponse {
|
|
|
|
return &gwapitypes.AddOrgMemberResponse{
|
2019-05-09 14:47:22 +00:00
|
|
|
Organization: createOrgResponse(org),
|
2019-07-31 13:39:07 +00:00
|
|
|
OrgMemberResponse: gwapitypes.OrgMemberResponse{
|
2019-05-14 09:25:12 +00:00
|
|
|
User: createUserResponse(user),
|
2019-07-31 13:39:07 +00:00
|
|
|
Role: gwapitypes.MemberRole(role),
|
2019-05-14 09:25:12 +00:00
|
|
|
},
|
2019-05-09 14:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.AddOrgMemberRequest
|
2019-05-09 14:47:22 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
ares, err := h.ah.AddOrgMember(ctx, orgRef, userRef, cstypes.MemberRole(req.Role))
|
2019-05-09 14:47:22 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:25:12 +00:00
|
|
|
res := createAddOrgMemberResponse(ares.Org, ares.User, ares.OrganizationMember.MemberRole)
|
2019-05-09 14:47:22 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 15:05:13 +00:00
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
type RemoveOrgMemberHandler struct {
|
2019-05-09 15:05:13 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
func NewRemoveOrgMemberHandler(logger *zap.Logger, ah *action.ActionHandler) *RemoveOrgMemberHandler {
|
|
|
|
return &RemoveOrgMemberHandler{log: logger.Sugar(), ah: ah}
|
2019-05-09 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
func (h *RemoveOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-05-09 15:05:13 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
orgRef := vars["orgref"]
|
|
|
|
userRef := vars["userref"]
|
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
err := h.ah.RemoveOrgMember(ctx, orgRef, userRef)
|
2019-05-09 15:05:13 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|