Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stackdriver_logging_config to cloud_tasks_queue resource #7487

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/4077.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
cloud_tasks: added stackdriver_logging_config` field to `cloud_tasks_queue` resource
```
76 changes: 76 additions & 0 deletions google/resource_cloud_tasks_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ specifies that the task should be retried.`,
},
},
},
"stackdriver_logging_config": {
Type: schema.TypeList,
Optional: true,
Description: `Configuration options for writing logs to Stackdriver Logging.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sampling_ratio": {
Type: schema.TypeFloat,
Required: true,
Description: `Specifies the fraction of operations to write to Stackdriver Logging.
This field may contain any value between 0.0 and 1.0, inclusive. 0.0 is the
default and means that no operations are logged.`,
},
},
},
},
"project": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -244,6 +261,12 @@ func resourceCloudTasksQueueCreate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("retry_config"); !isEmptyValue(reflect.ValueOf(retryConfigProp)) && (ok || !reflect.DeepEqual(v, retryConfigProp)) {
obj["retryConfig"] = retryConfigProp
}
stackdriverLoggingConfigProp, err := expandCloudTasksQueueStackdriverLoggingConfig(d.Get("stackdriver_logging_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("stackdriver_logging_config"); !isEmptyValue(reflect.ValueOf(stackdriverLoggingConfigProp)) && (ok || !reflect.DeepEqual(v, stackdriverLoggingConfigProp)) {
obj["stackdriverLoggingConfig"] = stackdriverLoggingConfigProp
}

url, err := replaceVars(d, config, "{{CloudTasksBasePath}}projects/{{project}}/locations/{{location}}/queues")
if err != nil {
Expand Down Expand Up @@ -327,6 +350,9 @@ func resourceCloudTasksQueueRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("retry_config", flattenCloudTasksQueueRetryConfig(res["retryConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Queue: %s", err)
}
if err := d.Set("stackdriver_logging_config", flattenCloudTasksQueueStackdriverLoggingConfig(res["stackdriverLoggingConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Queue: %s", err)
}

return nil
}
Expand Down Expand Up @@ -366,6 +392,12 @@ func resourceCloudTasksQueueUpdate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("retry_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, retryConfigProp)) {
obj["retryConfig"] = retryConfigProp
}
stackdriverLoggingConfigProp, err := expandCloudTasksQueueStackdriverLoggingConfig(d.Get("stackdriver_logging_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("stackdriver_logging_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, stackdriverLoggingConfigProp)) {
obj["stackdriverLoggingConfig"] = stackdriverLoggingConfigProp
}

url, err := replaceVars(d, config, "{{CloudTasksBasePath}}projects/{{project}}/locations/{{location}}/queues/{{name}}")
if err != nil {
Expand All @@ -386,6 +418,10 @@ func resourceCloudTasksQueueUpdate(d *schema.ResourceData, meta interface{}) err
if d.HasChange("retry_config") {
updateMask = append(updateMask, "retryConfig")
}

if d.HasChange("stackdriver_logging_config") {
updateMask = append(updateMask, "stackdriverLoggingConfig")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -615,6 +651,23 @@ func flattenCloudTasksQueueRetryConfigMaxDoublings(v interface{}, d *schema.Reso
return v // let terraform core handle it otherwise
}

func flattenCloudTasksQueueStackdriverLoggingConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["sampling_ratio"] =
flattenCloudTasksQueueStackdriverLoggingConfigSamplingRatio(original["samplingRatio"], d, config)
return []interface{}{transformed}
}
func flattenCloudTasksQueueStackdriverLoggingConfigSamplingRatio(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandCloudTasksQueueName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return replaceVars(d, config, "projects/{{project}}/locations/{{location}}/queues/{{name}}")
}
Expand Down Expand Up @@ -786,3 +839,26 @@ func expandCloudTasksQueueRetryConfigMaxBackoff(v interface{}, d TerraformResour
func expandCloudTasksQueueRetryConfigMaxDoublings(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudTasksQueueStackdriverLoggingConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedSamplingRatio, err := expandCloudTasksQueueStackdriverLoggingConfigSamplingRatio(original["sampling_ratio"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedSamplingRatio); val.IsValid() && !isEmptyValue(val) {
transformed["samplingRatio"] = transformedSamplingRatio
}

return transformed, nil
}

func expandCloudTasksQueueStackdriverLoggingConfigSamplingRatio(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
60 changes: 60 additions & 0 deletions google/resource_cloud_tasks_queue_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,66 @@ resource "google_cloud_tasks_queue" "default" {
`, context)
}

func TestAccCloudTasksQueue_cloudTasksQueueAdvancedExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
CheckDestroy: testAccCheckCloudTasksQueueDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccCloudTasksQueue_cloudTasksQueueAdvancedExample(context),
},
{
ResourceName: "google_cloud_tasks_queue.advanced_configuration",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "app_engine_routing_override.0.service", "app_engine_routing_override.0.version", "app_engine_routing_override.0.instance"},
},
},
})
}

func testAccCloudTasksQueue_cloudTasksQueueAdvancedExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_cloud_tasks_queue" "advanced_configuration" {
name = "tf-test-instance-name%{random_suffix}"
location = "us-central1"

app_engine_routing_override {
service = "worker"
version = "1.0"
instance = "test"
}

rate_limits {
max_concurrent_dispatches = 3
max_dispatches_per_second = 2
}

retry_config {
max_attempts = 5
max_retry_duration = "4s"
max_backoff = "3s"
min_backoff = "2s"
max_doublings = 1
}

stackdriver_logging_config {
sampling_ratio = 0.9
}
}
`, context)
}

func testAccCheckCloudTasksQueueDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
14 changes: 11 additions & 3 deletions google/resource_cloud_tasks_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ resource "google_cloud_tasks_queue" "default" {
retry_config {
max_attempts = 5
}

}
`, name)
}
Expand Down Expand Up @@ -106,7 +106,11 @@ resource "google_cloud_tasks_queue" "default" {
max_backoff = "3s"
min_backoff = "2s"
max_doublings = 1
}
}

stackdriver_logging_config {
sampling_ratio = 0.9
}
}
`, name)
}
Expand Down Expand Up @@ -134,7 +138,11 @@ resource "google_cloud_tasks_queue" "default" {
max_backoff = "4s"
min_backoff = "3s"
max_doublings = 2
}
}

stackdriver_logging_config {
sampling_ratio = 0.1
}
}
`, name)
}
50 changes: 50 additions & 0 deletions website/docs/r/cloud_tasks_queue.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,43 @@ resource "google_cloud_tasks_queue" "default" {
location = "us-central1"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=cloud_tasks_queue_advanced&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Cloud Tasks Queue Advanced


```hcl
resource "google_cloud_tasks_queue" "advanced_configuration" {
name = "instance-name"
location = "us-central1"

app_engine_routing_override {
service = "worker"
version = "1.0"
instance = "test"
}

rate_limits {
max_concurrent_dispatches = 3
max_dispatches_per_second = 2
}

retry_config {
max_attempts = 5
max_retry_duration = "4s"
max_backoff = "3s"
min_backoff = "2s"
max_doublings = 1
}

stackdriver_logging_config {
sampling_ratio = 0.9
}
}
```

## Argument Reference

Expand Down Expand Up @@ -80,6 +117,11 @@ The following arguments are supported:
Settings that determine the retry behavior.
Structure is documented below.

* `stackdriver_logging_config` -
(Optional)
Configuration options for writing logs to Stackdriver Logging.
Structure is documented below.

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

Expand Down Expand Up @@ -165,6 +207,14 @@ The `retry_config` block supports:
then increases linearly, and finally retries retries at intervals of maxBackoff
up to maxAttempts times.

The `stackdriver_logging_config` block supports:

* `sampling_ratio` -
(Required)
Specifies the fraction of operations to write to Stackdriver Logging.
This field may contain any value between 0.0 and 1.0, inclusive. 0.0 is the
default and means that no operations are logged.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down