Skip to content

Commit

Permalink
Feat: Add min_instances to cloudfunctions functions (#5513) (#3904)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Dec 8, 2021
1 parent 5f0644a commit 5c9bd78
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5513.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
cloudfunctions: added support for `min_instances` to `google_cloudfunctions_function`
```
19 changes: 19 additions & 0 deletions google-beta/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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, ",")
Expand Down
7 changes: 7 additions & 0 deletions google-beta/resource_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cloudfunctions_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a name="nested_event_trigger"></a>The `event_trigger` block supports:

* `event_type` - (Required) The type of event to observe. For example: `"google.storage.object.finalize"`.
Expand Down

0 comments on commit 5c9bd78

Please sign in to comment.