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-04-09 12:53:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-03-14 13:36:18 +00:00
|
|
|
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
|
|
|
"github.com/sorintlab/agola/internal/services/gateway/command"
|
2019-04-30 15:09:26 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateProjectGroupRequest struct {
|
2019-04-30 15:09:26 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ParentID string `json:"parent_id,omitempty"`
|
|
|
|
Visibility types.Visibility `json:"visibility,omitempty"`
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateProjectGroupHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ch *command.CommandHandler
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
exposedURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCreateProjectGroupHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectGroupHandler {
|
|
|
|
return &CreateProjectGroupHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req CreateProjectGroupRequest
|
|
|
|
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-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
userIDVal := ctx.Value("userid")
|
|
|
|
if userIDVal == nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("user not authenticated")))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
userID := userIDVal.(string)
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Infof("userID: %q", userID)
|
|
|
|
|
|
|
|
creq := &command.CreateProjectGroupRequest{
|
|
|
|
Name: req.Name,
|
|
|
|
ParentID: req.ParentID,
|
2019-04-30 15:09:26 +00:00
|
|
|
Visibility: req.Visibility,
|
2019-03-14 13:36:18 +00:00
|
|
|
CurrentUserID: userID,
|
|
|
|
}
|
|
|
|
|
|
|
|
projectGroup, err := h.ch.CreateProjectGroup(ctx, creq)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res := createProjectGroupResponse(projectGroup)
|
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGroupHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectGroupHandler {
|
|
|
|
return &ProjectGroupHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:04:55 +00:00
|
|
|
projectGroup, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectGroupResponse(projectGroup)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupProjectsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGroupProjectsHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectGroupProjectsHandler {
|
|
|
|
return &ProjectGroupProjectsHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:04:55 +00:00
|
|
|
csprojects, resp, err := h.configstoreClient.GetProjectGroupProjects(ctx, projectGroupRef)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projects := make([]*ProjectResponse, len(csprojects))
|
|
|
|
for i, p := range csprojects {
|
|
|
|
projects[i] = createProjectResponse(p)
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, projects); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupSubgroupsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGroupSubgroupsHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectGroupSubgroupsHandler {
|
|
|
|
return &ProjectGroupSubgroupsHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:04:55 +00:00
|
|
|
cssubgroups, resp, err := h.configstoreClient.GetProjectGroupSubgroups(ctx, projectGroupRef)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
subgroups := make([]*ProjectGroupResponse, len(cssubgroups))
|
|
|
|
for i, g := range cssubgroups {
|
|
|
|
subgroups[i] = createProjectGroupResponse(g)
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, subgroups); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupResponse struct {
|
2019-04-30 15:09:26 +00:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
ParentPath string `json:"parent_path,omitempty"`
|
|
|
|
GlobalVisibility string `json:"global_visibility,omitempty"`
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
func createProjectGroupResponse(r *csapi.ProjectGroup) *ProjectGroupResponse {
|
2019-03-14 13:36:18 +00:00
|
|
|
run := &ProjectGroupResponse{
|
2019-04-30 15:09:26 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
Path: r.Path,
|
|
|
|
ParentPath: r.ParentPath,
|
|
|
|
GlobalVisibility: string(r.GlobalVisibility),
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return run
|
|
|
|
}
|