-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCM-6311 | feat: Migrate common code to ocm-common repo
- Loading branch information
1 parent
15439b2
commit 747aaa3
Showing
44 changed files
with
4,063 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package aws_client | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudformation" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/aws/aws-sdk-go-v2/service/iam" | ||
"github.com/aws/aws-sdk-go-v2/service/kms" | ||
"github.com/aws/aws-sdk-go-v2/service/sts" | ||
|
||
"github.com/openshift-online/ocm-common/pkg/log" | ||
|
||
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing" | ||
"github.com/aws/aws-sdk-go-v2/service/route53" | ||
) | ||
|
||
type AWSClient struct { | ||
Ec2Client *ec2.Client | ||
Route53Client *route53.Client | ||
StackFormationClient *cloudformation.Client | ||
ElbClient *elb.Client | ||
StsClient *sts.Client | ||
Region string | ||
IamClient *iam.Client | ||
ClientContext context.Context | ||
AccountID string | ||
KmsClient *kms.Client | ||
} | ||
|
||
func CreateAWSClient(profileName string, region string) (*AWSClient, error) { | ||
var cfg aws.Config | ||
var err error | ||
|
||
if envCredential() { | ||
log.LogInfo("Got AWS_ACCESS_KEY_ID env settings, going to build the config with the env") | ||
cfg, err = config.LoadDefaultConfig(context.TODO(), | ||
config.WithRegion(region), | ||
config.WithCredentialsProvider( | ||
credentials.NewStaticCredentialsProvider( | ||
os.Getenv("AWS_ACCESS_KEY_ID"), | ||
os.Getenv("AWS_SECRET_ACCESS_KEY"), | ||
"")), | ||
) | ||
} else { | ||
if envAwsProfile() { | ||
file := os.Getenv("AWS_SHARED_CREDENTIALS_FILE") | ||
log.LogInfo("Got file path: %s from env variable AWS_SHARED_CREDENTIALS_FILE\n", file) | ||
cfg, err = config.LoadDefaultConfig(context.TODO(), | ||
config.WithRegion(region), | ||
config.WithSharedCredentialsFiles([]string{file}), | ||
) | ||
} else { | ||
cfg, err = config.LoadDefaultConfig(context.TODO(), | ||
config.WithRegion(region), | ||
config.WithSharedConfigProfile(profileName), | ||
) | ||
} | ||
|
||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
awsClient := &AWSClient{ | ||
Ec2Client: ec2.NewFromConfig(cfg), | ||
Route53Client: route53.NewFromConfig(cfg), | ||
StackFormationClient: cloudformation.NewFromConfig(cfg), | ||
ElbClient: elb.NewFromConfig(cfg), | ||
Region: region, | ||
StsClient: sts.NewFromConfig(cfg), | ||
IamClient: iam.NewFromConfig(cfg), | ||
ClientContext: context.TODO(), | ||
KmsClient: kms.NewFromConfig(cfg), | ||
} | ||
awsClient.AccountID = awsClient.GetAWSAccountID() | ||
return awsClient, nil | ||
} | ||
|
||
func (client *AWSClient) GetAWSAccountID() string { | ||
input := &sts.GetCallerIdentityInput{} | ||
out, err := client.StsClient.GetCallerIdentity(client.ClientContext, input) | ||
if err != nil { | ||
return "" | ||
} | ||
return *out.Account | ||
} | ||
|
||
func (client *AWSClient) EC2() *ec2.Client { | ||
return client.Ec2Client | ||
} | ||
|
||
func (client *AWSClient) Route53() *route53.Client { | ||
return client.Route53Client | ||
} | ||
func (client *AWSClient) CloudFormation() *cloudformation.Client { | ||
return client.StackFormationClient | ||
} | ||
func (client *AWSClient) ELB() *elb.Client { | ||
return client.ElbClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package aws_client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/openshift-online/ocm-common/pkg/log" | ||
) | ||
|
||
func (client *AWSClient) AllocateEIPAddress() (*ec2.AllocateAddressOutput, error) { | ||
inputs := &ec2.AllocateAddressInput{ | ||
Address: nil, | ||
CustomerOwnedIpv4Pool: nil, | ||
Domain: "", | ||
DryRun: nil, | ||
NetworkBorderGroup: nil, | ||
PublicIpv4Pool: nil, | ||
TagSpecifications: nil, | ||
} | ||
|
||
respEIP, err := client.Ec2Client.AllocateAddress(context.TODO(), inputs) | ||
if err != nil { | ||
log.LogError("Create eip failed " + err.Error()) | ||
return nil, err | ||
} | ||
log.LogInfo("Allocated EIP %s with ip %s", *respEIP.AllocationId, *respEIP.PublicIp) | ||
return respEIP, err | ||
} | ||
|
||
func (client *AWSClient) DisassociateAddress(associateID string) (*ec2.DisassociateAddressOutput, error) { | ||
inputDisassociate := &ec2.DisassociateAddressInput{ | ||
AssociationId: aws.String(associateID), | ||
DryRun: nil, | ||
PublicIp: nil, | ||
} | ||
|
||
respDisassociate, err := client.Ec2Client.DisassociateAddress(context.TODO(), inputDisassociate) | ||
if err != nil { | ||
log.LogError("Disassociate eip failed " + err.Error()) | ||
return nil, err | ||
} | ||
log.LogInfo("Disassociate eip success") | ||
return respDisassociate, err | ||
} | ||
|
||
func (client *AWSClient) AllocateEIPAndAssociateInstance(instanceID string) (string, error) { | ||
allocRes, err := client.AllocateEIPAddress() | ||
if err != nil { | ||
log.LogError("Failed allocated EIP: %s", err) | ||
} else { | ||
log.LogInfo("Successfully allocated EIP: %s", *allocRes.PublicIp) | ||
} | ||
assocRes, err := client.EC2().AssociateAddress(context.TODO(), | ||
&ec2.AssociateAddressInput{ | ||
AllocationId: allocRes.AllocationId, | ||
InstanceId: aws.String(instanceID), | ||
}) | ||
if err != nil { | ||
defer func() { | ||
_, err := client.ReleaseAddress(*allocRes.AllocationId) | ||
log.LogError("Associate EIP allocation %s failed to instance ID %s", *allocRes.AllocationId, instanceID) | ||
if err != nil { | ||
log.LogError("Failed allocated EIP: %s", err) | ||
} | ||
}() | ||
return "", err | ||
|
||
} | ||
log.LogInfo("Successfully allocated %s with instance %s.\n\tallocation id: %s, association id: %s\n", | ||
*allocRes.PublicIp, instanceID, *allocRes.AllocationId, *assocRes.AssociationId) | ||
return *allocRes.PublicIp, nil | ||
} | ||
|
||
func (client *AWSClient) ReleaseAddress(allocationID string) (*ec2.ReleaseAddressOutput, error) { | ||
inputRelease := &ec2.ReleaseAddressInput{ | ||
AllocationId: aws.String(allocationID), | ||
DryRun: nil, | ||
NetworkBorderGroup: nil, | ||
PublicIp: nil, | ||
} | ||
respRelease, err := client.Ec2Client.ReleaseAddress(context.TODO(), inputRelease) | ||
if err != nil { | ||
log.LogError("Release eip failed " + err.Error()) | ||
return nil, err | ||
} | ||
log.LogInfo("Release eip success: " + allocationID) | ||
return respRelease, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package aws_client | ||
|
||
import ( | ||
"context" | ||
|
||
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing" | ||
|
||
elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types" | ||
"github.com/openshift-online/ocm-common/pkg/log" | ||
) | ||
|
||
func (client *AWSClient) DescribeLoadBalancers(vpcID string) ([]elbtypes.LoadBalancerDescription, error) { | ||
|
||
listenedELB := []elbtypes.LoadBalancerDescription{} | ||
input := &elb.DescribeLoadBalancersInput{} | ||
resp, err := client.ElbClient.DescribeLoadBalancers(context.TODO(), input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// for _, lb := range resp.LoadBalancers { | ||
for _, lb := range resp.LoadBalancerDescriptions { | ||
|
||
// if *lb.VpcId == vpcID { | ||
if *lb.VPCId == vpcID { | ||
log.LogInfo("Got load balancer %s", *lb.LoadBalancerName) | ||
listenedELB = append(listenedELB, lb) | ||
} | ||
} | ||
|
||
return listenedELB, err | ||
} | ||
|
||
func (client *AWSClient) DeleteELB(ELB elbtypes.LoadBalancerDescription) error { | ||
log.LogInfo("Goint to delete ELB %s", *ELB.LoadBalancerName) | ||
|
||
deleteELBInput := &elb.DeleteLoadBalancerInput{ | ||
// LoadBalancerArn: ELB.LoadBalancerArn, | ||
LoadBalancerName: ELB.LoadBalancerName, | ||
} | ||
_, err := client.ElbClient.DeleteLoadBalancer(context.TODO(), deleteELBInput) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package aws_client | ||
|
||
import "os" | ||
|
||
func envCredential() bool { | ||
if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" { | ||
return true | ||
} | ||
return false | ||
} | ||
func envAwsProfile() bool { | ||
return os.Getenv("AWS_SHARED_CREDENTIALS_FILE") != "" | ||
} |
Oops, something went wrong.