github.com/openshift/installer@v1.4.17/pkg/quota/gcp/limits.go (about)

     1  package gcp
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	serviceusage "google.golang.org/api/serviceusage/v1beta1"
     9  )
    10  
    11  // loadLimits loads the consumer quota metric for a given project and the list of services.
    12  // The limits are defined wrt to the regions. If the limit is global, it's location is treated as `global` else the
    13  // region value is used for location.
    14  func loadLimits(ctx context.Context, client *serviceusage.ServicesService, project string, services ...string) ([]record, error) {
    15  	// services.consumerQuotaMetrics/list requires each service to be of the
    16  	// form projects/{project id}/services/{service name}
    17  	// see https://cloud.google.com/service-usage/docs/reference/rest/v1beta1/services.consumerQuotaMetrics/list
    18  	parent := fmt.Sprintf("projects/%s/services/", project)
    19  	for i := range services {
    20  		if !strings.HasPrefix(services[i], parent) {
    21  			services[i] = fmt.Sprintf("%s%s", parent, services[i])
    22  		}
    23  	}
    24  
    25  	var limits []record
    26  	for _, service := range services {
    27  		if err := client.ConsumerQuotaMetrics.
    28  			List(service).
    29  			Context(ctx).
    30  			Pages(ctx, func(page *serviceusage.ListConsumerQuotaMetricsResponse) error {
    31  				for _, qm := range page.Metrics {
    32  					for _, ql := range qm.ConsumerQuotaLimits {
    33  						for _, qlb := range ql.QuotaBuckets {
    34  							limit := record{
    35  								Service:  service[strings.LastIndex(service, "/")+1:],
    36  								Name:     ql.Metric,
    37  								Location: "global",
    38  							}
    39  							region, ok := qlb.Dimensions["region"]
    40  							if ok {
    41  								limit.Location = region
    42  							}
    43  							if qlb.EffectiveLimit < 0 {
    44  								continue
    45  							}
    46  							limit.Value = qlb.EffectiveLimit
    47  							limits = append(limits, limit)
    48  						}
    49  					}
    50  				}
    51  				return nil
    52  			}); err != nil {
    53  			return nil, err
    54  		}
    55  	}
    56  	return limits, nil
    57  }