yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/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 zstack
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SSnapshot struct {
    29  	multicloud.SResourceBase
    30  	ZStackTags
    31  	region *SRegion
    32  
    33  	ZStackBasic
    34  	PrimaryStorageUUID string `json:"primaryStorageUuid"`
    35  	VolumeUUID         string `json:"volumeUuid"`
    36  	VolumeType         string `json:"volumeType"`
    37  	Format             string `json:"format"`
    38  	Latest             bool   `json:"latest"`
    39  	Size               int    `json:"size"`
    40  	State              string `json:"state"`
    41  	Status             string `json:"status"`
    42  }
    43  
    44  func (snapshot *SSnapshot) GetId() string {
    45  	return snapshot.UUID
    46  }
    47  
    48  func (snapshot *SSnapshot) GetName() string {
    49  	return snapshot.Name
    50  }
    51  
    52  func (snapshot *SSnapshot) GetStatus() string {
    53  	switch snapshot.Status {
    54  	case "Ready":
    55  		return api.SNAPSHOT_READY
    56  	default:
    57  		log.Errorf("unknown snapshot %s(%s) status %s", snapshot.Name, snapshot.UUID, snapshot.Status)
    58  		return api.SNAPSHOT_UNKNOWN
    59  	}
    60  }
    61  
    62  func (snapshot *SSnapshot) GetSizeMb() int32 {
    63  	return int32(snapshot.Size / 1024 / 1024)
    64  }
    65  
    66  func (snapshot *SSnapshot) GetDiskId() string {
    67  	return snapshot.VolumeUUID
    68  }
    69  
    70  func (snapshot *SSnapshot) GetDiskType() string {
    71  	switch snapshot.VolumeType {
    72  	case "Root":
    73  		return api.DISK_TYPE_SYS
    74  	default:
    75  		return api.DISK_TYPE_DATA
    76  	}
    77  }
    78  
    79  func (snapshot *SSnapshot) Refresh() error {
    80  	new, err := snapshot.region.GetSnapshot(snapshot.UUID)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	return jsonutils.Update(snapshot, new)
    85  }
    86  
    87  func (snapshot *SSnapshot) GetGlobalId() string {
    88  	return snapshot.UUID
    89  }
    90  
    91  func (snapshot *SSnapshot) IsEmulated() bool {
    92  	return false
    93  }
    94  
    95  func (region *SRegion) GetSnapshot(snapshotId string) (*SSnapshot, error) {
    96  	snapshot := &SSnapshot{region: region}
    97  	return snapshot, region.client.getResource("volume-snapshots", snapshotId, snapshot)
    98  }
    99  
   100  func (region *SRegion) GetSnapshots(snapshotId string, diskId string) ([]SSnapshot, error) {
   101  	snapshots := []SSnapshot{}
   102  	params := url.Values{}
   103  	if len(snapshotId) > 0 {
   104  		params.Add("q", "uuid="+snapshotId)
   105  	}
   106  	if len(diskId) > 0 {
   107  		params.Add("q", "volumeUuid="+diskId)
   108  	}
   109  	if err := region.client.listAll("volume-snapshots", params, &snapshots); err != nil {
   110  		return nil, err
   111  	}
   112  	for i := 0; i < len(snapshots); i++ {
   113  		snapshots[i].region = region
   114  	}
   115  	return snapshots, nil
   116  }
   117  
   118  func (snapshot *SSnapshot) Delete() error {
   119  	return snapshot.region.DeleteSnapshot(snapshot.UUID)
   120  }
   121  
   122  func (region *SRegion) DeleteSnapshot(snapshotId string) error {
   123  	return region.client.delete("volume-snapshots", snapshotId, "Enforcing")
   124  }
   125  
   126  func (snapshot *SSnapshot) GetProjectId() string {
   127  	return ""
   128  }
   129  
   130  func (region *SRegion) CreateSnapshot(name, diskId, desc string) (*SSnapshot, error) {
   131  	params := map[string]interface{}{
   132  		"params": map[string]string{
   133  			"name":        name,
   134  			"description": desc,
   135  		},
   136  	}
   137  	resource := fmt.Sprintf("volumes/%s/volume-snapshots", diskId)
   138  	resp, err := region.client.post(resource, jsonutils.Marshal(params))
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	snapshot := &SSnapshot{region: region}
   143  	return snapshot, resp.Unmarshal(snapshot, "inventory")
   144  }