yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  )
    25  
    26  type SSnapshot struct {
    27  	region *SRegion
    28  	SResourceBase
    29  	GoogleTags
    30  
    31  	CreationTimestamp  time.Time
    32  	Status             string
    33  	SourceDisk         string
    34  	SourceDiskId       string
    35  	DiskSizeGb         int32
    36  	StorageBytes       int
    37  	StorageBytesStatus string
    38  	Licenses           []string
    39  	LabelFingerprint   string
    40  	LicenseCodes       []string
    41  	StorageLocations   []string
    42  	Kind               string
    43  }
    44  
    45  func (region *SRegion) GetSnapshots(disk string, maxResults int, pageToken string) ([]SSnapshot, error) {
    46  	snapshots := []SSnapshot{}
    47  	params := map[string]string{}
    48  	if len(disk) > 0 {
    49  		params["filter"] = fmt.Sprintf(`sourceDisk="%s"`, disk)
    50  	}
    51  	resource := "global/snapshots"
    52  	return snapshots, region.List(resource, params, maxResults, pageToken, &snapshots)
    53  }
    54  
    55  func (region *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
    56  	snapshot := &SSnapshot{region: region}
    57  	return snapshot, region.Get("global/snapshots", id, snapshot)
    58  }
    59  
    60  // CREATING, DELETING, FAILED, READY, or UPLOADING
    61  func (snapshot *SSnapshot) GetStatus() string {
    62  	switch snapshot.Status {
    63  	case "CREATING":
    64  		return api.SNAPSHOT_CREATING
    65  	case "DELETING":
    66  		return api.SNAPSHOT_DELETING
    67  	case "FAILED":
    68  		return api.SNAPSHOT_UNKNOWN
    69  	case "READY", "UPLOADING":
    70  		return api.SNAPSHOT_READY
    71  	default:
    72  		return api.SNAPSHOT_UNKNOWN
    73  	}
    74  }
    75  
    76  func (snapshot *SSnapshot) IsEmulated() bool {
    77  	return false
    78  }
    79  
    80  func (self *SSnapshot) GetCreatedAt() time.Time {
    81  	return self.CreationTimestamp
    82  }
    83  
    84  func (snapshot *SSnapshot) Refresh() error {
    85  	_snapshot, err := snapshot.region.GetSnapshot(snapshot.Id)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	return jsonutils.Update(snapshot, _snapshot)
    90  }
    91  
    92  func (snapshot *SSnapshot) GetSizeMb() int32 {
    93  	return snapshot.DiskSizeGb * 1024
    94  }
    95  
    96  func (snapshot *SSnapshot) GetDiskId() string {
    97  	return snapshot.SourceDisk
    98  }
    99  
   100  func (snapshot *SSnapshot) GetDiskType() string {
   101  	if len(snapshot.Licenses) > 0 {
   102  		return api.DISK_TYPE_SYS
   103  	}
   104  	return api.DISK_TYPE_DATA
   105  }
   106  
   107  func (snapshot *SSnapshot) Delete() error {
   108  	return snapshot.region.Delete(snapshot.SelfLink)
   109  }
   110  
   111  func (snapshot *SSnapshot) GetProjectId() string {
   112  	return snapshot.region.GetProjectId()
   113  }