| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- // 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 volcengine
- import (
- "fmt"
- "time"
- "yunion.io/x/jsonutils"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/utils"
- api "yunion.io/x/cloudmux/pkg/apis/compute"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- )
- type SSnapshot struct {
- multicloud.SResourceBase
- VolcEngineTags
- region *SRegion
- SnapshotId string
- ZoneId string
- VolumeId string
- Status string
- SnapshotName string
- Description string
- CreationTime time.Time
- SnapshotType string
- VolumeType string
- VolumeKind string
- VolumeName string
- VolumeStatus string
- RetentionDays int
- ProjectName string
- Progress int
- SnapshotGroupId string
- ImageId string
- VolumeSize int32
- }
- func (self *SSnapshot) GetId() string {
- return self.SnapshotId
- }
- func (self *SSnapshot) GetName() string {
- return self.SnapshotName
- }
- // available creating rollbacking deleting failed
- func (self *SSnapshot) GetStatus() string {
- switch self.Status {
- case "available":
- return api.SNAPSHOT_READY
- case "creating":
- return api.SNAPSHOT_CREATING
- case "failed":
- return api.SNAPSHOT_FAILED
- case "rollbacking":
- return api.SNAPSHOT_ROLLBACKING
- case "deleting":
- return api.SNAPSHOT_DELETING
- default:
- return self.Status
- }
- }
- func (self *SSnapshot) GetSizeMb() int32 {
- return self.VolumeSize * 1024
- }
- func (self *SSnapshot) GetDiskId() string {
- return self.VolumeId
- }
- func (self *SSnapshot) GetDiskType() string {
- if self.VolumeKind == "system" {
- return api.DISK_TYPE_SYS
- }
- return api.DISK_TYPE_DATA
- }
- func (self *SSnapshot) Refresh() error {
- snapshot, err := self.region.GetSnapshot(self.SnapshotId)
- if err != nil {
- return err
- }
- return jsonutils.Update(self, snapshot)
- }
- func (self *SSnapshot) GetGlobalId() string {
- return self.SnapshotId
- }
- func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
- snapshots, err := self.GetSnapshots("", "", nil)
- if err != nil {
- return nil, err
- }
- ret := make([]cloudprovider.ICloudSnapshot, len(snapshots))
- for i := 0; i < len(snapshots); i += 1 {
- snapshots[i].region = self
- ret[i] = &snapshots[i]
- }
- return ret, nil
- }
- func (self *SSnapshot) Delete() error {
- return self.region.DeleteSnapshot(self.SnapshotId)
- }
- func (self *SRegion) GetSnapshots(diskId string, snapshotName string, snapshotIds []string) ([]SSnapshot, error) {
- params := make(map[string]string)
- params["PageSize"] = "100"
- pageNum := 1
- if len(diskId) > 0 {
- params["VolumeId"] = diskId
- }
- if len(snapshotName) > 0 {
- params["SnapshotName"] = snapshotName
- }
- for i, id := range snapshotIds {
- params[fmt.Sprintf("SnapshotIds.%d", i+1)] = id
- }
- ret := []SSnapshot{}
- for {
- params["PageNumber"] = fmt.Sprintf("%d", pageNum)
- resp, err := self.storageRequest("DescribeSnapshots", params)
- if err != nil {
- return nil, errors.Wrapf(err, "DescribeSnapshots")
- }
- part := struct {
- Snapshots []SSnapshot
- TotalCount int64
- }{}
- err = resp.Unmarshal(&part)
- if err != nil {
- return nil, err
- }
- ret = append(ret, part.Snapshots...)
- if len(part.Snapshots) == 0 || len(ret) >= int(part.TotalCount) {
- break
- }
- pageNum++
- }
- return ret, nil
- }
- func (self *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
- snapshots, err := self.GetSnapshots("", "", []string{id})
- if err != nil {
- return nil, err
- }
- for i := range snapshots {
- snapshots[i].region = self
- if snapshots[i].SnapshotId == id {
- return &snapshots[i], nil
- }
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
- }
- func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
- snapshot, err := self.GetSnapshot(id)
- if err != nil {
- return nil, err
- }
- return snapshot, nil
- }
- func (self *SRegion) DeleteSnapshot(snapshotId string) error {
- params := make(map[string]string)
- params["SnapshotId"] = snapshotId
- params["ClientToken"] = utils.GenRequestId(20)
- _, err := self.storageRequest("DeleteSnapshot", params)
- return err
- }
- func (self *SSnapshot) GetProjectId() string {
- return self.ProjectName
- }
- func (self *SRegion) CreateSnapshot(diskId, name, desc string) (*SSnapshot, error) {
- params := map[string]string{
- "SnapshotName": name,
- "ClientToken": utils.GenRequestId(20),
- "Description": desc,
- "VolumeId": diskId,
- }
- resp, err := self.storageRequest("CreateSnapshot", params)
- if err != nil {
- return nil, err
- }
- id, err := resp.GetString("SnapshotId")
- if err != nil {
- return nil, err
- }
- return self.GetSnapshot(id)
- }
|