-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcreate.go
More file actions
122 lines (105 loc) · 3.42 KB
/
Copy pathcreate.go
File metadata and controls
122 lines (105 loc) · 3.42 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2021 The BFE Authors.
//
// 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 product_pool
import (
"net/http"
"strings"
"github.com/bfenetworks/api-server/lib"
"github.com/bfenetworks/api-server/lib/xerror"
"github.com/bfenetworks/api-server/lib/xreq"
"github.com/bfenetworks/api-server/model/iauth"
"github.com/bfenetworks/api-server/model/ibasic"
"github.com/bfenetworks/api-server/model/icluster_conf"
"github.com/bfenetworks/api-server/stateful/container"
)
// CreateRoute route
// AUTO GEN BY ctrl, MODIFY AS U NEED
var CreateEndpoint = &xreq.Endpoint{
Path: "/products/{product_name}/instance-pools",
Method: http.MethodPost,
Handler: xreq.Convert(CreateAction),
Authorizer: iauth.FAP(iauth.FeatureProductPool, iauth.ActionCreate),
}
// CreateParam Request Param
// AUTO GEN BY ctrl, MODIFY AS U NEED
type CreateParam struct {
Name *string `json:"name" validate:"required,min=2"`
Type *int8 `json:"type" validate:"oneof=1"`
Instances []*Instance `json:"instances" validate:"min=1,dive"`
}
// AUTO GEN BY ctrl, MODIFY AS U NEED
func NewCreateParam(req *http.Request) (*CreateParam, error) {
param := &CreateParam{
Type: lib.PInt8(icluster_conf.InstancePoolTypeRDB),
}
err := xreq.Bind(req, param)
if err != nil {
return nil, err
}
return param, err
}
var _ xreq.Handler = CreateAction
// CreateAction action
// AUTO GEN BY ctrl, MODIFY AS U NEED
func CreateAction(req *http.Request) (interface{}, error) {
param, err := NewCreateParam(req)
if err != nil {
return nil, err
}
product, err := ibasic.MustGetProduct(req.Context())
if err != nil {
return nil, err
}
if !strings.HasPrefix(*param.Name, product.Name+".") {
return nil, xerror.WrapParamErrorWithMsg("Want Prefix %s.", product.Name)
}
if len(*param.Name) == len(product.Name)+1 {
return nil, xerror.WrapParamErrorWithMsg("Want Pool Name")
}
oneData, err := CreateProcess(req, product, param)
if err != nil {
return nil, err
}
manager, err := container.InstancePoolManager.BatchFetchInstances(req.Context(), []*icluster_conf.Pool{oneData})
if err != nil {
return nil, err
}
return NewOneData(oneData, manager[oneData.Name]), nil
}
func Instancesc2i(is []*Instance) []icluster_conf.Instance {
rst := []icluster_conf.Instance{}
for _, instance := range is {
port := 0
if instance.Ports != nil {
port = instance.Ports["Default"]
}
rst = append(rst, icluster_conf.Instance{
HostName: instance.Hostname,
IP: instance.IP,
Weight: instance.Weight,
Ports: instance.Ports,
Port: port,
Tags: instance.Tags,
})
}
return rst
}
func CreateProcess(req *http.Request, product *ibasic.Product, param *CreateParam) (*icluster_conf.Pool, error) {
return container.PoolManager.CreateProductPool(req.Context(), product, &icluster_conf.PoolParam{
Name: param.Name,
Type: param.Type,
}, &icluster_conf.InstancePool{
Instances: Instancesc2i(param.Instances),
})
}