diff --git a/cmd/common.go b/cmd/common.go index 5d160e5..1bbeb58 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -4,6 +4,7 @@ package cmd import ( + "encoding/json" "fmt" "os" "strings" @@ -12,6 +13,31 @@ import ( "github.com/spf13/pflag" ) +func subscriptionIDFromAzureAuth(subscriptionID, authContents string) string { + if len(subscriptionID) > 0 { + return subscriptionID + } + + if len(authContents) == 0 { + return "" + } + + authMap := map[string]string{} + if err := json.Unmarshal([]byte(authContents), &authMap); err != nil { + return "" + } + + return authMap["subscriptionId"] +} + +func sampleDeleteCommand(provider, hostID, subscriptionID string) string { + cmd := fmt.Sprintf("inletsctl delete --provider %s --id \"%s\"", provider, hostID) + if provider == "azure" && len(subscriptionID) > 0 { + cmd += fmt.Sprintf(" --subscription-id %q", subscriptionID) + } + return cmd +} + func getFileOrString(flags *pflag.FlagSet, file, value string, required bool) (string, error) { var val string fileVal, _ := flags.GetString(file) diff --git a/cmd/common_test.go b/cmd/common_test.go new file mode 100644 index 0000000..6d9da78 --- /dev/null +++ b/cmd/common_test.go @@ -0,0 +1,43 @@ +// Copyright (c) Inlets Author(s) 2023. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package cmd + +import "testing" + +func Test_subscriptionIDFromAzureAuth(t *testing.T) { + t.Parallel() + + auth := `{"subscriptionId":"sub-from-file","clientId":"id","clientSecret":"secret","tenantId":"tenant"}` + + if got := subscriptionIDFromAzureAuth("", auth); got != "sub-from-file" { + t.Fatalf("want sub-from-file, got %q", got) + } + + if got := subscriptionIDFromAzureAuth("sub-from-flag", auth); got != "sub-from-flag" { + t.Fatalf("want sub-from-flag, got %q", got) + } + + if got := subscriptionIDFromAzureAuth("", "not-json"); got != "" { + t.Fatalf("want empty string, got %q", got) + } +} + +func Test_sampleDeleteCommand(t *testing.T) { + t.Parallel() + + want := `inletsctl delete --provider digitalocean --id "123"` + if got := sampleDeleteCommand("digitalocean", "123", ""); got != want { + t.Fatalf("want %q, got %q", want, got) + } + + want = `inletsctl delete --provider azure --id "group|deployment" --subscription-id "00000000-0000-0000-0000-000000000000"` + if got := sampleDeleteCommand("azure", "group|deployment", "00000000-0000-0000-0000-000000000000"); got != want { + t.Fatalf("want %q, got %q", want, got) + } + + want = `inletsctl delete --provider azure --id "group|deployment"` + if got := sampleDeleteCommand("azure", "group|deployment", ""); got != want { + t.Fatalf("want %q, got %q", want, got) + } +} diff --git a/cmd/create.go b/cmd/create.go index 23017cb..5e4a66e 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -371,7 +371,7 @@ inlets-pro http client --url "wss://%s:%d" \ --upstream http://127.0.0.1:8080 To delete: - inletsctl delete --provider %s --id "%s" + %s `, inletsProVersion, hostStatus.IP, @@ -380,8 +380,7 @@ To delete: hostStatus.IP, inletsProControlPort, inletsToken, - provider, - hostStatus.ID) + sampleDeleteCommand(provider, hostStatus.ID, subscriptionIDFromAzureAuth(subscriptionID, accessToken))) return nil } else { @@ -397,7 +396,7 @@ inlets-pro tcp client --url "wss://%s:%d" \ --ports 2222 To delete: - inletsctl delete --provider %s --id "%s" + %s `, inletsProVersion, hostStatus.IP, @@ -405,8 +404,7 @@ To delete: hostStatus.IP, inletsProControlPort, inletsToken, - provider, - hostStatus.ID) + sampleDeleteCommand(provider, hostStatus.ID, subscriptionIDFromAzureAuth(subscriptionID, accessToken))) return nil }