yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/image.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 ctyun
    16  
    17  import (
    18  	"context"
    19  	"strconv"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  	"yunion.io/x/onecloud/pkg/util/imagetools"
    29  )
    30  
    31  const (
    32  	ImageOwnerPublic string = "gold"    // 公共镜像:gold
    33  	ImageOwnerSelf   string = "private" // 私有镜像:private
    34  	ImageOwnerShared string = "shared"  // 共享镜像:shared
    35  )
    36  
    37  // http://ctyun-api-url/apiproxy/v3/order/getImages
    38  type SImage struct {
    39  	multicloud.SImageBase
    40  	CtyunTags
    41  	storageCache *SStoragecache
    42  
    43  	// normalized image info
    44  	imgInfo *imagetools.ImageInfo
    45  
    46  	ID        string `json:"id"`
    47  	OSType    string `json:"osType"`
    48  	Platform  string `json:"platform"`
    49  	Name      string `json:"name"`
    50  	OSBit     int64  `json:"osBit"`
    51  	OSVersion string `json:"osVersion"`
    52  	MinRAM    int64  `json:"minRam"`
    53  	MinDisk   int64  `json:"minDisk"`
    54  	ImageType string `json:"imageType"`
    55  	Virtual   bool   `json:"virtual"`
    56  }
    57  
    58  func (self *SImage) GetCreatedAt() time.Time {
    59  	return time.Time{}
    60  }
    61  
    62  func (self *SImage) GetId() string {
    63  	return self.ID
    64  }
    65  
    66  func (self *SImage) GetName() string {
    67  	return self.Name
    68  }
    69  
    70  func (self *SImage) GetGlobalId() string {
    71  	return self.GetId()
    72  }
    73  
    74  func (self *SImage) GetStatus() string {
    75  	return api.CACHED_IMAGE_STATUS_ACTIVE
    76  }
    77  
    78  func (self *SImage) Refresh() error {
    79  	new, err := self.storageCache.region.GetImage(self.GetId())
    80  	if err != nil {
    81  		return err
    82  	}
    83  	return jsonutils.Update(self, new)
    84  }
    85  
    86  func (self *SImage) IsEmulated() bool {
    87  	return false
    88  }
    89  
    90  func (self *SImage) Delete(ctx context.Context) error {
    91  	return cloudprovider.ErrNotSupported
    92  }
    93  
    94  func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    95  	return self.storageCache
    96  }
    97  
    98  func (self *SImage) GetSizeByte() int64 {
    99  	return self.MinDisk * 1024 * 1024 * 1024
   100  }
   101  
   102  func (self *SImage) GetImageType() cloudprovider.TImageType {
   103  	switch self.ImageType {
   104  	case "gold":
   105  		return cloudprovider.ImageTypeSystem
   106  	case "private":
   107  		return cloudprovider.ImageTypeCustomized
   108  	case "shared":
   109  		return cloudprovider.ImageTypeShared
   110  	default:
   111  		return cloudprovider.ImageTypeCustomized
   112  	}
   113  }
   114  
   115  func (self *SImage) GetImageStatus() string {
   116  	return cloudprovider.IMAGE_STATUS_ACTIVE
   117  }
   118  
   119  func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
   120  	if self.imgInfo == nil {
   121  		imgInfo := imagetools.NormalizeImageInfo(self.OSVersion, strconv.Itoa(int(self.OSBit)), self.OSType, self.Platform, self.OSVersion)
   122  		self.imgInfo = &imgInfo
   123  	}
   124  	return self.imgInfo
   125  }
   126  
   127  func (self *SImage) GetFullOsName() string {
   128  	return self.getNormalizedImageInfo().GetFullOsName()
   129  }
   130  
   131  func (self *SImage) GetOsType() cloudprovider.TOsType {
   132  	return cloudprovider.TOsType(self.getNormalizedImageInfo().OsType)
   133  }
   134  
   135  func (self *SImage) GetOsDist() string {
   136  	return self.getNormalizedImageInfo().OsDistro
   137  }
   138  
   139  func (self *SImage) GetOsVersion() string {
   140  	return self.getNormalizedImageInfo().OsVersion
   141  }
   142  
   143  func (self *SImage) GetOsLang() string {
   144  	return self.getNormalizedImageInfo().OsLang
   145  }
   146  
   147  func (self *SImage) GetOsArch() string {
   148  	return self.getNormalizedImageInfo().OsArch
   149  }
   150  
   151  func (self *SImage) GetBios() cloudprovider.TBiosType {
   152  	return cloudprovider.ToBiosType(self.getNormalizedImageInfo().OsBios)
   153  }
   154  
   155  func (self *SImage) GetMinOsDiskSizeGb() int {
   156  	return int(self.MinDisk)
   157  }
   158  
   159  func (self *SImage) GetMinRamSizeMb() int {
   160  	return int(self.MinRAM)
   161  }
   162  
   163  func (self *SImage) GetImageFormat() string {
   164  	return ""
   165  }
   166  
   167  func (self *SImage) GetCreateTime() time.Time {
   168  	return time.Time{}
   169  }
   170  
   171  func (self *SRegion) GetImages(imageType string) ([]SImage, error) {
   172  	params := map[string]string{
   173  		"regionId":  self.GetId(),
   174  		"imageType": imageType,
   175  	}
   176  
   177  	resp, err := self.client.DoGet("/apiproxy/v3/order/getImages", params)
   178  	if err != nil {
   179  		return nil, errors.Wrap(err, "Region.GetImages.DoGet")
   180  	}
   181  
   182  	images := make([]SImage, 0)
   183  	err = resp.Unmarshal(&images, "returnObj")
   184  	if err != nil {
   185  		return nil, errors.Wrap(err, "Region.GetImages.Unmarshal")
   186  	}
   187  
   188  	for i := range images {
   189  		images[i].storageCache = &SStoragecache{
   190  			region: self,
   191  		}
   192  	}
   193  
   194  	return images, nil
   195  }
   196  
   197  func (self *SRegion) GetImage(imageId string) (*SImage, error) {
   198  	images, err := self.GetImages("")
   199  	if err != nil {
   200  		return nil, errors.Wrap(err, "SRegion.GetImage.GetImages")
   201  	}
   202  
   203  	for i := range images {
   204  		if images[i].GetId() == imageId {
   205  			images[i].storageCache = &SStoragecache{
   206  				region: self,
   207  			}
   208  
   209  			return &images[i], nil
   210  		}
   211  	}
   212  
   213  	return nil, errors.Wrap(cloudprovider.ErrNotFound, "SRegion.GetImage")
   214  }