Skip to content

Commit a4fcaad

Browse files
committed
support list account claims
Signed-off-by: Ben Ye <yb532204897@gmail.com>
1 parent 8478e80 commit a4fcaad

File tree

9 files changed

+242
-32
lines changed

9 files changed

+242
-32
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ osdctl account set test-cr --patch='{"status":{"state": "Failed", "claimed": fal
7878

7979
### AWS Account CR list
8080

81-
`list` command lists the Account CRs in the cluster. You can use flags to filter the status.
81+
`list account` command lists the Account CRs in the cluster. You can use flags to filter the status.
8282

8383
```bash
84-
osdctl account list --state=Creating
84+
osdctl account list account --state=Creating
8585

8686
Name State AWS ACCOUNT ID Last Probe Time Last Transition Time Message
8787
test-cr Creating 181787396432 2020-06-18 10:38:40 -0400 EDT 2020-06-18 10:38:40 -0400 EDT AWS account already created
@@ -91,6 +91,15 @@ osdctl account list --reuse=true --claim=false
9191

9292
# custom output using jsonpath
9393
osdctl account list -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.awsAccountID}{"\t"}{.status.state}{"\n"}{end}'
94+
test-cr Creating 111111111111 2020-06-18 10:38:40 -0400 EDT 2020-06-18 10:38:40 -0400 EDT AWS account already created
95+
```
96+
97+
### AWS Account Claim CR list
98+
99+
`list account-claim` command lists the Account Claim CRs in the cluster. You can use flags to filter the status.
100+
101+
```bash
102+
osdctl account list account-claim --state=Ready
94103
```
95104

96105
### AWS Account Console URL generate

cmd/account/cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"k8s.io/cli-runtime/pkg/genericclioptions"
66

77
"github.com/openshift/osd-utils-cli/cmd/account/get"
8+
"github.com/openshift/osd-utils-cli/cmd/account/list"
89
)
910

1011
// NewCmdAccount implements the base account command
@@ -18,11 +19,11 @@ func NewCmdAccount(streams genericclioptions.IOStreams, flags *genericclioptions
1819
}
1920

2021
accountCmd.AddCommand(get.NewCmdGet(streams, flags))
22+
accountCmd.AddCommand(list.NewCmdList(streams, flags))
2123
accountCmd.AddCommand(newCmdReset(streams, flags))
2224
accountCmd.AddCommand(newCmdSet(streams, flags))
2325
accountCmd.AddCommand(newCmdConsole(streams, flags))
2426
accountCmd.AddCommand(newCmdCli(streams, flags))
25-
accountCmd.AddCommand(newCmdList(streams, flags))
2627
accountCmd.AddCommand(newCmdCleanVeleroSnapshots(streams))
2728
accountCmd.AddCommand(newCmdCheckSecrets(streams, flags))
2829
accountCmd.AddCommand(newCmdRotateSecret(streams, flags))

cmd/account/list/account-claim.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package list
2+
3+
import (
4+
"context"
5+
6+
awsv1alpha1 "github.com/openshift/aws-account-operator/pkg/apis/aws/v1alpha1"
7+
"github.com/spf13/cobra"
8+
9+
"k8s.io/cli-runtime/pkg/genericclioptions"
10+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
13+
"github.com/openshift/osd-utils-cli/pkg/k8s"
14+
"github.com/openshift/osd-utils-cli/pkg/printer"
15+
)
16+
17+
// newCmdListAccount implements the list account command to list account claim crs
18+
func newCmdListAccountClaim(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
19+
ops := newListAccountClaimOptions(streams, flags)
20+
listAccountClaimCmd := &cobra.Command{
21+
Use: "account-claim",
22+
Short: "List AWS Account Claim CR",
23+
Args: cobra.NoArgs,
24+
DisableAutoGenTag: true,
25+
Run: func(cmd *cobra.Command, args []string) {
26+
cmdutil.CheckErr(ops.complete(cmd, args))
27+
cmdutil.CheckErr(ops.run())
28+
},
29+
}
30+
31+
listAccountClaimCmd.Flags().StringVar(&ops.state, "state", "", "Account cr state. If not specified, it will list all crs by default.")
32+
33+
return listAccountClaimCmd
34+
}
35+
36+
// listAccountOptions defines the struct for running list account command
37+
type listAccountClaimOptions struct {
38+
state string
39+
40+
flags *genericclioptions.ConfigFlags
41+
genericclioptions.IOStreams
42+
kubeCli client.Client
43+
}
44+
45+
func newListAccountClaimOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *listAccountClaimOptions {
46+
return &listAccountClaimOptions{
47+
flags: flags,
48+
IOStreams: streams,
49+
}
50+
}
51+
52+
func (o *listAccountClaimOptions) complete(cmd *cobra.Command, _ []string) error {
53+
switch o.state {
54+
// state doesn't set, continue
55+
case "":
56+
57+
// valid value, continue
58+
case "Error", "Pending", "Ready":
59+
60+
// throw error
61+
default:
62+
return cmdutil.UsageErrorf(cmd, "unsupported account claim state "+o.state)
63+
}
64+
65+
var err error
66+
o.kubeCli, err = k8s.NewClient(o.flags)
67+
if err != nil {
68+
return err
69+
}
70+
71+
return nil
72+
}
73+
74+
func (o *listAccountClaimOptions) run() error {
75+
ctx := context.TODO()
76+
var claims awsv1alpha1.AccountClaimList
77+
if err := o.kubeCli.List(ctx, &claims, &client.ListOptions{}); err != nil {
78+
return err
79+
}
80+
81+
var matched bool
82+
p := printer.NewTablePrinter(o.IOStreams.Out, 20, 1, 3, ' ')
83+
p.AddRow([]string{"Namespace", "Name", "State", "Account", "AWS OU"})
84+
for _, claim := range claims.Items {
85+
if o.state != "" && string(claim.Status.State) != o.state {
86+
continue
87+
}
88+
89+
p.AddRow([]string{
90+
claim.Namespace,
91+
claim.Name,
92+
string(claim.Status.State),
93+
claim.Spec.AccountLink,
94+
claim.Spec.AccountOU,
95+
})
96+
97+
// this is used to mark whether there are matched accounts or not
98+
matched = true
99+
}
100+
101+
if matched {
102+
return p.Flush()
103+
}
104+
return nil
105+
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package account
1+
package list
22

33
import (
44
"context"
@@ -18,11 +18,11 @@ import (
1818
"github.com/openshift/osd-utils-cli/pkg/printer"
1919
)
2020

21-
// newCmdList implements the list command to list account crs
22-
func newCmdList(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
23-
ops := newListOptions(streams, flags)
24-
listCmd := &cobra.Command{
25-
Use: "list",
21+
// newCmdListAccount implements the list account command to list account crs
22+
func newCmdListAccount(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
23+
ops := newListAccountOptions(streams, flags)
24+
listAccountCmd := &cobra.Command{
25+
Use: "account",
2626
Short: "List AWS Account CR",
2727
Args: cobra.NoArgs,
2828
DisableAutoGenTag: true,
@@ -32,21 +32,21 @@ func newCmdList(streams genericclioptions.IOStreams, flags *genericclioptions.Co
3232
},
3333
}
3434

35-
ops.printFlags.AddFlags(listCmd)
36-
listCmd.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].")
37-
listCmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", common.AWSAccountNamespace,
35+
ops.printFlags.AddFlags(listAccountCmd)
36+
listAccountCmd.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].")
37+
listAccountCmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", common.AWSAccountNamespace,
3838
"The namespace to keep AWS accounts. The default value is aws-account-operator.")
39-
listCmd.Flags().StringVarP(&ops.reused, "reuse", "r", "",
39+
listAccountCmd.Flags().StringVarP(&ops.reused, "reuse", "r", "",
4040
"Filter account CRs by reused or not. Supported values are true, false. Otherwise it lists all accounts")
41-
listCmd.Flags().StringVarP(&ops.claimed, "claim", "c", "",
41+
listAccountCmd.Flags().StringVarP(&ops.claimed, "claim", "c", "",
4242
"Filter account CRs by claimed or not. Supported values are true, false. Otherwise it lists all accounts")
43-
listCmd.Flags().StringVar(&ops.state, "state", "all", "Account cr state. The default value is all to display all the crs")
43+
listAccountCmd.Flags().StringVar(&ops.state, "state", "all", "Account cr state. The default value is all to display all the crs")
4444

45-
return listCmd
45+
return listAccountCmd
4646
}
4747

48-
// listOptions defines the struct for running list command
49-
type listOptions struct {
48+
// listAccountOptions defines the struct for running list account command
49+
type listAccountOptions struct {
5050
accountNamespace string
5151

5252
reused string
@@ -61,15 +61,15 @@ type listOptions struct {
6161
kubeCli client.Client
6262
}
6363

64-
func newListOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *listOptions {
65-
return &listOptions{
64+
func newListAccountOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *listAccountOptions {
65+
return &listAccountOptions{
6666
flags: flags,
6767
printFlags: printer.NewPrintFlags(),
6868
IOStreams: streams,
6969
}
7070
}
7171

72-
func (o *listOptions) complete(cmd *cobra.Command, _ []string) error {
72+
func (o *listAccountOptions) complete(cmd *cobra.Command, _ []string) error {
7373
switch o.state {
7474
// display all the crs
7575
case "all":
@@ -104,7 +104,7 @@ func (o *listOptions) complete(cmd *cobra.Command, _ []string) error {
104104
return nil
105105
}
106106

107-
func (o *listOptions) run() error {
107+
func (o *listAccountOptions) run() error {
108108
ctx := context.TODO()
109109

110110
var (

cmd/account/list/cmd.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package list
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"k8s.io/cli-runtime/pkg/genericclioptions"
6+
)
7+
8+
// NewCmdGet implements the get command to get AWS Account related resources
9+
func NewCmdList(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
10+
listCmd := &cobra.Command{
11+
Use: "list",
12+
Short: "List resources",
13+
Args: cobra.NoArgs,
14+
DisableAutoGenTag: true,
15+
Run: help,
16+
}
17+
18+
listCmd.AddCommand(newCmdListAccount(streams, flags))
19+
listCmd.AddCommand(newCmdListAccountClaim(streams, flags))
20+
21+
return listCmd
22+
}
23+
24+
func help(cmd *cobra.Command, _ []string) {
25+
cmd.Help()
26+
}

docs/command/osdctl_account.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ osdctl account [flags]
3535
* [osdctl account cli](osdctl_account_cli.md) - Generate temporary AWS CLI credentials on demand
3636
* [osdctl account console](osdctl_account_console.md) - Generate an AWS console URL on the fly
3737
* [osdctl account get](osdctl_account_get.md) - get resources
38-
* [osdctl account list](osdctl_account_list.md) - List AWS Account CR
38+
* [osdctl account list](osdctl_account_list.md) - List resources
3939
* [osdctl account reset](osdctl_account_reset.md) - Reset AWS Account CR
4040
* [osdctl account rotate-secret](osdctl_account_rotate-secret.md) - Rotate IAM credentials secret
4141
* [osdctl account set](osdctl_account_set.md) - Set AWS Account CR status

docs/command/osdctl_account_list.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## osdctl account list
22

3-
List AWS Account CR
3+
List resources
44

55
### Synopsis
66

7-
List AWS Account CR
7+
List resources
88

99
```
1010
osdctl account list [flags]
@@ -13,13 +13,7 @@ osdctl account list [flags]
1313
### Options
1414

1515
```
16-
--account-namespace string The namespace to keep AWS accounts. The default value is aws-account-operator. (default "aws-account-operator")
17-
-c, --claim string Filter account CRs by claimed or not. Supported values are true, false. Otherwise it lists all accounts
18-
-h, --help help for list
19-
-o, --output string Output format. One of: json|yaml|jsonpath=...|jsonpath-file=... see jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
20-
-r, --reuse string Filter account CRs by reused or not. Supported values are true, false. Otherwise it lists all accounts
21-
--state string Account cr state. The default value is all to display all the crs (default "all")
22-
--template string Template string or path to template file to use when --output=jsonpath, --output=jsonpath-file.
16+
-h, --help help for list
2317
```
2418

2519
### Options inherited from parent commands
@@ -36,4 +30,6 @@ osdctl account list [flags]
3630
### SEE ALSO
3731

3832
* [osdctl account](osdctl_account.md) - AWS Account related utilities
33+
* [osdctl account list account](osdctl_account_list_account.md) - List AWS Account CR
34+
* [osdctl account list account-claim](osdctl_account_list_account-claim.md) - List AWS Account Claim CR
3935

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## osdctl account list account-claim
2+
3+
List AWS Account Claim CR
4+
5+
### Synopsis
6+
7+
List AWS Account Claim CR
8+
9+
```
10+
osdctl account list account-claim [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help help for account-claim
17+
--state string Account cr state. If not specified, it will list all crs by default.
18+
```
19+
20+
### Options inherited from parent commands
21+
22+
```
23+
--cluster string The name of the kubeconfig cluster to use
24+
--context string The name of the kubeconfig context to use
25+
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
26+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
27+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
28+
-s, --server string The address and port of the Kubernetes API server
29+
```
30+
31+
### SEE ALSO
32+
33+
* [osdctl account list](osdctl_account_list.md) - List resources
34+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## osdctl account list account
2+
3+
List AWS Account CR
4+
5+
### Synopsis
6+
7+
List AWS Account CR
8+
9+
```
10+
osdctl account list account [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
--account-namespace string The namespace to keep AWS accounts. The default value is aws-account-operator. (default "aws-account-operator")
17+
-c, --claim string Filter account CRs by claimed or not. Supported values are true, false. Otherwise it lists all accounts
18+
-h, --help help for account
19+
-o, --output string Output format. One of: json|yaml|jsonpath=...|jsonpath-file=... see jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
20+
-r, --reuse string Filter account CRs by reused or not. Supported values are true, false. Otherwise it lists all accounts
21+
--state string Account cr state. The default value is all to display all the crs (default "all")
22+
--template string Template string or path to template file to use when --output=jsonpath, --output=jsonpath-file.
23+
```
24+
25+
### Options inherited from parent commands
26+
27+
```
28+
--cluster string The name of the kubeconfig cluster to use
29+
--context string The name of the kubeconfig context to use
30+
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
31+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
32+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
33+
-s, --server string The address and port of the Kubernetes API server
34+
```
35+
36+
### SEE ALSO
37+
38+
* [osdctl account list](osdctl_account_list.md) - List resources
39+

0 commit comments

Comments
 (0)