yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/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 incloudsphere
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  	"yunion.io/x/onecloud/pkg/util/imagetools"
    25  )
    26  
    27  type SImage struct {
    28  	multicloud.SImageBase
    29  	InCloudSphereTags
    30  	cache *SStoragecache
    31  
    32  	imageInfo *imagetools.ImageInfo
    33  
    34  	Name          string `json:"name"`
    35  	FileSize      int64  `json:"fileSize"`
    36  	RealSize      int64  `json:"realSize"`
    37  	SourceType    string `json:"sourceType"`
    38  	Format        string `json:"format"`
    39  	FileType      string `json:"fileType"`
    40  	Size          int64  `json:"size"`
    41  	Date          string `json:"date"`
    42  	Path          string `json:"path"`
    43  	FTPServer     string `json:"ftpServer"`
    44  	DataStoreID   string `json:"dataStoreId"`
    45  	DataStoreName string `json:"dataStoreName"`
    46  	ServerID      string `json:"serverId"`
    47  	Md5           string `json:"md5"`
    48  }
    49  
    50  func (self *SImage) GetMinRamSizeMb() int {
    51  	return 0
    52  }
    53  
    54  func (self *SImage) GetId() string {
    55  	return fmt.Sprintf("%s/%s/%s", self.cache.zone.Id, self.Path, self.Name)
    56  }
    57  
    58  func (self *SImage) GetName() string {
    59  	return self.Name
    60  }
    61  
    62  func (self *SImage) Delete(ctx context.Context) error {
    63  	return cloudprovider.ErrNotImplemented
    64  }
    65  
    66  func (self *SImage) GetGlobalId() string {
    67  	return self.GetId()
    68  }
    69  
    70  func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    71  	return self.cache
    72  }
    73  
    74  func (self *SImage) GetStatus() string {
    75  	return api.CACHED_IMAGE_STATUS_ACTIVE
    76  }
    77  
    78  func (self *SImage) GetImageStatus() string {
    79  	return cloudprovider.IMAGE_STATUS_ACTIVE
    80  }
    81  
    82  func (self *SImage) GetImageType() cloudprovider.TImageType {
    83  	return cloudprovider.ImageTypeSystem
    84  }
    85  
    86  func (self *SImage) GetSizeByte() int64 {
    87  	return self.FileSize
    88  }
    89  
    90  func (i *SImage) GetOsType() cloudprovider.TOsType {
    91  	return cloudprovider.TOsType(i.getNormalizedImageInfo().OsType)
    92  }
    93  
    94  func (i *SImage) GetOsDist() string {
    95  	return i.getNormalizedImageInfo().OsDistro
    96  }
    97  
    98  func (i *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
    99  	if i.imageInfo == nil {
   100  		imgInfo := imagetools.NormalizeImageInfo(i.Name, "", "", "", "")
   101  		i.imageInfo = &imgInfo
   102  	}
   103  	return i.imageInfo
   104  }
   105  
   106  func (i *SImage) GetFullOsName() string {
   107  	return i.Name
   108  }
   109  
   110  func (i *SImage) GetOsVersion() string {
   111  	return i.getNormalizedImageInfo().OsVersion
   112  }
   113  
   114  func (i *SImage) GetOsArch() string {
   115  	return i.getNormalizedImageInfo().OsArch
   116  }
   117  
   118  func (i *SImage) GetOsLang() string {
   119  	return i.getNormalizedImageInfo().OsLang
   120  }
   121  
   122  func (self *SImage) GetMinOsDiskSizeGb() int {
   123  	if self.GetOsType() == "windows" {
   124  		return 40
   125  	}
   126  	return 30
   127  }
   128  
   129  func (self *SImage) GetImageFormat() string {
   130  	return self.Format
   131  }
   132  
   133  func (i *SImage) GetBios() cloudprovider.TBiosType {
   134  	return cloudprovider.ToBiosType(i.getNormalizedImageInfo().OsBios)
   135  }
   136  
   137  type SImageInfo struct {
   138  	OsType              string
   139  	OsDist              string
   140  	Model               string
   141  	SocketLimit         int
   142  	SupportCpuHotPlug   bool
   143  	SupportMemHotPlug   bool
   144  	SupportDiskHotPlug  bool
   145  	SupportUefiBootMode bool
   146  }
   147  
   148  func (self *SImageInfo) IsEquals(name string) bool {
   149  	model := imagetools.NormalizeImageInfo(self.Model, "", "", "", "")
   150  	image := imagetools.NormalizeImageInfo(name, "", "", "", "")
   151  	return model.OsDistro == image.OsDistro && model.OsVersion == image.OsVersion
   152  }
   153  
   154  type SImageTree struct {
   155  	Id       string
   156  	Name     string
   157  	Children []struct {
   158  		Id       string
   159  		Name     string
   160  		Children []struct {
   161  			Id     string
   162  			Name   string
   163  			Object SImageInfo
   164  		}
   165  	}
   166  }
   167  
   168  func (self *SImageTree) ToList() []SImageInfo {
   169  	ret := []SImageInfo{}
   170  	for i := range self.Children {
   171  		for j := range self.Children[i].Children {
   172  			self.Children[i].Children[j].Object.OsType = self.Name
   173  			self.Children[i].Children[j].Object.OsDist = self.Children[i].Name
   174  			ret = append(ret, self.Children[i].Children[j].Object)
   175  		}
   176  	}
   177  	return ret
   178  }
   179  
   180  func (self *SRegion) GetImageTrees() ([]SImageTree, error) {
   181  	ret := []SImageTree{}
   182  	return ret, self.get("/vms/ostrees", nil, &ret)
   183  }