-
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 | Migrate: common code to ocm-common repo
- Loading branch information
1 parent
4479726
commit ab2f695
Showing
46 changed files
with
4,178 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,56 @@ | ||
module github.com/openshift-online/ocm-common | ||
|
||
go 1.19 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go-v2 v1.22.2 | ||
github.com/apparentlymart/go-cidr v1.1.0 | ||
github.com/aws/aws-sdk-go-v2 v1.26.0 | ||
github.com/aws/aws-sdk-go-v2/config v1.27.9 | ||
github.com/aws/aws-sdk-go-v2/credentials v1.17.9 | ||
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.48.0 | ||
github.com/aws/aws-sdk-go-v2/service/ec2 v1.152.0 | ||
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.24.3 | ||
github.com/aws/aws-sdk-go-v2/service/iam v1.27.1 | ||
github.com/aws/aws-sdk-go-v2/service/kms v1.30.0 | ||
github.com/aws/aws-sdk-go-v2/service/route53 v1.40.3 | ||
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5 | ||
github.com/hashicorp/go-version v1.6.0 | ||
github.com/onsi/ginkgo/v2 v2.11.0 | ||
github.com/onsi/gomega v1.27.8 | ||
github.com/openshift-online/ocm-sdk-go v0.1.391 | ||
github.com/sirupsen/logrus v1.9.3 | ||
go.uber.org/mock v0.3.0 | ||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 | ||
golang.org/x/crypto v0.14.0 | ||
gopkg.in/square/go-jose.v2 v2.6.0 | ||
) | ||
|
||
require ( | ||
github.com/aws/smithy-go v1.16.0 | ||
github.com/aws/smithy-go v1.20.1 | ||
github.com/kr/pretty v0.1.0 // indirect | ||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect | ||
) | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 // indirect | ||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4 // indirect | ||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4 // indirect | ||
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.6 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/sso v1.20.3 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3 // indirect | ||
github.com/go-logr/logr v1.2.4 // indirect | ||
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect | ||
github.com/golang/glog v1.0.0 // indirect | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect | ||
github.com/jmespath/go-jmespath v0.4.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/stretchr/testify v1.7.0 // indirect | ||
golang.org/x/net v0.10.0 // indirect | ||
golang.org/x/sys v0.9.0 // indirect | ||
golang.org/x/text v0.9.0 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/sys v0.13.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
golang.org/x/tools v0.9.3 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,104 @@ | ||
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" | ||
) | ||
|
||
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}), | ||
) | ||
} | ||
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 | ||
} | ||
|
||
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 | ||
} |
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,83 @@ | ||
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 client.ReleaseAddress(*allocRes.AllocationId) | ||
log.LogError("Associate EIP allocation %s failed to instance ID %s", *allocRes.AllocationId, instanceID) | ||
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 | ||
} |
Oops, something went wrong.