Skip to content

Commit

Permalink
Support logs bucket creation and custom ID's (#4033) (#7492)
Browse files Browse the repository at this point in the history
* initial commit

* for review

* added resource create method and tests

* tabular format tests added

* pr comments implemented

* liniting issues fixed

* import error fixed

* linting issue fixed

* logging bucket creation added only at the project level`

* extra spaces removed

* example added

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 12, 2020
1 parent a7e4943 commit d7a0941
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .changelog/4033.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
logging: added bucket creation based on custom-id given for the resource `google_logging_project_bucket_config`

```
67 changes: 64 additions & 3 deletions google/resource_logging_bucket_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions google/resource_logging_bucket_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down Expand Up @@ -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),
}

}
11 changes: 11 additions & 0 deletions website/docs/r/logging_project_bucket_config.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d7a0941

Please sign in to comment.