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

     1  package aws
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/aws/awserr"
     8  	"github.com/aws/aws-sdk-go/aws/session"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  // InstanceTypeInfo describes the instance type
    14  type InstanceTypeInfo struct {
    15  	Name string
    16  	vCPU int64
    17  }
    18  
    19  // InstanceTypes returns information of the all the instance types available for a region.
    20  // It returns a map of instance type name to it's information.
    21  func InstanceTypes(ctx context.Context, sess *session.Session, region string) (map[string]InstanceTypeInfo, error) {
    22  	ret := map[string]InstanceTypeInfo{}
    23  
    24  	client := ec2.New(sess, aws.NewConfig().WithRegion(region))
    25  	if err := client.DescribeInstanceTypesPagesWithContext(ctx,
    26  		&ec2.DescribeInstanceTypesInput{},
    27  		func(page *ec2.DescribeInstanceTypesOutput, lastPage bool) bool {
    28  			for _, info := range page.InstanceTypes {
    29  				ti := InstanceTypeInfo{Name: aws.StringValue(info.InstanceType)}
    30  				if info.VCpuInfo == nil {
    31  					continue
    32  				}
    33  				ti.vCPU = aws.Int64Value(info.VCpuInfo.DefaultVCpus)
    34  				ret[ti.Name] = ti
    35  			}
    36  			return !lastPage
    37  		}); err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return ret, nil
    42  }
    43  
    44  // IsUnauthorizedOperation checks if the error is un authorized due to permission failure or lack of service availability.
    45  func IsUnauthorizedOperation(err error) bool {
    46  	if err == nil {
    47  		return false
    48  	}
    49  	var awsErr awserr.Error
    50  	if errors.As(err, &awsErr) {
    51  		// see reference:
    52  		// https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html#CommonErrors
    53  		return awsErr.Code() == "UnauthorizedOperation" || awsErr.Code() == "AuthFailure" || awsErr.Code() == "Blocked"
    54  	}
    55  	return false
    56  }