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

     1  package aws
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/aws/session"
    10  	"github.com/aws/aws-sdk-go/service/servicequotas"
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/openshift/installer/pkg/quota"
    14  )
    15  
    16  // Load load the quota information for a region. It provides information
    17  // about the usage and limit for each resource quota.
    18  func Load(ctx context.Context, sess *session.Session, region string, services ...string) ([]quota.Quota, error) {
    19  	client := servicequotas.New(sess, aws.NewConfig().WithRegion(region))
    20  	records, err := loadLimits(ctx, client, services...)
    21  	if err != nil {
    22  		return nil, errors.Wrap(err, "failed to load limits for servicequotas")
    23  	}
    24  	return newQuota(region, records), nil
    25  }
    26  
    27  func newQuota(region string, limits []record) []quota.Quota {
    28  	var ret []quota.Quota
    29  	for _, limit := range limits {
    30  		q := quota.Quota{
    31  			Service: limit.Service,
    32  			Name:    fmt.Sprintf("%s/%s", limit.Service, limit.Name),
    33  			Region:  region,
    34  			InUse:   0,
    35  			Limit:   limit.Value,
    36  		}
    37  		if limit.global {
    38  			q.Region = "global"
    39  		}
    40  		ret = append(ret, q)
    41  	}
    42  	return ret
    43  }
    44  
    45  // IsUnauthorized checks if the error is un authorized.
    46  func IsUnauthorized(err error) bool {
    47  	if err == nil {
    48  		return false
    49  	}
    50  	var awsErr awserr.Error
    51  	if errors.As(err, &awsErr) {
    52  		// see reference:
    53  		// https://docs.aws.amazon.com/servicequotas/2019-06-24/apireference/API_GetServiceQuota.html
    54  		// https://docs.aws.amazon.com/servicequotas/2019-06-24/apireference/API_GetAWSDefaultServiceQuota.html
    55  		return awsErr.Code() == "AccessDeniedException" || awsErr.Code() == "NoSuchResourceException"
    56  	}
    57  	return false
    58  }