yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/shell/storageaccount.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/azure"
    21  	"yunion.io/x/onecloud/pkg/util/shellutils"
    22  )
    23  
    24  func init() {
    25  	type StorageAccountListOptions struct {
    26  	}
    27  	shellutils.R(&StorageAccountListOptions{}, "storage-account-list", "List storage account", func(cli *azure.SRegion, args *StorageAccountListOptions) error {
    28  		accounts, err := cli.ListStorageAccounts()
    29  		if err != nil {
    30  			return err
    31  		}
    32  		printList(accounts, len(accounts), 0, 0, []string{})
    33  		return nil
    34  	})
    35  
    36  	shellutils.R(&StorageAccountListOptions{}, "classic-storage-account-list", "List classic storage account", func(cli *azure.SRegion, args *StorageAccountListOptions) error {
    37  		accounts, err := cli.ListClassicStorageAccounts()
    38  		if err != nil {
    39  			return err
    40  		}
    41  		printList(accounts, len(accounts), 0, 0, []string{})
    42  		return nil
    43  	})
    44  
    45  	type StorageAccountOptions struct {
    46  		ID string `help:"StorageAccount ID"`
    47  	}
    48  
    49  	shellutils.R(&StorageAccountOptions{}, "classic-storage-account-show", "Show storage account detail", func(cli *azure.SRegion, args *StorageAccountOptions) error {
    50  		account, err := cli.GetClassicStorageAccount(args.ID)
    51  		if err != nil {
    52  			return err
    53  		}
    54  		printObject(account)
    55  		return nil
    56  	})
    57  
    58  	shellutils.R(&StorageAccountOptions{}, "storage-account-delete", "Delete storage account", func(cli *azure.SRegion, args *StorageAccountOptions) error {
    59  		return cli.DeleteStorageAccount(args.ID)
    60  	})
    61  
    62  	shellutils.R(&StorageAccountOptions{}, "storage-account-show", "Show storage account detail", func(cli *azure.SRegion, args *StorageAccountOptions) error {
    63  		account, err := cli.GetStorageAccountDetail(args.ID)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		printObject(account)
    68  		return nil
    69  	})
    70  
    71  	shellutils.R(&StorageAccountOptions{}, "storage-account-key", "Get storage account key", func(cli *azure.SRegion, args *StorageAccountOptions) error {
    72  		if key, err := cli.GetStorageAccountKey(args.ID); err != nil {
    73  			return err
    74  		} else {
    75  			fmt.Printf("Key: %s", key)
    76  			return nil
    77  		}
    78  	})
    79  
    80  	shellutils.R(&StorageAccountOptions{}, "storage-container-list", "Get list of containers of a storage account", func(cli *azure.SRegion, args *StorageAccountOptions) error {
    81  		account, err := cli.GetStorageAccountDetail(args.ID)
    82  		if err != nil {
    83  			return err
    84  		}
    85  		containers, err := account.GetContainers()
    86  		if err != nil {
    87  			return err
    88  		}
    89  		printList(containers, len(containers), 0, 0, nil)
    90  		return nil
    91  	})
    92  
    93  	type StorageAccountCreateContainerOptions struct {
    94  		ACCOUNT   string `help:"storage account ID"`
    95  		CONTAINER string `help:"name of container to create"`
    96  	}
    97  	shellutils.R(&StorageAccountCreateContainerOptions{}, "storage-container-create", "Create a container in a storage account", func(cli *azure.SRegion, args *StorageAccountCreateContainerOptions) error {
    98  		account, err := cli.GetStorageAccountDetail(args.ACCOUNT)
    99  		if err != nil {
   100  			return err
   101  		}
   102  		container, err := account.CreateContainer(args.CONTAINER)
   103  		if err != nil {
   104  			return err
   105  		}
   106  		printObject(container)
   107  		return nil
   108  	})
   109  
   110  	shellutils.R(&StorageAccountCreateContainerOptions{}, "storage-container-list-objects", "Create a container in a storage account", func(cli *azure.SRegion, args *StorageAccountCreateContainerOptions) error {
   111  		account, err := cli.GetStorageAccountDetail(args.ACCOUNT)
   112  		if err != nil {
   113  			return err
   114  		}
   115  		container, err := account.GetContainer(args.CONTAINER)
   116  		if err != nil {
   117  			return err
   118  		}
   119  		blobs, err := container.ListAllFiles(nil)
   120  		if err != nil {
   121  			return err
   122  		}
   123  		printList(blobs, len(blobs), 0, 0, nil)
   124  		return nil
   125  	})
   126  
   127  	type StorageAccountUploadOptions struct {
   128  		ACCOUNT   string `help:"storage account ID"`
   129  		CONTAINER string `help:"name of container to create"`
   130  		FILE      string `help:"local file to upload"`
   131  	}
   132  	shellutils.R(&StorageAccountUploadOptions{}, "storage-container-upload", "Upload a container in a storage account", func(cli *azure.SRegion, args *StorageAccountUploadOptions) error {
   133  		account, err := cli.GetStorageAccountDetail(args.ACCOUNT)
   134  		if err != nil {
   135  			return err
   136  		}
   137  		container, err := account.GetContainer(args.CONTAINER)
   138  		if err != nil {
   139  			return err
   140  		}
   141  		url, err := container.UploadFile(args.FILE, nil)
   142  		if err != nil {
   143  			return err
   144  		}
   145  		fmt.Println(url)
   146  		return nil
   147  	})
   148  
   149  	type StorageAccountCreateOptions struct {
   150  		NAME string `help:"StorageAccount NAME"`
   151  	}
   152  
   153  	shellutils.R(&StorageAccountCreateOptions{}, "storage-account-create", "Create a storage account", func(cli *azure.SRegion, args *StorageAccountCreateOptions) error {
   154  		if account, err := cli.CreateStorageAccount(args.NAME); err != nil {
   155  			return err
   156  		} else {
   157  			printObject(account)
   158  			return nil
   159  		}
   160  	})
   161  
   162  	type StorageAccountCheckeOptions struct {
   163  	}
   164  
   165  	shellutils.R(&StorageAccountCheckeOptions{}, "storage-uniq-name", "Get a uniqel storage account name", func(cli *azure.SRegion, args *StorageAccountCheckeOptions) error {
   166  		uniqName, err := cli.GetUniqStorageAccountName()
   167  		if err != nil {
   168  			return err
   169  		}
   170  		fmt.Println(uniqName)
   171  		return nil
   172  	})
   173  
   174  	type SStorageAccountSkuOptions struct {
   175  	}
   176  	shellutils.R(&SStorageAccountSkuOptions{}, "storage-account-skus", "List skus of storage account", func(cli *azure.SRegion, args *SStorageAccountSkuOptions) error {
   177  		skus, err := cli.GetStorageAccountSkus()
   178  		if err != nil {
   179  			return err
   180  		}
   181  		printList(skus, 0, 0, 0, nil)
   182  		return nil
   183  	})
   184  }