yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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/cloudmux/pkg/multicloud/aliyun"
    21  	"yunion.io/x/onecloud/pkg/util/shellutils"
    22  )
    23  
    24  func init() {
    25  	type ImageListOptions struct {
    26  		Status string   `help:"image status type" choices:"Creating|Available|UnAvailable|CreateFailed"`
    27  		Owner  string   `help:"Owner type" choices:"system|self|others|marketplace"`
    28  		Id     []string `help:"Image ID"`
    29  		Name   string   `help:"image name"`
    30  		Limit  int      `help:"page size"`
    31  		Offset int      `help:"page offset"`
    32  	}
    33  	shellutils.R(&ImageListOptions{}, "image-list", "List images", func(cli *aliyun.SRegion, args *ImageListOptions) error {
    34  		images, total, e := cli.GetImages(aliyun.ImageStatusType(args.Status), aliyun.ImageOwnerType(args.Owner), args.Id, args.Name, args.Offset, args.Limit)
    35  		if e != nil {
    36  			return e
    37  		}
    38  		printList(images, total, args.Offset, args.Limit, []string{})
    39  		return nil
    40  	})
    41  
    42  	type ImageShowOptions struct {
    43  		ID string `help:"image ID"`
    44  	}
    45  	shellutils.R(&ImageShowOptions{}, "image-show", "Show image", func(cli *aliyun.SRegion, args *ImageShowOptions) error {
    46  		img, err := cli.GetImage(args.ID)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		printObject(img)
    51  		return nil
    52  	})
    53  
    54  	type ImageDeleteOptions struct {
    55  		ID string `help:"ID or Name to delete"`
    56  	}
    57  	shellutils.R(&ImageDeleteOptions{}, "image-delete", "Delete image", func(cli *aliyun.SRegion, args *ImageDeleteOptions) error {
    58  		return cli.DeleteImage(args.ID)
    59  	})
    60  
    61  	type ImageCreateOptions struct {
    62  		SNAPSHOT string `help:"Snapshot id"`
    63  		NAME     string `help:"Image name"`
    64  		Desc     string `help:"Image desc"`
    65  	}
    66  	shellutils.R(&ImageCreateOptions{}, "image-create", "Create image", func(cli *aliyun.SRegion, args *ImageCreateOptions) error {
    67  		imageId, err := cli.CreateImage(args.SNAPSHOT, args.NAME, args.Desc)
    68  		if err != nil {
    69  			return err
    70  		}
    71  		fmt.Println(imageId)
    72  		return nil
    73  	})
    74  
    75  	type ImageExportOptions struct {
    76  		ID     string `help:"ID or Name to export"`
    77  		BUCKET string `help:"Bucket name"`
    78  	}
    79  
    80  	shellutils.R(&ImageExportOptions{}, "image-export", "Export image", func(cli *aliyun.SRegion, args *ImageExportOptions) error {
    81  		oss, err := cli.GetOssClient()
    82  		if err != nil {
    83  			return err
    84  		}
    85  		exist, err := oss.IsBucketExist(args.BUCKET)
    86  		if err != nil {
    87  			return err
    88  		}
    89  		if !exist {
    90  			return fmt.Errorf("not exist bucket %s", args.BUCKET)
    91  		}
    92  		bucket, err := oss.Bucket(args.BUCKET)
    93  		if err != nil {
    94  			return err
    95  		}
    96  		task, err := cli.ExportImage(args.ID, bucket)
    97  		if err != nil {
    98  			return err
    99  		}
   100  		printObject(task)
   101  		return nil
   102  	})
   103  }