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

     1  package gcp
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  const (
     9  	// ServiceComputeEngineAPI is the GCE service URL
    10  	ServiceComputeEngineAPI = "compute.googleapis.com"
    11  	// ServiceIAMAPI is the IAM service URL
    12  	ServiceIAMAPI = "iam.googleapis.com"
    13  )
    14  
    15  // Quota is a record of the quota in GCP consumed by a cluster
    16  type Quota []QuotaUsage
    17  
    18  // QuotaUsage identifies a quota metric and records the usage
    19  type QuotaUsage struct {
    20  	*Metric `json:",inline"`
    21  	// Amount is the amount of the quota being used
    22  	Amount int64 `json:"amount,omitempty"`
    23  }
    24  
    25  // String formats the quota usage
    26  func (q *QuotaUsage) String() string {
    27  	return fmt.Sprintf("%s:%d", q.Metric.String(), q.Amount)
    28  }
    29  
    30  // Metric identify a quota. Service/Label matches the Google Quota API names for quota metrics
    31  type Metric struct {
    32  	// Service is the Google Cloud Service to which this quota belongs (e.g. compute.googleapis.com)
    33  	Service string `json:"service,omitempty"`
    34  	// Limit is the name of the item that's limited (e.g. cpus)
    35  	Limit string `json:"limit,omitempty"`
    36  	// Dimensions are unique axes on which this Limit is applied (e.g. region: us-central-1)
    37  	Dimensions map[string]string `json:"dimensions,omitempty"`
    38  }
    39  
    40  // String formats the metric
    41  func (m *Metric) String() string {
    42  	var dimensions []string
    43  	for key, value := range m.Dimensions {
    44  		dimensions = append(dimensions, fmt.Sprintf("%s=%s", key, value))
    45  	}
    46  	var suffix string
    47  	if len(dimensions) > 0 {
    48  		suffix = fmt.Sprintf("[%s]", strings.Join(dimensions, ","))
    49  	}
    50  	return fmt.Sprintf("%s/%s%s", m.Service, m.Limit, suffix)
    51  }
    52  
    53  // Matches determines if this metric matches the other
    54  func (m *Metric) Matches(other *Metric) bool {
    55  	if m.Service != other.Service {
    56  		return false
    57  	}
    58  	if m.Limit != other.Limit {
    59  		return false
    60  	}
    61  
    62  	if len(m.Dimensions) != len(other.Dimensions) {
    63  		return false
    64  	}
    65  	for key, value := range m.Dimensions {
    66  		otherValue, recorded := other.Dimensions[key]
    67  		if !recorded {
    68  			return false
    69  		}
    70  		if value != otherValue {
    71  			return false
    72  		}
    73  	}
    74  	for key, value := range other.Dimensions {
    75  		ourValue, recorded := m.Dimensions[key]
    76  		if !recorded {
    77  			return false
    78  		}
    79  		if value != ourValue {
    80  			return false
    81  		}
    82  	}
    83  	return true
    84  }