From e390d7ddf232d83fd6e0f8776edc333445fdc8d8 Mon Sep 17 00:00:00 2001 From: Florin Patan Date: Sun, 23 Aug 2015 20:58:25 +0200 Subject: [PATCH 01/10] Add elb access logs setting --- builtin/providers/aws/resource_aws_elb.go | 44 ++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 9955c7cf0..d80d7e1fa 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -101,6 +101,35 @@ func resourceAwsElb() *schema.Resource { Default: 300, }, + "access_logs": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "enabled": &schema.Schema{ + Type: schema.TypeBool, + Required: true, + Default: false, + }, + "interval": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 60, + }, + "bucket": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Default: "", + }, + "bucket_prefix": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: "", + }, + }, + }, + }, + "listener": &schema.Schema{ Type: schema.TypeSet, Required: true, @@ -305,6 +334,7 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { d.Set("idle_timeout", lbAttrs.ConnectionSettings.IdleTimeout) d.Set("connection_draining", lbAttrs.ConnectionDraining.Enabled) d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout) + d.Set("access_logs", lbAttrs.AccessLog) resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{ LoadBalancerNames: []*string{lb.LoadBalancerName}, @@ -405,7 +435,7 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { d.SetPartial("instances") } - if d.HasChange("cross_zone_load_balancing") || d.HasChange("idle_timeout") { + if d.HasChange("cross_zone_load_balancing") || d.HasChange("idle_timeout") || d.HasChange("access_logs") { attrs := elb.ModifyLoadBalancerAttributesInput{ LoadBalancerName: aws.String(d.Get("name").(string)), LoadBalancerAttributes: &elb.LoadBalancerAttributes{ @@ -418,6 +448,18 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { }, } + logs := d.Get("access_logs").(*schema.Set).List() + if len(logs) > 0 { + log := logs[0].(map[string]interface{}) + accessLogs := &elb.AccessLog{ + Enabled: aws.Bool(log["enabled"].(bool)), + EmitInterval: aws.Int64(log["interval"].(int64)), + S3BucketName: aws.String(log["bucket"].(string)), + S3BucketPrefix: aws.String(log["bucket"].(string)), + } + attrs.LoadBalancerAttributes.AccessLog = accessLogs + } + _, err := elbconn.ModifyLoadBalancerAttributes(&attrs) if err != nil { return fmt.Errorf("Failure configuring ELB attributes: %s", err) From e173b60f104c68553b44ed8a5e5eb5b3151d1ee2 Mon Sep 17 00:00:00 2001 From: Florin Patan Date: Sun, 13 Sep 2015 23:06:57 +0200 Subject: [PATCH 02/10] Changes per feedback --- builtin/providers/aws/resource_aws_elb.go | 29 ++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index d80d7e1fa..e0c273f12 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -108,8 +108,8 @@ func resourceAwsElb() *schema.Resource { Schema: map[string]*schema.Schema{ "enabled": &schema.Schema{ Type: schema.TypeBool, - Required: true, - Default: false, + Optional: true, + Default: true, }, "interval": &schema.Schema{ Type: schema.TypeInt, @@ -119,18 +119,17 @@ func resourceAwsElb() *schema.Resource { "bucket": &schema.Schema{ Type: schema.TypeString, Required: true, - Default: "", }, "bucket_prefix": &schema.Schema{ Type: schema.TypeString, Optional: true, - Default: "", }, }, }, + Set: resourceAwsElbAccessLogsHash, }, - "listener": &schema.Schema{ + "listener": &schema.Schema{ Type: schema.TypeSet, Required: true, Elem: &schema.Resource{ @@ -455,8 +454,12 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { Enabled: aws.Bool(log["enabled"].(bool)), EmitInterval: aws.Int64(log["interval"].(int64)), S3BucketName: aws.String(log["bucket"].(string)), - S3BucketPrefix: aws.String(log["bucket"].(string)), } + + if log["bucket_prefix"] != "" { + accessLogs.S3BucketPrefix = aws.String(log["bucket_prefix"].(string)) + } + attrs.LoadBalancerAttributes.AccessLog = accessLogs } @@ -592,6 +595,20 @@ func resourceAwsElbHealthCheckHash(v interface{}) int { return hashcode.String(buf.String()) } +func resourceAwsElbAccessLogsHash(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + buf.WriteString(fmt.Sprintf("%t-", m["enabled"].(bool))) + buf.WriteString(fmt.Sprintf("%d-", m["interval"].(int))) + buf.WriteString(fmt.Sprintf("%s-", + strings.ToLower(m["bucket"].(string)))) + if v, ok := m["bucket_prefix"]; ok { + buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(v.(string)))) + } + + return hashcode.String(buf.String()) +} + func resourceAwsElbListenerHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) From e8f1f57ead9a4a97f2b3739e42534c16324763e4 Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Fri, 30 Oct 2015 15:02:58 -0700 Subject: [PATCH 03/10] Fix int64 cast. --- builtin/providers/aws/resource_aws_elb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index e0c273f12..6bfb3f0f7 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -452,7 +452,7 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { log := logs[0].(map[string]interface{}) accessLogs := &elb.AccessLog{ Enabled: aws.Bool(log["enabled"].(bool)), - EmitInterval: aws.Int64(log["interval"].(int64)), + EmitInterval: aws.Int64(int64(log["interval"].(int))), S3BucketName: aws.String(log["bucket"].(string)), } From 91b1d0c23d4bcbf356173b26c39f236e844cacb5 Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Fri, 30 Oct 2015 15:03:38 -0700 Subject: [PATCH 04/10] Can only set access logs once per ELB. --- builtin/providers/aws/resource_aws_elb.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 6bfb3f0f7..620ae5ee0 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -445,10 +445,12 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { IdleTimeout: aws.Int64(int64(d.Get("idle_timeout").(int))), }, }, - } + } logs := d.Get("access_logs").(*schema.Set).List() - if len(logs) > 0 { + if len(logs) > 1 { + return fmt.Errorf("Only one access logs config per ELB is supported") + } else if len(logs) == 1 { log := logs[0].(map[string]interface{}) accessLogs := &elb.AccessLog{ Enabled: aws.Bool(log["enabled"].(bool)), From 4e3d0b5f6c366b92a3ce671c54cfa2f4d52c4564 Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Fri, 30 Oct 2015 15:45:43 -0700 Subject: [PATCH 05/10] Fix schema conversion. --- builtin/providers/aws/resource_aws_elb.go | 12 ++++++------ builtin/providers/aws/structure.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 620ae5ee0..8d64e0588 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -330,13 +330,13 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { d.Set("source_security_group", lb.SourceSecurityGroup.GroupName) } d.Set("subnets", lb.Subnets) - d.Set("idle_timeout", lbAttrs.ConnectionSettings.IdleTimeout) - d.Set("connection_draining", lbAttrs.ConnectionDraining.Enabled) - d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout) - d.Set("access_logs", lbAttrs.AccessLog) + d.Set("idle_timeout", lbAttrs.ConnectionSettings.IdleTimeout) + d.Set("connection_draining", lbAttrs.ConnectionDraining.Enabled) + d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout) + d.Set("access_logs", flattenAccessLog(lbAttrs.AccessLog)) - resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{ - LoadBalancerNames: []*string{lb.LoadBalancerName}, + resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{ + LoadBalancerNames: []*string{lb.LoadBalancerName}, }) var et []*elb.Tag diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index fd581c84a..e7590001f 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -234,6 +234,22 @@ func expandElastiCacheParameters(configured []interface{}) ([]*elasticache.Param return parameters, nil } +// Flattens an access log into something that flatmap.Flatten() can handle +func flattenAccessLog(log *elb.AccessLog) []map[string]interface{} { + result := make([]map[string]interface{}, 0, 1) + + if log != nil { + result = append(result, map[string]interface{}{ + "enabled": *log.Enabled, + "interval": *log.EmitInterval, + "bucket": *log.S3BucketName, + "bucket_prefix": *log.S3BucketPrefix, + }) + } + + return result +} + // Flattens a health check into something that flatmap.Flatten() // can handle func flattenHealthCheck(check *elb.HealthCheck) []map[string]interface{} { From 2e03a7ebff539b1989bf632c2be00354ce5567d9 Mon Sep 17 00:00:00 2001 From: clint shryock Date: Tue, 3 Nov 2015 16:30:18 -0600 Subject: [PATCH 06/10] go fmt after rebase --- builtin/providers/aws/resource_aws_elb.go | 122 +++++++++++----------- builtin/providers/aws/structure.go | 20 ++-- 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 8d64e0588..3fa0b1ddb 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -101,35 +101,35 @@ func resourceAwsElb() *schema.Resource { Default: 300, }, - "access_logs": &schema.Schema{ - Type: schema.TypeSet, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "enabled": &schema.Schema{ - Type: schema.TypeBool, - Optional: true, - Default: true, - }, - "interval": &schema.Schema{ - Type: schema.TypeInt, - Optional: true, - Default: 60, - }, - "bucket": &schema.Schema{ - Type: schema.TypeString, - Required: true, - }, - "bucket_prefix": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - }, - }, - }, - Set: resourceAwsElbAccessLogsHash, - }, + "access_logs": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "enabled": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + "interval": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 60, + }, + "bucket": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "bucket_prefix": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + }, + }, + Set: resourceAwsElbAccessLogsHash, + }, - "listener": &schema.Schema{ + "listener": &schema.Schema{ Type: schema.TypeSet, Required: true, Elem: &schema.Resource{ @@ -330,13 +330,13 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { d.Set("source_security_group", lb.SourceSecurityGroup.GroupName) } d.Set("subnets", lb.Subnets) - d.Set("idle_timeout", lbAttrs.ConnectionSettings.IdleTimeout) - d.Set("connection_draining", lbAttrs.ConnectionDraining.Enabled) - d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout) - d.Set("access_logs", flattenAccessLog(lbAttrs.AccessLog)) + d.Set("idle_timeout", lbAttrs.ConnectionSettings.IdleTimeout) + d.Set("connection_draining", lbAttrs.ConnectionDraining.Enabled) + d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout) + d.Set("access_logs", flattenAccessLog(lbAttrs.AccessLog)) - resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{ - LoadBalancerNames: []*string{lb.LoadBalancerName}, + resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{ + LoadBalancerNames: []*string{lb.LoadBalancerName}, }) var et []*elb.Tag @@ -434,7 +434,7 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { d.SetPartial("instances") } - if d.HasChange("cross_zone_load_balancing") || d.HasChange("idle_timeout") || d.HasChange("access_logs") { + if d.HasChange("cross_zone_load_balancing") || d.HasChange("idle_timeout") || d.HasChange("access_logs") { attrs := elb.ModifyLoadBalancerAttributesInput{ LoadBalancerName: aws.String(d.Get("name").(string)), LoadBalancerAttributes: &elb.LoadBalancerAttributes{ @@ -445,25 +445,25 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { IdleTimeout: aws.Int64(int64(d.Get("idle_timeout").(int))), }, }, - } + } - logs := d.Get("access_logs").(*schema.Set).List() - if len(logs) > 1 { - return fmt.Errorf("Only one access logs config per ELB is supported") - } else if len(logs) == 1 { - log := logs[0].(map[string]interface{}) - accessLogs := &elb.AccessLog{ - Enabled: aws.Bool(log["enabled"].(bool)), - EmitInterval: aws.Int64(int64(log["interval"].(int))), - S3BucketName: aws.String(log["bucket"].(string)), - } + logs := d.Get("access_logs").(*schema.Set).List() + if len(logs) > 1 { + return fmt.Errorf("Only one access logs config per ELB is supported") + } else if len(logs) == 1 { + log := logs[0].(map[string]interface{}) + accessLogs := &elb.AccessLog{ + Enabled: aws.Bool(log["enabled"].(bool)), + EmitInterval: aws.Int64(int64(log["interval"].(int))), + S3BucketName: aws.String(log["bucket"].(string)), + } - if log["bucket_prefix"] != "" { - accessLogs.S3BucketPrefix = aws.String(log["bucket_prefix"].(string)) - } + if log["bucket_prefix"] != "" { + accessLogs.S3BucketPrefix = aws.String(log["bucket_prefix"].(string)) + } - attrs.LoadBalancerAttributes.AccessLog = accessLogs - } + attrs.LoadBalancerAttributes.AccessLog = accessLogs + } _, err := elbconn.ModifyLoadBalancerAttributes(&attrs) if err != nil { @@ -598,17 +598,17 @@ func resourceAwsElbHealthCheckHash(v interface{}) int { } func resourceAwsElbAccessLogsHash(v interface{}) int { - var buf bytes.Buffer - m := v.(map[string]interface{}) - buf.WriteString(fmt.Sprintf("%t-", m["enabled"].(bool))) - buf.WriteString(fmt.Sprintf("%d-", m["interval"].(int))) - buf.WriteString(fmt.Sprintf("%s-", - strings.ToLower(m["bucket"].(string)))) - if v, ok := m["bucket_prefix"]; ok { - buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(v.(string)))) - } + var buf bytes.Buffer + m := v.(map[string]interface{}) + buf.WriteString(fmt.Sprintf("%t-", m["enabled"].(bool))) + buf.WriteString(fmt.Sprintf("%d-", m["interval"].(int))) + buf.WriteString(fmt.Sprintf("%s-", + strings.ToLower(m["bucket"].(string)))) + if v, ok := m["bucket_prefix"]; ok { + buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(v.(string)))) + } - return hashcode.String(buf.String()) + return hashcode.String(buf.String()) } func resourceAwsElbListenerHash(v interface{}) int { diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index e7590001f..9a9260df7 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -236,18 +236,18 @@ func expandElastiCacheParameters(configured []interface{}) ([]*elasticache.Param // Flattens an access log into something that flatmap.Flatten() can handle func flattenAccessLog(log *elb.AccessLog) []map[string]interface{} { - result := make([]map[string]interface{}, 0, 1) + result := make([]map[string]interface{}, 0, 1) - if log != nil { - result = append(result, map[string]interface{}{ - "enabled": *log.Enabled, - "interval": *log.EmitInterval, - "bucket": *log.S3BucketName, - "bucket_prefix": *log.S3BucketPrefix, - }) - } + if log != nil { + result = append(result, map[string]interface{}{ + "enabled": *log.Enabled, + "interval": *log.EmitInterval, + "bucket": *log.S3BucketName, + "bucket_prefix": *log.S3BucketPrefix, + }) + } - return result + return result } // Flattens a health check into something that flatmap.Flatten() From 5f7254eb1a689c0c716700cfa08f45993d268af7 Mon Sep 17 00:00:00 2001 From: clint shryock Date: Wed, 4 Nov 2015 11:50:34 -0600 Subject: [PATCH 07/10] providers/aws: Add ELB Access Logs (continues #3708) - continues #3708 - adds some tests - other fixes I found along the way --- builtin/providers/aws/resource_aws_elb.go | 20 ++-- .../providers/aws/resource_aws_elb_test.go | 104 ++++++++++++++++++ builtin/providers/aws/structure.go | 23 +++- 3 files changed, 133 insertions(+), 14 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 3fa0b1ddb..cc398c69b 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -106,11 +106,6 @@ func resourceAwsElb() *schema.Resource { Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "enabled": &schema.Schema{ - Type: schema.TypeBool, - Optional: true, - Default: true, - }, "interval": &schema.Schema{ Type: schema.TypeInt, Optional: true, @@ -333,7 +328,11 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { d.Set("idle_timeout", lbAttrs.ConnectionSettings.IdleTimeout) d.Set("connection_draining", lbAttrs.ConnectionDraining.Enabled) d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout) - d.Set("access_logs", flattenAccessLog(lbAttrs.AccessLog)) + if lbAttrs.AccessLog != nil { + if err := d.Set("access_logs", flattenAccessLog(lbAttrs.AccessLog)); err != nil { + log.Printf("[WARN] Error setting ELB Access Logs for (%s): %s", d.Id(), err) + } + } resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{ LoadBalancerNames: []*string{lb.LoadBalancerName}, @@ -453,7 +452,7 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { } else if len(logs) == 1 { log := logs[0].(map[string]interface{}) accessLogs := &elb.AccessLog{ - Enabled: aws.Bool(log["enabled"].(bool)), + Enabled: aws.Bool(true), EmitInterval: aws.Int64(int64(log["interval"].(int))), S3BucketName: aws.String(log["bucket"].(string)), } @@ -463,8 +462,14 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { } attrs.LoadBalancerAttributes.AccessLog = accessLogs + } else if len(logs) == 0 { + // disable access logs + attrs.LoadBalancerAttributes.AccessLog = &elb.AccessLog{ + Enabled: aws.Bool(false), + } } + log.Printf("[DEBUG] ELB Modify Load Balancer Attributes Request: %#v", attrs) _, err := elbconn.ModifyLoadBalancerAttributes(&attrs) if err != nil { return fmt.Errorf("Failure configuring ELB attributes: %s", err) @@ -600,7 +605,6 @@ func resourceAwsElbHealthCheckHash(v interface{}) int { func resourceAwsElbAccessLogsHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) - buf.WriteString(fmt.Sprintf("%t-", m["enabled"].(bool))) buf.WriteString(fmt.Sprintf("%d-", m["interval"].(int))) buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["bucket"].(string)))) diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index dadf4aba3..242365de2 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -75,6 +75,52 @@ func TestAccAWSELB_fullCharacterRange(t *testing.T) { }) } +func TestAccAWSELB_AccessLogs(t *testing.T) { + var conf elb.LoadBalancerDescription + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSELBDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSELBAccessLogs, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSELBExists("aws_elb.foo", &conf), + resource.TestCheckResourceAttr( + "aws_elb.foo", "name", "FoobarTerraform-test123"), + ), + }, + + resource.TestStep{ + Config: testAccAWSELBAccessLogsOn, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSELBExists("aws_elb.foo", &conf), + resource.TestCheckResourceAttr( + "aws_elb.foo", "name", "FoobarTerraform-test123"), + resource.TestCheckResourceAttr( + "aws_elb.foo", "access_logs.#", "1"), + resource.TestCheckResourceAttr( + "aws_elb.foo", "access_logs.1713209538.bucket", "terraform-access-logs-bucket"), + resource.TestCheckResourceAttr( + "aws_elb.foo", "access_logs.1713209538.interval", "5"), + ), + }, + + resource.TestStep{ + Config: testAccAWSELBAccessLogs, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSELBExists("aws_elb.foo", &conf), + resource.TestCheckResourceAttr( + "aws_elb.foo", "name", "FoobarTerraform-test123"), + resource.TestCheckResourceAttr( + "aws_elb.foo", "access_logs.#", "0"), + ), + }, + }, + }) +} + func TestAccAWSELB_generatedName(t *testing.T) { var conf elb.LoadBalancerDescription generatedNameRegexp := regexp.MustCompile("^tf-lb-") @@ -650,6 +696,64 @@ resource "aws_elb" "foo" { } ` +const testAccAWSELBAccessLogs = ` +resource "aws_elb" "foo" { + name = "FoobarTerraform-test123" + availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] + + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} +` +const testAccAWSELBAccessLogsOn = ` +# an S3 bucket configured for Access logs +# The 797873946194 is the AWS ID for us-west-2, so this test +# must be ran in us-west-2 +resource "aws_s3_bucket" "acceslogs_bucket" { + bucket = "terraform-access-logs-bucket" + acl = "private" + force_destroy = true + policy = < Date: Tue, 3 Nov 2015 23:15:02 -0800 Subject: [PATCH 08/10] Add website docs. --- .../source/docs/providers/aws/r/elb.html.markdown | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/website/source/docs/providers/aws/r/elb.html.markdown b/website/source/docs/providers/aws/r/elb.html.markdown index 824a5507f..13cf517b9 100644 --- a/website/source/docs/providers/aws/r/elb.html.markdown +++ b/website/source/docs/providers/aws/r/elb.html.markdown @@ -18,6 +18,13 @@ resource "aws_elb" "bar" { name = "foobar-terraform-elb" availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] + access_logs { + enabled = true + bucket = "foo" + bucket_prefix = "bar" + interval = 60 + } + listener { instance_port = 8000 instance_protocol = "http" @@ -58,6 +65,7 @@ resource "aws_elb" "bar" { The following arguments are supported: * `name` - (Optional) The name of the ELB. By default generated by terraform. +* `access_logs` - (Optional) An Access Logs block. Access Logs documented below. * `availability_zones` - (Required for an EC2-classic ELB) The AZ's to serve traffic in. * `security_groups` - (Optional) A list of security group IDs to assign to the ELB. * `subnets` - (Required for a VPC ELB) A list of subnet IDs to attach to the ELB. @@ -74,6 +82,13 @@ The following arguments are supported: Exactly one of `availability_zones` or `subnets` must be specified: this determines if the ELB exists in a VPC or in EC2-classic. +Access Logs support the following: + +* `bucket` - (Required) The S3 bucket name to store the logs in. +* `bucket_prefix` - (Optional) The S3 bucket prefix. Logs are stored in the root if not configured. +* `enabled` - (Optional) Whether capturing access logs is enabled. Default: true. +* `interval` - (Optional) The publishing interval in minutes. Default: 60 minutes. + Listeners support the following: * `instance_port` - (Required) The port on the instance to route to From e3be1d5f10b60620e83535dd31dd88f4d5d9ff0e Mon Sep 17 00:00:00 2001 From: clint shryock Date: Wed, 4 Nov 2015 14:15:40 -0600 Subject: [PATCH 09/10] update elb access logs docs --- website/source/docs/providers/aws/r/elb.html.markdown | 2 -- 1 file changed, 2 deletions(-) diff --git a/website/source/docs/providers/aws/r/elb.html.markdown b/website/source/docs/providers/aws/r/elb.html.markdown index 13cf517b9..024d71544 100644 --- a/website/source/docs/providers/aws/r/elb.html.markdown +++ b/website/source/docs/providers/aws/r/elb.html.markdown @@ -19,7 +19,6 @@ resource "aws_elb" "bar" { availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] access_logs { - enabled = true bucket = "foo" bucket_prefix = "bar" interval = 60 @@ -86,7 +85,6 @@ Access Logs support the following: * `bucket` - (Required) The S3 bucket name to store the logs in. * `bucket_prefix` - (Optional) The S3 bucket prefix. Logs are stored in the root if not configured. -* `enabled` - (Optional) Whether capturing access logs is enabled. Default: true. * `interval` - (Optional) The publishing interval in minutes. Default: 60 minutes. Listeners support the following: From 8c32536f3dbe1733e6685f9b908c6645ad186170 Mon Sep 17 00:00:00 2001 From: clint shryock Date: Tue, 10 Nov 2015 16:58:24 -0600 Subject: [PATCH 10/10] return err if we fail to set access_logs; other cleanups --- builtin/providers/aws/resource_aws_elb.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index dee5c6396..5ff3b3b28 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -348,7 +348,7 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout) if lbAttrs.AccessLog != nil { if err := d.Set("access_logs", flattenAccessLog(lbAttrs.AccessLog)); err != nil { - log.Printf("[WARN] Error setting ELB Access Logs for (%s): %s", d.Id(), err) + return err } } @@ -469,17 +469,17 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Only one access logs config per ELB is supported") } else if len(logs) == 1 { log := logs[0].(map[string]interface{}) - accessLogs := &elb.AccessLog{ + accessLog := &elb.AccessLog{ Enabled: aws.Bool(true), EmitInterval: aws.Int64(int64(log["interval"].(int))), S3BucketName: aws.String(log["bucket"].(string)), } if log["bucket_prefix"] != "" { - accessLogs.S3BucketPrefix = aws.String(log["bucket_prefix"].(string)) + accessLog.S3BucketPrefix = aws.String(log["bucket_prefix"].(string)) } - attrs.LoadBalancerAttributes.AccessLog = accessLogs + attrs.LoadBalancerAttributes.AccessLog = accessLog } else if len(logs) == 0 { // disable access logs attrs.LoadBalancerAttributes.AccessLog = &elb.AccessLog{