yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/storagecache.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  	"context"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/onecloud/pkg/apis/compute"
    25  	"yunion.io/x/onecloud/pkg/mcclient"
    26  	"yunion.io/x/onecloud/pkg/mcclient/modules/compute"
    27  
    28  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    29  	"yunion.io/x/cloudmux/pkg/multicloud"
    30  )
    31  
    32  type SStoragecache struct {
    33  	multicloud.SResourceBase
    34  	CloudpodsTags
    35  	region *SRegion
    36  
    37  	api.StoragecacheDetails
    38  }
    39  
    40  func (self *SStoragecache) GetName() string {
    41  	return self.Name
    42  }
    43  
    44  func (self *SStoragecache) GetId() string {
    45  	return self.Id
    46  }
    47  
    48  func (self *SStoragecache) GetGlobalId() string {
    49  	return self.Id
    50  }
    51  
    52  func (self *SStoragecache) GetStatus() string {
    53  	return "available"
    54  }
    55  
    56  func (self *SStoragecache) GetICustomizedCloudImages() ([]cloudprovider.ICloudImage, error) {
    57  	return nil, cloudprovider.ErrNotImplemented
    58  }
    59  
    60  func (self *SStoragecache) GetIImageById(id string) (cloudprovider.ICloudImage, error) {
    61  	image, err := self.region.GetImage(id)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	image.cache = self
    66  	return image, nil
    67  }
    68  
    69  func (self *SStoragecache) GetPath() string {
    70  	return self.Path
    71  }
    72  
    73  func (self *SStoragecache) CreateIImage(snapshotId, name, osType, desc string) (cloudprovider.ICloudImage, error) {
    74  	return nil, cloudprovider.ErrNotImplemented
    75  }
    76  
    77  func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) {
    78  	return nil, cloudprovider.ErrNotImplemented
    79  }
    80  
    81  func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, opts *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) {
    82  	id, err := self.region.UploadImage(ctx, userCred, opts, callback)
    83  	if err != nil {
    84  		return "", errors.Wrapf(err, "UploadImage")
    85  	}
    86  	image, err := self.GetIImageById(id)
    87  	if err != nil {
    88  		return "", errors.Wrapf(err, "GetIImageById(%s)", id)
    89  	}
    90  	err = cloudprovider.WaitStatus(image, cloudprovider.IMAGE_STATUS_ACTIVE, time.Second*5, time.Minute*10)
    91  	if err != nil {
    92  		return "", errors.Wrapf(err, "WaitStatus")
    93  	}
    94  	if callback != nil {
    95  		callback(100)
    96  	}
    97  	return id, nil
    98  }
    99  
   100  func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) {
   101  	caches, err := self.GetStoragecaches()
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	ret := []cloudprovider.ICloudStoragecache{}
   106  	for i := range caches {
   107  		caches[i].region = self
   108  		ret = append(ret, &caches[i])
   109  	}
   110  	return ret, nil
   111  }
   112  
   113  func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) {
   114  	cache, err := self.GetStoragecache(id)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	return cache, nil
   119  }
   120  
   121  func (self *SRegion) GetStoragecaches() ([]SStoragecache, error) {
   122  	caches := []SStoragecache{}
   123  	return caches, self.list(&compute.Storagecaches, nil, &caches)
   124  }
   125  
   126  func (self *SRegion) GetStoragecache(id string) (*SStoragecache, error) {
   127  	cache := &SStoragecache{region: self}
   128  	return cache, self.cli.get(&compute.Storagecaches, id, nil, cache)
   129  }