diff --git a/.changelog/4033.txt b/.changelog/4033.txt new file mode 100644 index 00000000000..195deff171b --- /dev/null +++ b/.changelog/4033.txt @@ -0,0 +1,4 @@ +```release-note:enhancement +logging: added bucket creation based on custom-id given for the resource `google_logging_project_bucket_config` + +``` diff --git a/google/resource_logging_bucket_config.go b/google/resource_logging_bucket_config.go index 3b40d94ee95..25305067636 100644 --- a/google/resource_logging_bucket_config.go +++ b/google/resource_logging_bucket_config.go @@ -52,7 +52,7 @@ type loggingBucketConfigIDFunc func(d *schema.ResourceData, config *Config) (str // config resource. In practice the only difference between these resources is the url location. func ResourceLoggingBucketConfig(parentType string, parentSpecificSchema map[string]*schema.Schema, iDFunc loggingBucketConfigIDFunc) *schema.Resource { return &schema.Resource{ - Create: resourceLoggingBucketConfigAcquire(iDFunc), + Create: resourceLoggingBucketConfigAcquireOrCreate(parentType, iDFunc), Read: resourceLoggingBucketConfigRead, Update: resourceLoggingBucketConfigUpdate, Delete: resourceLoggingBucketConfigDelete, @@ -104,9 +104,13 @@ func resourceLoggingBucketConfigImportState(parent string) schema.StateFunc { } } -func resourceLoggingBucketConfigAcquire(iDFunc loggingBucketConfigIDFunc) func(*schema.ResourceData, interface{}) error { +func resourceLoggingBucketConfigAcquireOrCreate(parentType string, iDFunc loggingBucketConfigIDFunc) func(*schema.ResourceData, interface{}) error { return func(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } id, err := iDFunc(d, config) if err != nil { @@ -115,10 +119,68 @@ func resourceLoggingBucketConfigAcquire(iDFunc loggingBucketConfigIDFunc) func(* d.SetId(id) + if parentType == "project" { + //logging bucket can be created only at the project level, in future api may allow for folder, org and other parent resources + + log.Printf("[DEBUG] Fetching logging bucket config: %#v", d.Id()) + url, err := replaceVars(d, config, fmt.Sprintf("{{LoggingBasePath}}%s", d.Id())) + if err != nil { + return err + } + + res, _ := sendRequest(config, "GET", "", url, userAgent, nil) + if res == nil { + log.Printf("[DEGUG] Loggin Bucket not exist %s", d.Id()) + return resourceLoggingBucketConfigCreate(d, meta) + } + } + return resourceLoggingBucketConfigUpdate(d, meta) } } +func resourceLoggingBucketConfigCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + obj["name"] = d.Get("name") + obj["description"] = d.Get("description") + obj["retentionDays"] = d.Get("retention_days") + obj["locked"] = d.Get("locked") + + url, err := replaceVars(d, config, "{{LoggingBasePath}}projects/{{project}}/locations/{{location}}/buckets?bucketId={{bucket_id}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Bucket: %#v", obj) + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return err + } + billingProject = project + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) + if err != nil { + return fmt.Errorf("Error creating Bucket: %s", err) + } + + log.Printf("[DEBUG] Finished creating Bucket %q: %#v", d.Id(), res) + + return resourceLoggingBucketConfigRead(d, meta) +} + func resourceLoggingBucketConfigRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) userAgent, err := generateUserAgentString(d, config.userAgent) @@ -185,7 +247,6 @@ func resourceLoggingBucketConfigUpdate(d *schema.ResourceData, meta interface{}) if err != nil { return err } - _, err = sendRequestWithTimeout(config, "PATCH", "", url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) if err != nil { return fmt.Errorf("Error updating Logging Bucket Config %q: %s", d.Id(), err) diff --git a/google/resource_logging_bucket_config_test.go b/google/resource_logging_bucket_config_test.go index 22cfdc8b2fc..5c2b4b27614 100644 --- a/google/resource_logging_bucket_config_test.go +++ b/google/resource_logging_bucket_config_test.go @@ -181,6 +181,38 @@ resource "google_logging_project_bucket_config" "basic" { `, context), retention, retention) } +func TestAccLoggingBucketConfig_CreateBuckets_withCustomId(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "billing_account_name": "billingAccounts/" + getTestBillingAccountFromEnv(t), + "org_id": getTestOrgFromEnv(t), + "project_name": "tf-test-" + randString(t, 10), + "bucket_id": "tf-test-bucket-" + randString(t, 10), + } + + configList := getLoggingBucketConfigs(context) + + for res, config := range configlst { + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + ResourceName: fmt.Sprintf("google_logging_%s_bucket_config.basic", res), + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{res}, + }, + }, + }) + } +} + func testAccLoggingBucketConfigBillingAccount_basic(context map[string]interface{}, retention int) string { return fmt.Sprintf(Nprintf(` @@ -213,3 +245,23 @@ resource "google_logging_organization_bucket_config" "basic" { } `, context), retention, retention) } + +func getLoggingBucketConfigs(context map[string]interface{}) map[string]string { + + return map[string]string{ + "project": Nprintf(`resource "google_project" "default" { + project_id = "%{project_name}" + name = "%{project_name}" + org_id = "%{org_id}" + } + + resource "google_logging_project_bucket_config" "basic" { + project = google_project.default.name + location = "global" + retention_days = 10 + description = "retention test 10 days" + bucket_id = "%{bucket_id}" + }`, context), + } + +} diff --git a/website/docs/r/logging_project_bucket_config.html.markdown b/website/docs/r/logging_project_bucket_config.html.markdown index 10bce5197ab..423ead5d1f5 100644 --- a/website/docs/r/logging_project_bucket_config.html.markdown +++ b/website/docs/r/logging_project_bucket_config.html.markdown @@ -32,6 +32,17 @@ resource "google_logging_project_bucket_config" "basic" { } ``` +Create logging bucket with customId + +```hcl +resource "google_logging_project_bucket_config" "basic" { + project = "project_id" + location = "global" + retention_days = 30 + bucket_id = "custom-bucket" +} +``` + ## Argument Reference The following arguments are supported: