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

     1  package aws
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/session"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  )
    12  
    13  // DescribeSecurityGroups returns the list of ec2 Security Groups that contain the group id and vpc id.
    14  func DescribeSecurityGroups(ctx context.Context, session *session.Session, securityGroupIDs []string, region string) ([]*ec2.SecurityGroup, error) {
    15  	client := ec2.New(session, aws.NewConfig().WithRegion(region))
    16  
    17  	sgIDPtrs := []*string{}
    18  	for _, sgid := range securityGroupIDs {
    19  		sgid := sgid
    20  		sgIDPtrs = append(sgIDPtrs, &sgid)
    21  	}
    22  
    23  	cctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
    24  	defer cancel()
    25  
    26  	sgOutput, err := client.DescribeSecurityGroupsWithContext(cctx, &ec2.DescribeSecurityGroupsInput{GroupIds: sgIDPtrs})
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return sgOutput.SecurityGroups, nil
    31  }
    32  
    33  // DescribePublicIpv4Pool returns the ec2 public IPv4 Pool attributes from the given ID.
    34  func DescribePublicIpv4Pool(ctx context.Context, session *session.Session, region string, poolID string) (*ec2.PublicIpv4Pool, error) {
    35  	client := ec2.New(session, aws.NewConfig().WithRegion(region))
    36  
    37  	cctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
    38  	defer cancel()
    39  
    40  	poolOutputs, err := client.DescribePublicIpv4PoolsWithContext(cctx, &ec2.DescribePublicIpv4PoolsInput{PoolIds: []*string{aws.String(poolID)}})
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	if len(poolOutputs.PublicIpv4Pools) == 0 {
    45  		return nil, fmt.Errorf("public IPv4 Pool not found: %s", poolID)
    46  	}
    47  	// it should not happen
    48  	if len(poolOutputs.PublicIpv4Pools) > 1 {
    49  		return nil, fmt.Errorf("more than one Public IPv4 Pool: %s", poolID)
    50  	}
    51  	return poolOutputs.PublicIpv4Pools[0], nil
    52  }