Skip to content

Commit 4da9faa

Browse files
committed
Add a new flag to match account claim by the given account cr name
Signed-off-by: Ben Ye <yb532204897@gmail.com>
1 parent 8478e80 commit 4da9faa

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ osdctl account check-secrets <Account CR Name>
138138
# Get Account Name by AWS Account ID, output to json
139139
osdctl account get account -i <Account ID> -o json
140140

141+
# Get Account Claim CR by Account CR Name
142+
osdctl account get account-claim -a <Account CR Name>
143+
141144
# Get Account Claim CR by AWS Account ID, output to yaml
142145
osdctl account get account-claim -i <Account ID> -o yaml
143146

cmd/account/get/account-claim.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ func newCmdGetAccountClaim(streams genericclioptions.IOStreams, flags *genericcl
3535
getAccountClaimCmd.Flags().StringVarP(&ops.output, "output", "o", "", "Output format. One of: json|yaml|jsonpath=...|jsonpath-file=... see jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].")
3636
getAccountClaimCmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", common.AWSAccountNamespace,
3737
"The namespace to keep AWS accounts. The default value is aws-account-operator.")
38+
getAccountClaimCmd.Flags().StringVarP(&ops.accountName, "account", "a", "", "Account CR Name")
3839
getAccountClaimCmd.Flags().StringVarP(&ops.accountID, "account-id", "i", "", "AWS account ID")
3940

4041
return getAccountClaimCmd
4142
}
4243

4344
// getAccountClaimOptions defines the struct for running get account-claim command
4445
type getAccountClaimOptions struct {
46+
accountName string
4547
accountID string
4648
accountNamespace string
4749

@@ -62,8 +64,11 @@ func newGetAccountClaimOptions(streams genericclioptions.IOStreams, flags *gener
6264
}
6365

6466
func (o *getAccountClaimOptions) complete(cmd *cobra.Command, _ []string) error {
65-
if o.accountID == "" {
66-
return cmdutil.UsageErrorf(cmd, accountIDRequired)
67+
if o.accountID == "" && o.accountName == "" {
68+
return cmdutil.UsageErrorf(cmd, "AWS account ID and Account CR Name cannot be empty at the same time")
69+
}
70+
if o.accountID != "" && o.accountName != "" {
71+
return cmdutil.UsageErrorf(cmd, "AWS account ID and Account CR Name cannot be set at the same time")
6772
}
6873

6974
var err error
@@ -85,20 +90,25 @@ func (o *getAccountClaimOptions) run() error {
8590
accountClaimName string
8691
accountClaim awsv1alpha1.AccountClaim
8792
)
88-
if err := o.kubeCli.List(ctx, &accounts, &client.ListOptions{
89-
Namespace: o.accountNamespace,
90-
}); err != nil {
91-
return nil
92-
}
9393

94-
for _, a := range accounts.Items {
95-
if a.Spec.AwsAccountID == o.accountID {
96-
accountCRName = a.Name
97-
break
94+
if o.accountName != "" {
95+
accountCRName = o.accountName
96+
} else {
97+
if err := o.kubeCli.List(ctx, &accounts, &client.ListOptions{
98+
Namespace: o.accountNamespace,
99+
}); err != nil {
100+
return nil
101+
}
102+
103+
for _, a := range accounts.Items {
104+
if a.Spec.AwsAccountID == o.accountID {
105+
accountCRName = a.Name
106+
break
107+
}
108+
}
109+
if accountCRName == "" {
110+
return fmt.Errorf("Account matched for AWS Account ID %s not found\n", o.accountID)
98111
}
99-
}
100-
if accountCRName == "" {
101-
return fmt.Errorf("Account matched for AWS Account ID %s not found\n", o.accountID)
102112
}
103113

104114
if err := o.kubeCli.List(ctx, &accountClaims); err != nil {

cmd/account/get/account-claim_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ func TestGetAccountClaimCmdComplete(t *testing.T) {
2121
errContent string
2222
}{
2323
{
24-
title: "empty account ID",
24+
title: "account id and account cr name empty at the same time",
2525
option: &getAccountClaimOptions{
26-
accountID: "",
26+
accountID: "",
27+
accountName: "",
2728
},
2829
errExpected: true,
29-
errContent: accountIDRequired,
30+
errContent: "AWS account ID and Account CR Name cannot be empty at the same time",
31+
},
32+
{
33+
title: "account id and account cr name set at the same time",
34+
option: &getAccountClaimOptions{
35+
accountID: "foo",
36+
accountName: "bar",
37+
},
38+
errExpected: true,
39+
errContent: "AWS account ID and Account CR Name cannot be set at the same time",
3040
},
3141
{
3242
title: "succeed",

docs/command/osdctl_account_get_account-claim.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ osdctl account get account-claim [flags]
1313
### Options
1414

1515
```
16+
-a, --account string Account CR Name
1617
-i, --account-id string AWS account ID
1718
--account-namespace string The namespace to keep AWS accounts. The default value is aws-account-operator. (default "aws-account-operator")
1819
-h, --help help for account-claim

0 commit comments

Comments
 (0)