2019-04-08 09:35:45 +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-04-08 09:35:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateOrgRequest struct {
|
|
|
|
Name string
|
2019-05-03 12:24:18 +00:00
|
|
|
|
|
|
|
CreatorUserID string
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func (h *ActionHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) {
|
2019-04-08 09:35:45 +00:00
|
|
|
if req.Name == "" {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("organization name required"))
|
|
|
|
}
|
|
|
|
if !util.ValidateName(req.Name) {
|
2019-04-08 10:08:31 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid organization name %q", req.Name))
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
org := &types.Organization{
|
|
|
|
Name: req.Name,
|
|
|
|
}
|
2019-05-03 12:24:18 +00:00
|
|
|
if req.CreatorUserID != "" {
|
|
|
|
org.CreatorUserID = req.CreatorUserID
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("creating organization")
|
|
|
|
org, resp, err := h.configstoreClient.CreateOrg(ctx, org)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create organization"))
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("organization %s created, ID: %s", org.Name, org.ID)
|
2019-04-08 09:35:45 +00:00
|
|
|
|
|
|
|
return org, nil
|
|
|
|
}
|