Skip to content

Commit 3eeed81

Browse files
Add support for locking to google_logging_project_bucket_config (#8201) (#14977)
Signed-off-by: Modular Magician <[email protected]>
1 parent 0cc83a8 commit 3eeed81

5 files changed

+102
-3
lines changed

.changelog/8201.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
logging: added support for `locked` to `google_logging_project_bucket_config`
3+
```

google/resource_logging_bucket_config_test.go

+62-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,42 @@ func TestAccLoggingBucketConfigProject_analyticsEnabled(t *testing.T) {
124124
})
125125
}
126126

127+
func TestAccLoggingBucketConfigProject_locked(t *testing.T) {
128+
t.Parallel()
129+
130+
context := map[string]interface{}{
131+
"random_suffix": RandString(t, 10),
132+
"project_name": "tf-test-" + RandString(t, 10),
133+
"org_id": acctest.GetTestOrgFromEnv(t),
134+
"billing_account": acctest.GetTestBillingAccountFromEnv(t),
135+
}
136+
137+
VcrTest(t, resource.TestCase{
138+
PreCheck: func() { acctest.AccTestPreCheck(t) },
139+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
140+
Steps: []resource.TestStep{
141+
{
142+
Config: testAccLoggingBucketConfigProject_locked(context, false),
143+
},
144+
{
145+
ResourceName: "google_logging_project_bucket_config.variable_locked",
146+
ImportState: true,
147+
ImportStateVerify: true,
148+
ImportStateVerifyIgnore: []string{"project"},
149+
},
150+
{
151+
Config: testAccLoggingBucketConfigProject_locked(context, true),
152+
},
153+
{
154+
ResourceName: "google_logging_project_bucket_config.variable_locked",
155+
ImportState: true,
156+
ImportStateVerify: true,
157+
ImportStateVerifyIgnore: []string{"project"},
158+
},
159+
},
160+
})
161+
}
162+
127163
func TestAccLoggingBucketConfigProject_cmekSettings(t *testing.T) {
128164
t.Parallel()
129165

@@ -285,6 +321,32 @@ resource "google_logging_project_bucket_config" "basic" {
285321
`, context), analytics)
286322
}
287323

324+
func testAccLoggingBucketConfigProject_locked(context map[string]interface{}, locked bool) string {
325+
return fmt.Sprintf(Nprintf(`
326+
resource "google_project" "default" {
327+
project_id = "%{project_name}"
328+
name = "%{project_name}"
329+
org_id = "%{org_id}"
330+
billing_account = "%{billing_account}"
331+
}
332+
333+
resource "google_logging_project_bucket_config" "fixed_locked" {
334+
project = google_project.default.name
335+
location = "global"
336+
locked = true
337+
bucket_id = "fixed-locked"
338+
}
339+
340+
resource "google_logging_project_bucket_config" "variable_locked" {
341+
project = google_project.default.name
342+
location = "global"
343+
description = "lock status is %v" # test simultaneous update
344+
locked = %t
345+
bucket_id = "variable-locked"
346+
}
347+
`, context), locked, locked)
348+
}
349+
288350
func testAccLoggingBucketConfigProject_preCmekSettings(context map[string]interface{}, keyRingName, cryptoKeyName, cryptoKeyNameUpdate string) string {
289351
return fmt.Sprintf(Nprintf(`
290352
resource "google_project" "default" {
@@ -444,7 +506,6 @@ resource "google_logging_organization_bucket_config" "basic" {
444506
}
445507

446508
func getLoggingBucketConfigs(context map[string]interface{}) map[string]string {
447-
448509
return map[string]string{
449510
"project": Nprintf(`resource "google_project" "default" {
450511
project_id = "%{project_name}"

google/services/logging/resource_logging_bucket_config.go

-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ func resourceLoggingBucketConfigCreate(d *schema.ResourceData, meta interface{},
204204
obj["name"] = d.Get("name")
205205
obj["description"] = d.Get("description")
206206
obj["retentionDays"] = d.Get("retention_days")
207-
obj["locked"] = d.Get("locked")
208207
obj["cmekSettings"] = expandCmekSettings(d.Get("cmek_settings"))
209208

210209
url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}projects/{{project}}/locations/{{location}}/buckets?bucketId={{bucket_id}}")

google/services/logging/resource_logging_project_bucket_config.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ var loggingProjectBucketConfigSchema = map[string]*schema.Schema{
4444
Computed: true,
4545
Description: `An optional description for this bucket.`,
4646
},
47+
"locked": {
48+
Type: schema.TypeBool,
49+
Optional: true,
50+
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.`,
51+
},
4752
"retention_days": {
4853
Type: schema.TypeInt,
4954
Optional: true,
@@ -185,9 +190,9 @@ func resourceLoggingProjectBucketConfigCreate(d *schema.ResourceData, meta inter
185190
obj := make(map[string]interface{})
186191
obj["name"] = d.Get("name")
187192
obj["description"] = d.Get("description")
193+
obj["locked"] = d.Get("locked")
188194
obj["retentionDays"] = d.Get("retention_days")
189195
obj["analyticsEnabled"] = d.Get("enable_analytics")
190-
obj["locked"] = d.Get("locked")
191196
obj["cmekSettings"] = expandCmekSettings(d.Get("cmek_settings"))
192197

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

358+
// Check if locked is being changed (although removal will fail). Locking is
359+
// an atomic operation and can not be performed while other fields.
360+
// update locked last so that we lock *after* setting the right settings
361+
if d.HasChange("locked") {
362+
updateMaskLocked := []string{"locked"}
363+
objLocked := map[string]interface{}{
364+
"locked": d.Get("locked"),
365+
}
366+
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMaskLocked, ",")})
367+
if err != nil {
368+
return err
369+
}
370+
371+
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
372+
Config: config,
373+
Method: "PATCH",
374+
RawURL: url,
375+
UserAgent: userAgent,
376+
Body: objLocked,
377+
Timeout: d.Timeout(schema.TimeoutUpdate),
378+
})
379+
if err != nil {
380+
return fmt.Errorf("Error updating Logging Bucket Config %q: %s", d.Id(), err)
381+
}
382+
}
383+
350384
return resourceLoggingProjectBucketConfigRead(d, meta)
351385
}
352386

website/docs/r/logging_project_bucket_config.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ The following arguments are supported:
105105

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

108+
* `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.
109+
108110
* `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.
109111

110112
* `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.

0 commit comments

Comments
 (0)