Skip to content

Commit cba7360

Browse files
committed
fix(region): create disk with tags
1 parent 14fb60e commit cba7360

File tree

24 files changed

+181
-91
lines changed

24 files changed

+181
-91
lines changed

pkg/cloudprovider/disk.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type DiskCreateConfig struct {
2121
Iops int
2222
Throughput int
2323
ProjectId string
24+
SnapshotId string
25+
ImageId string
2426

2527
Tags map[string]string
2628
}

pkg/multicloud/aliyun/disk.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,12 @@ func (self *SDisk) GetMountpoint() string {
271271
return ""
272272
}
273273

274-
func (self *SRegion) CreateDisk(zoneId string, category string, name string, sizeGb int, desc string, projectId string) (string, error) {
274+
func (self *SRegion) CreateDisk(zoneId string, category string, opts *cloudprovider.DiskCreateConfig) (string, error) {
275275
params := make(map[string]string)
276276
params["ZoneId"] = zoneId
277-
params["DiskName"] = name
278-
if len(desc) > 0 {
279-
params["Description"] = desc
277+
params["DiskName"] = opts.Name
278+
if len(opts.Desc) > 0 {
279+
params["Description"] = opts.Desc
280280
}
281281
params["Encrypted"] = "false"
282282
params["DiskCategory"] = category
@@ -296,10 +296,18 @@ func (self *SRegion) CreateDisk(zoneId string, category string, name string, siz
296296
params["BurstingEnabled"] = "true"
297297
}
298298

299-
if len(projectId) > 0 {
300-
params["ResourceGroupId"] = projectId
299+
if len(opts.ProjectId) > 0 {
300+
params["ResourceGroupId"] = opts.ProjectId
301301
}
302-
params["Size"] = fmt.Sprintf("%d", sizeGb)
302+
303+
tagIdx := 1
304+
for k, v := range opts.Tags {
305+
params[fmt.Sprintf("Tag.%d.Key", tagIdx)] = k
306+
params[fmt.Sprintf("Tag.%d.Value", tagIdx)] = v
307+
tagIdx += 1
308+
}
309+
310+
params["Size"] = fmt.Sprintf("%d", opts.SizeGb)
303311
params["ClientToken"] = utils.GenRequestId(20)
304312

305313
body, err := self.ecsRequest("CreateDisk", params)

pkg/multicloud/aliyun/storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
142142
return self.zone.region.getStoragecache()
143143
}
144144

145-
func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
146-
diskId, err := self.zone.region.CreateDisk(self.zone.ZoneId, self.storageType, conf.Name, conf.SizeGb, conf.Desc, conf.ProjectId)
145+
func (self *SStorage) CreateIDisk(opts *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
146+
diskId, err := self.zone.region.CreateDisk(self.zone.ZoneId, self.storageType, opts)
147147
if err != nil {
148148
return nil, errors.Wrapf(err, "CreateDisk")
149149
}

pkg/multicloud/aws/disk.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,14 @@ func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error)
240240
if self.State != "available" {
241241
return "", errors.Wrapf(cloudprovider.ErrInvalidStatus, "invalid status %s", self.State)
242242
}
243-
disk, err := self.storage.zone.region.CreateDisk(self.AvailabilityZone, self.VolumeType, self.GetName(), self.GetDiskSizeMB()/1024, self.Iops, self.Throughput, snapshotId, self.GetDescription())
243+
opts := &cloudprovider.DiskCreateConfig{
244+
Name: self.GetName(),
245+
SizeGb: self.GetDiskSizeMB() / 1024,
246+
Iops: self.Iops,
247+
Throughput: self.Throughput,
248+
SnapshotId: snapshotId,
249+
}
250+
disk, err := self.storage.zone.region.CreateDisk(self.AvailabilityZone, self.VolumeType, opts)
244251
if err != nil {
245252
return "", errors.Wrapf(err, "CreateDisk")
246253
}
@@ -353,41 +360,47 @@ func GenDiskIops(diskType string, sizeGB int) int64 {
353360
return 0
354361
}
355362

356-
func (self *SRegion) CreateDisk(zoneId string, volumeType string, name string, sizeGb, iops, throughput int, snapshotId string, desc string) (*SDisk, error) {
363+
func (self *SRegion) CreateDisk(zoneId string, volumeType string, opts *cloudprovider.DiskCreateConfig) (*SDisk, error) {
357364
params := map[string]string{
358365
"AvailabilityZone": zoneId,
359366
"ClientToken": utils.GenRequestId(20),
360-
"Size": fmt.Sprintf("%d", sizeGb),
367+
"Size": fmt.Sprintf("%d", opts.SizeGb),
361368
"VolumeType": volumeType,
362369
}
363370
tagIdx := 1
364-
if len(name) > 0 {
365-
params[fmt.Sprintf("TagSpecification.%d.ResourceType", tagIdx)] = "volume"
366-
params[fmt.Sprintf("TagSpecification.%d.Tag.1.Key", tagIdx)] = "Name"
367-
params[fmt.Sprintf("TagSpecification.%d.Tag.1.Value", tagIdx)] = name
368-
if len(desc) > 0 {
369-
params[fmt.Sprintf("TagSpecification.%d.Tag.2.Key", tagIdx)] = "Description"
370-
params[fmt.Sprintf("TagSpecification.%d.Tag.2.Value", tagIdx)] = desc
371+
if len(opts.Name) > 0 {
372+
params["TagSpecification.1.ResourceType"] = "volume"
373+
params[fmt.Sprintf("TagSpecification.1.Tag.%d.Key", tagIdx)] = "Name"
374+
params[fmt.Sprintf("TagSpecification.1.Tag.%d.Value", tagIdx)] = opts.Name
375+
tagIdx++
376+
if len(opts.Desc) > 0 {
377+
params[fmt.Sprintf("TagSpecification.1.Tag.%d.Key", tagIdx)] = "Description"
378+
params[fmt.Sprintf("TagSpecification.1.Tag.%d.Value", tagIdx)] = opts.Desc
371379
}
380+
}
381+
for k, v := range opts.Tags {
382+
params["TagSpecification.1.ResourceType"] = "volume"
383+
params[fmt.Sprintf("TagSpecification.1.Tag.%d.Key", tagIdx)] = k
384+
params[fmt.Sprintf("TagSpecification.1.Tag.%d.Value", tagIdx)] = v
372385
tagIdx++
373386
}
374-
if len(snapshotId) > 0 {
375-
params["SnapshotId"] = snapshotId
387+
if len(opts.SnapshotId) > 0 {
388+
params["SnapshotId"] = opts.SnapshotId
376389
}
377-
if throughput >= 125 && throughput <= 1000 && volumeType == api.STORAGE_GP3_SSD {
378-
params["Throughput"] = fmt.Sprintf("%d", throughput)
390+
if opts.Throughput >= 125 && opts.Throughput <= 1000 && volumeType == api.STORAGE_GP3_SSD {
391+
params["Throughput"] = fmt.Sprintf("%d", opts.Throughput)
379392
}
380393

381-
if iops == 0 {
382-
iops = int(GenDiskIops(volumeType, sizeGb))
394+
if opts.Iops == 0 {
395+
opts.Iops = int(GenDiskIops(volumeType, opts.SizeGb))
383396
}
384397

385398
if utils.IsInStringArray(volumeType, []string{
386399
api.STORAGE_IO1_SSD,
387400
api.STORAGE_IO2_SSD,
388401
api.STORAGE_GP3_SSD,
389402
}) {
390-
params["Iops"] = fmt.Sprintf("%d", iops)
403+
params["Iops"] = fmt.Sprintf("%d", opts.Iops)
391404
}
392405
ret := &SDisk{}
393406
return ret, self.ec2Request("CreateVolume", params, ret)

pkg/multicloud/aws/shell/disk.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package shell
1717
import (
1818
"yunion.io/x/pkg/util/shellutils"
1919

20+
"yunion.io/x/cloudmux/pkg/cloudprovider"
2021
"yunion.io/x/cloudmux/pkg/multicloud/aws"
2122
)
2223

@@ -71,7 +72,15 @@ func init() {
7172
}
7273

7374
shellutils.R(&VolumeCreateOptions{}, "disk-create", "create a volume", func(cli *aws.SRegion, args *VolumeCreateOptions) error {
74-
volume, err := cli.CreateDisk(args.ZoneId, args.VolumeType, args.Name, args.SizeGb, args.Iops, args.Throughput, args.SnapshotId, args.Desc)
75+
opts := &cloudprovider.DiskCreateConfig{
76+
Name: args.Name,
77+
SizeGb: args.SizeGb,
78+
Iops: args.Iops,
79+
Throughput: args.Throughput,
80+
SnapshotId: args.SnapshotId,
81+
Desc: args.Desc,
82+
}
83+
volume, err := cli.CreateDisk(args.ZoneId, args.VolumeType, opts)
7584
if err != nil {
7685
return err
7786
}

pkg/multicloud/aws/storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func (self *SStorage) GetEnabled() bool {
119119
return true
120120
}
121121

122-
func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
123-
disk, err := self.zone.region.CreateDisk(self.zone.ZoneName, self.storageType, conf.Name, conf.SizeGb, conf.Iops, conf.Throughput, "", conf.Desc)
122+
func (self *SStorage) CreateIDisk(opts *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
123+
disk, err := self.zone.region.CreateDisk(self.zone.ZoneName, self.storageType, opts)
124124
if err != nil {
125125
return nil, errors.Wrap(err, "CreateDisk")
126126
}

pkg/multicloud/azure/disk.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ type SDisk struct {
8282
Properties DiskProperties `json:"properties,omitempty"`
8383
}
8484

85-
func (self *SRegion) CreateDisk(storageType string, name string, sizeGb int32, imageId, snapshotId, resourceGroup string) (*SDisk, error) {
85+
func (self *SRegion) CreateDisk(storageType string, opts *cloudprovider.DiskCreateConfig) (*SDisk, error) {
8686
params := jsonutils.Marshal(map[string]interface{}{
87-
"Name": name,
87+
"Name": opts.Name,
8888
"Location": self.Name,
8989
"Sku": map[string]string{
9090
"Name": storageType,
@@ -95,12 +95,12 @@ func (self *SRegion) CreateDisk(storageType string, name string, sizeGb int32, i
9595
"CreationData": map[string]string{
9696
"CreateOption": "Empty",
9797
},
98-
"DiskSizeGB": sizeGb,
98+
"DiskSizeGB": opts.SizeGb,
9999
}
100-
if len(imageId) > 0 {
101-
image, err := self.GetImageById(imageId)
100+
if len(opts.ImageId) > 0 {
101+
image, err := self.GetImageById(opts.ImageId)
102102
if err != nil {
103-
return nil, errors.Wrapf(err, "GetImageById(%s)", imageId)
103+
return nil, errors.Wrapf(err, "GetImageById(%s)", opts.ImageId)
104104
}
105105
// 通过镜像创建的磁盘只能传ID参数,不能通过sku,offer等参数创建.
106106
imageId, err := self.getOfferedImageId(&image)
@@ -115,17 +115,17 @@ func (self *SRegion) CreateDisk(storageType string, name string, sizeGb int32, i
115115
},
116116
},
117117
}
118-
} else if len(snapshotId) > 0 {
118+
} else if len(opts.SnapshotId) > 0 {
119119
properties = map[string]interface{}{
120120
"CreationData": map[string]interface{}{
121121
"CreateOption": "Copy",
122-
"sourceResourceId": snapshotId,
122+
"sourceResourceId": opts.SnapshotId,
123123
},
124124
}
125125
}
126126
params.Add(jsonutils.Marshal(properties), "Properties")
127127
disk := &SDisk{}
128-
return disk, self.create(resourceGroup, params, disk)
128+
return disk, self.create(opts.ProjectId, params, disk)
129129
}
130130

131131
func (self *SRegion) DeleteDisk(diskId string) error {
@@ -312,7 +312,13 @@ func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error)
312312
if self.Properties.DiskState != "Unattached" {
313313
return "", fmt.Errorf("Azure reset disk needs to be done in the Unattached state, current status: %s", self.Properties.DiskState)
314314
}
315-
disk, err := self.storage.zone.region.CreateDisk(self.Sku.Name, self.Name, 0, "", snapshotId, self.GetProjectId())
315+
opts := &cloudprovider.DiskCreateConfig{
316+
Name: self.Name,
317+
SizeGb: 0,
318+
SnapshotId: snapshotId,
319+
ProjectId: self.GetProjectId(),
320+
}
321+
disk, err := self.storage.zone.region.CreateDisk(self.Sku.Name, opts)
316322
if err != nil {
317323
return "", errors.Wrap(err, "CreateDisk")
318324
}

pkg/multicloud/azure/shell/disk.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package shell
1717
import (
1818
"yunion.io/x/pkg/util/shellutils"
1919

20+
"yunion.io/x/cloudmux/pkg/cloudprovider"
2021
"yunion.io/x/cloudmux/pkg/multicloud/azure"
2122
)
2223

@@ -35,14 +36,21 @@ func init() {
3536
type DiskCreateOptions struct {
3637
NAME string `help:"Disk name"`
3738
STORAGETYPE string `help:"Storage type" choices:"Standard_LRS|Premium_LRS|StandardSSD_LRS"`
38-
SizeGb int32 `help:"Disk size"`
39+
SizeGb int `help:"Disk size"`
3940
Image string `help:"Image id"`
4041
SnapshotId string `help:"Create disk by snapshot"`
4142
ResourceGroup string `help:"ResourceGroup Name"`
4243
}
4344

4445
shellutils.R(&DiskCreateOptions{}, "disk-create", "Create disk", func(cli *azure.SRegion, args *DiskCreateOptions) error {
45-
disk, err := cli.CreateDisk(args.STORAGETYPE, args.NAME, args.SizeGb, args.Image, args.SnapshotId, args.ResourceGroup)
46+
opts := &cloudprovider.DiskCreateConfig{
47+
Name: args.NAME,
48+
SizeGb: args.SizeGb,
49+
ImageId: args.Image,
50+
SnapshotId: args.SnapshotId,
51+
ProjectId: args.ResourceGroup,
52+
}
53+
disk, err := cli.CreateDisk(args.STORAGETYPE, opts)
4654
if err != nil {
4755
return err
4856
}

pkg/multicloud/azure/storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func (self *SStorage) GetCapacityUsedMB() int64 {
8383
return 0
8484
}
8585

86-
func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
87-
disk, err := self.zone.region.CreateDisk(self.storageType, conf.Name, int32(conf.SizeGb), "", "", conf.ProjectId)
86+
func (self *SStorage) CreateIDisk(opts *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
87+
disk, err := self.zone.region.CreateDisk(self.storageType, opts)
8888
if err != nil {
8989
return nil, err
9090
}

pkg/multicloud/google/disk.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"yunion.io/x/jsonutils"
24+
"yunion.io/x/pkg/util/encode"
2425

2526
billing "yunion.io/x/cloudmux/pkg/apis/billing"
2627
api "yunion.io/x/cloudmux/pkg/apis/compute"
@@ -219,20 +220,25 @@ func (disk *SDisk) GetProjectId() string {
219220
return disk.storage.zone.region.GetProjectId()
220221
}
221222

222-
func (region *SRegion) CreateDisk(name string, sizeGb int, zone string, storageType string, image string, desc string) (*SDisk, error) {
223+
func (region *SRegion) CreateDisk(zone string, storageType string, opts *cloudprovider.DiskCreateConfig) (*SDisk, error) {
223224
if !strings.HasPrefix(storageType, GOOGLE_COMPUTE_DOMAIN) {
224225
storageType = fmt.Sprintf("projects/%s/zones/%s/diskTypes/%s", region.GetProjectId(), zone, storageType)
225226
}
227+
labels := map[string]string{}
228+
for k, v := range opts.Tags {
229+
labels[encode.EncodeGoogleLabel(k)] = encode.EncodeGoogleLabel(v)
230+
}
226231
body := map[string]interface{}{
227-
"name": name,
228-
"description": desc,
232+
"name": opts.Name,
233+
"description": opts.Desc,
229234
// https://www.googleapis.com/compute/v1/projects/my-project-15390453537169/zones/us-west2-c/diskTypes/pd-standard
230235
// projects/my-project-15390453537169/zones/us-west2-c/diskTypes/pd-standard
231-
"type": storageType,
236+
"type": storageType,
237+
"labels": labels,
232238
}
233-
body["sizeGb"] = sizeGb
234-
if len(image) > 0 {
235-
body["sourceImage"] = image
239+
body["sizeGb"] = opts.SizeGb
240+
if len(opts.ImageId) > 0 {
241+
body["sourceImage"] = opts.ImageId
236242
}
237243
disk := &SDisk{}
238244
resource := fmt.Sprintf("zones/%s/disks", zone)

0 commit comments

Comments
 (0)