Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/flow_log - add arn attribute #13656

Merged
merged 2 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions aws/resource_aws_flow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -111,6 +112,10 @@ func resourceAwsFlowLog() *schema.Resource {
},

"tags": tagsSchema(),
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -187,7 +192,7 @@ func resourceAwsLogFlowCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error: multiple Flow Logs created for (%s)", resourceId)
}

d.SetId(*resp.FlowLogIds[0])
d.SetId(aws.StringValue(resp.FlowLogIds[0]))

return resourceAwsLogFlowRead(d, meta)
}
Expand All @@ -202,13 +207,13 @@ func resourceAwsLogFlowRead(d *schema.ResourceData, meta interface{}) error {

resp, err := conn.DescribeFlowLogs(opts)
if err != nil {
log.Printf("[WARN] Error describing Flow Logs for id (%s)", d.Id())
log.Printf("[WARN] Flow Log (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if len(resp.FlowLogs) == 0 {
log.Printf("[WARN] No Flow Logs found for id (%s)", d.Id())
log.Printf("[WARN] Flow Log (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand Down Expand Up @@ -237,6 +242,16 @@ func resourceAwsLogFlowRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting tags: %s", err)
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ec2",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("vpc-flow-log/%s", d.Id()),
}.String()

d.Set("arn", arn)

return nil
}

Expand Down
24 changes: 6 additions & 18 deletions aws/resource_aws_flow_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestAccAWSFlowLog_VPCID(t *testing.T) {
Config: testAccFlowLogConfig_VPCID(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckFlowLogExists(resourceName, &flowLog),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`vpc-flow-log/fl-.+`)),
testAccCheckAWSFlowLogAttributes(&flowLog),
resource.TestCheckResourceAttrPair(resourceName, "iam_role_arn", iamRoleResourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "log_destination", ""),
Expand Down Expand Up @@ -338,7 +339,7 @@ func TestAccAWSFlowLog_disappears(t *testing.T) {
Config: testAccFlowLogConfig_VPCID(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckFlowLogExists(resourceName, &flowLog),
testAccCheckFlowLogDisappears(&flowLog),
testAccCheckResourceDisappears(testAccProvider, resourceAwsFlowLog(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -374,18 +375,6 @@ func testAccCheckFlowLogExists(n string, flowLog *ec2.FlowLog) resource.TestChec
}
}

func testAccCheckFlowLogDisappears(flowLog *ec2.FlowLog) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn
input := &ec2.DeleteFlowLogsInput{
FlowLogIds: []*string{flowLog.FlowLogId},
}
_, err := conn.DeleteFlowLogs(input)

return err
}
}

func testAccCheckAWSFlowLogAttributes(flowLog *ec2.FlowLog) resource.TestCheckFunc {
return func(s *terraform.State) error {
if flowLog.FlowLogStatus != nil && *flowLog.FlowLogStatus == "ACTIVE" {
Expand Down Expand Up @@ -603,15 +592,14 @@ resource "aws_cloudwatch_log_group" "test" {
name = %[1]q
}
resource "aws_s3_bucket" "test" {
bucket = %[1]q
force_destroy = true
}

bucket = %[1]q
force_destroy = true
}

resource "aws_flow_log" "test" {
log_destination = "${aws_s3_bucket.test.arn}"
log_destination_type = "s3"
iam_role_arn = "${aws_iam_role.test.arn}"
iam_role_arn = "${aws_iam_role.test.arn}"

traffic_type = "ALL"
vpc_id = "${aws_vpc.test.id}"
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ for more information about connecting to alternate AWS endpoints or AWS compatib
- [`aws_elasticache_cluster` resource](/docs/providers/aws/r/elasticache_cluster.html)
- [`aws_elb` data source](/docs/providers/aws/d/elb.html)
- [`aws_elb` resource](/docs/providers/aws/r/elb.html)
- [`aws_flow_log` resource](/docs/providers/aws/r/flow_log.html)
- [`aws_glue_connection` resource](/docs/providers/aws/r/glue_connection.html)
- [`aws_glue_crawler` resource](/docs/providers/aws/r/glue_crawler.html)
- [`aws_glue_job` resource](/docs/providers/aws/r/glue_job.html)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/flow_log.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - The Flow Log ID
* `arn` - The ARN of the Flow Log.

## Import

Expand Down