|
| 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 | +} |
0 commit comments