Skip to content

Commit 2250093

Browse files
committed
add more display fields for account list
Signed-off-by: yeya24 <yb532204897@gmail.com>
1 parent 9a78969 commit 2250093

File tree

5 files changed

+155
-20
lines changed

5 files changed

+155
-20
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ osdctl account list --state=Creating
8080

8181
Name State AWS ACCOUNT ID Last Probe Time Last Transition Time Message
8282
test-cr Creating 181787396432 2020-06-18 10:38:40 -0400 EDT 2020-06-18 10:38:40 -0400 EDT AWS account already created
83+
84+
# filter accounts by reused or claimed status
85+
osdctl account list --reuse=true --claim=false
86+
87+
# custom output using jsonpath
88+
osdctl account list -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.awsAccountID}{"\t"}{.status.state}{"\n"}{end}'
8389
```
8490

8591
### AWS Account Console URL generate

cmd/account/list.go

Lines changed: 95 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package account
22

33
import (
44
"context"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
"k8s.io/cli-runtime/pkg/printers"
57
"time"
68

79
awsv1alpha1 "github.com/openshift/aws-account-operator/pkg/apis/aws/v1alpha1"
@@ -30,10 +32,15 @@ func newCmdList(streams genericclioptions.IOStreams, flags *genericclioptions.Co
3032
},
3133
}
3234

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].")
3337
listCmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", common.AWSAccountNamespace,
3438
"The namespace to keep AWS accounts. The default value is aws-account-operator.")
35-
listCmd.Flags().BoolVarP(&ops.reused, "reuse", "r", false, "Only list reused accounts CR if true")
36-
listCmd.Flags().StringVar(&ops.state, "state", "", "Account cr state. If not specified, it will list all crs by default.")
39+
listCmd.Flags().StringVarP(&ops.reused, "reuse", "r", "",
40+
"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", "",
42+
"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")
3744

3845
return listCmd
3946
}
@@ -42,35 +49,52 @@ func newCmdList(streams genericclioptions.IOStreams, flags *genericclioptions.Co
4249
type listOptions struct {
4350
accountNamespace string
4451

45-
reused bool
46-
state string
52+
reused string
53+
claimed string
54+
state string
4755

48-
flags *genericclioptions.ConfigFlags
56+
output string
57+
58+
flags *genericclioptions.ConfigFlags
59+
printFlags *printer.PrintFlags
4960
genericclioptions.IOStreams
5061
kubeCli client.Client
5162
}
5263

5364
func newListOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *listOptions {
5465
return &listOptions{
55-
flags: flags,
56-
IOStreams: streams,
66+
flags: flags,
67+
printFlags: printer.NewPrintFlags(),
68+
IOStreams: streams,
5769
}
5870
}
5971

6072
func (o *listOptions) complete(cmd *cobra.Command, _ []string) error {
6173
switch o.state {
62-
// state doesn't set, continue
63-
case "":
74+
// display all the crs
75+
case "all":
6476

6577
// valid value, continue
6678
case "Creating", "Pending", "PendingVerification",
67-
"Failed", "Ready":
79+
"Failed", "Ready", "":
6880

6981
// throw error
7082
default:
7183
return cmdutil.UsageErrorf(cmd, "unsupported account state "+o.state)
7284
}
7385

86+
switch o.reused {
87+
case "", "true", "false":
88+
default:
89+
return cmdutil.UsageErrorf(cmd, "unsupported reused status filter "+o.reused)
90+
}
91+
92+
switch o.claimed {
93+
case "", "true", "false":
94+
default:
95+
return cmdutil.UsageErrorf(cmd, "unsupported claimed status filter "+o.claimed)
96+
}
97+
7498
var err error
7599
o.kubeCli, err = k8s.NewClient(o.flags)
76100
if err != nil {
@@ -82,21 +106,69 @@ func (o *listOptions) complete(cmd *cobra.Command, _ []string) error {
82106

83107
func (o *listOptions) run() error {
84108
ctx := context.TODO()
85-
var accounts awsv1alpha1.AccountList
109+
110+
var (
111+
accounts awsv1alpha1.AccountList
112+
outputAccounts awsv1alpha1.AccountList
113+
resourcePrinter printers.ResourcePrinter
114+
matched bool
115+
reused bool
116+
claimed bool
117+
err error
118+
)
119+
if o.reused != "" {
120+
if o.reused == "true" {
121+
reused = true
122+
}
123+
}
124+
125+
if o.claimed != "" {
126+
if o.claimed == "true" {
127+
claimed = true
128+
}
129+
}
130+
86131
if err := o.kubeCli.List(ctx, &accounts, &client.ListOptions{
87132
Namespace: o.accountNamespace}); err != nil {
88133
return err
89134
}
90135

91-
var matched bool
136+
if o.output != "" {
137+
outputAccounts = awsv1alpha1.AccountList{
138+
TypeMeta: metav1.TypeMeta{
139+
APIVersion: "v1",
140+
Kind: "List",
141+
},
142+
Items: make([]awsv1alpha1.Account, 0),
143+
}
144+
145+
resourcePrinter, err = o.printFlags.ToPrinter(o.output)
146+
if err != nil {
147+
return err
148+
}
149+
}
150+
92151
p := printer.NewTablePrinter(o.IOStreams.Out, 20, 1, 3, ' ')
93152
p.AddRow([]string{"Name", "State", "AWS ACCOUNT ID", "Last Probe Time", "Last Transition Time", "Message"})
153+
94154
for _, account := range accounts.Items {
95-
if o.reused != account.Status.Reused {
155+
if o.claimed != "" {
156+
if account.Status.Claimed != claimed {
157+
continue
158+
}
159+
}
160+
if o.reused != "" {
161+
if account.Status.Reused != reused {
162+
continue
163+
}
164+
}
165+
166+
if o.state != "all" && account.Status.State != o.state {
96167
continue
97168
}
98169

99-
if o.state != "" && account.Status.State != o.state {
170+
if o.output != "" {
171+
outputAccounts.Items = append(outputAccounts.Items, account)
100172
continue
101173
}
102174

@@ -111,19 +183,26 @@ func (o *listOptions) run() error {
111183
lastTransitionTime = account.Status.Conditions[conditionLen-1].LastTransitionTime.Time
112184
message = account.Status.Conditions[conditionLen-1].Message
113185
}
114-
p.AddRow([]string{
186+
187+
rows := []string{
115188
account.Name,
116189
account.Status.State,
117190
account.Spec.AwsAccountID,
118191
lastProbeTime.String(),
119192
lastTransitionTime.String(),
120193
message,
121-
})
194+
}
195+
196+
p.AddRow(rows)
122197

123198
// this is used to mark whether there are matched accounts or not
124199
matched = true
125200
}
126201

202+
if o.output != "" {
203+
return resourcePrinter.PrintObj(&outputAccounts, o.Out)
204+
}
205+
127206
if matched {
128207
return p.Flush()
129208
}

docs/command/osdctl_account_list.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ osdctl account list [flags]
1414

1515
```
1616
--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
1718
-h, --help help for list
18-
-r, --reuse Only list reused accounts CR if true
19-
--state string Account cr state. If not specified, it will list all crs by default.
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.
2023
```
2124

2225
### Options inherited from parent commands

pkg/printer/print_flags.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package printer
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"k8s.io/cli-runtime/pkg/genericclioptions"
7+
"k8s.io/cli-runtime/pkg/printers"
8+
)
9+
10+
type PrintFlags struct {
11+
JSONYamlFlags *genericclioptions.JSONYamlPrintFlags
12+
JSONPathFlags *genericclioptions.JSONPathPrintFlags
13+
}
14+
15+
func NewPrintFlags() *PrintFlags {
16+
template := ""
17+
return &PrintFlags{
18+
JSONYamlFlags: genericclioptions.NewJSONYamlPrintFlags(),
19+
JSONPathFlags: &genericclioptions.JSONPathPrintFlags{TemplateArgument: &template},
20+
}
21+
}
22+
23+
func (p *PrintFlags) AddFlags(c *cobra.Command) {
24+
p.JSONYamlFlags.AddFlags(c)
25+
p.JSONPathFlags.AddFlags(c)
26+
}
27+
28+
func (p *PrintFlags) ToPrinter(output string) (printers.ResourcePrinter, error) {
29+
if p, err := p.JSONYamlFlags.ToPrinter(output); !genericclioptions.IsNoCompatiblePrinterError(err) {
30+
return p, err
31+
}
32+
33+
if p, err := p.JSONPathFlags.ToPrinter(output); !genericclioptions.IsNoCompatiblePrinterError(err) {
34+
return p, err
35+
}
36+
37+
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &output, AllowedFormats: p.AllowedFormats()}
38+
}
39+
40+
// AllowedFormats is the list of formats in which data can be displayed
41+
func (p *PrintFlags) AllowedFormats() []string {
42+
formats := p.JSONYamlFlags.AllowedFormats()
43+
formats = append(formats, p.JSONPathFlags.AllowedFormats()...)
44+
return formats
45+
}

pkg/printer/printer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package printer
33
import (
44
"encoding/json"
55
"fmt"
6-
"gopkg.in/yaml.v2"
76
"io"
8-
"k8s.io/apimachinery/pkg/runtime"
97
"os"
108
"strings"
119
"text/tabwriter"
10+
11+
"gopkg.in/yaml.v2"
12+
13+
"k8s.io/apimachinery/pkg/runtime"
1214
)
1315

1416
// printer use to output something on screen with table format.

0 commit comments

Comments
 (0)