yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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 azure
    16  
    17  import (
    18  	"net/url"
    19  	"strings"
    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/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SnapshotSku struct {
    30  	Name string
    31  	Tier string
    32  }
    33  
    34  type SSnapshot struct {
    35  	multicloud.SResourceBase
    36  	AzureTags
    37  	region *SRegion
    38  
    39  	ID         string
    40  	Name       string
    41  	Location   string
    42  	ManagedBy  string
    43  	Sku        *SnapshotSku
    44  	Properties DiskProperties
    45  	Type       string
    46  }
    47  
    48  func (self *SSnapshot) GetId() string {
    49  	return self.ID
    50  }
    51  
    52  func (self *SSnapshot) GetGlobalId() string {
    53  	return strings.ToLower(self.ID)
    54  }
    55  
    56  func (self *SSnapshot) GetName() string {
    57  	return self.Name
    58  }
    59  
    60  func (self *SSnapshot) GetStatus() string {
    61  	switch self.Properties.ProvisioningState {
    62  	case "Succeeded":
    63  		return api.SNAPSHOT_READY
    64  	default:
    65  		log.Errorf("Unknow azure snapshot %s status: %s", self.ID, self.Properties.ProvisioningState)
    66  		return api.SNAPSHOT_UNKNOWN
    67  	}
    68  }
    69  
    70  func (self *SRegion) CreateSnapshot(diskId, name, desc string) (*SSnapshot, error) {
    71  	params := map[string]interface{}{
    72  		"Name":     name,
    73  		"Location": self.Name,
    74  		"Properties": map[string]interface{}{
    75  			"CreationData": map[string]string{
    76  				"CreateOption":     "Copy",
    77  				"SourceResourceID": diskId,
    78  			},
    79  		},
    80  		"Type": "Microsoft.Compute/snapshots",
    81  	}
    82  	snapshot := &SSnapshot{region: self}
    83  	return snapshot, self.create("", jsonutils.Marshal(params), snapshot)
    84  }
    85  
    86  func (self *SSnapshot) Delete() error {
    87  	return self.region.DeleteSnapshot(self.ID)
    88  }
    89  
    90  func (self *SSnapshot) GetSizeMb() int32 {
    91  	return self.Properties.DiskSizeGB.Int32() * 1024
    92  }
    93  
    94  func (self *SRegion) DeleteSnapshot(snapshotId string) error {
    95  	return self.del(snapshotId)
    96  }
    97  
    98  type AccessURIOutput struct {
    99  	AccessSas string
   100  }
   101  
   102  type AccessProperties struct {
   103  	Output AccessURIOutput
   104  }
   105  
   106  type AccessURI struct {
   107  	Name       string
   108  	Properties AccessProperties
   109  }
   110  
   111  func (self *SRegion) GrantAccessSnapshot(snapshotId string) (string, error) {
   112  	params := map[string]interface{}{
   113  		"access":            "Read",
   114  		"durationInSeconds": 3600 * 24,
   115  	}
   116  	body, err := self.perform(snapshotId, "beginGetAccess", jsonutils.Marshal(params))
   117  	if err != nil {
   118  		return "", err
   119  	}
   120  	accessURI := AccessURI{}
   121  	return accessURI.Properties.Output.AccessSas, body.Unmarshal(&accessURI)
   122  }
   123  
   124  func (self *SSnapshot) Refresh() error {
   125  	snapshot, err := self.region.GetSnapshot(self.ID)
   126  	if err != nil {
   127  		return err
   128  	}
   129  	return jsonutils.Update(self, snapshot)
   130  }
   131  
   132  func (self *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
   133  	return self.GetSnapshot(snapshotId)
   134  }
   135  
   136  func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
   137  	snapshots, err := self.ListSnapshots()
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	ret := []cloudprovider.ICloudSnapshot{}
   142  	for i := range snapshots {
   143  		snapshots[i].region = self
   144  		ret = append(ret, &snapshots[i])
   145  	}
   146  	return ret, nil
   147  }
   148  
   149  func (self *SSnapshot) GetDiskId() string {
   150  	return strings.ToLower(self.Properties.CreationData.SourceResourceID)
   151  }
   152  
   153  func (self *SSnapshot) GetDiskType() string {
   154  	return ""
   155  }
   156  
   157  func (self *SSnapshot) GetProjectId() string {
   158  	return getResourceGroup(self.ID)
   159  }
   160  
   161  func (region *SRegion) GetSnapshot(snapshotId string) (*SSnapshot, error) {
   162  	snapshot := SSnapshot{region: region}
   163  	return &snapshot, region.get(snapshotId, url.Values{}, &snapshot)
   164  }
   165  
   166  func (region *SRegion) ListSnapshots() ([]SSnapshot, error) {
   167  	result := []SSnapshot{}
   168  	err := region.list("Microsoft.Compute/snapshots", url.Values{}, &result)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	return result, nil
   173  }