diff --git a/.changelog/4077.txt b/.changelog/4077.txt new file mode 100644 index 00000000000..6009baaf1fe --- /dev/null +++ b/.changelog/4077.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +cloud_tasks: added stackdriver_logging_config` field to `cloud_tasks_queue` resource +``` diff --git a/google/resource_cloud_tasks_queue.go b/google/resource_cloud_tasks_queue.go index f976bc1b600..c9d9fb83277 100644 --- a/google/resource_cloud_tasks_queue.go +++ b/google/resource_cloud_tasks_queue.go @@ -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, @@ -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 { @@ -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 } @@ -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 { @@ -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, ",")}) @@ -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}}") } @@ -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 +} diff --git a/google/resource_cloud_tasks_queue_generated_test.go b/google/resource_cloud_tasks_queue_generated_test.go index 737cb4d0f74..251d80c172b 100644 --- a/google/resource_cloud_tasks_queue_generated_test.go +++ b/google/resource_cloud_tasks_queue_generated_test.go @@ -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 { diff --git a/google/resource_cloud_tasks_queue_test.go b/google/resource_cloud_tasks_queue_test.go index 5b1a3317678..66530d44cf1 100644 --- a/google/resource_cloud_tasks_queue_test.go +++ b/google/resource_cloud_tasks_queue_test.go @@ -78,7 +78,7 @@ resource "google_cloud_tasks_queue" "default" { retry_config { max_attempts = 5 } - + } `, name) } @@ -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) } @@ -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) } diff --git a/website/docs/r/cloud_tasks_queue.html.markdown b/website/docs/r/cloud_tasks_queue.html.markdown index 8d2df81a6ba..e5d520aabea 100644 --- a/website/docs/r/cloud_tasks_queue.html.markdown +++ b/website/docs/r/cloud_tasks_queue.html.markdown @@ -40,6 +40,43 @@ resource "google_cloud_tasks_queue" "default" { location = "us-central1" } ``` +
+ + Open in Cloud Shell + +
+## 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 @@ -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. @@ -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: