@@ -2,6 +2,8 @@ package account
22
33import (
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
4249type 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
5364func 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
6072func (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
83107func (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 }
0 commit comments