-
Notifications
You must be signed in to change notification settings - Fork 132
/
create_warehouse_v1alpha1.go
54 lines (49 loc) · 1.8 KB
/
create_warehouse_v1alpha1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package api
import (
"context"
"connectrpc.com/connect"
"github.com/pkg/errors"
kubeerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
typesv1alpha1 "github.com/akuity/kargo/internal/api/types/v1alpha1"
svcv1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1"
)
func (s *server) CreateWarehouse(
ctx context.Context,
req *connect.Request[svcv1alpha1.CreateWarehouseRequest],
) (*connect.Response[svcv1alpha1.CreateWarehouseResponse], error) {
var warehouse kargoapi.Warehouse
switch {
case req.Msg.GetYaml() != "":
if err := yaml.Unmarshal([]byte(req.Msg.GetYaml()), &warehouse); err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrap(err, "invalid yaml"))
}
case req.Msg.GetTyped() != nil:
warehouse = kargoapi.Warehouse{
ObjectMeta: metav1.ObjectMeta{
Namespace: req.Msg.GetTyped().GetProject(),
Name: req.Msg.GetTyped().GetName(),
},
Spec: typesv1alpha1.FromWarehouseSpecProto(req.Msg.GetTyped().GetSpec()),
}
default:
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("warehouse should not be empty"))
}
if err := validateProjectAndWarehouseName(warehouse.GetNamespace(), warehouse.GetName()); err != nil {
return nil, err
}
if err := s.validateProject(ctx, warehouse.GetNamespace()); err != nil {
return nil, err
}
if err := s.client.Create(ctx, &warehouse); err != nil {
if kubeerr.IsAlreadyExists(err) {
return nil, connect.NewError(connect.CodeAlreadyExists, err)
}
return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "create warehouse"))
}
return connect.NewResponse(&svcv1alpha1.CreateWarehouseResponse{
Warehouse: typesv1alpha1.ToWarehouseProto(warehouse),
}), nil
}