Skip to content

Commit

Permalink
Add support for locking to google_logging_project_bucket_config (#8201)…
Browse files Browse the repository at this point in the history
… (#14977)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 23, 2023
1 parent 0cc83a8 commit 3eeed81
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/8201.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
logging: added support for `locked` to `google_logging_project_bucket_config`
```
63 changes: 62 additions & 1 deletion google/resource_logging_bucket_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,42 @@ func TestAccLoggingBucketConfigProject_analyticsEnabled(t *testing.T) {
})
}

func TestAccLoggingBucketConfigProject_locked(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": RandString(t, 10),
"project_name": "tf-test-" + RandString(t, 10),
"org_id": acctest.GetTestOrgFromEnv(t),
"billing_account": acctest.GetTestBillingAccountFromEnv(t),
}

VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccLoggingBucketConfigProject_locked(context, false),
},
{
ResourceName: "google_logging_project_bucket_config.variable_locked",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"project"},
},
{
Config: testAccLoggingBucketConfigProject_locked(context, true),
},
{
ResourceName: "google_logging_project_bucket_config.variable_locked",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"project"},
},
},
})
}

func TestAccLoggingBucketConfigProject_cmekSettings(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -285,6 +321,32 @@ resource "google_logging_project_bucket_config" "basic" {
`, context), analytics)
}

func testAccLoggingBucketConfigProject_locked(context map[string]interface{}, locked bool) string {
return fmt.Sprintf(Nprintf(`
resource "google_project" "default" {
project_id = "%{project_name}"
name = "%{project_name}"
org_id = "%{org_id}"
billing_account = "%{billing_account}"
}
resource "google_logging_project_bucket_config" "fixed_locked" {
project = google_project.default.name
location = "global"
locked = true
bucket_id = "fixed-locked"
}
resource "google_logging_project_bucket_config" "variable_locked" {
project = google_project.default.name
location = "global"
description = "lock status is %v" # test simultaneous update
locked = %t
bucket_id = "variable-locked"
}
`, context), locked, locked)
}

func testAccLoggingBucketConfigProject_preCmekSettings(context map[string]interface{}, keyRingName, cryptoKeyName, cryptoKeyNameUpdate string) string {
return fmt.Sprintf(Nprintf(`
resource "google_project" "default" {
Expand Down Expand Up @@ -444,7 +506,6 @@ resource "google_logging_organization_bucket_config" "basic" {
}

func getLoggingBucketConfigs(context map[string]interface{}) map[string]string {

return map[string]string{
"project": Nprintf(`resource "google_project" "default" {
project_id = "%{project_name}"
Expand Down
1 change: 0 additions & 1 deletion google/services/logging/resource_logging_bucket_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ func resourceLoggingBucketConfigCreate(d *schema.ResourceData, meta interface{},
obj["name"] = d.Get("name")
obj["description"] = d.Get("description")
obj["retentionDays"] = d.Get("retention_days")
obj["locked"] = d.Get("locked")
obj["cmekSettings"] = expandCmekSettings(d.Get("cmek_settings"))

url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}projects/{{project}}/locations/{{location}}/buckets?bucketId={{bucket_id}}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ var loggingProjectBucketConfigSchema = map[string]*schema.Schema{
Computed: true,
Description: `An optional description for this bucket.`,
},
"locked": {
Type: schema.TypeBool,
Optional: true,
Description: `Whether the bucket is locked. The retention period on a locked bucket cannot be changed. Locked buckets may only be deleted if they are empty.`,
},
"retention_days": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -185,9 +190,9 @@ func resourceLoggingProjectBucketConfigCreate(d *schema.ResourceData, meta inter
obj := make(map[string]interface{})
obj["name"] = d.Get("name")
obj["description"] = d.Get("description")
obj["locked"] = d.Get("locked")
obj["retentionDays"] = d.Get("retention_days")
obj["analyticsEnabled"] = d.Get("enable_analytics")
obj["locked"] = d.Get("locked")
obj["cmekSettings"] = expandCmekSettings(d.Get("cmek_settings"))

url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}projects/{{project}}/locations/{{location}}/buckets?bucketId={{bucket_id}}")
Expand Down Expand Up @@ -262,6 +267,9 @@ func resourceLoggingProjectBucketConfigRead(d *schema.ResourceData, meta interfa
if err := d.Set("description", res["description"]); err != nil {
return fmt.Errorf("Error setting description: %s", err)
}
if err := d.Set("locked", res["locked"]); err != nil {
return fmt.Errorf("Error setting locked: %s", err)
}
if err := d.Set("lifecycle_state", res["lifecycleState"]); err != nil {
return fmt.Errorf("Error setting lifecycle_state: %s", err)
}
Expand Down Expand Up @@ -347,6 +355,32 @@ func resourceLoggingProjectBucketConfigUpdate(d *schema.ResourceData, meta inter
return fmt.Errorf("Error updating Logging Bucket Config %q: %s", d.Id(), err)
}

// Check if locked is being changed (although removal will fail). Locking is
// an atomic operation and can not be performed while other fields.
// update locked last so that we lock *after* setting the right settings
if d.HasChange("locked") {
updateMaskLocked := []string{"locked"}
objLocked := map[string]interface{}{
"locked": d.Get("locked"),
}
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMaskLocked, ",")})
if err != nil {
return err
}

_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PATCH",
RawURL: url,
UserAgent: userAgent,
Body: objLocked,
Timeout: d.Timeout(schema.TimeoutUpdate),
})
if err != nil {
return fmt.Errorf("Error updating Logging Bucket Config %q: %s", d.Id(), err)
}
}

return resourceLoggingProjectBucketConfigRead(d, meta)
}

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/logging_project_bucket_config.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ The following arguments are supported:

* `description` - (Optional) Describes this bucket.

* `locked` - (Optional) Whether the bucket is locked. The retention period on a locked bucket cannot be changed. Locked buckets may only be deleted if they are empty.

* `retention_days` - (Optional) Logs will be retained by default for this amount of time, after which they will automatically be deleted. The minimum retention period is 1 day. If this value is set to zero at bucket creation time, the default time of 30 days will be used.

* `enable_analytics` - (Optional) Whether or not Log Analytics is enabled. Logs for buckets with Log Analytics enabled can be queried in the **Log Analytics** page using SQL queries. Cannot be disabled once enabled.
Expand Down

0 comments on commit 3eeed81

Please sign in to comment.