yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vm/apis"
    24  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vm/client"
    25  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vm/models"
    26  
    27  	"yunion.io/x/log"
    28  	"yunion.io/x/pkg/errors"
    29  
    30  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    31  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    32  	"yunion.io/x/onecloud/pkg/httperrors"
    33  	"yunion.io/x/cloudmux/pkg/multicloud"
    34  	"yunion.io/x/onecloud/pkg/util/imagetools"
    35  )
    36  
    37  type SImage struct {
    38  	multicloud.SImageBase
    39  	JdcloudTags
    40  
    41  	storageCache *SStoragecache
    42  
    43  	imageInfo *imagetools.ImageInfo
    44  
    45  	models.Image
    46  }
    47  
    48  func (i *SImage) GetCreatedAt() time.Time {
    49  	return parseTime(i.CreateTime)
    50  }
    51  
    52  func (i *SImage) GetId() string {
    53  	return i.ImageId
    54  }
    55  
    56  func (i *SImage) GetName() string {
    57  	return i.Name
    58  }
    59  
    60  func (i *SImage) GetDescription() string {
    61  	return i.Desc
    62  }
    63  
    64  func (i *SImage) GetGlobalId() string {
    65  	return i.GetId()
    66  }
    67  
    68  func (i *SImage) GetStatus() string {
    69  	switch i.Status {
    70  	case "pending":
    71  		return api.CACHED_IMAGE_STATUS_SAVING
    72  	case "ready":
    73  		return api.CACHED_IMAGE_STATUS_ACTIVE
    74  	case "deleting":
    75  		return api.CACHED_IMAGE_STATUS_DELETING
    76  	case "error":
    77  		return api.CACHED_IMAGE_STATUS_UNKNOWN
    78  	default:
    79  		return api.CACHED_IMAGE_STATUS_UNKNOWN
    80  	}
    81  }
    82  
    83  func (i *SImage) Refresh() error {
    84  	return nil
    85  }
    86  
    87  func (i *SImage) IsEmulated() bool {
    88  	return false
    89  }
    90  
    91  func (i *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
    92  	if i.imageInfo == nil {
    93  		imgInfo := imagetools.NormalizeImageInfo(i.Name, i.Architecture, i.OsType, i.Platform, i.OsVersion)
    94  		i.imageInfo = &imgInfo
    95  	}
    96  	return i.imageInfo
    97  }
    98  
    99  func (i *SImage) GetFullOsName() string {
   100  	return i.Name
   101  }
   102  
   103  func (i *SImage) GetOsType() cloudprovider.TOsType {
   104  	return cloudprovider.TOsType(i.getNormalizedImageInfo().OsType)
   105  }
   106  
   107  func (i *SImage) GetOsDist() string {
   108  	return i.getNormalizedImageInfo().OsDistro
   109  }
   110  
   111  func (i *SImage) GetOsVersion() string {
   112  	return i.getNormalizedImageInfo().OsVersion
   113  }
   114  
   115  func (i *SImage) GetOsLang() string {
   116  	return i.getNormalizedImageInfo().OsLang
   117  }
   118  
   119  func (i *SImage) GetOsArch() string {
   120  	return i.getNormalizedImageInfo().OsArch
   121  }
   122  
   123  func (i *SImage) GetBios() cloudprovider.TBiosType {
   124  	return cloudprovider.ToBiosType(i.getNormalizedImageInfo().OsBios)
   125  }
   126  
   127  func (i *SImage) GetMinOsDiskSizeGb() int {
   128  	return i.SystemDiskSizeGB
   129  }
   130  
   131  func (i *SImage) GetMinRamSizeMb() int {
   132  	return 0
   133  }
   134  
   135  func (i *SImage) GetImageFormat() string {
   136  	return ""
   137  }
   138  
   139  func (i *SImage) Delete(ctx context.Context) error {
   140  	return cloudprovider.ErrNotImplemented
   141  }
   142  
   143  func (i *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   144  	return nil
   145  }
   146  
   147  func (i *SImage) GetSizeByte() int64 {
   148  	return int64(i.SizeMB) * 1024 * 1024
   149  }
   150  
   151  func (i *SImage) GetImageType() cloudprovider.TImageType {
   152  	switch i.ImageSource {
   153  	case "jcloud":
   154  		return cloudprovider.ImageTypeSystem
   155  	case "marketplace":
   156  		return cloudprovider.ImageTypeMarket
   157  	case "self":
   158  		return cloudprovider.ImageTypeCustomized
   159  	case "shared":
   160  		return cloudprovider.ImageTypeShared
   161  	default:
   162  		return cloudprovider.TImageType("")
   163  	}
   164  }
   165  
   166  func (i *SImage) GetImageStatus() string {
   167  	switch i.Status {
   168  	case "pending":
   169  		return cloudprovider.IMAGE_STATUS_QUEUED
   170  	default:
   171  		return cloudprovider.IMAGE_STATUS_ACTIVE
   172  	}
   173  }
   174  
   175  func (r *SRegion) GetImage(imageId string) (*SImage, error) {
   176  	req := apis.NewDescribeImageRequest(r.ID, imageId)
   177  	client := client.NewVmClient(r.getCredential())
   178  	client.Logger = Logger{debug: r.client.debug}
   179  	resp, err := client.DescribeImage(req)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	if resp.Error.Code >= 400 {
   184  		err = fmt.Errorf(resp.Error.Message)
   185  		return nil, err
   186  	}
   187  	return &SImage{
   188  		Image: resp.Result.Image,
   189  	}, nil
   190  }
   191  
   192  func (r *SRegion) GetImages(imageIds []string, imageSource string, pageNumber, pageSize int) ([]SImage, int, error) {
   193  	req := apis.NewDescribeImagesRequestWithAllParams(r.ID, &imageSource, nil, nil, nil, imageIds, nil, nil, nil, nil, &pageNumber, &pageSize)
   194  	client := client.NewVmClient(r.getCredential())
   195  	client.Logger = Logger{debug: r.client.debug}
   196  	resp, err := client.DescribeImages(req)
   197  	if err != nil {
   198  		log.Errorf("err: %v", err)
   199  		return nil, 0, err
   200  	}
   201  	if resp.Error.Code >= 400 {
   202  		if strings.Contains(resp.Error.Message, "secret key is nul") || strings.Contains(resp.Error.Message, "sign result is not same") {
   203  			return nil, 0, errors.Wrapf(httperrors.ErrInvalidAccessKey, resp.Error.Message)
   204  		}
   205  		err = fmt.Errorf(resp.Error.Message)
   206  		return nil, 0, err
   207  	}
   208  	images := make([]SImage, len(resp.Result.Images))
   209  	for i := range resp.Result.Images {
   210  		images[i] = SImage{
   211  			Image: resp.Result.Images[i],
   212  		}
   213  	}
   214  	return images, resp.Result.TotalCount, nil
   215  }