Add support for setting bucket's logging config (#946)

* Add support for setting bucket's logging config

* Add log_object_prefix and fix nil Logging condition
This commit is contained in:
Patrick Pelletier 2018-01-25 14:02:08 -05:00 committed by Dana Hoffman
parent 0902a9f686
commit eaac260f36
3 changed files with 141 additions and 0 deletions

View File

@ -214,6 +214,24 @@ func resourceStorageBucket() *schema.Resource {
},
},
},
"logging": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"log_bucket": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"log_object_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
},
}
}
@ -273,6 +291,10 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
sb.Cors = expandCors(v.([]interface{}))
}
if v, ok := d.GetOk("logging"); ok {
sb.Logging = expandBucketLogging(v.([]interface{}))
}
var res *storage.Bucket
err = retry(func() error {
@ -343,6 +365,14 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
sb.Cors = expandCors(v.([]interface{}))
}
if d.HasChange("logging") {
if v, ok := d.GetOk("logging"); ok {
sb.Logging = expandBucketLogging(v.([]interface{}))
} else {
sb.NullFields = append(sb.NullFields, "Logging")
}
}
if d.HasChange("labels") {
sb.Labels = expandLabels(d)
if len(sb.Labels) == 0 {
@ -394,6 +424,7 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("storage_class", res.StorageClass)
d.Set("location", res.Location)
d.Set("cors", flattenCors(res.Cors))
d.Set("logging", flattenBucketLogging(res.Logging))
d.Set("versioning", flattenBucketVersioning(res.Versioning))
d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle))
d.Set("labels", res.Labels)
@ -495,6 +526,34 @@ func flattenCors(corsRules []*storage.BucketCors) []map[string]interface{} {
return corsRulesSchema
}
func expandBucketLogging(configured interface{}) *storage.BucketLogging {
loggings := configured.([]interface{})
logging := loggings[0].(map[string]interface{})
bucketLogging := &storage.BucketLogging{
LogBucket: logging["log_bucket"].(string),
LogObjectPrefix: logging["log_object_prefix"].(string),
}
return bucketLogging
}
func flattenBucketLogging(bucketLogging *storage.BucketLogging) []map[string]interface{} {
loggings := make([]map[string]interface{}, 0, 1)
if bucketLogging == nil {
return loggings
}
logging := map[string]interface{}{
"log_bucket": bucketLogging.LogBucket,
"log_object_prefix": bucketLogging.LogObjectPrefix,
}
loggings = append(loggings, logging)
return loggings
}
func expandBucketVersioning(configured interface{}) *storage.BucketVersioning {
versionings := configured.([]interface{})
versioning := versionings[0].(map[string]interface{})

View File

@ -377,6 +377,56 @@ func TestAccStorageBucket_versioning(t *testing.T) {
})
}
func TestAccStorageBucket_logging(t *testing.T) {
t.Parallel()
var bucket storage.Bucket
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccStorageBucketDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccStorageBucket_logging(bucketName, "log-bucket"),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "logging.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "logging.0.log_bucket", "log-bucket"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "logging.0.log_object_prefix", bucketName),
),
},
resource.TestStep{
Config: testAccStorageBucket_loggingWithPrefix(bucketName, "another-log-bucket", "object-prefix"),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "logging.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "logging.0.log_bucket", "another-log-bucket"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "logging.0.log_object_prefix", "object-prefix"),
),
},
resource.TestStep{
Config: testAccStorageBucket_basic(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "logging.#", "0"),
),
},
},
})
}
func TestAccStorageBucket_cors(t *testing.T) {
t.Parallel()
@ -715,6 +765,29 @@ resource "google_storage_bucket" "bucket" {
`, bucketName)
}
func testAccStorageBucket_logging(bucketName string, logBucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
logging = {
log_bucket = "%s"
}
}
`, bucketName, logBucketName)
}
func testAccStorageBucket_loggingWithPrefix(bucketName string, logBucketName string, prefix string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
logging = {
log_bucket = "%s"
log_object_prefix = "%s"
}
}
`, bucketName, logBucketName, prefix)
}
func testAccStorageBucket_lifecycleRules(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {

View File

@ -62,6 +62,8 @@ The following arguments are supported:
* `labels` - (Optional) A set of key/value label pairs to assign to the bucket.
* `logging` - (Optional) The bucket's [Access & Storage Logs](https://cloud.google.com/storage/docs/access-logs) configuration.
The `lifecycle_rule` block supports:
* `action` - (Required) The Lifecycle Rule's action configuration. A single block of this type is supported. Structure is documented below.
@ -108,6 +110,13 @@ The `cors` block supports:
* `max_age_seconds` - (Optional) The value, in seconds, to return in the [Access-Control-Max-Age header](https://www.w3.org/TR/cors/#access-control-max-age-response-header) used in preflight responses.
The `logging` block supports:
* `log_bucket` - (Required) The bucket that will receive log objects.
* `log_object_prefix` - (Optional, Computed) The object prefix for log objects. If it's not provided,
by default GCS sets this to the log_bucket's name.
## Attributes Reference
In addition to the arguments listed above, the following computed attributes are