yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/snapshot.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cloudpods
    16  
    17  import (
    18  	"yunion.io/x/jsonutils"
    19  
    20  	api "yunion.io/x/onecloud/pkg/apis/compute"
    21  	modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SSnapshot struct {
    28  	multicloud.SVirtualResourceBase
    29  	CloudpodsTags
    30  	region *SRegion
    31  
    32  	api.SnapshotDetails
    33  }
    34  
    35  func (self *SSnapshot) GetName() string {
    36  	return self.Name
    37  }
    38  
    39  func (self *SSnapshot) GetId() string {
    40  	return self.Id
    41  }
    42  
    43  func (self *SSnapshot) GetGlobalId() string {
    44  	return self.Id
    45  }
    46  
    47  func (self *SSnapshot) GetStatus() string {
    48  	return self.Status
    49  }
    50  
    51  func (self *SSnapshot) GetProjectId() string {
    52  	return self.TenantId
    53  }
    54  
    55  func (self *SSnapshot) GetSizeMb() int32 {
    56  	return int32(self.Size)
    57  }
    58  
    59  func (self *SSnapshot) GetDiskId() string {
    60  	return self.DiskId
    61  }
    62  
    63  func (self *SSnapshot) GetDiskType() string {
    64  	return self.DiskType
    65  }
    66  
    67  func (self *SSnapshot) Delete() error {
    68  	return self.region.cli.delete(&modules.Snapshots, self.Id)
    69  }
    70  
    71  func (self *SSnapshot) Refresh() error {
    72  	snapshot, err := self.region.GetSnapshot(self.Id)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	return jsonutils.Update(self, snapshot)
    77  }
    78  
    79  func (self *SRegion) GetSnapshots(diskId string) ([]SSnapshot, error) {
    80  	ret := []SSnapshot{}
    81  	params := map[string]interface{}{}
    82  	if len(diskId) > 0 {
    83  		params["disk_id"] = diskId
    84  	}
    85  	return ret, self.list(&modules.Snapshots, params, &ret)
    86  }
    87  
    88  func (self *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
    89  	snapshot := &SSnapshot{region: self}
    90  	return snapshot, self.cli.get(&modules.Snapshots, id, nil, snapshot)
    91  }
    92  
    93  func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
    94  	snapshots, err := self.GetSnapshots("")
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	ret := []cloudprovider.ICloudSnapshot{}
    99  	for i := range snapshots {
   100  		snapshots[i].region = self
   101  		ret = append(ret, &snapshots[i])
   102  	}
   103  	return ret, nil
   104  }
   105  
   106  func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
   107  	snapshot, err := self.GetSnapshot(id)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return snapshot, nil
   112  }