/
legal-entity.go
117 lines (92 loc) · 3.15 KB
/
legal-entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package get
import (
"context"
"fmt"
awsv1alpha1 "github.com/openshift/aws-account-operator/api/v1alpha1"
outputflag "github.com/openshift/osdctl/cmd/getoutput"
"github.com/openshift/osdctl/internal/utils/globalflags"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/openshift/osdctl/cmd/common"
)
// newCmdGetLegalEntity implements the get legal-entity command which get
// the legal entity information related to the specified AWS Account ID
func newCmdGetLegalEntity(streams genericclioptions.IOStreams, client client.Client, globalOpts *globalflags.GlobalOptions) *cobra.Command {
ops := newGetLegalEntityOptions(streams, client, globalOpts)
getLegalEntityCmd := &cobra.Command{
Use: "legal-entity",
Short: "Get AWS Account Legal Entity",
Args: cobra.NoArgs,
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(ops.complete(cmd, args))
cmdutil.CheckErr(ops.run())
},
}
getLegalEntityCmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", common.AWSAccountNamespace,
"The namespace to keep AWS accounts. The default value is aws-account-operator.")
getLegalEntityCmd.Flags().StringVarP(&ops.accountID, "account-id", "i", "", "AWS account ID")
return getLegalEntityCmd
}
// getLegalEntityOptions defines the struct for running get legal-entity command
type getLegalEntityOptions struct {
accountID string
accountNamespace string
output string
genericclioptions.IOStreams
kubeCli client.Client
GlobalOptions *globalflags.GlobalOptions
}
type legalEntityResponse struct {
Name string `json:"name" yaml:"name"`
Id string `json:"id" yaml:"id"`
}
func (f legalEntityResponse) String() string {
return fmt.Sprintf(" Name: %s\n Id: %s\n", f.Name, f.Id)
}
func newGetLegalEntityOptions(streams genericclioptions.IOStreams, client client.Client, globalOpts *globalflags.GlobalOptions) *getLegalEntityOptions {
return &getLegalEntityOptions{
IOStreams: streams,
kubeCli: client,
GlobalOptions: globalOpts,
}
}
func (o *getLegalEntityOptions) complete(cmd *cobra.Command, _ []string) error {
if o.accountID == "" {
return cmdutil.UsageErrorf(cmd, accountIDRequired)
}
o.output = o.GlobalOptions.Output
return nil
}
func (o *getLegalEntityOptions) run() error {
ctx := context.TODO()
var (
accounts awsv1alpha1.AccountList
)
if err := o.kubeCli.List(ctx, &accounts, &client.ListOptions{
Namespace: o.accountNamespace,
}); err != nil {
return err
}
for _, account := range accounts.Items {
if account.Spec.AwsAccountID == o.accountID {
resp := legalEntityResponse{
Name: account.Spec.LegalEntity.Name,
Id: account.Spec.LegalEntity.ID,
}
err := outputflag.PrintResponse(o.output, resp)
if err != nil {
fmt.Println("Error while printing response: ", err.Error())
return err
}
}
}
// matched account not found
_, err := fmt.Fprintf(o.IOStreams.Out, "Account matched for AWS Account ID %s not found\n", o.accountID)
if err != nil {
return err
}
return nil
}