Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --vpc support #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ func (c *Command) Flags() *FlagSet {
}

func (c *Command) Execute(ctx *Context, args []string) {

//
// if completion
if ctx.completion != nil {
args = ctx.completion.GetArgs()
Expand Down Expand Up @@ -148,35 +146,30 @@ func (c *Command) ExecuteComplete(ctx *Context, args []string) {
}

func (c *Command) executeInner(ctx *Context, args []string) error {
//
// fmt.Printf(">>> Execute Command: %s args=%v\n", c.Name, args)
parser := NewParser(args, ctx)

//
// get next arg
nextArg, _, err := parser.ReadNextArg()
if err != nil {
return err
}
//

// if next arg is help, run help
if nextArg == "help" {
ctx.help = true
return c.executeInner(ctx, parser.GetRemains())
}

//
// if next args is not empty, try find sub commands
if nextArg != "" {
//
// if has sub command, run it
subCommand := c.GetSubCommand(nextArg)
if subCommand != nil {
ctx.EnterCommand(subCommand)
return subCommand.executeInner(ctx, parser.GetRemains())
}

//
// no sub command and command.Run == nil
// raise error
if c.Run == nil {
Expand All @@ -192,7 +185,6 @@ func (c *Command) executeInner(ctx *Context, args []string) error {
return fmt.Errorf("parse failed %s", err)
}

//
// check flags
err = ctx.CheckFlags()
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

//
// default help flag

func HelpFlag(fs *FlagSet) *Flag {
return fs.Get("help")
}
Expand Down
9 changes: 5 additions & 4 deletions config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func NewConfigureCommand() *cli.Command {
if len(args) > 0 {
return cli.NewInvalidCommandError(args[0], ctx)
}
profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
mode, _ := ModeFlag(ctx.Flags()).GetValue()
return doConfigure(ctx, profileName, mode)
return doConfigure(ctx)
},
}

Expand All @@ -63,14 +61,17 @@ func NewConfigureCommand() *cli.Command {
return c
}

func doConfigure(ctx *cli.Context, profileName string, mode string) error {
func doConfigure(ctx *cli.Context) error {
w := ctx.Writer()

conf, err := loadConfiguration()
if err != nil {
return err
}

profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
mode, _ := ModeFlag(ctx.Flags()).GetValue()

if profileName == "" {
if conf.CurrentProfile == "" {
profileName = "default"
Expand Down
12 changes: 9 additions & 3 deletions config/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,23 @@ func TestDoConfigure(t *testing.T) {
stderr := new(bytes.Buffer)
ctx := cli.NewCommandContext(w, stderr)
AddFlags(ctx.Flags())
err := doConfigure(ctx, "profile", "AK")
ProfileFlag(ctx.Flags()).SetValue("profile")
ModeFlag(ctx.Flags()).SetValue("AK")
err := doConfigure(ctx)
assert.Nil(t, err)
assert.Equal(t, "Configuring profile 'profile' in 'AK' authenticate mode...\nAccess Key Id []: Access Key Secret []: Default Region Id []: Default Output Format [json]: json (Only support json)\nDefault Language [zh|en] en: Saving profile[profile] ...Done.\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\nAccessKeyId/AccessKeySecret is empty! run `aliyun configure` first\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\n", w.String())
w.Reset()

err = doConfigure(ctx, "", "")
ProfileFlag(ctx.Flags()).SetValue("")
ModeFlag(ctx.Flags()).SetValue("")
err = doConfigure(ctx)
assert.Nil(t, err)
assert.Equal(t, "Configuring profile 'default' in 'AK' authenticate mode...\nAccess Key Id [*************************_id]: Access Key Secret [*****************************ret]: Default Region Id []: Default Output Format [json]: json (Only support json)\nDefault Language [zh|en] : Saving profile[default] ...Done.\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\ndefault RegionId is empty! run `aliyun configure` first\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\n", w.String())
w.Reset()

err = doConfigure(ctx, "", "StsToken")
ProfileFlag(ctx.Flags()).SetValue("")
ModeFlag(ctx.Flags()).SetValue("StsToken")
err = doConfigure(ctx)
assert.Nil(t, err)
assert.True(t, strings.Contains(w.String(), "Warning: You are changing the authentication type of profile 'default' from 'AK' to 'StsToken'\nConfiguring profile 'default' in 'StsToken' authenticate mode...\nAccess Key Id [*************************_id]: Access Key Secret [*****************************ret]: Sts Token []: Default Region Id []: Default Output Format [json]: json (Only support json)\nDefault Language [zh|en] : Saving profile[default] ...Done.\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\n"))
w.Reset()
Expand Down
19 changes: 19 additions & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
const (
ProfileFlagName = "profile"
ModeFlagName = "mode"
VpcFlagName = "vpc"
AccessKeyIdFlagName = "access-key-id"
AccessKeySecretFlagName = "access-key-secret"
StsTokenFlagName = "sts-token"
Expand Down Expand Up @@ -63,6 +64,7 @@ func AddFlags(fs *cli.FlagSet) {
fs.Add(NewSkipSecureVerify())
fs.Add(NewExpiredSecondsFlag())
fs.Add(NewProcessCommandFlag())
fs.Add(NewVpcFlag())
}

func ConnectTimeoutFlag(fs *cli.FlagSet) *cli.Flag {
Expand All @@ -77,6 +79,10 @@ func ModeFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(ModeFlagName)
}

func VpcFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(VpcFlagName)
}

func AccessKeyIdFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(AccessKeyIdFlagName)
}
Expand Down Expand Up @@ -136,12 +142,15 @@ func RetryCountFlag(fs *cli.FlagSet) *cli.Flag {
func SkipSecureVerify(fs *cli.FlagSet) *cli.Flag {
return fs.Get(SkipSecureVerifyName)
}

func ConfigurePathFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(ConfigurePathFlagName)
}

func ExpiredSecondsFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(ExpiredSecondsFlagName)
}

func ProcessCommandFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(ProcessCommandFlagName)
}
Expand Down Expand Up @@ -175,6 +184,16 @@ func NewModeFlag() *cli.Flag {
"使用 `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair|RamRoleArnWithRoleName}` 指定认证方式")}
}

func NewVpcFlag() *cli.Flag {
return &cli.Flag{
Category: "config",
Name: VpcFlagName,
AssignedMode: cli.AssignedNone,
Short: i18n.T(
"use `--vpc` to enable vpc endpoint",
"使用 `--vpc` 启用 VPC 接入点地址")}
}

func NewAccessKeyIdFlag() *cli.Flag {
return &cli.Flag{
Category: "config",
Expand Down
5 changes: 5 additions & 0 deletions config/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
func DoHello(ctx *cli.Context, profile *Profile) {
w := ctx.Writer()

vpc := VpcFlag(ctx.Flags()).IsAssigned()
client, err := profile.GetClient(ctx)
if vpc {
client.Network = "vpc"
}

if err != nil {
cli.Println(w, "-----------------------------------------------")
Expand All @@ -36,6 +40,7 @@ func DoHello(ctx *cli.Context, profile *Profile) {
cli.Println(w, "-----------------------------------------------")
return
}

request := ecs.CreateDescribeRegionsRequest()
response := ecs.CreateDescribeRegionsResponse()
if vendorEnv, ok := os.LookupEnv("ALIBABA_CLOUD_VENDOR"); ok {
Expand Down
14 changes: 14 additions & 0 deletions openapi/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
func AddFlags(fs *cli.FlagSet) {
fs.Add(NewSecureFlag())
fs.Add(NewForceFlag())
// fs.Add(NewVpcFlag())
fs.Add(NewEndpointFlag())
fs.Add(NewVersionFlag())
fs.Add(NewHeaderFlag())
Expand All @@ -38,6 +39,7 @@ func AddFlags(fs *cli.FlagSet) {
const (
SecureFlagName = "secure"
ForceFlagName = "force"
VpcFlagName = "vpc"
EndpointFlagName = "endpoint"
VersionFlagName = "version"
HeaderFlagName = "header"
Expand All @@ -62,6 +64,10 @@ func ForceFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(ForceFlagName)
}

func VpcFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(VpcFlagName)
}

func EndpointFlag(fs *cli.FlagSet) *cli.Flag {
return fs.Get(EndpointFlagName)
}
Expand Down Expand Up @@ -125,6 +131,14 @@ func NewForceFlag() *cli.Flag {
"添加 `--force` 开关可跳过API与参数的合法性检查")}
}

func NewVpcFlag() *cli.Flag {
return &cli.Flag{Category: "caller",
Name: VpcFlagName, AssignedMode: cli.AssignedOnce,
Short: i18n.T(
"use `--vpc` to enable vpc endpoint",
"使用 `--vpc` 来启用 VPC 接入点地址")}
}

func NewEndpointFlag() *cli.Flag {
return &cli.Flag{Category: "caller",
Name: EndpointFlagName, AssignedMode: cli.AssignedOnce,
Expand Down