yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/azurecli/main.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 main 16 17 import ( 18 "fmt" 19 "net/http" 20 "net/url" 21 "os" 22 23 "golang.org/x/net/http/httpproxy" 24 25 "yunion.io/x/structarg" 26 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 "yunion.io/x/cloudmux/pkg/multicloud/azure" 29 _ "yunion.io/x/cloudmux/pkg/multicloud/azure/shell" 30 "yunion.io/x/onecloud/pkg/util/printutils" 31 "yunion.io/x/onecloud/pkg/util/shellutils" 32 ) 33 34 type BaseOptions struct { 35 Debug bool `help:"debug mode"` 36 DirectoryID string `help:"Azure account Directory ID/Tenant ID" default:"$AZURE_DIRECTORY_ID" metavar:"AZURE_DIRECTORY_ID"` 37 SubscriptionID string `help:"Azure account subscription ID" default:"$AZURE_SUBSCRIPTION_ID" metavar:"AZURE_SUBSCRIPTION_ID"` 38 ApplicationID string `help:"Azure application ID" default:"$AZURE_APPLICATION_ID" metavar:"AZURE_APPLICATION_ID"` 39 ApplicationKey string `help:"Azure application key" default:"$AZURE_APPLICATION_KEY" metavar:"AZURE_APPLICATION_KEY"` 40 RegionId string `help:"RegionId" default:"$AZURE_REGION_ID" metavar:"AZURE_REGION_ID"` 41 CloudEnv string `help:"Cloud Environment" default:"$AZURE_CLOUD_ENV" choices:"AzureGermanCloud|AzureChinaCloud|AzureUSGovernmentCloud|AzurePublicCloud" metavar:"AZURE_CLOUD_ENV"` 42 SUBCOMMAND string `help:"azurecli subcommand" subcommand:"true"` 43 } 44 45 func getSubcommandParser() (*structarg.ArgumentParser, error) { 46 parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{}, 47 "azurecli", 48 "Command-line interface to azure API.", 49 `See "azurecli COMMAND --help" for help on a specific command.`) 50 51 if e != nil { 52 return nil, e 53 } 54 55 subcmd := parse.GetSubcommand() 56 if subcmd == nil { 57 return nil, fmt.Errorf("No subcommand argument.") 58 } 59 for _, v := range shellutils.CommandTable { 60 _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback) 61 if e != nil { 62 return nil, e 63 } 64 } 65 return parse, nil 66 } 67 68 func showErrorAndExit(e error) { 69 fmt.Fprintf(os.Stderr, "%s", e) 70 fmt.Fprintln(os.Stderr) 71 os.Exit(1) 72 } 73 74 func newClient(options *BaseOptions) (*azure.SRegion, error) { 75 if len(options.DirectoryID) == 0 { 76 return nil, fmt.Errorf("Missing Directory ID") 77 } 78 79 if len(options.SubscriptionID) == 0 { 80 return nil, fmt.Errorf("Missing subscription ID") 81 } 82 83 if len(options.ApplicationID) == 0 { 84 return nil, fmt.Errorf("Missing Application ID") 85 } 86 87 if len(options.ApplicationKey) == 0 { 88 return nil, fmt.Errorf("Missing Application Key") 89 } 90 91 if len(options.CloudEnv) == 0 { 92 return nil, fmt.Errorf("Missing Cloud Environment") 93 } 94 95 cfg := &httpproxy.Config{ 96 HTTPProxy: os.Getenv("HTTP_PROXY"), 97 HTTPSProxy: os.Getenv("HTTPS_PROXY"), 98 NoProxy: os.Getenv("NO_PROXY"), 99 } 100 cfgProxyFunc := cfg.ProxyFunc() 101 proxyFunc := func(req *http.Request) (*url.URL, error) { 102 return cfgProxyFunc(req.URL) 103 } 104 105 cli, err := azure.NewAzureClient( 106 azure.NewAzureClientConfig( 107 options.CloudEnv, 108 options.DirectoryID, 109 options.ApplicationID, 110 options.ApplicationKey, 111 ). 112 SubscriptionId(options.SubscriptionID). 113 Debug(options.Debug). 114 CloudproviderConfig( 115 cloudprovider.ProviderConfig{ 116 ProxyFunc: proxyFunc, 117 }, 118 ), 119 ) 120 121 if err != nil { 122 return nil, err 123 } 124 region := cli.GetRegion(options.RegionId) 125 if region == nil { 126 fmt.Println("Please chooce which region you are going to use:") 127 regions := cli.GetRegions() 128 printutils.PrintInterfaceList(regions, 0, 0, 0, nil) 129 return nil, fmt.Errorf("No such region %s", options.RegionId) 130 } 131 132 return region, nil 133 } 134 135 func main() { 136 parser, e := getSubcommandParser() 137 if e != nil { 138 showErrorAndExit(e) 139 } 140 e = parser.ParseArgs(os.Args[1:], false) 141 options := parser.Options().(*BaseOptions) 142 143 if parser.IsHelpSet() { 144 fmt.Print(parser.HelpString()) 145 return 146 } 147 subcmd := parser.GetSubcommand() 148 subparser := subcmd.GetSubParser() 149 if e != nil || subparser == nil { 150 if subparser != nil { 151 fmt.Print(subparser.Usage()) 152 } else { 153 fmt.Print(parser.Usage()) 154 } 155 showErrorAndExit(e) 156 } 157 suboptions := subparser.Options() 158 if subparser.IsHelpSet() { 159 fmt.Print(subparser.HelpString()) 160 return 161 } 162 var region *azure.SRegion 163 region, e = newClient(options) 164 if e != nil { 165 showErrorAndExit(e) 166 } 167 e = subcmd.Invoke(region, suboptions) 168 if e != nil { 169 showErrorAndExit(e) 170 } 171 }