Skip to content

Commit

Permalink
Merge pull request #166 from iamkirkbater/fix-output
Browse files Browse the repository at this point in the history
Fix output
  • Loading branch information
openshift-merge-robot authored Dec 14, 2021
2 parents 040133d + 66f327f commit 5734e6b
Show file tree
Hide file tree
Showing 27 changed files with 344 additions and 248 deletions.
21 changes: 12 additions & 9 deletions cmd/account/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/spf13/cobra"

"github.com/openshift/osdctl/internal/utils/globalflags"
k8spkg "github.com/openshift/osdctl/pkg/k8s"
awsprovider "github.com/openshift/osdctl/pkg/provider/aws"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

k8spkg "github.com/openshift/osdctl/pkg/k8s"
awsprovider "github.com/openshift/osdctl/pkg/provider/aws"
)

// newCmdCli implements the Cli command which generates temporary STS cli credentials for the specified account cr
func newCmdCli(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
ops := newCliOptions(streams, flags)
func newCmdCli(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, globalOpts *globalflags.GlobalOptions) *cobra.Command {
ops := newCliOptions(streams, flags, globalOpts)
cliCmd := &cobra.Command{
Use: "cli",
Short: "Generate temporary AWS CLI credentials on demand",
Expand All @@ -30,7 +30,6 @@ func newCmdCli(streams genericclioptions.IOStreams, flags *genericclioptions.Con

ops.k8sclusterresourcefactory.AttachCobraCliFlags(cliCmd)

cliCmd.Flags().StringVarP(&ops.output, "out", "o", "default", "Output format [default | json | env]")
cliCmd.Flags().BoolVarP(&ops.verbose, "verbose", "", false, "Verbose output")

return cliCmd
Expand All @@ -44,14 +43,16 @@ type cliOptions struct {
verbose bool

genericclioptions.IOStreams
GlobalOptions *globalflags.GlobalOptions
}

func newCliOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cliOptions {
func newCliOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, globalOpts *globalflags.GlobalOptions) *cliOptions {
return &cliOptions{
k8sclusterresourcefactory: k8spkg.ClusterResourceFactoryOptions{
Flags: flags,
},
IOStreams: streams,
IOStreams: streams,
GlobalOptions: globalOpts,
}
}

Expand All @@ -70,6 +71,8 @@ func (o *cliOptions) complete(cmd *cobra.Command) error {
}
}

o.output = o.GlobalOptions.Output

return nil
}

Expand All @@ -94,7 +97,7 @@ func (o *cliOptions) run() error {
}
}

if o.output == "default" {
if o.output == "" {
fmt.Fprintf(o.IOStreams.Out, "Temporary AWS Credentials:\n%s\n", creds)
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/account/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/openshift/osdctl/cmd/account/list"
"github.com/openshift/osdctl/cmd/account/mgmt"
"github.com/openshift/osdctl/cmd/account/servicequotas"
"github.com/openshift/osdctl/internal/utils/globalflags"
)

// NewCmdAccount implements the base account command
func NewCmdAccount(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client) *cobra.Command {
func NewCmdAccount(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client, globalOpts *globalflags.GlobalOptions) *cobra.Command {
accountCmd := &cobra.Command{
Use: "account",
Short: "AWS Account related utilities",
Expand All @@ -21,14 +22,14 @@ func NewCmdAccount(streams genericclioptions.IOStreams, flags *genericclioptions
Run: help,
}

accountCmd.AddCommand(get.NewCmdGet(streams, flags, client))
accountCmd.AddCommand(list.NewCmdList(streams, flags, client))
accountCmd.AddCommand(get.NewCmdGet(streams, flags, client, globalOpts))
accountCmd.AddCommand(list.NewCmdList(streams, flags, client, globalOpts))
accountCmd.AddCommand(servicequotas.NewCmdServiceQuotas(streams, flags))
accountCmd.AddCommand(mgmt.NewCmdMgmt(streams, flags))
accountCmd.AddCommand(mgmt.NewCmdMgmt(streams, flags, globalOpts))
accountCmd.AddCommand(newCmdReset(streams, flags, client))
accountCmd.AddCommand(newCmdSet(streams, flags, client))
accountCmd.AddCommand(newCmdConsole(streams, flags))
accountCmd.AddCommand(newCmdCli(streams, flags))
accountCmd.AddCommand(newCmdCli(streams, flags, globalOpts))
accountCmd.AddCommand(newCmdCleanVeleroSnapshots(streams))
accountCmd.AddCommand(newCmdVerifySecrets(streams, flags, client))
accountCmd.AddCommand(newCmdRotateSecret(streams, flags, client))
Expand Down
26 changes: 14 additions & 12 deletions cmd/account/get/account-claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
awsv1alpha1 "github.com/openshift/aws-account-operator/pkg/apis/aws/v1alpha1"
"github.com/spf13/cobra"

"github.com/openshift/osdctl/cmd/common"
"github.com/openshift/osdctl/internal/utils/globalflags"
"github.com/openshift/osdctl/pkg/printer"
"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"
"github.com/openshift/osdctl/pkg/printer"
)

// newCmdGetAccountClaim implements the get account-claim command which get
// the Account Claim CR related to the specified AWS Account ID
func newCmdGetAccountClaim(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client) *cobra.Command {
ops := newGetAccountClaimOptions(streams, flags, client)
func newCmdGetAccountClaim(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client, globalOpts *globalflags.GlobalOptions) *cobra.Command {
ops := newGetAccountClaimOptions(streams, flags, client, globalOpts)
getAccountClaimCmd := &cobra.Command{
Use: "account-claim",
Short: "Get AWS Account Claim CR",
Expand All @@ -31,7 +31,6 @@ func newCmdGetAccountClaim(streams genericclioptions.IOStreams, flags *genericcl
}

ops.printFlags.AddFlags(getAccountClaimCmd)
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].")
getAccountClaimCmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", common.AWSAccountNamespace,
"The namespace to keep AWS accounts. The default value is aws-account-operator.")
getAccountClaimCmd.Flags().StringVarP(&ops.accountName, "account", "a", "", "Account CR Name")
Expand All @@ -51,15 +50,17 @@ type getAccountClaimOptions struct {
flags *genericclioptions.ConfigFlags
printFlags *printer.PrintFlags
genericclioptions.IOStreams
kubeCli client.Client
kubeCli client.Client
GlobalOptions *globalflags.GlobalOptions
}

func newGetAccountClaimOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client) *getAccountClaimOptions {
func newGetAccountClaimOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client, globalOpts *globalflags.GlobalOptions) *getAccountClaimOptions {
return &getAccountClaimOptions{
flags: flags,
printFlags: printer.NewPrintFlags(),
IOStreams: streams,
kubeCli: client,
flags: flags,
printFlags: printer.NewPrintFlags(),
IOStreams: streams,
kubeCli: client,
GlobalOptions: globalOpts,
}
}

Expand All @@ -71,6 +72,7 @@ func (o *getAccountClaimOptions) complete(cmd *cobra.Command, _ []string) error
return cmdutil.UsageErrorf(cmd, "AWS account ID and Account CR Name cannot be set at the same time")
}

o.output = o.GlobalOptions.Output
return nil
}

Expand Down
21 changes: 14 additions & 7 deletions cmd/account/get/account-claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
. "github.com/onsi/gomega"

mockk8s "github.com/openshift/osdctl/cmd/clusterdeployment/mock/k8s"
"github.com/openshift/osdctl/internal/utils/globalflags"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand All @@ -17,6 +18,7 @@ func TestGetAccountClaimCmdComplete(t *testing.T) {
mockCtrl := gomock.NewController(t)
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
kubeFlags := genericclioptions.NewConfigFlags(false)
globalFlags := globalflags.GlobalOptions{Output: ""}
testCases := []struct {
title string
option *getAccountClaimOptions
Expand All @@ -26,34 +28,39 @@ func TestGetAccountClaimCmdComplete(t *testing.T) {
{
title: "account id and account cr name empty at the same time",
option: &getAccountClaimOptions{
accountID: "",
accountName: "",
accountID: "",
accountName: "",
flags: kubeFlags,
GlobalOptions: &globalFlags,
},
errExpected: true,
errContent: "AWS account ID and Account CR Name cannot be empty at the same time",
},
{
title: "account id and account cr name set at the same time",
option: &getAccountClaimOptions{
accountID: "foo",
accountName: "bar",
accountID: "foo",
accountName: "bar",
flags: kubeFlags,
GlobalOptions: &globalFlags,
},
errExpected: true,
errContent: "AWS account ID and Account CR Name cannot be set at the same time",
},
{
title: "succeed",
option: &getAccountClaimOptions{
accountID: "foo",
flags: kubeFlags,
accountID: "foo",
flags: kubeFlags,
GlobalOptions: &globalFlags,
},
errExpected: false,
},
}

for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
cmd := newCmdGetAccountClaim(streams, kubeFlags, mockk8s.NewMockClient(mockCtrl))
cmd := newCmdGetAccountClaim(streams, kubeFlags, mockk8s.NewMockClient(mockCtrl), &globalFlags)
err := tc.option.complete(cmd, nil)
if tc.errExpected {
g.Expect(err).Should(HaveOccurred())
Expand Down
21 changes: 12 additions & 9 deletions cmd/account/get/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/openshift/osdctl/cmd/common"
"github.com/openshift/osdctl/internal/utils/globalflags"
"github.com/openshift/osdctl/pkg/k8s"
"github.com/openshift/osdctl/pkg/printer"
)

// newCmdGetAccount implements the get account command which get the Account CR
// related to the specified AWS Account ID or the specified Account Claim CR
func newCmdGetAccount(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client) *cobra.Command {
ops := newGetAccountOptions(streams, flags, client)
func newCmdGetAccount(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client, globalOpts *globalflags.GlobalOptions) *cobra.Command {
ops := newGetAccountOptions(streams, flags, client, globalOpts)
getAccountCmd := &cobra.Command{
Use: "account",
Short: "Get AWS Account CR",
Expand All @@ -32,7 +33,6 @@ func newCmdGetAccount(streams genericclioptions.IOStreams, flags *genericcliopti
}

ops.printFlags.AddFlags(getAccountCmd)
getAccountCmd.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].")
getAccountCmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", common.AWSAccountNamespace,
"The namespace to keep AWS accounts. The default value is aws-account-operator.")
getAccountCmd.Flags().StringVarP(&ops.accountID, "account-id", "i", "", "AWS account ID")
Expand All @@ -54,15 +54,17 @@ type getAccountOptions struct {
flags *genericclioptions.ConfigFlags
printFlags *printer.PrintFlags
genericclioptions.IOStreams
kubeCli client.Client
kubeCli client.Client
GlobalOptions *globalflags.GlobalOptions
}

func newGetAccountOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client) *getAccountOptions {
func newGetAccountOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client, globalOpts *globalflags.GlobalOptions) *getAccountOptions {
return &getAccountOptions{
flags: flags,
printFlags: printer.NewPrintFlags(),
IOStreams: streams,
kubeCli: client,
flags: flags,
printFlags: printer.NewPrintFlags(),
IOStreams: streams,
kubeCli: client,
GlobalOptions: globalOpts,
}
}

Expand All @@ -76,6 +78,7 @@ func (o *getAccountOptions) complete(cmd *cobra.Command, _ []string) error {
return cmdutil.UsageErrorf(cmd, "AWS account ID and AccountClaim CR Name cannot be set at the same time")
}

o.output = o.GlobalOptions.Output
return nil
}

Expand Down
14 changes: 11 additions & 3 deletions cmd/account/get/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
. "github.com/onsi/gomega"

mockk8s "github.com/openshift/osdctl/cmd/clusterdeployment/mock/k8s"
"github.com/openshift/osdctl/internal/utils/globalflags"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand All @@ -17,6 +18,7 @@ func TestGetAccountCmdComplete(t *testing.T) {
mockCtrl := gomock.NewController(t)
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
kubeFlags := genericclioptions.NewConfigFlags(false)
globalFlags := globalflags.GlobalOptions{Output: ""}
testCases := []struct {
title string
option *getAccountOptions
Expand All @@ -28,6 +30,8 @@ func TestGetAccountCmdComplete(t *testing.T) {
option: &getAccountOptions{
accountID: "",
accountClaimName: "",
flags: kubeFlags,
GlobalOptions: &globalFlags,
},
errExpected: true,
errContent: "AWS account ID and AccountClaim CR Name cannot be empty at the same time",
Expand All @@ -37,15 +41,18 @@ func TestGetAccountCmdComplete(t *testing.T) {
option: &getAccountOptions{
accountID: "foo",
accountClaimName: "bar",
flags: kubeFlags,
GlobalOptions: &globalFlags,
},
errExpected: true,
errContent: "AWS account ID and AccountClaim CR Name cannot be set at the same time",
},
{
title: "succeed",
option: &getAccountOptions{
accountID: "foo",
flags: kubeFlags,
accountID: "foo",
flags: kubeFlags,
GlobalOptions: &globalFlags,
},
errExpected: false,
},
Expand All @@ -54,14 +61,15 @@ func TestGetAccountCmdComplete(t *testing.T) {
option: &getAccountOptions{
accountClaimName: "foo",
flags: kubeFlags,
GlobalOptions: &globalFlags,
},
errExpected: false,
},
}

for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
cmd := newCmdGetAccount(streams, kubeFlags, mockk8s.NewMockClient(mockCtrl))
cmd := newCmdGetAccount(streams, kubeFlags, mockk8s.NewMockClient(mockCtrl), &globalFlags)
err := tc.option.complete(cmd, nil)
if tc.errExpected {
g.Expect(err).Should(HaveOccurred())
Expand Down
11 changes: 6 additions & 5 deletions cmd/account/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/openshift/osdctl/internal/utils/globalflags"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand All @@ -12,7 +13,7 @@ const (
)

// NewCmdGet implements the get command to get AWS Account related resources
func NewCmdGet(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client) *cobra.Command {
func NewCmdGet(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, client client.Client, globalOpts *globalflags.GlobalOptions) *cobra.Command {
getCmd := &cobra.Command{
Use: "get",
Short: "Get resources",
Expand All @@ -21,10 +22,10 @@ func NewCmdGet(streams genericclioptions.IOStreams, flags *genericclioptions.Con
Run: help,
}

getCmd.AddCommand(newCmdGetAccount(streams, flags, client))
getCmd.AddCommand(newCmdGetAccountClaim(streams, flags, client))
getCmd.AddCommand(newCmdGetLegalEntity(streams, flags, client))
getCmd.AddCommand(newCmdGetSecrets(streams, flags, client))
getCmd.AddCommand(newCmdGetAccount(streams, flags, client, globalOpts))
getCmd.AddCommand(newCmdGetAccountClaim(streams, flags, client, globalOpts))
getCmd.AddCommand(newCmdGetLegalEntity(streams, flags, client, globalOpts))
getCmd.AddCommand(newCmdGetSecrets(streams, flags, client, globalOpts))
getCmd.AddCommand(newCmdGetAWSAccount(streams, flags, client))

return getCmd
Expand Down
Loading

0 comments on commit 5734e6b

Please sign in to comment.