yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/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 ecloud
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    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  	"yunion.io/x/onecloud/pkg/util/imagetools"
    28  )
    29  
    30  type SImage struct {
    31  	multicloud.SImageBase
    32  	EcloudTags
    33  	SZoneRegionBase
    34  
    35  	storageCache *SStoragecache
    36  	imgInfo      *imagetools.ImageInfo
    37  
    38  	ImageId         string
    39  	ServerId        string
    40  	ImageAlias      string
    41  	Name            string
    42  	Url             string
    43  	SrourceImageId  string
    44  	Status          string
    45  	SizeMb          int `json:"size"`
    46  	IsPublic        int
    47  	CreateTime      time.Time
    48  	Note            string
    49  	OsType          string
    50  	MinDiskGB       int `json:"minDisk"`
    51  	ImageType       string
    52  	PublicImageType string
    53  	BackupType      string
    54  	BackupWay       string
    55  	SnapshotId      string
    56  	OsName          string
    57  }
    58  
    59  func (r *SRegion) GetImage(imageId string) (*SImage, error) {
    60  	request := NewNovaRequest(NewApiRequest(r.ID, fmt.Sprintf("/api/v2/image/%s", imageId), nil, nil))
    61  	var image SImage
    62  	err := r.client.doGet(context.Background(), request, &image)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return &image, nil
    67  }
    68  
    69  func (r *SRegion) GetImages(isPublic bool) ([]SImage, error) {
    70  	if isPublic {
    71  		return nil, cloudprovider.ErrNotImplemented
    72  	}
    73  	request := NewNovaRequest(NewApiRequest(r.ID, "/api/v2/image", nil, nil))
    74  	images := make([]SImage, 0, 5)
    75  	err := r.client.doList(context.Background(), request, &images)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	return images, nil
    80  }
    81  
    82  func (i *SImage) GetCreatedAt() time.Time {
    83  	return i.CreateTime
    84  }
    85  
    86  func (i *SImage) GetId() string {
    87  	return i.ImageId
    88  }
    89  
    90  func (i *SImage) GetName() string {
    91  	return i.Name
    92  }
    93  
    94  func (i *SImage) GetGlobalId() string {
    95  	return i.ImageId
    96  }
    97  
    98  func (i *SImage) GetStatus() string {
    99  	switch i.Status {
   100  	case "active":
   101  		return api.CACHED_IMAGE_STATUS_ACTIVE
   102  	case "queued":
   103  		return api.CACHED_IMAGE_STATUS_INIT
   104  	case "saving":
   105  		return api.CACHED_IMAGE_STATUS_SAVING
   106  	case "caching":
   107  		return api.CACHED_IMAGE_STATUS_CACHING
   108  	case "pending_delete":
   109  		return api.CACHED_IMAGE_STATUS_DELETING
   110  	default:
   111  		return api.CACHED_IMAGE_STATUS_UNKNOWN
   112  	}
   113  }
   114  
   115  func (i *SImage) Refresh() error {
   116  	new, err := i.storageCache.region.GetImage(i.GetId())
   117  	if err != nil {
   118  		return err
   119  	}
   120  	return jsonutils.Update(i, new)
   121  }
   122  
   123  func (i *SImage) IsEmulated() bool {
   124  	return false
   125  }
   126  
   127  func (i *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
   128  	if i.imgInfo == nil {
   129  		imgInfo := imagetools.NormalizeImageInfo(i.OsName, "", i.OsType, "", "")
   130  		i.imgInfo = &imgInfo
   131  	}
   132  	return i.imgInfo
   133  }
   134  
   135  func (i *SImage) GetFullOsName() string {
   136  	return i.OsName
   137  }
   138  
   139  func (i *SImage) GetOsType() cloudprovider.TOsType {
   140  	return cloudprovider.TOsType(i.getNormalizedImageInfo().OsType)
   141  }
   142  
   143  func (i *SImage) GetOsArch() string {
   144  	return i.getNormalizedImageInfo().OsArch
   145  }
   146  
   147  func (i *SImage) GetOsDist() string {
   148  	return i.getNormalizedImageInfo().OsDistro
   149  }
   150  
   151  func (i *SImage) GetOsVersion() string {
   152  	return i.getNormalizedImageInfo().OsVersion
   153  }
   154  
   155  func (i *SImage) GetOsLang() string {
   156  	return i.getNormalizedImageInfo().OsLang
   157  }
   158  
   159  func (i *SImage) GetBios() cloudprovider.TBiosType {
   160  	return cloudprovider.ToBiosType(i.getNormalizedImageInfo().OsBios)
   161  }
   162  
   163  func (i *SImage) GetMinOsDiskSizeGb() int {
   164  	return i.MinDiskGB
   165  }
   166  
   167  func (i *SImage) GetMinRamSizeMb() int {
   168  	return 0
   169  }
   170  
   171  func (i *SImage) GetImageFormat() string {
   172  	return ""
   173  }
   174  
   175  func (self *SImage) Delete(ctx context.Context) error {
   176  	return cloudprovider.ErrNotSupported
   177  }
   178  
   179  func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   180  	return nil
   181  }
   182  
   183  func (self *SImage) GetSizeByte() int64 {
   184  	return int64(self.SizeMb) * 1024 * 1024
   185  }
   186  
   187  func (self *SImage) GetImageType() cloudprovider.TImageType {
   188  	if self.IsPublic == 1 {
   189  		return cloudprovider.ImageTypeSystem
   190  	}
   191  	return cloudprovider.ImageTypeCustomized
   192  }
   193  
   194  func (self *SImage) GetImageStatus() string {
   195  	return cloudprovider.IMAGE_STATUS_ACTIVE
   196  }