2015-06-17 23:25:21 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-18 15:35:45 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-06-17 23:25:21 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2016-12-15 23:33:10 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-06-17 23:25:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSFlowLog_basic(t *testing.T) {
|
2015-06-17 23:25:21 +02:00
|
|
|
var flowLog ec2.FlowLog
|
|
|
|
|
2017-05-30 17:23:21 +02:00
|
|
|
rInt := acctest.RandInt()
|
2016-12-15 23:33:10 +01:00
|
|
|
|
2015-06-17 23:25:21 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 02:45:57 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_flow_log.test_flow_log",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckFlowLogDestroy,
|
2015-06-17 23:25:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-05-30 17:23:21 +02:00
|
|
|
Config: testAccFlowLogConfig_basic(rInt),
|
2015-06-17 23:25:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckFlowLogExists("aws_flow_log.test_flow_log", &flowLog),
|
|
|
|
testAccCheckAWSFlowLogAttributes(&flowLog),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSFlowLog_subnet(t *testing.T) {
|
2015-06-17 23:25:21 +02:00
|
|
|
var flowLog ec2.FlowLog
|
|
|
|
|
2017-05-30 17:23:21 +02:00
|
|
|
rInt := acctest.RandInt()
|
2016-12-15 23:33:10 +01:00
|
|
|
|
2015-06-17 23:25:21 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 02:45:57 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_flow_log.test_flow_log_subnet",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckFlowLogDestroy,
|
2015-06-17 23:25:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-05-30 17:23:21 +02:00
|
|
|
Config: testAccFlowLogConfig_subnet(rInt),
|
2015-06-17 23:25:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckFlowLogExists("aws_flow_log.test_flow_log_subnet", &flowLog),
|
|
|
|
testAccCheckAWSFlowLogAttributes(&flowLog),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckFlowLogExists(n string, flowLog *ec2.FlowLog) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Flow Log ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
describeOpts := &ec2.DescribeFlowLogsInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
FlowLogIds: []*string{aws.String(rs.Primary.ID)},
|
2015-06-17 23:25:21 +02:00
|
|
|
}
|
|
|
|
resp, err := conn.DescribeFlowLogs(describeOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.FlowLogs) > 0 {
|
|
|
|
*flowLog = *resp.FlowLogs[0]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("No Flow Logs found for id (%s)", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSFlowLogAttributes(flowLog *ec2.FlowLog) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if flowLog.FlowLogStatus != nil && *flowLog.FlowLogStatus == "ACTIVE" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if flowLog.FlowLogStatus == nil {
|
|
|
|
return fmt.Errorf("Flow Log status is not ACTIVE, is nil")
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Flow Log status is not ACTIVE, got: %s", *flowLog.FlowLogStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckFlowLogDestroy(s *terraform.State) error {
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_flow_log" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:23:21 +02:00
|
|
|
func testAccFlowLogConfig_basic(rInt int) string {
|
2016-12-15 23:33:10 +01:00
|
|
|
return fmt.Sprintf(`
|
2015-06-17 23:25:21 +02:00
|
|
|
resource "aws_vpc" "default" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-flow-log-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "test_subnet" {
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
cidr_block = "10.0.1.0/24"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-flow-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role" "test_role" {
|
2017-05-30 17:23:21 +02:00
|
|
|
name = "tf_test_flow_log_basic_%d"
|
2015-06-17 23:25:21 +02:00
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {
|
|
|
|
"Service": [
|
|
|
|
"ec2.amazonaws.com"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"Action": [
|
|
|
|
"sts:AssumeRole"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2015-11-20 20:15:20 +01:00
|
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
2017-05-30 17:23:21 +02:00
|
|
|
name = "tf-test-fl-%d"
|
2015-11-20 20:15:20 +01:00
|
|
|
}
|
2015-06-17 23:25:21 +02:00
|
|
|
resource "aws_flow_log" "test_flow_log" {
|
2017-05-30 17:23:21 +02:00
|
|
|
log_group_name = "${aws_cloudwatch_log_group.foobar.name}"
|
2015-06-17 23:25:21 +02:00
|
|
|
iam_role_arn = "${aws_iam_role.test_role.arn}"
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
traffic_type = "ALL"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_flow_log" "test_flow_log_subnet" {
|
2015-11-20 20:15:20 +01:00
|
|
|
log_group_name = "${aws_cloudwatch_log_group.foobar.name}"
|
2015-06-17 23:25:21 +02:00
|
|
|
iam_role_arn = "${aws_iam_role.test_role.arn}"
|
|
|
|
subnet_id = "${aws_subnet.test_subnet.id}"
|
|
|
|
traffic_type = "ALL"
|
|
|
|
}
|
2017-05-30 17:23:21 +02:00
|
|
|
`, rInt, rInt)
|
2016-12-15 23:33:10 +01:00
|
|
|
}
|
2015-06-17 23:25:21 +02:00
|
|
|
|
2017-05-30 17:23:21 +02:00
|
|
|
func testAccFlowLogConfig_subnet(rInt int) string {
|
2016-12-15 23:33:10 +01:00
|
|
|
return fmt.Sprintf(`
|
2015-06-17 23:25:21 +02:00
|
|
|
resource "aws_vpc" "default" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-flow-log-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "test_subnet" {
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
cidr_block = "10.0.1.0/24"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-flow-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role" "test_role" {
|
2017-05-30 17:23:21 +02:00
|
|
|
name = "tf_test_flow_log_subnet_%d"
|
2015-06-17 23:25:21 +02:00
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {
|
|
|
|
"Service": [
|
|
|
|
"ec2.amazonaws.com"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"Action": [
|
|
|
|
"sts:AssumeRole"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
2015-11-19 23:27:56 +01:00
|
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
2017-05-30 17:23:21 +02:00
|
|
|
name = "tf-test-fl-%d"
|
2015-11-19 23:27:56 +01:00
|
|
|
}
|
2015-06-17 23:25:21 +02:00
|
|
|
|
|
|
|
resource "aws_flow_log" "test_flow_log_subnet" {
|
2015-11-19 23:27:56 +01:00
|
|
|
log_group_name = "${aws_cloudwatch_log_group.foobar.name}"
|
2015-06-17 23:25:21 +02:00
|
|
|
iam_role_arn = "${aws_iam_role.test_role.arn}"
|
|
|
|
subnet_id = "${aws_subnet.test_subnet.id}"
|
|
|
|
traffic_type = "ALL"
|
|
|
|
}
|
2017-05-30 17:23:21 +02:00
|
|
|
`, rInt, rInt)
|
2016-12-15 23:33:10 +01:00
|
|
|
}
|