Skip to content

Commit cc3a496

Browse files
authored
Merge pull request #1216 from ioito/hotfix/qx-ksyun-rds
fix(ksyun): support rds
2 parents 60917be + fd2bc67 commit cc3a496

34 files changed

+858
-38
lines changed

pkg/apis/compute/dbinstance_const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ const (
129129

130130
// Azure
131131
AZURE_DBINSTANCE_STORAGE_TYPE_DEFAULT = "default"
132+
// 金山云
133+
KSYUN_DBINSTANCE_STORAGE_TYPE_DEFAULT = "default"
132134
)
133135

134136
var (

pkg/multicloud/ksyun/dbinstance.go

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ksyun
16+
17+
import (
18+
"fmt"
19+
"time"
20+
21+
billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
22+
api "yunion.io/x/cloudmux/pkg/apis/compute"
23+
"yunion.io/x/cloudmux/pkg/cloudprovider"
24+
"yunion.io/x/cloudmux/pkg/multicloud"
25+
"yunion.io/x/jsonutils"
26+
"yunion.io/x/log"
27+
"yunion.io/x/pkg/errors"
28+
)
29+
30+
type SDBInstance struct {
31+
region *SRegion
32+
33+
multicloud.SDBInstanceBase
34+
SKsyunTags
35+
DBInstanceClass struct {
36+
Id string
37+
Vcpus int
38+
Disk int
39+
RAM int
40+
}
41+
DBInstanceIdentifier string
42+
DBInstanceName string
43+
DBInstanceStatus string
44+
DBInstanceType string
45+
DBParameterGroupId string
46+
GroupId string
47+
Vip string
48+
Port int
49+
Engine string
50+
EngineVersion string
51+
InstanceCreateTime string
52+
MasterUserName string
53+
VpcId string
54+
SubnetId string
55+
DiskUsed int
56+
PubliclyAccessible bool
57+
ReadReplicaDBInstanceIdentifiers []interface{}
58+
BillType string
59+
OrderType string
60+
OrderSource string
61+
ProductType int
62+
MultiAvailabilityZone bool
63+
MasterAvailabilityZone string
64+
SlaveAvailabilityZone string
65+
ProductId string
66+
OrderUse string
67+
SupportIPV6 bool
68+
ProjectId string
69+
ProjectName string
70+
Region string
71+
BillTypeId int
72+
Eip string
73+
EipPort int
74+
NetworkType int
75+
}
76+
77+
func (region *SRegion) GetIDBInstanceById(id string) (cloudprovider.ICloudDBInstance, error) {
78+
vm, err := region.GetDBInstance(id)
79+
if err != nil {
80+
return nil, err
81+
}
82+
return vm, nil
83+
}
84+
85+
func (region *SRegion) GetIDBInstances() ([]cloudprovider.ICloudDBInstance, error) {
86+
vms, err := region.GetDBInstances("")
87+
if err != nil {
88+
return nil, err
89+
}
90+
ret := []cloudprovider.ICloudDBInstance{}
91+
for i := range vms {
92+
vms[i].region = region
93+
ret = append(ret, &vms[i])
94+
}
95+
return ret, nil
96+
}
97+
98+
func (region *SRegion) GetDBInstance(id string) (*SDBInstance, error) {
99+
vms, err := region.GetDBInstances(id)
100+
if err != nil {
101+
return nil, err
102+
}
103+
for i := range vms {
104+
if vms[i].GetGlobalId() == id {
105+
vms[i].region = region
106+
return &vms[i], nil
107+
}
108+
}
109+
return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
110+
}
111+
112+
func (region *SRegion) GetDBInstances(id string) ([]SDBInstance, error) {
113+
params := map[string]string{}
114+
if len(id) > 0 {
115+
params["DBInstanceIdentifier"] = id
116+
}
117+
ret := []SDBInstance{}
118+
for {
119+
resp, err := region.rdsRequest("DescribeDBInstances", params)
120+
if err != nil {
121+
return nil, err
122+
}
123+
part := struct {
124+
Instances []SDBInstance
125+
TotalCount int
126+
Marker string
127+
}{}
128+
err = resp.Unmarshal(&part, "Data")
129+
if err != nil {
130+
return nil, err
131+
}
132+
ret = append(ret, part.Instances...)
133+
if len(ret) >= part.TotalCount {
134+
break
135+
}
136+
params["Marker"] = part.Marker
137+
}
138+
return ret, nil
139+
}
140+
141+
func (rds *SDBInstance) GetName() string {
142+
if len(rds.DBInstanceName) > 0 {
143+
return rds.DBInstanceName
144+
}
145+
return rds.DBInstanceIdentifier
146+
}
147+
148+
func (rds *SDBInstance) GetId() string {
149+
return rds.DBInstanceIdentifier
150+
}
151+
152+
func (rds *SDBInstance) GetGlobalId() string {
153+
return rds.GetId()
154+
}
155+
156+
func (rds *SDBInstance) GetStatus() string {
157+
switch rds.DBInstanceStatus {
158+
case "ACTIVE":
159+
return api.DBINSTANCE_RUNNING
160+
default:
161+
log.Errorf("Unknown dbinstance status %s", rds.DBInstanceStatus)
162+
return api.DBINSTANCE_UNKNOWN
163+
}
164+
}
165+
166+
func (rds *SDBInstance) GetBillingType() string {
167+
if rds.BillType != "HourlyInstantSettlement" {
168+
return billing_api.BILLING_TYPE_PREPAID
169+
}
170+
return billing_api.BILLING_TYPE_POSTPAID
171+
}
172+
173+
func (rds *SDBInstance) GetExpiredAt() time.Time {
174+
return time.Time{}
175+
}
176+
177+
func (rds *SDBInstance) GetCreatedAt() time.Time {
178+
t, _ := time.Parse("2006-01-02T15:04:05-0700", rds.InstanceCreateTime)
179+
return t
180+
}
181+
182+
func (rds *SDBInstance) GetStorageType() string {
183+
return api.KSYUN_DBINSTANCE_STORAGE_TYPE_DEFAULT
184+
}
185+
186+
func (rds *SDBInstance) GetEngine() string {
187+
return api.DBINSTANCE_TYPE_MYSQL
188+
}
189+
190+
func (rds *SDBInstance) GetEngineVersion() string {
191+
return rds.EngineVersion
192+
}
193+
194+
func (rds *SDBInstance) GetInstanceType() string {
195+
return rds.DBInstanceType
196+
}
197+
198+
func (rds *SDBInstance) GetCategory() string {
199+
if rds.MultiAvailabilityZone {
200+
return api.ALIYUN_DBINSTANCE_CATEGORY_HA
201+
}
202+
return api.ALIYUN_DBINSTANCE_CATEGORY_BASIC
203+
}
204+
205+
func (rds *SDBInstance) GetVcpuCount() int {
206+
return rds.DBInstanceClass.Vcpus
207+
}
208+
209+
func (rds *SDBInstance) GetVmemSizeMB() int {
210+
return rds.DBInstanceClass.RAM * 1024
211+
}
212+
213+
func (rds *SDBInstance) GetDiskSizeGB() int {
214+
return rds.DBInstanceClass.Disk * 1024
215+
}
216+
217+
func (rds *SDBInstance) GetDiskSizeUsedMB() int {
218+
return rds.DiskUsed * 1024
219+
}
220+
221+
func (rds *SDBInstance) GetPort() int {
222+
return rds.Port
223+
}
224+
225+
func (rds *SDBInstance) GetMaintainTime() string {
226+
return ""
227+
}
228+
229+
func (rds *SDBInstance) GetIVpcId() string {
230+
return rds.VpcId
231+
}
232+
233+
func (rds *SDBInstance) Refresh() error {
234+
vm, err := rds.region.GetDBInstance(rds.DBInstanceIdentifier)
235+
if err != nil {
236+
return err
237+
}
238+
return jsonutils.Update(rds, vm)
239+
}
240+
241+
func (rds *SDBInstance) GetZone1Id() string {
242+
return rds.MasterAvailabilityZone
243+
}
244+
245+
func (rds *SDBInstance) GetZone2Id() string {
246+
if rds.SlaveAvailabilityZone != rds.MasterAvailabilityZone {
247+
return rds.SlaveAvailabilityZone
248+
}
249+
return ""
250+
}
251+
252+
func (rds *SDBInstance) GetZone3Id() string {
253+
return ""
254+
}
255+
256+
func (rds *SDBInstance) GetIOPS() int {
257+
return 0
258+
}
259+
260+
func (rds *SDBInstance) GetNetworkAddress() string {
261+
return rds.Vip
262+
}
263+
264+
func (rds *SDBInstance) GetDBNetworks() ([]cloudprovider.SDBInstanceNetwork, error) {
265+
return []cloudprovider.SDBInstanceNetwork{
266+
cloudprovider.SDBInstanceNetwork{
267+
IP: rds.Vip,
268+
NetworkId: rds.SubnetId,
269+
},
270+
}, nil
271+
}
272+
273+
func (rds *SDBInstance) GetInternalConnectionStr() string {
274+
return fmt.Sprintf("%s:%d", rds.Vip, rds.Port)
275+
}
276+
277+
func (rds *SDBInstance) GetConnectionStr() string {
278+
if len(rds.Eip) > 0 {
279+
return fmt.Sprintf("%s:%d", rds.Eip, rds.EipPort)
280+
}
281+
return ""
282+
}
283+
284+
func (rds *SDBInstance) GetProjectId() string {
285+
return rds.ProjectId
286+
}
287+
288+
func (rds *SDBInstance) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) {
289+
databases, err := rds.region.GetDBInstanceDatabases(rds.DBInstanceIdentifier)
290+
if err != nil {
291+
return nil, err
292+
}
293+
ret := []cloudprovider.ICloudDBInstanceDatabase{}
294+
for i := 0; i < len(databases); i++ {
295+
databases[i].instance = rds
296+
ret = append(ret, &databases[i])
297+
}
298+
return ret, nil
299+
}
300+
301+
func (rds *SDBInstance) Delete() error {
302+
return rds.region.DeleteDBInstance(rds.DBInstanceIdentifier)
303+
}
304+
305+
func (region *SRegion) DeleteDBInstance(instanceId string) error {
306+
params := map[string]string{}
307+
params["DBInstanceIdentifier"] = instanceId
308+
_, err := region.rdsRequest("DeleteDBInstance", params)
309+
return err
310+
}

0 commit comments

Comments
 (0)