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_* (GoogleCloudPlatform#9749)

* 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
  • Loading branch information
BBBmau authored Jan 22, 2024
1 parent ff9f783 commit 941bbdc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 @@ -97,12 +98,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 @@ -265,7 +279,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 @@ -279,11 +293,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
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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 @@ -249,6 +249,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 @@ -332,19 +339,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 941bbdc

Please sign in to comment.