yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/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 shell
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/log"
    21  
    22  	"yunion.io/x/cloudmux/pkg/apis"
    23  	"yunion.io/x/cloudmux/pkg/multicloud/qcloud"
    24  	"yunion.io/x/onecloud/pkg/util/shellutils"
    25  )
    26  
    27  func init() {
    28  	type ImageListOptions struct {
    29  		Status string `help:"Image status"`
    30  		Owner  string `help:"Image owner" choices:"PRIVATE_IMAGE|PUBLIC_IMAGE|MARKET_IMAGE|SHARED_IMAGE"`
    31  		Image  string `help:"Image Id"`
    32  		Name   string `help:"Image Name"`
    33  		Limit  int    `help:"page size"`
    34  		Offset int    `help:"page offset"`
    35  	}
    36  	shellutils.R(&ImageListOptions{}, "image-list", "List images", func(cli *qcloud.SRegion, args *ImageListOptions) error {
    37  		imageIds := []string{}
    38  		if len(args.Image) > 0 {
    39  			imageIds = append(imageIds, args.Image)
    40  		}
    41  		images, total, err := cli.GetImages(args.Status, args.Owner, imageIds, args.Name, args.Offset, args.Limit)
    42  		if err != nil {
    43  			return err
    44  		}
    45  		printList(images, total, args.Offset, args.Limit, []string{})
    46  		return nil
    47  	})
    48  
    49  	type ImageCreateOptions struct {
    50  		NAME      string `helo:"Image name"`
    51  		OSTYPE    string `helo:"Operation system" choices:"CentOS|Ubuntu|Debian|OpenSUSE|SUSE|CoreOS|FreeBSD|Other Linux|Windows Server 2008|Windows Server 2012|Windows Server 2016"`
    52  		OSARCH    string `help:"OS Architecture" choices:"x86_64|i386"`
    53  		osVersion string `help:"OS Version"`
    54  		URL       string `helo:"Cos URL"`
    55  	}
    56  
    57  	shellutils.R(&ImageCreateOptions{}, "image-create", "Create image", func(cli *qcloud.SRegion, args *ImageCreateOptions) error {
    58  		image, err := cli.ImportImage(args.NAME, args.OSARCH, args.OSTYPE, args.osVersion, args.URL)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		printObject(image)
    63  		return nil
    64  	})
    65  
    66  	type ImageDeleteOptions struct {
    67  		ID string `helo:"Image ID"`
    68  	}
    69  
    70  	shellutils.R(&ImageDeleteOptions{}, "image-delete", "Delete image", func(cli *qcloud.SRegion, args *ImageDeleteOptions) error {
    71  		return cli.DeleteImage(args.ID)
    72  	})
    73  
    74  	type ImageSupportSetOptions struct {
    75  	}
    76  
    77  	shellutils.R(&ImageSupportSetOptions{}, "image-support-set", "Show image support set", func(cli *qcloud.SRegion, args *ImageSupportSetOptions) error {
    78  		imageSet, err := cli.GetSupportImageSet()
    79  		if err != nil {
    80  			return err
    81  		}
    82  		printObject(imageSet)
    83  		return nil
    84  	})
    85  
    86  	type ImageParam struct {
    87  		osArch          string
    88  		osDist          string
    89  		osVersion       string
    90  		OutputArch      string
    91  		OutputDist      string
    92  		OutputOsVersion string
    93  	}
    94  
    95  	shellutils.R(&ImageSupportSetOptions{}, "image-import-test", "Test image import params", func(cli *qcloud.SRegion, args *ImageSupportSetOptions) error {
    96  		imageParams := []ImageParam{
    97  			{
    98  				osArch:          "test",
    99  				osDist:          "Centos",
   100  				osVersion:       "3.4",
   101  				OutputArch:      apis.OS_ARCH_X86_64,
   102  				OutputDist:      "CentOS",
   103  				OutputOsVersion: "-",
   104  			},
   105  			{
   106  				osArch:          "i386",
   107  				osDist:          "Centos",
   108  				osVersion:       "6.9",
   109  				OutputArch:      "i386",
   110  				OutputDist:      "CentOS",
   111  				OutputOsVersion: "6",
   112  			},
   113  			{
   114  				osArch:          "i386",
   115  				osDist:          "Centos",
   116  				osVersion:       "7.1.1503",
   117  				OutputArch:      "i386",
   118  				OutputDist:      "CentOS",
   119  				OutputOsVersion: "7",
   120  			},
   121  			{
   122  				osArch:          "",
   123  				osDist:          "",
   124  				osVersion:       "7.1",
   125  				OutputArch:      apis.OS_ARCH_X86_64,
   126  				OutputDist:      "Other Linux",
   127  				OutputOsVersion: "-",
   128  			},
   129  			{
   130  				osArch:          apis.OS_ARCH_X86_64,
   131  				osDist:          "Windows Server",
   132  				osVersion:       "2008 R2 Datacenter Evaluation",
   133  				OutputArch:      apis.OS_ARCH_X86_64,
   134  				OutputDist:      "Windows Server 2008",
   135  				OutputOsVersion: "-",
   136  			},
   137  			{
   138  				osArch:          apis.OS_ARCH_X86_64,
   139  				osDist:          "Ubuntu",
   140  				osVersion:       "16.04.5",
   141  				OutputArch:      apis.OS_ARCH_X86_64,
   142  				OutputDist:      "Ubuntu",
   143  				OutputOsVersion: "16",
   144  			},
   145  			{
   146  				osArch:          apis.OS_ARCH_X86_64,
   147  				osDist:          "Windows%20Server%202008%20R2%20Datacenter",
   148  				osVersion:       "6.1",
   149  				OutputArch:      apis.OS_ARCH_X86_64,
   150  				OutputDist:      "Windows Server 2008",
   151  				OutputOsVersion: "-",
   152  			},
   153  			{
   154  				osArch:          apis.OS_ARCH_X86_64,
   155  				osDist:          "Windows Server 2012 R2 Datacenter Evaluation",
   156  				osVersion:       "6.2",
   157  				OutputArch:      apis.OS_ARCH_X86_64,
   158  				OutputDist:      "Windows Server 2012",
   159  				OutputOsVersion: "-",
   160  			},
   161  		}
   162  		for _, imageParam := range imageParams {
   163  			log.Debugf("process %s", imageParam.osDist)
   164  			params, err := cli.GetImportImageParams("", imageParam.osArch, imageParam.osDist, imageParam.osVersion, "")
   165  			if err != nil {
   166  				return err
   167  			}
   168  			if params["OsType"] != imageParam.OutputDist {
   169  				return fmt.Errorf("params: %s osType should be %s not %s", imageParam, imageParam.OutputDist, params["OsType"])
   170  			}
   171  			if params["OsVersion"] != imageParam.OutputOsVersion {
   172  				return fmt.Errorf("params: %s OsVersion should be %s not %s", imageParam, imageParam.OutputOsVersion, params["OsVersion"])
   173  			}
   174  			if params["Architecture"] != imageParam.OutputArch {
   175  				return fmt.Errorf("params: %s Architecture should be %s not %s", imageParam, imageParam.OutputArch, params["Architecture"])
   176  			}
   177  		}
   178  		return nil
   179  	})
   180  
   181  }