yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/disk.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  	"context"
    19  	"fmt"
    20  
    21  	"yunion.io/x/cloudmux/pkg/multicloud/qcloud"
    22  	"yunion.io/x/onecloud/pkg/util/shellutils"
    23  )
    24  
    25  func init() {
    26  	type DiskListOptions struct {
    27  		Instance string `help:"Instance ID"`
    28  		Zone     string `help:"Zone ID"`
    29  		Category string `help:"Disk category"`
    30  		Offset   int    `help:"List offset"`
    31  		Limit    int    `help:"List limit"`
    32  	}
    33  	shellutils.R(&DiskListOptions{}, "disk-list", "List disks", func(cli *qcloud.SRegion, args *DiskListOptions) error {
    34  		disks, total, e := cli.GetDisks(args.Instance, args.Zone, args.Category, nil, args.Offset, args.Limit)
    35  		if e != nil {
    36  			return e
    37  		}
    38  		printList(disks, total, args.Offset, args.Limit, []string{})
    39  		return nil
    40  	})
    41  
    42  	type DiskOptions struct {
    43  		ID string `help:"Disk ID"`
    44  	}
    45  	shellutils.R(&DiskOptions{}, "disk-delete", "Delete disks", func(cli *qcloud.SRegion, args *DiskOptions) error {
    46  		e := cli.DeleteDisk(args.ID)
    47  		if e != nil {
    48  			return e
    49  		}
    50  		return nil
    51  	})
    52  
    53  	shellutils.R(&DiskOptions{}, "disk-show", "Show disk", func(cli *qcloud.SRegion, args *DiskOptions) error {
    54  		disk, err := cli.GetDisk(args.ID)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		printObject(disk)
    59  		return nil
    60  	})
    61  
    62  	type DiskCreateOptions struct {
    63  		ZONE      string `help:"Zone ID"`
    64  		CATEGORY  string `help:"Disk category" choices:"CLOUD_BASIC|CLOUD_PREMIUM|CLOUD_SSD"`
    65  		NAME      string `help:"Disk Name"`
    66  		SIZE      int    `help:"Disk Size GB"`
    67  		Desc      string `help:"Description"`
    68  		ProjectId string `help:"Project Id"`
    69  	}
    70  	shellutils.R(&DiskCreateOptions{}, "disk-create", "Create disk", func(cli *qcloud.SRegion, args *DiskCreateOptions) error {
    71  		diskId, err := cli.CreateDisk(args.ZONE, args.CATEGORY, args.NAME, args.SIZE, args.Desc, args.ProjectId)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		fmt.Println(diskId)
    76  		return nil
    77  	})
    78  
    79  	type DiskResizeOptions struct {
    80  		ID   string `help:"Disk ID"`
    81  		SIZE int64  `help:"Disk Size GB"`
    82  	}
    83  	shellutils.R(&DiskResizeOptions{}, "disk-resize", "Resize disk", func(cli *qcloud.SRegion, args *DiskResizeOptions) error {
    84  		ctx := context.Background()
    85  		return cli.ResizeDisk(ctx, args.ID, args.SIZE)
    86  	})
    87  
    88  	type DiskResetOptions struct {
    89  		ID       string `help:"Disk ID"`
    90  		SNAPSHOT string `help:"Snapshot ID"`
    91  	}
    92  	shellutils.R(&DiskResetOptions{}, "disk-reset", "Reset disk", func(cli *qcloud.SRegion, args *DiskResetOptions) error {
    93  		return cli.ResetDisk(args.ID, args.SNAPSHOT)
    94  	})
    95  }