Skip to content

Commit 0bcd3dc

Browse files
authored
whoami expose subscription level (#860)
* in progress update * update whoami output. Add platform to provider account info. update test, use tier info from fabric fix to show tiers * update tier mapping to name * proto rebuild * remove tier information account info. fabric will be source of info for teir, not provider identity. * update to use providerID * make the display function convert tier to string
1 parent c69f499 commit 0bcd3dc

File tree

7 files changed

+92
-21
lines changed

7 files changed

+92
-21
lines changed

src/pkg/cli/client/byoc/aws/byoc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ func (b *ByocAws) AccountInfo(ctx context.Context) (client.AccountInfo, error) {
409409
if err != nil {
410410
return nil, AnnotateAwsError(err)
411411
}
412+
412413
return AWSAccountInfo{
413414
region: cfg.Region,
414415
accountID: *identity.Account,
@@ -426,6 +427,10 @@ func (i AWSAccountInfo) AccountID() string {
426427
return i.accountID
427428
}
428429

430+
func (i AWSAccountInfo) Provider() client.ProviderID {
431+
return client.ProviderAWS
432+
}
433+
429434
func (i AWSAccountInfo) Region() string {
430435
return i.region
431436
}

src/pkg/cli/client/byoc/do/byoc.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,18 +495,26 @@ func (b *ByocDo) AccountInfo(ctx context.Context) (client.AccountInfo, error) {
495495
if err != nil {
496496
return nil, err
497497
}
498-
return DoAccountInfo{region: b.driver.Region.String(), accountID: account.Email}, nil
498+
return DoAccountInfo{
499+
accountID: account.Email,
500+
region: b.driver.Region.String(),
501+
},
502+
nil
499503
}
500504

501505
type DoAccountInfo struct {
502-
region string
503506
accountID string
507+
region string
504508
}
505509

506510
func (i DoAccountInfo) AccountID() string {
507511
return i.accountID
508512
}
509513

514+
func (i DoAccountInfo) Provider() client.ProviderID {
515+
return client.ProviderDO
516+
}
517+
510518
func (i DoAccountInfo) Region() string {
511519
return i.region
512520
}

src/pkg/cli/client/playground.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (g *PlaygroundProvider) PrepareDomainDelegation(ctx context.Context, req Pr
124124

125125
type PlaygroundAccountInfo struct{}
126126

127-
func (g PlaygroundAccountInfo) AccountID() string { return "playground" }
128-
func (g PlaygroundAccountInfo) Region() string { return "us-west-2" } // Hardcoded for now for prod1
129-
func (g PlaygroundAccountInfo) Details() string { return "" }
127+
func (g PlaygroundAccountInfo) AccountID() string { return "" }
128+
func (g PlaygroundAccountInfo) Details() string { return "" }
129+
func (g PlaygroundAccountInfo) Provider() ProviderID { return ProviderDefang }
130+
func (g PlaygroundAccountInfo) Region() string { return "us-west-2" } // Hardcoded for now for prod1

src/pkg/cli/client/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ type Provider interface {
135135

136136
type AccountInfo interface {
137137
AccountID() string
138-
Region() string
139138
Details() string
139+
Provider() ProviderID
140+
Region() string
140141
}
141142

142143
type Loader interface {

src/pkg/cli/whoami.go

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,52 @@ package cli
22

33
import (
44
"context"
5-
"fmt"
65

6+
"github.com/DefangLabs/defang/src/pkg"
77
"github.com/DefangLabs/defang/src/pkg/cli/client"
8+
9+
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
810
)
911

12+
type ShowAccountData struct {
13+
Provider string
14+
AccountID string
15+
Details string
16+
Region string
17+
SubscriberTier defangv1.SubscriptionTier
18+
Tenant string
19+
}
20+
21+
func showAccountInfo(showData ShowAccountData) (string, error) {
22+
if showData.Provider == "" {
23+
showData.Provider = "Defang"
24+
}
25+
outputText := "WhoAmI - \n\tProvider: " + showData.Provider
26+
27+
if showData.AccountID != "" {
28+
outputText += "\n\tAccountID: " + showData.AccountID
29+
}
30+
31+
if showData.Tenant != "" {
32+
outputText += "\n\tTenant: " + showData.Tenant
33+
}
34+
35+
outputText += "\n\tSubscription Tier: " + pkg.SubscriptionTierToString(showData.SubscriberTier)
36+
37+
if showData.Region != "" {
38+
outputText += "\n\tRegion: " + showData.Region
39+
}
40+
41+
if showData.Details != "" {
42+
outputText += "\n\tDetails: " + showData.Details
43+
}
44+
45+
return outputText, nil
46+
}
47+
1048
func Whoami(ctx context.Context, fabric client.FabricClient, provider client.Provider) (string, error) {
49+
showData := ShowAccountData{}
50+
1151
resp, err := fabric.WhoAmI(ctx)
1252
if err != nil {
1353
return "", err
@@ -18,23 +58,19 @@ func Whoami(ctx context.Context, fabric client.FabricClient, provider client.Pro
1858
return "", err
1959
}
2060

21-
accountID := resp.Account
2261
if account.AccountID() != "" {
23-
accountID = account.AccountID()
62+
showData.AccountID = account.AccountID()
2463
}
2564

26-
region := resp.Region
65+
showData.Region = resp.Region
2766
if account.Region() != "" {
28-
region = account.Region()
67+
showData.Region = account.Region()
2968
}
3069

31-
if account.Details() != "" {
32-
accountID += " (" + account.Details() + ")"
33-
}
70+
showData.Details = account.Details()
71+
showData.Provider = account.Provider().Name()
72+
showData.SubscriberTier = resp.Tier
73+
showData.Tenant = resp.Tenant
3474

35-
// TODO: Add provider name here?
36-
return fmt.Sprintf(
37-
"You are logged into %s region %s with tenant %q",
38-
accountID, region, resp.Tenant,
39-
), nil
75+
return showAccountInfo(showData)
4076
}

src/pkg/cli/whoami_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (g *grpcWhoamiMockHandler) WhoAmI(context.Context, *connect.Request[emptypb
2222
Tenant: "tenant-1",
2323
Account: "playground",
2424
Region: "us-test-2",
25+
Tier: defangv1.SubscriptionTier_PRO,
2526
}), nil
2627
}
2728

@@ -43,9 +44,9 @@ func TestWhoami(t *testing.T) {
4344
}
4445

4546
// Playground provider is hardcoded to return "us-west-2" as the region
46-
want := `You are logged into playground region us-west-2 with tenant "tenant-1"`
47+
want := "WhoAmI - \n\tProvider: Defang Playground\n\tTenant: tenant-1\n\tSubscription Tier: Pro\n\tRegion: us-west-2"
4748

4849
if got != want {
49-
t.Errorf("Whoami() = %v, want: %v", got, want)
50+
t.Errorf("Whoami() = %v, \nwant: %v", got, want)
5051
}
5152
}

src/pkg/utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strconv"
1111
"strings"
1212
"time"
13+
14+
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
1315
)
1416

1517
var (
@@ -114,3 +116,20 @@ func Contains[T comparable](s []T, v T) bool {
114116
}
115117
return false
116118
}
119+
120+
func SubscriptionTierToString(tier defangv1.SubscriptionTier) string {
121+
switch tier {
122+
case defangv1.SubscriptionTier_SUBSCRIPTION_TIER_UNSPECIFIED:
123+
return "Unknown"
124+
case defangv1.SubscriptionTier_PERSONAL:
125+
return "Free"
126+
case defangv1.SubscriptionTier_BASIC:
127+
return "Basic"
128+
case defangv1.SubscriptionTier_PRO:
129+
return "Pro"
130+
case defangv1.SubscriptionTier_TEAM:
131+
return "Team"
132+
default:
133+
return "Unknown"
134+
}
135+
}

0 commit comments

Comments
 (0)