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

Storage bucket doesn't remove lifecycle rules properly #850

Merged
merged 2 commits into from
Dec 14, 2017
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
32 changes: 0 additions & 32 deletions google/import_storage_bucket_test.go

This file was deleted.

39 changes: 39 additions & 0 deletions google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("location", res.Location)
d.Set("cors", flattenCors(res.Cors))
d.Set("versioning", flattenBucketVersioning(res.Versioning))
d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle))
d.Set("labels", res.Labels)
d.Set("project", project)
d.SetId(res.Id)
Expand Down Expand Up @@ -514,6 +515,40 @@ func flattenBucketVersioning(bucketVersioning *storage.BucketVersioning) []map[s
return versionings
}

func flattenBucketLifecycle(lifecycle *storage.BucketLifecycle) []map[string]interface{} {
if lifecycle == nil || lifecycle.Rule == nil {
return []map[string]interface{}{}
}

rules := make([]map[string]interface{}, 0, len(lifecycle.Rule))

for _, rule := range lifecycle.Rule {
rules = append(rules, map[string]interface{}{
"action": schema.NewSet(resourceGCSBucketLifecycleRuleActionHash, []interface{}{flattenBucketLifecycleRuleAction(rule.Action)}),
"condition": schema.NewSet(resourceGCSBucketLifecycleRuleConditionHash, []interface{}{flattenBucketLifecycleRuleCondition(rule.Condition)}),
})
}

return rules
}

func flattenBucketLifecycleRuleAction(action *storage.BucketLifecycleRuleAction) map[string]interface{} {
return map[string]interface{}{
"type": action.Type,
"storage_class": action.StorageClass,
}
}

func flattenBucketLifecycleRuleCondition(condition *storage.BucketLifecycleRuleCondition) map[string]interface{} {
return map[string]interface{}{
"age": int(condition.Age),
"created_before": condition.CreatedBefore,
"is_live": *condition.IsLive,
"matches_storage_class": convertStringArrToInterface(condition.MatchesStorageClass),
"num_newer_versions": int(condition.NumNewerVersions),
}
}

func resourceGCSBucketLifecycleCreateOrUpdate(d *schema.ResourceData, sb *storage.Bucket) error {
if v, ok := d.GetOk("lifecycle_rule"); ok {
lifecycle_rules := v.([]interface{})
Expand Down Expand Up @@ -584,6 +619,10 @@ func resourceGCSBucketLifecycleCreateOrUpdate(d *schema.ResourceData, sb *storag

sb.Lifecycle.Rule = append(sb.Lifecycle.Rule, target_lifecycle_rule)
}
} else {
sb.Lifecycle = &storage.BucketLifecycle{
ForceSendFields: []string{"Rule"},
}
}

return nil
Expand Down
28 changes: 24 additions & 4 deletions google/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func TestAccStorageBucket_basic(t *testing.T) {
"google_storage_bucket.bucket", "force_destroy", "false"),
),
},
resource.TestStep{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
Expand Down Expand Up @@ -132,6 +138,12 @@ func TestAccStorageBucket_lifecycleRules(t *testing.T) {
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.condition.%d.age", hash_step0_lc1_condition), "10"),
),
},
resource.TestStep{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
Expand Down Expand Up @@ -176,6 +188,12 @@ func TestAccStorageBucket_storageClass(t *testing.T) {
"google_storage_bucket.bucket", "location", "US-CENTRAL1"),
),
},
{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
Expand Down Expand Up @@ -209,8 +227,6 @@ func TestAccStorageBucket_update(t *testing.T) {
"google_storage_bucket.bucket", "location", "US"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "force_destroy", "false"),
resource.TestCheckNoResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.#"),
),
},
resource.TestStep{
Expand All @@ -222,8 +238,6 @@ func TestAccStorageBucket_update(t *testing.T) {
"google_storage_bucket.bucket", "location", "EU"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "force_destroy", "true"),
resource.TestCheckNoResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.#"),
),
},
resource.TestStep{
Expand Down Expand Up @@ -353,6 +367,12 @@ func TestAccStorageBucket_versioning(t *testing.T) {
"google_storage_bucket.bucket", "versioning.0.enabled", "true"),
),
},
resource.TestStep{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
Expand Down
22 changes: 7 additions & 15 deletions vendor/google.golang.org/api/storage/v1/storage-api.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading