providers/aws: cleanup of connections, upgrades network acl test
This commit is contained in:
parent
2566975c8e
commit
331f6705b8
|
@ -6,17 +6,14 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/multierror"
|
"github.com/hashicorp/terraform/helper/multierror"
|
||||||
|
|
||||||
"github.com/awslabs/aws-sdk-go/service/elb"
|
"github.com/awslabs/aws-sdk-go/aws"
|
||||||
"github.com/awslabs/aws-sdk-go/service/route53"
|
|
||||||
"github.com/awslabs/aws-sdk-go/service/s3"
|
|
||||||
"github.com/hashicorp/aws-sdk-go/aws"
|
|
||||||
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
|
||||||
|
|
||||||
awsSDK "github.com/awslabs/aws-sdk-go/aws"
|
|
||||||
"github.com/awslabs/aws-sdk-go/service/autoscaling"
|
"github.com/awslabs/aws-sdk-go/service/autoscaling"
|
||||||
awsEC2 "github.com/awslabs/aws-sdk-go/service/ec2"
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||||
|
"github.com/awslabs/aws-sdk-go/service/elb"
|
||||||
"github.com/awslabs/aws-sdk-go/service/iam"
|
"github.com/awslabs/aws-sdk-go/service/iam"
|
||||||
"github.com/awslabs/aws-sdk-go/service/rds"
|
"github.com/awslabs/aws-sdk-go/service/rds"
|
||||||
|
"github.com/awslabs/aws-sdk-go/service/route53"
|
||||||
|
"github.com/awslabs/aws-sdk-go/service/s3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -28,6 +25,7 @@ type Config struct {
|
||||||
|
|
||||||
type AWSClient struct {
|
type AWSClient struct {
|
||||||
ec2conn *ec2.EC2
|
ec2conn *ec2.EC2
|
||||||
|
ec2SDKconn *ec2.EC2
|
||||||
elbconn *elb.ELB
|
elbconn *elb.ELB
|
||||||
autoscalingconn *autoscaling.AutoScaling
|
autoscalingconn *autoscaling.AutoScaling
|
||||||
s3conn *s3.S3
|
s3conn *s3.S3
|
||||||
|
@ -35,7 +33,6 @@ type AWSClient struct {
|
||||||
region string
|
region string
|
||||||
rdsconn *rds.RDS
|
rdsconn *rds.RDS
|
||||||
iamconn *iam.IAM
|
iamconn *iam.IAM
|
||||||
ec2SDKconn *awsEC2.EC2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client configures and returns a fully initailized AWSClient
|
// Client configures and returns a fully initailized AWSClient
|
||||||
|
@ -59,40 +56,35 @@ func (c *Config) Client() (interface{}, error) {
|
||||||
|
|
||||||
log.Println("[INFO] Building AWS auth structure")
|
log.Println("[INFO] Building AWS auth structure")
|
||||||
creds := aws.DetectCreds(c.AccessKey, c.SecretKey, c.Token)
|
creds := aws.DetectCreds(c.AccessKey, c.SecretKey, c.Token)
|
||||||
|
awsConfig := &aws.Config{
|
||||||
log.Println("[INFO] Building AWS SDK auth structure")
|
Credentials: creds,
|
||||||
sdkCreds := awsSDK.DetectCreds(c.AccessKey, c.SecretKey, c.Token)
|
|
||||||
awsConfig := &awsSDK.Config{
|
|
||||||
Credentials: sdkCreds,
|
|
||||||
Region: c.Region,
|
Region: c.Region,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("[INFO] Initializing ELB SDK connection")
|
log.Println("[INFO] Initializing ELB connection")
|
||||||
client.elbconn = elb.New(awsConfig)
|
client.elbconn = elb.New(awsConfig)
|
||||||
|
|
||||||
log.Println("[INFO] Initializing S3 connection")
|
log.Println("[INFO] Initializing S3 connection")
|
||||||
client.s3conn = s3.New(awsConfig)
|
client.s3conn = s3.New(awsConfig)
|
||||||
|
|
||||||
log.Println("[INFO] Initializing EC2 Connection")
|
log.Println("[INFO] Initializing RDS Connection")
|
||||||
client.ec2conn = ec2.New(creds, c.Region, nil)
|
|
||||||
|
|
||||||
log.Println("[INFO] Initializing RDS SDK Connection")
|
|
||||||
client.rdsconn = rds.New(awsConfig)
|
client.rdsconn = rds.New(awsConfig)
|
||||||
|
|
||||||
log.Println("[INFO] Initializing IAM SDK Connection")
|
log.Println("[INFO] Initializing IAM Connection")
|
||||||
client.iamconn = iam.New(awsConfig)
|
client.iamconn = iam.New(awsConfig)
|
||||||
log.Println("[INFO] Initializing AutoScaling SDK connection")
|
log.Println("[INFO] Initializing AutoScaling connection")
|
||||||
client.autoscalingconn = autoscaling.New(awsConfig)
|
client.autoscalingconn = autoscaling.New(awsConfig)
|
||||||
|
|
||||||
log.Println("[INFO] Initializing EC2 SDK Connection")
|
log.Println("[INFO] Initializing EC2 Connection")
|
||||||
client.ec2SDKconn = awsEC2.New(awsConfig)
|
client.ec2conn = ec2.New(awsConfig)
|
||||||
|
client.ec2SDKconn = ec2.New(awsConfig)
|
||||||
|
|
||||||
// aws-sdk-go uses v4 for signing requests, which requires all global
|
// aws-sdk-go uses v4 for signing requests, which requires all global
|
||||||
// endpoints to use 'us-east-1'.
|
// endpoints to use 'us-east-1'.
|
||||||
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
|
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
|
||||||
log.Println("[INFO] Initializing Route 53 SDK connection")
|
log.Println("[INFO] Initializing Route 53 SDK connection")
|
||||||
client.r53conn = route53.New(&awsSDK.Config{
|
client.r53conn = route53.New(&aws.Config{
|
||||||
Credentials: sdkCreds,
|
Credentials: creds,
|
||||||
Region: "us-east-1",
|
Region: "us-east-1",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/aws-sdk-go/aws"
|
"github.com/awslabs/aws-sdk-go/aws"
|
||||||
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
|
||||||
// "github.com/hashicorp/terraform/helper/hashcode"
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
// "github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccAWSNetworkAcl_EgressAndIngressRules(t *testing.T) {
|
func TestAccAWSNetworkAcl_EgressAndIngressRules(t *testing.T) {
|
||||||
|
@ -151,7 +149,7 @@ func TestAccAWSNetworkAcl_OnlyEgressRules(t *testing.T) {
|
||||||
Config: testAccAWSNetworkAclEgressConfig,
|
Config: testAccAWSNetworkAclEgressConfig,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckAWSNetworkAclExists("aws_network_acl.bond", &networkAcl),
|
testAccCheckAWSNetworkAclExists("aws_network_acl.bond", &networkAcl),
|
||||||
testAccCheckTags(&networkAcl.Tags, "foo", "bar"),
|
testAccCheckTagsSDK(&networkAcl.Tags, "foo", "bar"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -192,8 +190,8 @@ func testAccCheckAWSNetworkAclDestroy(s *terraform.State) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the network acl
|
// Retrieve the network acl
|
||||||
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsRequest{
|
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsInput{
|
||||||
NetworkACLIDs: []string{rs.Primary.ID},
|
NetworkACLIDs: []*string{aws.String(rs.Primary.ID)},
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(resp.NetworkACLs) > 0 && *resp.NetworkACLs[0].NetworkACLID == rs.Primary.ID {
|
if len(resp.NetworkACLs) > 0 && *resp.NetworkACLs[0].NetworkACLID == rs.Primary.ID {
|
||||||
|
@ -228,15 +226,15 @@ func testAccCheckAWSNetworkAclExists(n string, networkAcl *ec2.NetworkACL) resou
|
||||||
}
|
}
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||||
|
|
||||||
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsRequest{
|
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsInput{
|
||||||
NetworkACLIDs: []string{rs.Primary.ID},
|
NetworkACLIDs: []*string{aws.String(rs.Primary.ID)},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resp.NetworkACLs) > 0 && *resp.NetworkACLs[0].NetworkACLID == rs.Primary.ID {
|
if len(resp.NetworkACLs) > 0 && *resp.NetworkACLs[0].NetworkACLID == rs.Primary.ID {
|
||||||
*networkAcl = resp.NetworkACLs[0]
|
*networkAcl = *resp.NetworkACLs[0]
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +244,7 @@ func testAccCheckAWSNetworkAclExists(n string, networkAcl *ec2.NetworkACL) resou
|
||||||
|
|
||||||
func testIngressRuleLength(networkAcl *ec2.NetworkACL, length int) resource.TestCheckFunc {
|
func testIngressRuleLength(networkAcl *ec2.NetworkACL, length int) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
var ingressEntries []ec2.NetworkACLEntry
|
var ingressEntries []*ec2.NetworkACLEntry
|
||||||
for _, e := range networkAcl.Entries {
|
for _, e := range networkAcl.Entries {
|
||||||
if *e.Egress == false {
|
if *e.Egress == false {
|
||||||
ingressEntries = append(ingressEntries, e)
|
ingressEntries = append(ingressEntries, e)
|
||||||
|
@ -267,12 +265,12 @@ func testAccCheckSubnetIsAssociatedWithAcl(acl string, sub string) resource.Test
|
||||||
subnet := s.RootModule().Resources[sub]
|
subnet := s.RootModule().Resources[sub]
|
||||||
|
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||||
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsRequest{
|
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsInput{
|
||||||
NetworkACLIDs: []string{networkAcl.Primary.ID},
|
NetworkACLIDs: []*string{aws.String(networkAcl.Primary.ID)},
|
||||||
Filters: []ec2.Filter{
|
Filters: []*ec2.Filter{
|
||||||
ec2.Filter{
|
&ec2.Filter{
|
||||||
Name: aws.String("association.subnet-id"),
|
Name: aws.String("association.subnet-id"),
|
||||||
Values: []string{subnet.Primary.ID},
|
Values: []*string{aws.String(subnet.Primary.ID)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -297,12 +295,12 @@ func testAccCheckSubnetIsNotAssociatedWithAcl(acl string, subnet string) resourc
|
||||||
subnet := s.RootModule().Resources[subnet]
|
subnet := s.RootModule().Resources[subnet]
|
||||||
|
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||||
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsRequest{
|
resp, err := conn.DescribeNetworkACLs(&ec2.DescribeNetworkACLsInput{
|
||||||
NetworkACLIDs: []string{networkAcl.Primary.ID},
|
NetworkACLIDs: []*string{aws.String(networkAcl.Primary.ID)},
|
||||||
Filters: []ec2.Filter{
|
Filters: []*ec2.Filter{
|
||||||
ec2.Filter{
|
&ec2.Filter{
|
||||||
Name: aws.String("association.subnet-id"),
|
Name: aws.String("association.subnet-id"),
|
||||||
Values: []string{subnet.Primary.ID},
|
Values: []*string{aws.String(subnet.Primary.ID)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue