From 5c9bd784de737fee185e4d2c01993040b27984fc Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 8 Dec 2021 10:46:23 -0800 Subject: [PATCH] Feat: Add min_instances to cloudfunctions functions (#5513) (#3904) Signed-off-by: Modular Magician --- .changelog/5513.txt | 3 +++ .../resource_cloudfunctions_function.go | 19 +++++++++++++++++++ .../resource_cloudfunctions_function_test.go | 7 +++++++ .../r/cloudfunctions_function.html.markdown | 2 ++ 4 files changed, 31 insertions(+) create mode 100644 .changelog/5513.txt diff --git a/.changelog/5513.txt b/.changelog/5513.txt new file mode 100644 index 0000000000..073f9a27e8 --- /dev/null +++ b/.changelog/5513.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +cloudfunctions: added support for `min_instances` to `google_cloudfunctions_function` +``` diff --git a/google-beta/resource_cloudfunctions_function.go b/google-beta/resource_cloudfunctions_function.go index 7632c3c499..3925c3feaa 100644 --- a/google-beta/resource_cloudfunctions_function.go +++ b/google-beta/resource_cloudfunctions_function.go @@ -293,6 +293,13 @@ func resourceCloudFunctionsFunction() *schema.Resource { Description: `The limit on the maximum number of function instances that may coexist at a given time.`, }, + "min_instances": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntAtLeast(0), + Description: `The limit on the minimum number of function instances that may coexist at a given time.`, + }, + "project": { Type: schema.TypeString, Optional: true, @@ -409,6 +416,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro function.MaxInstances = int64(v.(int)) } + if v, ok := d.GetOk("min_instances"); ok { + function.MinInstances = int64(v.(int)) + } + log.Printf("[DEBUG] Creating cloud function: %s", function.Name) // We retry the whole create-and-wait because Cloud Functions @@ -527,6 +538,9 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error if err := d.Set("max_instances", function.MaxInstances); err != nil { return fmt.Errorf("Error setting max_instances: %s", err) } + if err := d.Set("min_instances", function.MinInstances); err != nil { + return fmt.Errorf("Error setting min_instances: %s", err) + } if err := d.Set("region", cloudFuncId.Region); err != nil { return fmt.Errorf("Error setting region: %s", err) } @@ -641,6 +655,11 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro updateMaskArr = append(updateMaskArr, "maxInstances") } + if d.HasChange("min_instances") { + function.MinInstances = int64(d.Get("min_instances").(int)) + updateMaskArr = append(updateMaskArr, "minInstances") + } + if len(updateMaskArr) > 0 { log.Printf("[DEBUG] Send Patch CloudFunction Configuration request: %#v", function) updateMask := strings.Join(updateMaskArr, ",") diff --git a/google-beta/resource_cloudfunctions_function_test.go b/google-beta/resource_cloudfunctions_function_test.go index 3742eabc2e..49c8819a59 100644 --- a/google-beta/resource_cloudfunctions_function_test.go +++ b/google-beta/resource_cloudfunctions_function_test.go @@ -145,6 +145,8 @@ func TestAccCloudFunctionsFunction_basic(t *testing.T) { "available_memory_mb", "128"), resource.TestCheckResourceAttr(funcResourceName, "max_instances", "10"), + resource.TestCheckResourceAttr(funcResourceName, + "min_instances", "3"), resource.TestCheckResourceAttr(funcResourceName, "ingress_settings", "ALLOW_INTERNAL_ONLY"), testAccCloudFunctionsFunctionSource(fmt.Sprintf("gs://%s/index.zip", bucketName), &function), @@ -215,6 +217,8 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) { "timeout", "91"), resource.TestCheckResourceAttr(funcResourceName, "max_instances", "15"), + resource.TestCheckResourceAttr(funcResourceName, + "min_instances", "5"), resource.TestCheckResourceAttr(funcResourceName, "ingress_settings", "ALLOW_ALL"), testAccCloudFunctionsFunctionHasLabel("my-label", "my-updated-label-value", &function), @@ -623,6 +627,7 @@ resource "google_cloudfunctions_function" "function" { TEST_ENV_VARIABLE = "test-build-env-variable-value" } max_instances = 10 + min_instances = 3 } `, bucketName, zipFilePath, functionName) } @@ -664,6 +669,7 @@ resource "google_cloudfunctions_function" "function" { NEW_ENV_VARIABLE = "new-build-env-variable-value" } max_instances = 15 + min_instances = 5 } `, bucketName, zipFilePath, functionName) } @@ -903,6 +909,7 @@ resource "google_cloudfunctions_function" "function" { TEST_ENV_VARIABLE = "test-env-variable-value" } max_instances = 10 + min_instances = 3 vpc_connector = google_vpc_access_connector.%s.self_link vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY" diff --git a/website/docs/r/cloudfunctions_function.html.markdown b/website/docs/r/cloudfunctions_function.html.markdown index dc2c7a34e6..aa1b86cde4 100644 --- a/website/docs/r/cloudfunctions_function.html.markdown +++ b/website/docs/r/cloudfunctions_function.html.markdown @@ -150,6 +150,8 @@ Eg. `"nodejs10"`, `"nodejs12"`, `"nodejs14"`, `"python37"`, `"python38"`, `"pyth * `max_instances` - (Optional) The limit on the maximum number of function instances that may coexist at a given time. +* `min_instances` - (Optional) The limit on the minimum 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"`.