Skip to content

Commit

Permalink
Set Bucket Id as context variable in TestAccLoggingBucketConfigProjec…
Browse files Browse the repository at this point in the history
…t_* (#9749) (#17067)

* use context variable bucketid

* fix typo

* set bucket create to be async

* add async logic to create function

* add opRes

* add async operation to project_bucket_config resource

* use updateSync to create analytics in logging bucket

* revert use of updateSync

* add time_sleep to allow project permissions to set in analyticsEnabled test

* add time_sleep comment

* add time provider to analyticsEnabled test

* increase time duration

* WIP: time_sleep 10m

* duration change

* add depends_on

* update basic config to check for updateSync usage

* add missing argument

* WIP: making update analytics be async

* async typo

* patch typo
[upstream:941bbdcbb0924ffa3765a1e1dce576306aebe5d6]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Jan 22, 2024
1 parent 9d577db commit d9d02fd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/9749.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
logging: updated the `google_logging_project_bucket_config` resource to be created using the asynchronous create method
```
29 changes: 27 additions & 2 deletions google/services/logging/resource_logging_bucket_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestAccLoggingBucketConfigProject_basic(t *testing.T) {
"project_name": "tf-test-" + acctest.RandString(t, 10),
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
"org_id": envvar.GetTestOrgFromEnv(t),
"bucket_id": "tf-test-bucket-" + acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down Expand Up @@ -99,12 +100,25 @@ func TestAccLoggingBucketConfigProject_analyticsEnabled(t *testing.T) {
"project_name": "tf-test-" + acctest.RandString(t, 10),
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
"org_id": envvar.GetTestOrgFromEnv(t),
"bucket_id": "tf-test-bucket-" + acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
ExternalProviders: map[string]resource.ExternalProvider{
"time": {},
},
Steps: []resource.TestStep{
{
Config: testAccLoggingBucketConfigProject_basic(context, 30),
},
{
ResourceName: "google_logging_project_bucket_config.basic",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"project"},
},
{
Config: testAccLoggingBucketConfigProject_analyticsEnabled(context, true),
},
Expand Down Expand Up @@ -267,7 +281,7 @@ resource "google_logging_project_bucket_config" "basic" {
location = "global"
retention_days = %d
description = "retention test %d days"
bucket_id = "test-bucket"
bucket_id = "%{bucket_id}"
}
`, context), retention, retention)
}
Expand All @@ -281,11 +295,22 @@ resource "google_project" "default" {
billing_account = "%{billing_account}"
}
// time_sleep would allow for permissions to be granted before creating log bucket
resource "time_sleep" "wait_1_minute" {
create_duration = "1m"
depends_on = [
google_project.default,
]
}
resource "google_logging_project_bucket_config" "basic" {
project = google_project.default.name
location = "global"
enable_analytics = %t
bucket_id = "test-bucket"
bucket_id = "%{bucket_id}"
depends_on = [time_sleep.wait_1_minute]
}
`, context), analytics)
}
Expand Down
24 changes: 18 additions & 6 deletions google/services/logging/resource_logging_project_bucket_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func resourceLoggingProjectBucketConfigCreate(d *schema.ResourceData, meta inter
obj["cmekSettings"] = expandCmekSettings(d.Get("cmek_settings"))
obj["indexConfigs"] = expandIndexConfigs(d.Get("index_configs"))

url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}projects/{{project}}/locations/{{location}}/buckets?bucketId={{bucket_id}}")
url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}projects/{{project}}/locations/{{location}}/buckets:createAsync?bucketId={{bucket_id}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -251,6 +251,13 @@ func resourceLoggingProjectBucketConfigCreate(d *schema.ResourceData, meta inter
}

d.SetId(id)
var opRes map[string]interface{}
// Wait for the operation to complete
waitErr := LoggingOperationWaitTimeWithResponse(config, res, &opRes, "Bucket to create", userAgent, d.Timeout(schema.TimeoutCreate))
if waitErr != nil {
d.SetId("")
return waitErr
}

log.Printf("[DEBUG] Finished creating Bucket %q: %#v", d.Id(), res)

Expand Down Expand Up @@ -334,19 +341,24 @@ func resourceLoggingProjectBucketConfigUpdate(d *schema.ResourceData, meta inter
if d.HasChange("enable_analytics") {
obj["analyticsEnabled"] = d.Get("enable_analytics")
updateMaskAnalytics = append(updateMaskAnalytics, "analyticsEnabled")
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMaskAnalytics, ",")})
asyncUrl := url + ":updateAsync"
asyncUrl, err = transport_tpg.AddQueryParams(asyncUrl, map[string]string{"updateMask": strings.Join(updateMaskAnalytics, ",")})
if err != nil {
return err
}
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PATCH",
RawURL: url,
Method: "POST",
RawURL: asyncUrl,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
})
if err != nil {

var opRes map[string]interface{}
// Wait for the operation to complete
waitErr := LoggingOperationWaitTimeWithResponse(config, res, &opRes, "Updating Bucket with analytics", userAgent, d.Timeout(schema.TimeoutCreate))
if waitErr != nil {
return fmt.Errorf("Error updating Logging Bucket Config %q: %s", d.Id(), err)
}
}
Expand Down

0 comments on commit d9d02fd

Please sign in to comment.