Skip to content

Commit 896afcd

Browse files
ServiceQuotas: Add ServiceQuotas command routing with aliases
ServiceQuotas: Query for specific QuotaCode Account CLI: Have different output formats including default, json, env Factory: use target credentials from assumed role
1 parent f79873b commit 896afcd

File tree

13 files changed

+333
-47
lines changed

13 files changed

+333
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ bin/
1717

1818
# IDE
1919
.idea
20+
.vscode
2021

2122
# MacOS
2223
.DS_Store

cmd/account/cli.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package account
33
import (
44
"fmt"
55

6-
"github.com/aws/aws-sdk-go/aws"
76
"github.com/spf13/cobra"
87

98
"k8s.io/cli-runtime/pkg/genericclioptions"
109
cmdutil "k8s.io/kubectl/pkg/cmd/util"
1110

1211
k8spkg "github.com/openshift/osd-utils-cli/pkg/k8s"
13-
awsprovider "github.com/openshift/osd-utils-cli/pkg/provider/aws"
1412
)
1513

1614
// newCmdCli implements the Cli command which generates temporary STS cli credentials for the specified account cr
@@ -29,6 +27,7 @@ func newCmdCli(streams genericclioptions.IOStreams, flags *genericclioptions.Con
2927

3028
ops.k8sclusterresourcefactory.AttachCobraCliFlags(cliCmd)
3129

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

3433
return cliCmd
@@ -38,6 +37,7 @@ func newCmdCli(streams genericclioptions.IOStreams, flags *genericclioptions.Con
3837
type cliOptions struct {
3938
k8sclusterresourcefactory k8spkg.ClusterResourceFactoryOptions
4039

40+
output string
4141
verbose bool
4242

4343
genericclioptions.IOStreams
@@ -71,20 +71,29 @@ func (o *cliOptions) complete(cmd *cobra.Command) error {
7171
}
7272

7373
func (o *cliOptions) run() error {
74-
awsClient, err := o.k8sclusterresourcefactory.GetCloudProvider(o.verbose)
74+
_, err := o.k8sclusterresourcefactory.GetCloudProvider(o.verbose)
7575
if err != nil {
7676
return err
7777
}
7878

79-
credentials, err := awsprovider.GetAssumeRoleCredentials(awsClient, &o.k8sclusterresourcefactory.Awscloudfactory.ConsoleDuration,
80-
o.k8sclusterresourcefactory.Awscloudfactory.CallerIdentity.UserId,
81-
aws.String(fmt.Sprintf("arn:aws:iam::%s:role/%s",
82-
o.k8sclusterresourcefactory.AccountID,
83-
o.k8sclusterresourcefactory.Awscloudfactory.RoleName)))
84-
if err != nil {
85-
return err
79+
creds := o.k8sclusterresourcefactory.Awscloudfactory.Credentials
80+
81+
if o.output == "default" {
82+
fmt.Fprintf(o.IOStreams.Out, "Temporary AWS Credentials:\n%s\n", creds)
83+
}
84+
85+
if o.output == "json" {
86+
fmt.Fprintf(o.IOStreams.Out, "%s\n", creds)
87+
}
88+
89+
if o.output == "env" {
90+
fmt.Fprintf(o.IOStreams.Out, "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s AWS_DEFAULT_REGION=%s",
91+
*creds.AccessKeyId,
92+
*creds.SecretAccessKey,
93+
*creds.SessionToken,
94+
o.k8sclusterresourcefactory.Awscloudfactory.Region,
95+
)
8696
}
87-
fmt.Fprintf(o.IOStreams.Out, "Temporary AWS Credentials:\n%s\n", credentials)
8897

8998
return nil
9099
}

cmd/account/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/openshift/osd-utils-cli/cmd/account/get"
88
"github.com/openshift/osd-utils-cli/cmd/account/list"
9+
"github.com/openshift/osd-utils-cli/cmd/account/servicequotas"
910
)
1011

1112
// NewCmdAccount implements the base account command
@@ -20,6 +21,7 @@ func NewCmdAccount(streams genericclioptions.IOStreams, flags *genericclioptions
2021

2122
accountCmd.AddCommand(get.NewCmdGet(streams, flags))
2223
accountCmd.AddCommand(list.NewCmdList(streams, flags))
24+
accountCmd.AddCommand(servicequotas.NewCmdServiceQuotas(streams, flags))
2325
accountCmd.AddCommand(newCmdReset(streams, flags))
2426
accountCmd.AddCommand(newCmdSet(streams, flags))
2527
accountCmd.AddCommand(newCmdConsole(streams, flags))

cmd/account/servicequotas/cmd.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package servicequotas
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"k8s.io/cli-runtime/pkg/genericclioptions"
6+
)
7+
8+
// NewCmdServiceQuotas implements commands related to AWS service-quotas
9+
func NewCmdServiceQuotas(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
10+
baseCmd := &cobra.Command{
11+
Use: "servicequotas",
12+
Short: "Interact with AWS service-quotas",
13+
Args: cobra.NoArgs,
14+
DisableAutoGenTag: true,
15+
Run: help,
16+
Aliases: []string{"service-quotas", "service-quota"},
17+
}
18+
19+
baseCmd.AddCommand(newCmdDescribe(streams, flags))
20+
21+
return baseCmd
22+
}
23+
24+
func help(cmd *cobra.Command, _ []string) {
25+
cmd.Help()
26+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package servicequotas
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
//"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/service/servicequotas"
9+
10+
"github.com/spf13/cobra"
11+
12+
"k8s.io/cli-runtime/pkg/genericclioptions"
13+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
14+
15+
k8spkg "github.com/openshift/osd-utils-cli/pkg/k8s"
16+
awsprovider "github.com/openshift/osd-utils-cli/pkg/provider/aws"
17+
)
18+
19+
// newCmdDescribe implements servicequotas describe
20+
func newCmdDescribe(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
21+
ops := newDescribeOptions(streams, flags)
22+
describeCmd := &cobra.Command{
23+
Use: "describe",
24+
Short: "Describe AWS service-quotas",
25+
Args: cobra.NoArgs,
26+
DisableAutoGenTag: true,
27+
Run: func(cmd *cobra.Command, args []string) {
28+
cmdutil.CheckErr(ops.complete(cmd))
29+
cmdutil.CheckErr(ops.run())
30+
},
31+
}
32+
33+
ops.k8sclusterresourcefactory.AttachCobraCliFlags(describeCmd)
34+
35+
describeCmd.Flags().StringVarP(&ops.queryServiceCode, "service-code", "", "ec2", "Query for ServiceCode (default: ec2)")
36+
describeCmd.Flags().StringVarP(&ops.queryQuotaCode, "quota-code", "q", "", "Query for QuotaCode")
37+
38+
describeCmd.Flags().BoolVarP(&ops.verbose, "verbose", "v", false, "Verbose output")
39+
40+
return describeCmd
41+
}
42+
43+
// describeOptions defines the struct for running list account command
44+
type describeOptions struct {
45+
k8sclusterresourcefactory k8spkg.ClusterResourceFactoryOptions
46+
47+
queryServiceCode string
48+
queryQuotaCode string
49+
50+
verbose bool
51+
52+
genericclioptions.IOStreams
53+
}
54+
55+
func newDescribeOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *describeOptions {
56+
return &describeOptions{
57+
k8sclusterresourcefactory: k8spkg.ClusterResourceFactoryOptions{
58+
Flags: flags,
59+
},
60+
IOStreams: streams,
61+
}
62+
}
63+
64+
func (o *describeOptions) complete(cmd *cobra.Command) error {
65+
k8svalid, err := o.k8sclusterresourcefactory.ValidateIdentifiers()
66+
if !k8svalid {
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
72+
awsvalid, err := o.k8sclusterresourcefactory.Awscloudfactory.ValidateIdentifiers()
73+
if !awsvalid {
74+
if err != nil {
75+
return err
76+
}
77+
}
78+
79+
return nil
80+
}
81+
82+
func (o *describeOptions) run() error {
83+
awsClient, err := o.k8sclusterresourcefactory.GetCloudProvider(o.verbose)
84+
if err != nil {
85+
return err
86+
}
87+
88+
var foundServiceQuotas []*servicequotas.ServiceQuota
89+
90+
searchQuery := &servicequotas.ListServiceQuotasInput{
91+
ServiceCode: &o.queryServiceCode,
92+
}
93+
94+
for {
95+
servicequotas, err := awsprovider.Client.ListServiceQuotas(awsClient, searchQuery)
96+
if err != nil {
97+
return err
98+
}
99+
100+
for _, foundQuota := range servicequotas.Quotas {
101+
foundServiceQuotas = append(foundServiceQuotas, foundQuota)
102+
}
103+
104+
// for pagination
105+
searchQuery.NextToken = servicequotas.NextToken
106+
if servicequotas.NextToken == nil {
107+
break
108+
}
109+
}
110+
111+
found := false
112+
if o.queryQuotaCode == "" {
113+
fmt.Println(foundServiceQuotas)
114+
} else {
115+
for _, quota := range foundServiceQuotas {
116+
if *quota.QuotaCode == o.queryQuotaCode {
117+
fmt.Println(quota)
118+
found = true
119+
}
120+
}
121+
}
122+
if !found {
123+
return errors.New("Cannot find ServiceQuota (service:" + o.queryServiceCode + " quota:" + o.queryQuotaCode + ")")
124+
}
125+
126+
return nil
127+
}

docs/command/osdctl_account.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ osdctl account [flags]
3838
* [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
41+
* [osdctl account servicequotas](osdctl_account_servicequotas.md) - Interact with AWS service-quotas
4142
* [osdctl account set](osdctl_account_set.md) - Set AWS Account CR status
4243
* [osdctl account verify-secrets](osdctl_account_verify-secrets.md) - Verify AWS Account CR IAM User credentials
4344

docs/command/osdctl_account_cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ osdctl account cli [flags]
2222
-C, --cluster-id string The Internal Cluster ID from Hive to create AWS console URL for
2323
-d, --duration int The duration of the console session. Default value is 3600 seconds(1 hour) (default 3600)
2424
-h, --help help for cli
25+
-o, --out string Output format [default | json | env] (default "default")
2526
-v, --verbose Verbose output
2627
```
2728

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## osdctl account servicequotas
2+
3+
Interact with AWS service-quotas
4+
5+
### Synopsis
6+
7+
Interact with AWS service-quotas
8+
9+
```
10+
osdctl account servicequotas [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help help for servicequotas
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
--cluster string The name of the kubeconfig cluster to use
23+
--context string The name of the kubeconfig context to use
24+
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
25+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
26+
--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")
27+
-s, --server string The address and port of the Kubernetes API server
28+
```
29+
30+
### SEE ALSO
31+
32+
* [osdctl account](osdctl_account.md) - AWS Account related utilities
33+
* [osdctl account servicequotas describe](osdctl_account_servicequotas_describe.md) - Describe AWS service-quotas
34+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## osdctl account servicequotas describe
2+
3+
Describe AWS service-quotas
4+
5+
### Synopsis
6+
7+
Describe AWS service-quotas
8+
9+
```
10+
osdctl account servicequotas describe [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-i, --account-id string The AWS account ID we need to create AWS credentials for -- This argument will not work for CCS accounts
17+
-a, --account-name string The AWS account CR we need to create a temporary AWS console URL for
18+
--account-namespace string The namespace to keep AWS accounts. The default value is aws-account-operator. (default "aws-account-operator")
19+
-c, --aws-config string specify AWS config file path
20+
-p, --aws-profile string specify AWS profile
21+
-r, --aws-region string specify AWS region (default "us-east-1")
22+
-C, --cluster-id string The Internal Cluster ID from Hive to create AWS console URL for
23+
-d, --duration int The duration of the console session. Default value is 3600 seconds(1 hour) (default 3600)
24+
-h, --help help for describe
25+
-q, --quota-code string Query for QuotaCode
26+
--service-code string Query for ServiceCode (default: ec2) (default "ec2")
27+
-v, --verbose Verbose output
28+
```
29+
30+
### Options inherited from parent commands
31+
32+
```
33+
--cluster string The name of the kubeconfig cluster to use
34+
--context string The name of the kubeconfig context to use
35+
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
36+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
37+
--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")
38+
-s, --server string The address and port of the Kubernetes API server
39+
```
40+
41+
### SEE ALSO
42+
43+
* [osdctl account servicequotas](osdctl_account_servicequotas.md) - Interact with AWS service-quotas
44+

0 commit comments

Comments
 (0)