Skip to content

Commit 4debc82

Browse files
authored
feat: add support for direct vpc in Cloud Functions v2 beta provider (#15539)
1 parent 81daec8 commit 4debc82

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed

mmv1/products/cloudfunctions2/Function.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,23 @@ examples:
302302
ignore_read_extra:
303303
- 'build_config.0.source.0.storage_source.0.object'
304304
- 'build_config.0.source.0.storage_source.0.bucket'
305+
- name: 'cloudfunctions2_directvpc'
306+
primary_resource_id: 'function'
307+
primary_resource_name: 'fmt.Sprintf("tf-test-function-v2%s", context["random_suffix"])'
308+
vars:
309+
function: 'function-v2'
310+
bucket_name: 'gcf-source'
311+
zip_path: 'function-source.zip'
312+
test_env_vars:
313+
project: 'PROJECT_NAME'
314+
test_vars_overrides:
315+
'location': '"us-central1"'
316+
'zip_path': '"./test-fixtures/function-source.zip"'
317+
# ignore these fields during import step
318+
ignore_read_extra:
319+
- 'build_config.0.source.0.storage_source.0.object'
320+
- 'build_config.0.source.0.storage_source.0.bucket'
321+
min_version: 'beta'
305322
parameters:
306323
- name: 'location'
307324
type: String
@@ -559,6 +576,34 @@ properties:
559576
- 'VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED'
560577
- 'PRIVATE_RANGES_ONLY'
561578
- 'ALL_TRAFFIC'
579+
- name: 'directVpcNetworkInterface'
580+
min_version: 'beta'
581+
type: Array
582+
description: 'The Direct VPC network interface for the Cloud Function. Currently only a single Direct VPC is supported.'
583+
item_type:
584+
type: NestedObject
585+
properties:
586+
- name: network
587+
type: String
588+
description: |
589+
The name of the VPC network to which the function will be connected. Specify either a VPC network or a subnet, or both. If you specify only a network, the subnet uses the same name as the network.
590+
- name: subnetwork
591+
type: String
592+
description: |
593+
The name of the VPC subnetwork that the Cloud Function resource will get IPs from. Specify either a VPC network or a subnet, or both. If both network and subnetwork are specified, the given VPC subnetwork must belong to the given VPC network. If subnetwork is not specified, the subnetwork with the same name with the network will be used.
594+
- name: tags
595+
type: Array
596+
description: 'Network tags applied to this Cloud Function resource.'
597+
item_type:
598+
type: String
599+
- name: 'directVpcEgress'
600+
min_version: 'beta'
601+
type: Enum
602+
description: 'Egress settings for direct VPC. If not provided, it defaults to VPC_EGRESS_PRIVATE_RANGES_ONLY.'
603+
default_from_api: true
604+
enum_values:
605+
- 'VPC_EGRESS_ALL_TRAFFIC'
606+
- 'VPC_EGRESS_PRIVATE_RANGES_ONLY'
562607
- name: 'ingressSettings'
563608
type: Enum
564609
description:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
locals {
2+
project = "{{index $.TestEnvVars "project"}}" # Google Cloud Platform Project ID
3+
}
4+
5+
resource "google_storage_bucket" "bucket" {
6+
provider = google-beta
7+
name = "${local.project}-{{index $.Vars "bucket_name"}}" # Every bucket name must be globally unique
8+
location = "US"
9+
uniform_bucket_level_access = true
10+
}
11+
12+
resource "google_storage_bucket_object" "object" {
13+
provider = google-beta
14+
name = "function-source.zip"
15+
bucket = google_storage_bucket.bucket.name
16+
source = "{{index $.Vars "zip_path"}}" # Add path to the zipped function source code
17+
}
18+
19+
resource "google_cloudfunctions2_function" "{{$.PrimaryResourceId}}" {
20+
provider = google-beta
21+
name = "{{index $.Vars "function"}}"
22+
location = "us-central1"
23+
description = "a new function"
24+
25+
build_config {
26+
runtime = "nodejs20"
27+
entry_point = "helloHttp" # Set the entry point
28+
source {
29+
storage_source {
30+
bucket = google_storage_bucket.bucket.name
31+
object = google_storage_bucket_object.object.name
32+
}
33+
}
34+
}
35+
36+
service_config {
37+
max_instance_count = 1
38+
available_memory = "256M"
39+
timeout_seconds = 60
40+
direct_vpc_network_interface {
41+
network = "default"
42+
subnetwork = "default"
43+
tags = ["tag1", "tag2"]
44+
}
45+
direct_vpc_egress = "VPC_EGRESS_ALL_TRAFFIC"
46+
}
47+
}

mmv1/third_party/terraform/services/cloudfunctions2/resource_cloudfunctions2_function_test.go renamed to mmv1/third_party/terraform/services/cloudfunctions2/resource_cloudfunctions2_function_test.go.tmpl

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"testing"
55

66
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
{{ if ne $.TargetVersionName "ga" }}
8+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
9+
{{ end }}
710
"github.com/hashicorp/terraform-provider-google/google/acctest"
811
"github.com/hashicorp/terraform-provider-google/google/envvar"
912
)
@@ -642,3 +645,152 @@ output "binary_authorization_policy_eq" {
642645
}
643646
`, context)
644647
}
648+
649+
{{ if ne $.TargetVersionName "ga" }}
650+
651+
func TestAccCloudfunctions2function_cloudfunctions2DirectvpcExample_update(t *testing.T) {
652+
t.Parallel()
653+
654+
context := map[string]interface{}{
655+
"project": envvar.GetTestProjectFromEnv(),
656+
"location": "us-central1",
657+
"zip_path": "./test-fixtures/function-source.zip",
658+
"random_suffix": acctest.RandString(t, 10),
659+
}
660+
661+
acctest.VcrTest(t, resource.TestCase{
662+
PreCheck: func() { acctest.AccTestPreCheck(t) },
663+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
664+
CheckDestroy: testAccCheckCloudfunctions2functionDestroyProducer(t),
665+
Steps: []resource.TestStep{
666+
{
667+
Config: testAccCloudfunctions2function_cloudfunctions2DirectvpcExample_basic(context),
668+
},
669+
{
670+
ResourceName: "google_cloudfunctions2_function.function",
671+
ImportState: true,
672+
ImportStateVerify: true,
673+
ImportStateVerifyIgnore: []string{"build_config.0.source.0.storage_source.0.bucket", "build_config.0.source.0.storage_source.0.object", "labels", "location", "terraform_labels"},
674+
},
675+
{
676+
Config: testAccCloudfunctions2function_cloudfunctions2DirectvpcExample_update(context),
677+
ConfigPlanChecks: resource.ConfigPlanChecks{
678+
PreApply: []plancheck.PlanCheck{
679+
plancheck.ExpectResourceAction("google_cloudfunctions2_function.function", plancheck.ResourceActionUpdate),
680+
},
681+
},
682+
},
683+
{
684+
ResourceName: "google_cloudfunctions2_function.function",
685+
ImportState: true,
686+
ImportStateVerify: true,
687+
ImportStateVerifyIgnore: []string{"build_config.0.source.0.storage_source.0.bucket", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.generation", "labels", "location", "terraform_labels"},
688+
},
689+
},
690+
})
691+
}
692+
693+
func testAccCloudfunctions2function_cloudfunctions2DirectvpcExample_basic(context map[string]interface{}) string {
694+
return acctest.Nprintf(`
695+
locals {
696+
project = "%{project}" # Google Cloud Platform Project ID
697+
}
698+
699+
resource "google_storage_bucket" "bucket" {
700+
provider = google-beta
701+
name = "${local.project}-tf-test-gcf-source%{random_suffix}" # Every bucket name must be globally unique
702+
location = "US"
703+
uniform_bucket_level_access = true
704+
}
705+
706+
resource "google_storage_bucket_object" "object" {
707+
provider = google-beta
708+
name = "function-source.zip"
709+
bucket = google_storage_bucket.bucket.name
710+
source = "%{zip_path}" # Add path to the zipped function source code
711+
}
712+
713+
resource "google_cloudfunctions2_function" "function" {
714+
provider = google-beta
715+
name = "tf-test-function-v2%{random_suffix}"
716+
location = "us-central1"
717+
description = "a new function"
718+
719+
build_config {
720+
runtime = "nodejs20"
721+
entry_point = "helloHttp" # Set the entry point
722+
source {
723+
storage_source {
724+
bucket = google_storage_bucket.bucket.name
725+
object = google_storage_bucket_object.object.name
726+
}
727+
}
728+
}
729+
730+
service_config {
731+
max_instance_count = 1
732+
available_memory = "256M"
733+
timeout_seconds = 60
734+
direct_vpc_network_interface {
735+
network = "default"
736+
subnetwork = "default"
737+
tags = ["tag1", "tag2"]
738+
}
739+
direct_vpc_egress = "VPC_EGRESS_ALL_TRAFFIC"
740+
}
741+
}
742+
`, context)
743+
}
744+
745+
func testAccCloudfunctions2function_cloudfunctions2DirectvpcExample_update(context map[string]interface{}) string {
746+
return acctest.Nprintf(`
747+
locals {
748+
project = "%{project}" # Google Cloud Platform Project ID
749+
}
750+
751+
resource "google_storage_bucket" "bucket" {
752+
provider = google-beta
753+
name = "${local.project}-tf-test-gcf-source%{random_suffix}" # Every bucket name must be globally unique
754+
location = "US"
755+
uniform_bucket_level_access = true
756+
}
757+
758+
resource "google_storage_bucket_object" "object" {
759+
provider = google-beta
760+
name = "function-source.zip"
761+
bucket = google_storage_bucket.bucket.name
762+
source = "%{zip_path}" # Add path to the zipped function source code
763+
}
764+
765+
resource "google_cloudfunctions2_function" "function" {
766+
provider = google-beta
767+
name = "tf-test-function-v2%{random_suffix}"
768+
location = "us-central1"
769+
description = "a new function"
770+
771+
build_config {
772+
runtime = "nodejs20"
773+
entry_point = "helloHttp" # Set the entry point
774+
source {
775+
storage_source {
776+
bucket = google_storage_bucket.bucket.name
777+
object = google_storage_bucket_object.object.name
778+
}
779+
}
780+
}
781+
782+
service_config {
783+
max_instance_count = 1
784+
available_memory = "256M"
785+
timeout_seconds = 60
786+
direct_vpc_network_interface {
787+
network = "default"
788+
subnetwork = "default"
789+
tags = ["tag3", "tag4"]
790+
}
791+
direct_vpc_egress = "VPC_EGRESS_PRIVATE_RANGES_ONLY"
792+
}
793+
}
794+
`, context)
795+
}
796+
{{ end }}

0 commit comments

Comments
 (0)