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

Adding support for concurrency #13315

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/7015.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Adding support to 2nd Gen Cloud Functions Terraform Provider for: max_instance_request_concurrency and available_cpu to allow for setting concurrency.
```
57 changes: 57 additions & 0 deletions google/resource_cloudfunctions2_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ region. If not provided, defaults to the same region as the function.`,
Description: `Whether 100% of traffic is routed to the latest revision. Defaults to true.`,
Default: true,
},
"available_cpu": {
Type: schema.TypeString,
Optional: true,
Description: `The number of CPUs used in a single container instance. Default value is calculated from available memory.`,
},
"available_memory": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -299,6 +304,11 @@ supplied the value is interpreted as bytes.`,
Description: `The limit on the maximum number of function instances that may coexist at a
given time.`,
},
"max_instance_request_concurrency": {
Type: schema.TypeInt,
Optional: true,
Description: `Sets the maximum number of concurrent requests that each instance can receive. Defaults to 1.`,
},
"min_instance_count": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -1036,6 +1046,10 @@ func flattenCloudfunctions2functionServiceConfig(v interface{}, d *schema.Resour
flattenCloudfunctions2functionServiceConfigTimeoutSeconds(original["timeoutSeconds"], d, config)
transformed["available_memory"] =
flattenCloudfunctions2functionServiceConfigAvailableMemory(original["availableMemory"], d, config)
transformed["max_instance_request_concurrency"] =
flattenCloudfunctions2functionServiceConfigMaxInstanceRequestConcurrency(original["maxInstanceRequestConcurrency"], d, config)
transformed["available_cpu"] =
flattenCloudfunctions2functionServiceConfigAvailableCpu(original["availableCpu"], d, config)
transformed["environment_variables"] =
flattenCloudfunctions2functionServiceConfigEnvironmentVariables(original["environmentVariables"], d, config)
transformed["max_instance_count"] =
Expand Down Expand Up @@ -1087,6 +1101,27 @@ func flattenCloudfunctions2functionServiceConfigAvailableMemory(v interface{}, d
return v
}

func flattenCloudfunctions2functionServiceConfigMaxInstanceRequestConcurrency(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := stringToFixed64(strVal); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenCloudfunctions2functionServiceConfigAvailableCpu(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudfunctions2functionServiceConfigEnvironmentVariables(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -1629,6 +1664,20 @@ func expandCloudfunctions2functionServiceConfig(v interface{}, d TerraformResour
transformed["availableMemory"] = transformedAvailableMemory
}

transformedMaxInstanceRequestConcurrency, err := expandCloudfunctions2functionServiceConfigMaxInstanceRequestConcurrency(original["max_instance_request_concurrency"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedMaxInstanceRequestConcurrency); val.IsValid() && !isEmptyValue(val) {
transformed["maxInstanceRequestConcurrency"] = transformedMaxInstanceRequestConcurrency
}

transformedAvailableCpu, err := expandCloudfunctions2functionServiceConfigAvailableCpu(original["available_cpu"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAvailableCpu); val.IsValid() && !isEmptyValue(val) {
transformed["availableCpu"] = transformedAvailableCpu
}

transformedEnvironmentVariables, err := expandCloudfunctions2functionServiceConfigEnvironmentVariables(original["environment_variables"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1728,6 +1777,14 @@ func expandCloudfunctions2functionServiceConfigAvailableMemory(v interface{}, d
return v, nil
}

func expandCloudfunctions2functionServiceConfigMaxInstanceRequestConcurrency(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudfunctions2functionServiceConfigAvailableCpu(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudfunctions2functionServiceConfigEnvironmentVariables(v interface{}, d TerraformResourceData, config *Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
Expand Down
4 changes: 3 additions & 1 deletion google/resource_cloudfunctions2_function_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ resource "google_cloudfunctions2_function" "function" {
service_config {
max_instance_count = 3
min_instance_count = 1
available_memory = "256M"
available_memory = "4Gi"
timeout_seconds = 60
max_instance_request_concurrency = 80
available_cpu = "4"
environment_variables = {
SERVICE_CONFIG_TEST = "config_test"
}
Expand Down
12 changes: 11 additions & 1 deletion website/docs/r/cloudfunctions2_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ resource "google_cloudfunctions2_function" "function" {
service_config {
max_instance_count = 3
min_instance_count = 1
available_memory = "256M"
available_memory = "4Gi"
timeout_seconds = 60
max_instance_request_concurrency = 80
available_cpu = "4"
environment_variables = {
SERVICE_CONFIG_TEST = "config_test"
}
Expand Down Expand Up @@ -675,6 +677,14 @@ The following arguments are supported:
Defaults to 256M. Supported units are k, M, G, Mi, Gi. If no unit is
supplied the value is interpreted as bytes.

* `max_instance_request_concurrency` -
(Optional)
Sets the maximum number of concurrent requests that each instance can receive. Defaults to 1.

* `available_cpu` -
(Optional)
The number of CPUs used in a single container instance. Default value is calculated from available memory.

* `environment_variables` -
(Optional)
Environment variables that shall be available during function execution.
Expand Down