diff --git a/google/resource_cloudfunctions_function.go b/google/resource_cloudfunctions_function.go index b4418936..7d736533 100644 --- a/google/resource_cloudfunctions_function.go +++ b/google/resource_cloudfunctions_function.go @@ -268,6 +268,13 @@ func resourceCloudFunctionsFunction() *schema.Resource { Computed: true, }, + "max_instances": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntAtLeast(0), + }, + "project": { Type: schema.TypeString, Optional: true, @@ -366,6 +373,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro function.EnvironmentVariables = expandEnvironmentVariables(d) } + if v, ok := d.GetOk("max_instances"); ok { + function.MaxInstances = int64(v.(int)) + } + log.Printf("[DEBUG] Creating cloud function: %s", function.Name) op, err := config.clientCloudFunctions.Projects.Locations.Functions.Create( cloudFuncId.locationId(), function).Do() @@ -432,7 +443,7 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error } d.Set("event_trigger", flattenEventTrigger(function.EventTrigger)) - + d.Set("max_instances", function.MaxInstances) d.Set("region", cloudFuncId.Region) d.Set("project", cloudFuncId.Project) @@ -500,7 +511,7 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro if d.HasChange("environment_variables") { function.EnvironmentVariables = expandEnvironmentVariables(d) - updateMaskArr = append(updateMaskArr, "environment_variables") + updateMaskArr = append(updateMaskArr, "environmentVariables") } if d.HasChange("event_trigger") { @@ -508,6 +519,11 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro updateMaskArr = append(updateMaskArr, "eventTrigger", "eventTrigger.failurePolicy.retry") } + if d.HasChange("max_instances") { + function.MaxInstances = int64(d.Get("max_instances").(int)) + updateMaskArr = append(updateMaskArr, "maxInstances") + } + if len(updateMaskArr) > 0 { log.Printf("[DEBUG] Send Patch CloudFunction Configuration request: %#v", function) updateMask := strings.Join(updateMaskArr, ",") diff --git a/google/resource_cloudfunctions_function_test.go b/google/resource_cloudfunctions_function_test.go index 6e336882..7fc64047 100644 --- a/google/resource_cloudfunctions_function_test.go +++ b/google/resource_cloudfunctions_function_test.go @@ -90,6 +90,8 @@ func TestAccCloudFunctionsFunction_basic(t *testing.T) { "description", "test function"), resource.TestCheckResourceAttr(funcResourceName, "available_memory_mb", "128"), + resource.TestCheckResourceAttr(funcResourceName, + "max_instances", "10"), testAccCloudFunctionsFunctionSource(fmt.Sprintf("gs://%s/index.zip", bucketName), &function), testAccCloudFunctionsFunctionTrigger(FUNCTION_TRIGGER_HTTP, &function), resource.TestCheckResourceAttr(funcResourceName, @@ -157,6 +159,8 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) { "description", "test function updated"), resource.TestCheckResourceAttr(funcResourceName, "timeout", "91"), + resource.TestCheckResourceAttr(funcResourceName, + "max_instances", "15"), testAccCloudFunctionsFunctionHasLabel("my-label", "my-updated-label-value", &function), testAccCloudFunctionsFunctionHasLabel("a-new-label", "a-new-label-value", &function), testAccCloudFunctionsFunctionHasEnvironmentVariable("TEST_ENV_VARIABLE", @@ -498,6 +502,7 @@ resource "google_cloudfunctions_function" "function" { environment_variables = { TEST_ENV_VARIABLE = "test-env-variable-value" } + max_instances = 10 } `, bucketName, zipFilePath, functionName) } @@ -532,6 +537,7 @@ resource "google_cloudfunctions_function" "function" { TEST_ENV_VARIABLE = "test-env-variable-value" NEW_ENV_VARIABLE = "new-env-variable-value" } + max_instances = 15 }`, bucketName, zipFilePath, functionName) } diff --git a/website/docs/r/cloudfunctions_function.html.markdown b/website/docs/r/cloudfunctions_function.html.markdown index 28e048a1..98383e1c 100644 --- a/website/docs/r/cloudfunctions_function.html.markdown +++ b/website/docs/r/cloudfunctions_function.html.markdown @@ -80,6 +80,8 @@ The following arguments are supported: * `source_repository` - (Optional) Represents parameters related to source repository where a function is hosted. Cannot be set alongside `source_archive_bucket` or `source_archive_object`. Structure is documented below. +* `max_instances` - (Optional) The limit on the maximum number of function instances that may coexist at a given time. + The `event_trigger` block supports: * `event_type` - (Required) The type of event to observe. For example: `"google.storage.object.finalize"`.