2019-03-14 13:36:18 +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"
|
|
|
|
"net/url"
|
|
|
|
|
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
|
|
|
csapitypes "agola.io/agola/services/configstore/api/types"
|
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
|
|
|
gwapitypes "agola.io/agola/services/gateway/api/types"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-03-14 13:36:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateProjectGroupHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-11 22:28:11 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateProjectGroupHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateProjectGroupHandler {
|
|
|
|
return &CreateProjectGroupHandler{log: log, ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.CreateProjectGroupRequest
|
2019-03-14 13:36:18 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-24 10:18:29 +00:00
|
|
|
userID := common.CurrentUserID(ctx)
|
|
|
|
if userID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated")))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.CreateProjectGroupRequest{
|
2019-03-14 13:36:18 +00:00
|
|
|
Name: req.Name,
|
2019-05-05 15:19:23 +00:00
|
|
|
ParentRef: req.ParentRef,
|
2019-07-31 13:39:07 +00:00
|
|
|
Visibility: cstypes.Visibility(req.Visibility),
|
2019-03-14 13:36:18 +00:00
|
|
|
CurrentUserID: userID,
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
projectGroup, err := h.ah.CreateProjectGroup(ctx, creq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res := createProjectGroupResponse(projectGroup)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
type UpdateProjectGroupHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-14 15:53:48 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUpdateProjectGroupHandler(log zerolog.Logger, ah *action.ActionHandler) *UpdateProjectGroupHandler {
|
|
|
|
return &UpdateProjectGroupHandler{log: log, ah: ah}
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UpdateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-14 15:53:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.UpdateProjectGroupRequest
|
2019-05-14 15:53:48 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-14 15:53:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-01 07:37:13 +00:00
|
|
|
var visibility *cstypes.Visibility
|
|
|
|
if req.Visibility != nil {
|
|
|
|
v := cstypes.Visibility(*req.Visibility)
|
|
|
|
visibility = &v
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
areq := &action.UpdateProjectGroupRequest{
|
|
|
|
Name: req.Name,
|
2019-10-01 07:37:13 +00:00
|
|
|
ParentRef: req.ParentRef,
|
|
|
|
Visibility: visibility,
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
projectGroup, err := h.ah.UpdateProjectGroup(ctx, projectGroupRef, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-14 15:53:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectGroupResponse(projectGroup)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:24:16 +00:00
|
|
|
type DeleteProjectGroupHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-12 22:24:16 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteProjectGroupHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteProjectGroupHandler {
|
|
|
|
return &DeleteProjectGroupHandler{log: log, ah: ah}
|
2019-05-12 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-12 22:24:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.ah.DeleteProjectGroup(ctx, projectGroupRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-12 22:24:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-12 22:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
type ProjectGroupHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:27:22 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewProjectGroupHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectGroupHandler {
|
|
|
|
return &ProjectGroupHandler{log: log, ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
projectGroup, err := h.ah.GetProjectGroup(ctx, projectGroupRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectGroupResponse(projectGroup)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupProjectsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:27:22 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewProjectGroupProjectsHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectGroupProjectsHandler {
|
|
|
|
return &ProjectGroupProjectsHandler{log: log, ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
csprojects, err := h.ah.GetProjectGroupProjects(ctx, projectGroupRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
projects := make([]*gwapitypes.ProjectResponse, len(csprojects))
|
2019-03-14 13:36:18 +00:00
|
|
|
for i, p := range csprojects {
|
|
|
|
projects[i] = createProjectResponse(p)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, projects); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupSubgroupsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:27:22 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewProjectGroupSubgroupsHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectGroupSubgroupsHandler {
|
|
|
|
return &ProjectGroupSubgroupsHandler{log: log, ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupSubgroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
cssubgroups, err := h.ah.GetProjectGroupSubgroups(ctx, projectGroupRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
subgroups := make([]*gwapitypes.ProjectGroupResponse, len(cssubgroups))
|
2019-03-14 13:36:18 +00:00
|
|
|
for i, g := range cssubgroups {
|
|
|
|
subgroups[i] = createProjectGroupResponse(g)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, subgroups); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createProjectGroupResponse(r *csapitypes.ProjectGroup) *gwapitypes.ProjectGroupResponse {
|
|
|
|
run := &gwapitypes.ProjectGroupResponse{
|
2019-04-30 15:09:26 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
Path: r.Path,
|
|
|
|
ParentPath: r.ParentPath,
|
2019-07-31 13:39:07 +00:00
|
|
|
Visibility: gwapitypes.Visibility(r.Visibility),
|
2019-04-30 15:09:26 +00:00
|
|
|
GlobalVisibility: string(r.GlobalVisibility),
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return run
|
|
|
|
}
|