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 command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateProjectGroupRequest struct {
|
|
|
|
Name string
|
|
|
|
ParentID string
|
|
|
|
CurrentUserID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*types.ProjectGroup, error) {
|
|
|
|
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-04-09 12:53:00 +00:00
|
|
|
user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parentID := req.ParentID
|
|
|
|
if parentID == "" {
|
|
|
|
// create projectGroup in current user namespace
|
2019-04-30 10:56:43 +00:00
|
|
|
parentID = path.Join("user", user.Name)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := &types.ProjectGroup{
|
|
|
|
Name: req.Name,
|
|
|
|
Parent: types.Parent{
|
|
|
|
Type: types.ConfigTypeProjectGroup,
|
|
|
|
ID: parentID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Infof("creating projectGroup")
|
2019-04-09 12:53:00 +00:00
|
|
|
p, resp, err = c.configstoreClient.CreateProjectGroup(ctx, p)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create projectGroup"))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("projectGroup %s created, ID: %s", p.Name, p.ID)
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|