-
Notifications
You must be signed in to change notification settings - Fork 373
/
factory.go
137 lines (122 loc) · 4.13 KB
/
factory.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package redis
import (
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/cmp/dbclient"
aliyun_resources "github.com/erda-project/erda/modules/cmp/impl/aliyun-resources"
resource_factory "github.com/erda-project/erda/modules/cmp/impl/resource-factory"
"github.com/erda-project/erda/pkg/crypto/uuid"
)
type RedisFactory struct {
*resource_factory.BaseResourceFactory
}
func creator(ctx aliyun_resources.Context, m resource_factory.BaseResourceMaterial, r *dbclient.Record, d *apistructs.CreateCloudResourceRecord, v apistructs.CloudResourceVpcBaseInfo) (*apistructs.AddonConfigCallBackResponse, *dbclient.ResourceRouting, error) {
var err error
req, ok := m.(apistructs.CreateCloudResourceRedisRequest)
if !ok {
return nil, nil, errors.Errorf("convert material failed, material: %+v", m)
}
regionids := aliyun_resources.ActiveRegionIDs(ctx)
list, err := List(ctx, aliyun_resources.DefaultPageOption, regionids.ECS, "")
if err != nil {
err = errors.Wrap(err, "list redis failed")
return nil, nil, err
}
for _, item := range list {
if req.InstanceName == item.InstanceName {
err := errors.Errorf("redis instance already exist, region:%s, name:%s", item.RegionId, item.InstanceName)
return nil, nil, err
}
}
// auto generate password if not provide
if req.Password == "" {
req.Password = uuid.UUID()[:8] + "r@1" + uuid.UUID()[:8]
}
// auto generate AutoRenewPeriod by ChargePeriod
if strings.ToLower(req.ChargeType) == aliyun_resources.ChargeTypePrepaid {
p, err := strconv.Atoi(req.ChargePeriod)
if err != nil {
return nil, nil, errors.New("invalid charge period, support format:1-9,12,24,36, (month)")
}
if p >= 12 {
req.AutoRenewPeriod = "12"
} else if p >= 6 {
req.AutoRenewPeriod = "6"
} else if p >= 3 {
req.AutoRenewPeriod = "3"
} else if p <= 0 {
req.AutoRenewPeriod = "1"
}
}
// get available vswitch/zone
ctx.VpcID = req.VpcID
vsw, err := GetAvailableVsw(ctx, apistructs.CreateCloudResourceBaseInfo{VSwitchID: req.VSwitchID, ZoneID: req.ZoneID})
if err != nil {
logrus.Errorf("get available vswitch failed, error: %v", err)
return nil, nil, err
}
req.VSwitchID = vsw.VSwitchId
req.ZoneID = vsw.ZoneId
logrus.Infof("start to create redis instance, request: %+v", req)
resp, err := CreateInstance(ctx, req)
if err != nil {
return nil, nil, errors.Wrap(err, "create redis instance failed")
}
d.InstanceID = resp.InstanceId
if req.Source != apistructs.CloudResourceSourceAddon {
return nil, nil, nil
}
cbResp := &apistructs.AddonConfigCallBackResponse{
Config: []apistructs.AddonConfigCallBackItemResponse{
{
Name: "REDIS_HOST",
Value: resp.ConnectionDomain,
},
{
Name: "REDIS_PORT",
Value: resp.Port,
},
{
Name: "REDIS_PASSWORD",
Value: req.Password,
},
},
}
routing := &dbclient.ResourceRouting{
ResourceID: resp.InstanceId,
ResourceName: req.InstanceName,
ResourceType: dbclient.ResourceTypeRedis,
Vendor: req.Vendor,
OrgID: req.OrgID,
ClusterName: req.ClusterName,
ProjectID: req.ProjectID,
AddonID: req.AddonID,
Status: dbclient.ResourceStatusAttached,
RecordID: r.ID,
}
return cbResp, routing, nil
}
func init() {
factory := RedisFactory{BaseResourceFactory: &resource_factory.BaseResourceFactory{}}
factory.Creator = creator
factory.RecordType = dbclient.RecordTypeCreateAliCloudRedis
err := resource_factory.Register(dbclient.ResourceTypeRedis, factory)
if err != nil {
panic(err)
}
}