forked from yunionio/cloudmux
/
filesystem.go
362 lines (323 loc) · 9.35 KB
/
filesystem.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2019 Yunion
//
// 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 aliyun
import (
"fmt"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
api "yunion.io/x/cloudmux/pkg/apis/compute"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
)
type SupportedFeatures struct {
SupportedFeature []string
}
type MountTargets struct {
MountTarget []SMountTarget
}
type Package struct {
}
type Packages struct {
Package []Package
}
type SFileSystem struct {
multicloud.SNasBase
AliyunTags
region *SRegion
Status string
Description string
StorageType string
MountTargetCountLimit int
Ldap string
ZoneId string
// 2021-03-22T10:08:15CST
CreateTime string
MeteredIASize int
SupportedFeatures SupportedFeatures
MountTargets MountTargets
AutoSnapshotPolicyId string
MeteredSize int64
EncryptType int
Capacity int64
ProtocolType string
ChargeType string
Packages Packages
ExpiredTime string
FileSystemType string
FileSystemId string
RegionId string
}
func (self *SFileSystem) GetId() string {
return self.FileSystemId
}
func (self *SFileSystem) GetGlobalId() string {
return self.FileSystemId
}
func (self *SFileSystem) GetName() string {
if len(self.Description) > 0 {
return self.Description
}
return self.FileSystemId
}
func (self *SFileSystem) GetFileSystemType() string {
return self.FileSystemType
}
func (self *SFileSystem) GetMountTargetCountLimit() int {
return self.MountTargetCountLimit
}
func (self *SFileSystem) GetStatus() string {
switch self.Status {
case "", "Running":
return api.NAS_STATUS_AVAILABLE
case "Extending":
return api.NAS_STATUS_EXTENDING
case "Stopping", "Stopped":
return api.NAS_STATUS_UNAVAILABLE
case "Pending":
return api.NAS_STATUS_CREATING
default:
return api.NAS_STATUS_UNKNOWN
}
}
func (self *SFileSystem) GetBillintType() string {
if self.ChargeType == "PayAsYouGo" {
return billing_api.BILLING_TYPE_POSTPAID
}
return billing_api.BILLING_TYPE_PREPAID
}
func (self *SFileSystem) GetStorageType() string {
return strings.ToLower(self.StorageType)
}
func (self *SFileSystem) GetProtocol() string {
return self.ProtocolType
}
func (self *SFileSystem) GetCapacityGb() int64 {
return self.Capacity
}
func (self *SFileSystem) GetUsedCapacityGb() int64 {
return self.MeteredSize
}
func (self *SFileSystem) GetZoneId() string {
return self.ZoneId
}
func (self *SFileSystem) Delete() error {
return self.region.DeleteFileSystem(self.FileSystemId)
}
func (self *SFileSystem) GetCreatedAt() time.Time {
ret, _ := time.Parse("2006-01-02T15:04:05CST", self.CreateTime)
if !ret.IsZero() {
ret = ret.Add(time.Hour * 8)
}
return ret
}
func (self *SFileSystem) GetExpiredAt() time.Time {
ret, _ := time.Parse("2006-01-02T15:04:05CST", self.ExpiredTime)
if !ret.IsZero() {
ret = ret.Add(time.Hour * 8)
}
return ret
}
func (self *SFileSystem) Refresh() error {
fs, err := self.region.GetFileSystem(self.FileSystemId)
if err != nil {
return errors.Wrapf(err, "GetFileSystem(%s)", self.FileSystemId)
}
return jsonutils.Update(self, fs)
}
func (self *SRegion) GetFileSystems(id string, pageSize, pageNum int) ([]SFileSystem, int, error) {
if pageSize < 1 || pageSize > 100 {
pageSize = 50
}
if pageNum < 1 {
pageNum = 1
}
params := map[string]string{
"RegionId": self.RegionId,
"PageSize": fmt.Sprintf("%d", pageSize),
"PageNumber": fmt.Sprintf("%d", pageNum),
}
if len(id) > 0 {
params["FileSystemId"] = id
}
resp, err := self.nasRequest("DescribeFileSystems", params)
if err != nil {
return nil, 0, errors.Wrapf(err, "DescribeFileSystems")
}
ret := struct {
TotalCount int
FileSystems struct {
FileSystem []SFileSystem
}
}{}
err = resp.Unmarshal(&ret)
if err != nil {
return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
}
return ret.FileSystems.FileSystem, ret.TotalCount, nil
}
func (self *SRegion) GetICloudFileSystems() ([]cloudprovider.ICloudFileSystem, error) {
nas := []SFileSystem{}
num := 1
for {
part, total, err := self.GetFileSystems("", 100, num)
if err != nil {
return nil, errors.Wrapf(err, "GetFileSystems")
}
nas = append(nas, part...)
if total <= len(nas) {
break
}
num++
}
ret := []cloudprovider.ICloudFileSystem{}
for i := range nas {
nas[i].region = self
ret = append(ret, &nas[i])
}
return ret, nil
}
func (self *SRegion) GetFileSystem(id string) (*SFileSystem, error) {
if len(id) == 0 {
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty id")
}
nas, total, err := self.GetFileSystems(id, 1, 1)
if err != nil {
return nil, errors.Wrapf(err, "GetFileSystems")
}
if total == 1 {
nas[0].region = self
return &nas[0], nil
}
if total == 0 {
return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
}
return nil, errors.Wrapf(cloudprovider.ErrDuplicateId, id)
}
func (self *SRegion) GetICloudFileSystemById(id string) (cloudprovider.ICloudFileSystem, error) {
fs, err := self.GetFileSystem(id)
if err != nil {
return nil, errors.Wrapf(err, "self.GetFileSystem")
}
return fs, nil
}
func (self *SRegion) CreateMountTarget(opts *cloudprovider.SMountTargetCreateOptions) (*SMountTarget, error) {
params := map[string]string{
"RegionId": self.RegionId,
"FileSystemId": opts.FileSystemId,
"AccessGroupName": strings.TrimPrefix(strings.TrimPrefix(opts.AccessGroupId, "extreme/"), "standard/"),
"NetworkType": utils.Capitalize(opts.NetworkType),
"VpcId": opts.VpcId,
"VSwitchId": opts.NetworkId,
}
resp, err := self.nasRequest("CreateMountTarget", params)
if err != nil {
return nil, errors.Wrapf(err, "CreateMountTarget")
}
ret := struct {
MountTargetDomain string
}{}
err = resp.Unmarshal(&ret)
if err != nil {
return nil, errors.Wrapf(err, "resp.Unmarshal")
}
mts, _, err := self.GetMountTargets(opts.FileSystemId, ret.MountTargetDomain, 10, 1)
if err != nil {
return nil, errors.Wrapf(err, "self.GetMountTargets")
}
for i := range mts {
if mts[i].MountTargetDomain == ret.MountTargetDomain {
return &mts[i], nil
}
}
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "afeter create with mount domain %s", ret.MountTargetDomain)
}
func (self *SFileSystem) CreateMountTarget(opts *cloudprovider.SMountTargetCreateOptions) (cloudprovider.ICloudMountTarget, error) {
mt, err := self.region.CreateMountTarget(opts)
if err != nil {
return nil, errors.Wrapf(err, "CreateMountTarget")
}
mt.fs = self
return mt, nil
}
func (self *SRegion) DeleteFileSystem(id string) error {
params := map[string]string{
"RegionId": self.RegionId,
"FileSystemId": id,
}
_, err := self.nasRequest("DeleteFileSystem", params)
return errors.Wrapf(err, "DeleteFileSystem")
}
func (self *SRegion) CreateICloudFileSystem(opts *cloudprovider.FileSystemCraeteOptions) (cloudprovider.ICloudFileSystem, error) {
fs, err := self.CreateFileSystem(opts)
if err != nil {
return nil, errors.Wrapf(err, "self.CreateFileSystem")
}
return fs, nil
}
func (self *SRegion) CreateFileSystem(opts *cloudprovider.FileSystemCraeteOptions) (*SFileSystem, error) {
params := map[string]string{
"RegionId": self.RegionId,
"ProtocolType": opts.Protocol,
"ZoneId": opts.ZoneId,
"EncryptType": "0",
"FileSystemType": opts.FileSystemType,
"StorageType": opts.StorageType,
"ClientToken": utils.GenRequestId(20),
"Description": opts.Name,
}
if self.GetCloudEnv() == ALIYUN_FINANCE_CLOUDENV {
if opts.FileSystemType == "standard" {
opts.ZoneId = strings.Replace(opts.ZoneId, "cn-shanghai-finance-1", "jr-cn-shanghai-", 1)
opts.ZoneId = strings.Replace(opts.ZoneId, "cn-shenzhen-finance-1", "jr-cn-shenzhen-", 1)
params["ZoneId"] = opts.ZoneId
}
}
switch opts.FileSystemType {
case "standard":
params["StorageType"] = utils.Capitalize(opts.StorageType)
case "cpfs":
params["ProtocolType"] = "cpfs"
switch opts.StorageType {
case "advance_100":
params["Bandwidth"] = "100"
case "advance_200":
params["Bandwidth"] = "200"
}
case "extreme":
params["Capacity"] = fmt.Sprintf("%d", opts.Capacity)
}
if len(opts.VpcId) > 0 {
params["VpcId"] = opts.VpcId
}
if len(opts.NetworkId) > 0 {
params["VSwitchId"] = opts.NetworkId
}
if opts.BillingCycle != nil {
params["ChargeType"] = "Subscription"
params["Duration"] = fmt.Sprintf("%d", opts.BillingCycle.GetMonths())
}
resp, err := self.nasRequest("CreateFileSystem", params)
if err != nil {
return nil, errors.Wrapf(err, "CreateFileSystem")
}
fsId, _ := resp.GetString("FileSystemId")
return self.GetFileSystem(fsId)
}
func (self *SFileSystem) SetTags(tags map[string]string, replace bool) error {
return self.region.SetResourceTags(ALIYUN_SERVICE_NAS, "filesystem", self.FileSystemId, tags, replace)
}