yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/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  	"os"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/multicloud/openstack"
    23  	"yunion.io/x/onecloud/pkg/util/shellutils"
    24  )
    25  
    26  func init() {
    27  	type ImageListOptions struct {
    28  		Name   string
    29  		Id     string
    30  		Status string
    31  	}
    32  	shellutils.R(&ImageListOptions{}, "image-list", "List images", func(cli *openstack.SRegion, args *ImageListOptions) error {
    33  		images, err := cli.GetImages(args.Name, args.Status, args.Id)
    34  		if err != nil {
    35  			return err
    36  		}
    37  		printList(images, 0, 0, 0, []string{})
    38  		return nil
    39  	})
    40  
    41  	type ImageOptions struct {
    42  		ID string
    43  	}
    44  
    45  	shellutils.R(&ImageOptions{}, "image-show", "Show image", func(cli *openstack.SRegion, args *ImageOptions) error {
    46  		image, err := cli.GetImage(args.ID)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		printObject(image)
    51  		return nil
    52  	})
    53  
    54  	shellutils.R(&ImageOptions{}, "image-delete", "Delete image", func(cli *openstack.SRegion, args *ImageOptions) error {
    55  		return cli.DeleteImage(args.ID)
    56  	})
    57  
    58  	type ImageCreateOptions struct {
    59  		NAME          string
    60  		OsType        string `help:"os type" default:"linux" choices:"linux|windows"`
    61  		OsDistro      string
    62  		MinDiskSizeGB int
    63  		MinRamMb      int
    64  		FILE          string
    65  	}
    66  
    67  	shellutils.R(&ImageCreateOptions{}, "image-create", "Create image", func(cli *openstack.SRegion, args *ImageCreateOptions) error {
    68  		file, err := os.Open(args.FILE)
    69  		if err != nil {
    70  			return errors.Wrap(err, "os.Open")
    71  		}
    72  		defer file.Close()
    73  		image, err := cli.CreateImage(args.NAME, args.OsType, args.OsDistro, args.MinDiskSizeGB, args.MinRamMb, 0, file, nil)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		printObject(image)
    78  		return nil
    79  	})
    80  
    81  }