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.
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
package action
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
|
|
|
|
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"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-14 13:36:18 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) GetProjectGroup(ctx context.Context, projectGroupRef string) (*csapitypes.ProjectGroup, error) {
|
2019-05-05 12:27:22 +00:00
|
|
|
projectGroup, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
return projectGroup, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) GetProjectGroupSubgroups(ctx context.Context, projectGroupRef string) ([]*csapitypes.ProjectGroup, error) {
|
2019-05-05 12:27:22 +00:00
|
|
|
projectGroups, resp, err := h.configstoreClient.GetProjectGroupSubgroups(ctx, projectGroupRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
return projectGroups, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*csapitypes.Project, error) {
|
2019-05-05 12:27:22 +00:00
|
|
|
projects, resp, err := h.configstoreClient.GetProjectGroupProjects(ctx, projectGroupRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
return projects, nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
type CreateProjectGroupRequest struct {
|
2019-04-30 15:09:26 +00:00
|
|
|
CurrentUserID string
|
2019-03-14 13:36:18 +00:00
|
|
|
Name string
|
2019-05-05 15:19:23 +00:00
|
|
|
ParentRef string
|
2019-07-31 13:17:54 +00:00
|
|
|
Visibility cstypes.Visibility
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapitypes.ProjectGroup, error) {
|
2019-03-14 13:36:18 +00:00
|
|
|
if !util.ValidateName(req.Name) {
|
2019-04-08 10:08:31 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid projectGroup name %q", req.Name))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:19:23 +00:00
|
|
|
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, req.ParentRef)
|
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return nil, errors.Errorf("failed to get project group %q: %w", req.ParentRef, ErrFromRemote(resp, err))
|
2019-05-03 21:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to determine ownership: %w", err)
|
2019-05-03 21:19:23 +00:00
|
|
|
}
|
|
|
|
if !isProjectOwner {
|
|
|
|
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return nil, errors.Errorf("failed to get user %q: %w", req.CurrentUserID, ErrFromRemote(resp, err))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 15:19:23 +00:00
|
|
|
parentRef := req.ParentRef
|
|
|
|
if parentRef == "" {
|
2019-03-14 13:36:18 +00:00
|
|
|
// create projectGroup in current user namespace
|
2019-05-05 15:19:23 +00:00
|
|
|
parentRef = path.Join("user", user.Name)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
p := &cstypes.ProjectGroup{
|
2019-03-14 13:36:18 +00:00
|
|
|
Name: req.Name,
|
2019-07-31 13:17:54 +00:00
|
|
|
Parent: cstypes.Parent{
|
|
|
|
Type: cstypes.ConfigTypeProjectGroup,
|
2019-05-05 15:19:23 +00:00
|
|
|
ID: parentRef,
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 15:09:26 +00:00
|
|
|
Visibility: req.Visibility,
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("creating projectGroup")
|
|
|
|
rp, resp, err := h.configstoreClient.CreateProjectGroup(ctx, p)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return nil, errors.Errorf("failed to create projectGroup: %w", ErrFromRemote(resp, err))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID)
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
return rp, nil
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-05-12 22:24:16 +00:00
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
type UpdateProjectGroupRequest struct {
|
|
|
|
Name string
|
2019-07-31 13:17:54 +00:00
|
|
|
Visibility cstypes.Visibility
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) UpdateProjectGroup(ctx context.Context, projectGroupRef string, req *UpdateProjectGroupRequest) (*csapitypes.ProjectGroup, error) {
|
2019-05-14 15:53:48 +00:00
|
|
|
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
|
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return nil, errors.Errorf("failed to get project group %q: %w", projectGroupRef, ErrFromRemote(resp, err))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to determine ownership: %w", err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
if !isProjectOwner {
|
|
|
|
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
|
|
|
pg.Name = req.Name
|
|
|
|
pg.Visibility = req.Visibility
|
|
|
|
|
|
|
|
h.log.Infof("updating project group")
|
|
|
|
rp, resp, err := h.configstoreClient.UpdateProjectGroup(ctx, pg.ID, pg.ProjectGroup)
|
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return nil, errors.Errorf("failed to update project group: %w", ErrFromRemote(resp, err))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
h.log.Infof("project group %q updated, ID: %s", pg.Name, pg.ID)
|
|
|
|
|
|
|
|
return rp, nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:24:16 +00:00
|
|
|
func (h *ActionHandler) DeleteProjectGroup(ctx context.Context, projectRef string) error {
|
|
|
|
p, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectRef)
|
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return errors.Errorf("failed to get project %q: %w", projectRef, ErrFromRemote(resp, err))
|
2019-05-12 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to determine ownership: %w", err)
|
2019-05-12 22:24:16 +00:00
|
|
|
}
|
|
|
|
if !isProjectOwner {
|
|
|
|
return util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = h.configstoreClient.DeleteProjectGroup(ctx, projectRef)
|
|
|
|
if err != nil {
|
|
|
|
return ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|