yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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  	"sort"
    20  
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  	"yunion.io/x/cloudmux/pkg/multicloud/azure"
    23  	"yunion.io/x/onecloud/pkg/util/shellutils"
    24  )
    25  
    26  func init() {
    27  	type ImageListOptions struct {
    28  		ImageType string `help:"image type" choices:"customized|system|shared|market"`
    29  	}
    30  	shellutils.R(&ImageListOptions{}, "image-list", "List images", func(cli *azure.SRegion, args *ImageListOptions) error {
    31  		images, err := cli.GetImages(cloudprovider.TImageType(args.ImageType))
    32  		if err != nil {
    33  			return err
    34  		}
    35  		printList(images, len(images), 0, 0, []string{})
    36  		return nil
    37  	})
    38  
    39  	type ImagePublishersOptions struct {
    40  	}
    41  	shellutils.R(&ImagePublishersOptions{}, "image-publisher-list", "List image providers", func(cli *azure.SRegion, args *ImagePublishersOptions) error {
    42  		providers, err := cli.GetImagePublishers(nil)
    43  		if err != nil {
    44  			return err
    45  		}
    46  		sort.Strings(providers)
    47  		fmt.Println(providers)
    48  		return nil
    49  	})
    50  
    51  	type ImageOfferedIDOptions struct {
    52  		Publisher []string `help:"publisher candidates"`
    53  		Offer     []string `help:"offer candidates"`
    54  		Sku       []string `help:"sku candidates"`
    55  		Version   []string `help:"version candidates"`
    56  		Latest    bool     `help:"show latest version only"`
    57  	}
    58  	shellutils.R(&ImageOfferedIDOptions{}, "public-image-id-list", "List image providers", func(cli *azure.SRegion, args *ImageOfferedIDOptions) error {
    59  		idList, err := cli.GetOfferedImageIDs(args.Publisher, args.Offer, args.Sku, args.Version, args.Latest)
    60  		if err != nil {
    61  			return err
    62  		}
    63  		//sort.Strings(idList)
    64  		for id, image := range idList {
    65  			fmt.Printf("id: %s detail: %v\n", id, image)
    66  		}
    67  		return nil
    68  	})
    69  
    70  	type ImageCreateOptions struct {
    71  		NAME     string `helo:"Image name"`
    72  		OSTYPE   string `helo:"Operation system" choices:"Linux|Windows"`
    73  		Snapshot string `help:"Snapshot ID"`
    74  		BlobUri  string `helo:"page blob uri"`
    75  		DiskSize int32  `helo:"Image size"`
    76  		Desc     string `help:"Image desc"`
    77  	}
    78  
    79  	shellutils.R(&ImageCreateOptions{}, "image-create", "Create image", func(cli *azure.SRegion, args *ImageCreateOptions) error {
    80  		if len(args.Snapshot) > 0 {
    81  			if image, err := cli.CreateImage(args.Snapshot, args.NAME, args.OSTYPE, args.Desc); err != nil {
    82  				return err
    83  			} else {
    84  				printObject(image)
    85  				return nil
    86  			}
    87  		} else {
    88  			if image, err := cli.CreateImageByBlob(args.NAME, args.OSTYPE, args.BlobUri, args.DiskSize); err != nil {
    89  				return err
    90  			} else {
    91  				printObject(image)
    92  				return nil
    93  			}
    94  		}
    95  	})
    96  
    97  	type ImageIdOptions struct {
    98  		ID string `helo:"Image ID"`
    99  	}
   100  
   101  	shellutils.R(&ImageIdOptions{}, "image-delete", "Delete image", func(cli *azure.SRegion, args *ImageIdOptions) error {
   102  		return cli.DeleteImage(args.ID)
   103  	})
   104  
   105  	shellutils.R(&ImageIdOptions{}, "image-show", "Delete image", func(cli *azure.SRegion, args *ImageIdOptions) error {
   106  		image, err := cli.GetImageById(args.ID)
   107  		if err != nil {
   108  			return err
   109  		}
   110  		printObject(image)
   111  		return nil
   112  	})
   113  
   114  }