-
Notifications
You must be signed in to change notification settings - Fork 373
/
list.go
124 lines (111 loc) · 3.67 KB
/
list.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
// 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 (
"fmt"
"strings"
"time"
"github.com/erda-project/erda/pkg/strutil"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
kvstore "github.com/aliyun/alibaba-cloud-sdk-go/services/r-kvstore"
"github.com/sirupsen/logrus"
aliyun_resources "github.com/erda-project/erda/modules/cmp/impl/aliyun-resources"
)
// list instance
func List(ctx aliyun_resources.Context, page aliyun_resources.PageOption,
regions []string,
cluster string) ([]kvstore.KVStoreInstance, error) {
instances := []kvstore.KVStoreInstance{}
for _, region := range regions {
ctx.Region = region
response, err := DescribeResource(ctx, page, cluster, []string{})
if err != nil {
return nil, err
}
instances = append(instances, response.Instances.KVStoreInstance...)
}
return instances, nil
}
// describe instance
func DescribeResource(ctx aliyun_resources.Context, page aliyun_resources.PageOption, cluster string, instanceIDs []string) (*kvstore.DescribeInstancesResponse, error) {
// create client
client, err := kvstore.NewClientWithAccessKey(ctx.Region, ctx.AccessKeyID, ctx.AccessSecret)
if err != nil {
logrus.Errorf("create ecs client error: %+v", err)
return nil, err
}
// create request
request := kvstore.CreateDescribeInstancesRequest()
request.Scheme = "https"
// Query multi instances, using (",") to separate the IDs
// If ID is empty, query all instances in this user.
request.InstanceIds = strings.Join(instanceIDs, ",")
if page.PageNumber == nil || page.PageSize == nil || *page.PageSize <= 0 || *page.PageNumber <= 0 {
err := fmt.Errorf("invalid page parameters: %+v", page)
logrus.Errorf(err.Error())
return nil, err
}
if page.PageSize != nil {
request.PageSize = requests.NewInteger(*page.PageSize)
}
if page.PageNumber != nil {
request.PageNumber = requests.NewInteger(*page.PageNumber)
}
request.RegionId = ctx.Region
if cluster != "" {
tagKey, tagValue := aliyun_resources.GenClusterTag(cluster)
request.Tag = &[]kvstore.DescribeInstancesTag{{Key: tagKey, Value: tagValue}}
}
response, err := client.DescribeInstances(request)
if err != nil {
e := fmt.Errorf("describe redis instances failed, error:%v", err)
logrus.Errorf(e.Error())
return nil, e
}
return response, nil
}
func Classify(ins []kvstore.KVStoreInstance) (runningCount, gonnaExpiredCount, expiredCount, stoppedCount,
postpaidCount, prepaidCount int, err error) {
now := time.Now()
for _, i := range ins {
if strutil.ToLower(i.ChargeType) == "postpaid" {
postpaidCount += 1
} else {
prepaidCount += 1
}
// stopped status
if strutil.ToLower(i.InstanceStatus) == "released" {
stoppedCount += 1
continue
}
// postpaid running status
if strutil.ToLower(i.ChargeType) == "postpaid" {
runningCount += 1
continue
}
var t time.Time
t, err = time.Parse("2006-01-02T15:04:05Z", i.EndTime)
if err != nil {
logrus.Errorf("redis, failed to parse expiredtime: %v, %s", err, i.EndTime)
continue
}
if t.Before(now) {
expiredCount += 1
} else if t.Before(now.Add(24 * 10 * time.Hour)) {
gonnaExpiredCount += 1
} else {
runningCount += 1
}
}
return
}