agola/internal/services/gateway/command/projectgroup.go
2019-04-30 17:09:26 +02:00

69 lines
2.0 KiB
Go

// 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 command
import (
"context"
"path"
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util"
"github.com/pkg/errors"
)
type CreateProjectGroupRequest struct {
CurrentUserID string
Name string
ParentID string
Visibility types.Visibility
}
func (c *CommandHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapi.ProjectGroup, error) {
if !util.ValidateName(req.Name) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid projectGroup name %q", req.Name))
}
user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID)
if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
}
parentID := req.ParentID
if parentID == "" {
// create projectGroup in current user namespace
parentID = path.Join("user", user.Name)
}
p := &types.ProjectGroup{
Name: req.Name,
Parent: types.Parent{
Type: types.ConfigTypeProjectGroup,
ID: parentID,
},
Visibility: req.Visibility,
}
c.log.Infof("creating projectGroup")
rp, resp, err := c.configstoreClient.CreateProjectGroup(ctx, p)
if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create projectGroup"))
}
c.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID)
return rp, nil
}