yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/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 incloudsphere
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/url"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  	"yunion.io/x/pkg/utils"
    25  
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/onecloud/pkg/mcclient"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type SStoragecache struct {
    32  	multicloud.SResourceBase
    33  	InCloudSphereTags
    34  
    35  	zone *SZone
    36  }
    37  
    38  func (self *SStoragecache) GetGlobalId() string {
    39  	return self.zone.GetGlobalId()
    40  }
    41  
    42  func (self *SStoragecache) GetId() string {
    43  	return self.zone.GetId()
    44  }
    45  
    46  func (self *SStoragecache) GetName() string {
    47  	return self.zone.GetName()
    48  }
    49  
    50  func (self *SStoragecache) GetStatus() string {
    51  	return "available"
    52  }
    53  
    54  func (self *SStoragecache) GetICloudImages() ([]cloudprovider.ICloudImage, error) {
    55  	ret := []cloudprovider.ICloudImage{}
    56  	iss, err := self.zone.region.GetImageStorages(self.zone.Id)
    57  	if err != nil {
    58  		return nil, errors.Wrapf(err, "GetImageStorages")
    59  	}
    60  	for i := range iss {
    61  		images, err := self.zone.region.GetImageList(iss[i].Id)
    62  		if err != nil {
    63  			return nil, err
    64  		}
    65  		for j := range images {
    66  			if utils.IsInStringArray(images[j].GetImageFormat(), []string{"iso", "ova"}) {
    67  				continue
    68  			}
    69  			images[j].cache = self
    70  			ret = append(ret, &images[j])
    71  		}
    72  	}
    73  	return ret, nil
    74  }
    75  
    76  func (seflf *SStoragecache) GetICustomizedCloudImages() ([]cloudprovider.ICloudImage, error) {
    77  	return nil, cloudprovider.ErrNotSupported
    78  }
    79  
    80  func (self *SStoragecache) GetIImageById(id string) (cloudprovider.ICloudImage, error) {
    81  	images, err := self.GetICloudImages()
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	for i := range images {
    86  		if images[i].GetGlobalId() == id {
    87  			return images[i], nil
    88  		}
    89  	}
    90  	return nil, cloudprovider.ErrNotFound
    91  }
    92  
    93  func (self *SStoragecache) GetPath() string {
    94  	return ""
    95  }
    96  
    97  func (self *SStoragecache) CreateIImage(snpId, imageName, osType, imageDesc string) (cloudprovider.ICloudImage, error) {
    98  	return nil, cloudprovider.ErrNotSupported
    99  }
   100  
   101  func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) {
   102  	return nil, cloudprovider.ErrNotSupported
   103  }
   104  
   105  func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) {
   106  	return "", cloudprovider.ErrNotSupported
   107  }
   108  
   109  type SImageStorage struct {
   110  	Id   string
   111  	Name string
   112  }
   113  
   114  func (self *SRegion) GetImageStorages(dsId string) ([]SImageStorage, error) {
   115  	params := url.Values{}
   116  	params.Set("type", "imagestorage")
   117  	res := fmt.Sprintf("/datacenters/%s/storages", dsId)
   118  	ret := []SImageStorage{}
   119  	return ret, self.get(res, params, &ret)
   120  }
   121  
   122  func (self *SRegion) GetImageList(storageId string) ([]SImage, error) {
   123  	res := fmt.Sprintf("/storages/%s/files", storageId)
   124  	ret := []SImage{}
   125  	return ret, self.list(res, url.Values{}, &ret)
   126  }