From 2dae75ca49d4811669d448bda39a6d4d325e5f48 Mon Sep 17 00:00:00 2001 From: TangBin Date: Sat, 27 Oct 2018 17:35:52 +0800 Subject: [PATCH] less code --- pkg/util/aws/disk.go | 18 +++++++----------- pkg/util/aws/image.go | 2 +- pkg/util/aws/shell/snapshot.go | 3 +-- pkg/util/aws/snapshot.go | 7 +++++++ 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pkg/util/aws/disk.go b/pkg/util/aws/disk.go index 157e286b81..ac1e81a703 100644 --- a/pkg/util/aws/disk.go +++ b/pkg/util/aws/disk.go @@ -368,21 +368,17 @@ func (self *SRegion) resetDisk(diskId, snapshotId string) error { } func (self *SRegion) CreateDisk(zoneId string, category string, name string, sizeGb int, desc string) (string, error) { + tagspec := TagSpec{ResourceType: "volume"} + tagspec.SetNameTag(name) + tagspec.SetDescTag(desc) + ec2Tags, _ := tagspec.GetTagSpecifications() + params := &ec2.CreateVolumeInput{} params.SetAvailabilityZone(zoneId) params.SetVolumeType(category) params.SetSize(int64(sizeGb)) - // todo: less code here - tagSpec := &ec2.TagSpecification{} - tagSpec.SetResourceType(ec2.ResourceTypeVolume) - nameTag := &ec2.Tag{} - nameTag.SetKey("Name") - nameTag.SetValue(name) - descTag := &ec2.Tag{} - descTag.SetKey("Description") - descTag.SetValue(desc) - tagSpec.SetTags([]*ec2.Tag{nameTag, descTag}) - params.SetTagSpecifications([]*ec2.TagSpecification{tagSpec}) + params.SetTagSpecifications([]*ec2.TagSpecification{ec2Tags}) + ret, err := self.ec2Client.CreateVolume(params) if err != nil { return "", err diff --git a/pkg/util/aws/image.go b/pkg/util/aws/image.go index 12d67a11c4..911c3b8102 100644 --- a/pkg/util/aws/image.go +++ b/pkg/util/aws/image.go @@ -202,6 +202,7 @@ func getRootDiskSize(image *ec2.Image) (int, error) { return 0, fmt.Errorf("image size not found: %s", image.String()) } + func (self *SRegion) GetImages(status ImageStatusType, owner ImageOwnerType, imageId []string, name string, offset int, limit int) ([]SImage, int, error) { params := &ec2.DescribeImagesInput{} filters := make([]*ec2.Filter, 0) @@ -253,7 +254,6 @@ func (self *SRegion) GetImages(status ImageStatusType, owner ImageOwnerType, ima CreationTime: *image.CreationDate, Size: size, // Usage: "", - //Size: image., // OSName: *image.Platform, }) } diff --git a/pkg/util/aws/shell/snapshot.go b/pkg/util/aws/shell/snapshot.go index 28f4351070..2c8f916e60 100644 --- a/pkg/util/aws/shell/snapshot.go +++ b/pkg/util/aws/shell/snapshot.go @@ -28,8 +28,7 @@ func init() { } shellutils.R(&SnapshotDeleteOptions{}, "snapshot-delete", "Delete snapshot", func(cli *aws.SRegion, args *SnapshotDeleteOptions) error { - // todo:// - return nil + return cli.DeleteSnapshot(args.ID) }) type SnapshotCreateOptions struct { diff --git a/pkg/util/aws/snapshot.go b/pkg/util/aws/snapshot.go index 72bf7d0159..5b1fdf8432 100644 --- a/pkg/util/aws/snapshot.go +++ b/pkg/util/aws/snapshot.go @@ -169,3 +169,10 @@ func (self *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) { _, err := self.ec2Client.CreateSnapshot(params) return "", err } + +func (self *SRegion) DeleteSnapshot(snapshotId string) error { + params := &ec2.DeleteSnapshotInput{} + params.SetSnapshotId(snapshotId) + _, err := self.ec2Client.DeleteSnapshot(params) + return err +} \ No newline at end of file