Skip to content

Commit

Permalink
Add Update Connector Support for VPC Access Connectors (#8205)
Browse files Browse the repository at this point in the history
* Add Update Connetor Support for VPC Access Connectors

* fix lint

* fix go lint

* fix tests
  • Loading branch information
jashansudan authored Jul 14, 2023
1 parent a3e513f commit 85ff1c2
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 15 deletions.
26 changes: 17 additions & 9 deletions mmv1/products/vpcaccess/Connector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
name: 'Connector'
kind: 'vpcaccess#Connector'
description: 'Serverless VPC Access connector resource.'
immutable: true
base_url: projects/{{project}}/locations/{{region}}/connectors
create_url: projects/{{project}}/locations/{{region}}/connectors?connectorId={{name}}
update_verb: :PATCH
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Configuring Serverless VPC Access': 'https://cloud.google.com/vpc/docs/configure-serverless-vpc-access'
api: 'https://cloud.google.com/vpc/docs/reference/vpcaccess/rest/v1/projects.locations.connectors'
update_mask: true
async: !ruby/object:Api::OpAsync
operation: !ruby/object:Api::OpAsync::Operation
path: 'name'
Expand Down Expand Up @@ -55,6 +56,8 @@ custom_code: !ruby/object:Provider::Terraform::CustomCode
encoder: templates/terraform/encoders/no_send_name.go.erb
post_create: templates/terraform/post_create/sleep.go.erb
decoder: templates/terraform/decoders/long_name_to_self_link.go.erb
constants: templates/terraform/constants/connector.erb
resource_definition: templates/terraform/resource_definition/connector.erb
parameters:
- !ruby/object:Api::Type::String
name: 'region'
Expand All @@ -68,6 +71,7 @@ properties:
name: name
description: |
The name of the resource (Max 25 characters).
immutable: true
required: true
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'
- !ruby/object:Api::Type::String
Expand All @@ -77,6 +81,7 @@ properties:
exactly_one_of:
- network
- subnet.0.name
immutable: true
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'
custom_expand: 'templates/terraform/custom_expand/resource_from_self_link.go.erb'
diff_suppress_func: 'tpgresource.CompareResourceNames'
Expand All @@ -87,6 +92,7 @@ properties:
The range of internal addresses that follows RFC 4632 notation. Example: `10.132.0.0/28`.
required_with:
- network
immutable: true
- !ruby/object:Api::Type::Enum
name: state
description: |
Expand All @@ -102,34 +108,36 @@ properties:
name: machineType
description: |
Machine type of VM Instance underlying connector. Default is e2-micro
immutable: false
default_value: e2-micro
- !ruby/object:Api::Type::Integer
name: minThroughput
description: |
Minimum throughput of the connector in Mbps. Default and min is 200.
default_value: 200
immutable: true
default_from_api: true
validation: !ruby/object:Provider::Terraform::Validation
function: 'validation.IntBetween(200, 1000)'
function: 'validation.IntBetween(200, 900)'
- !ruby/object:Api::Type::Integer
name: minInstances
description: |
Minimum value of instances in autoscaling group underlying the connector.
immutable: false
default_from_api: true
- !ruby/object:Api::Type::Integer
name: maxInstances
description: |
Maximum value of instances in autoscaling group underlying the connector.
immutable: false
default_from_api: true
- !ruby/object:Api::Type::Integer
name: maxThroughput
# The API documentation says this will default to 200, but when I tried that I got an error that the minimum
# throughput must be lower than the maximum. The console defaults to 1000, so I changed it to that.
# API returns 300 if it is not sent
description: |
Maximum throughput of the connector in Mbps, must be greater than `min_throughput`. Default is 300.
default_value: 300
Maximum throughput of the connector in Mbps, must be greater than `min_throughput`. Default is 1000.
immutable: true
default_from_api: true
validation: !ruby/object:Provider::Terraform::Validation
function: 'validation.IntBetween(200, 1000)'
function: 'validation.IntBetween(300, 1000)'
- !ruby/object:Api::Type::String
name: 'selfLink'
description: |
Expand Down
4 changes: 4 additions & 0 deletions mmv1/templates/terraform/constants/connector.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Are the number of min/max instances reduced?
func AreInstancesReduced(_ context.Context, old, new, _ interface{}) bool {
return new.(int) < old.(int)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
resource "google_vpc_access_connector" "connector" {
name = "<%= ctx[:vars]['name'] %>"
ip_cidr_range = "10.8.0.0/28"
ip_cidr_range = "10.18.0.0/28"
network = "default"
min_instances = 2
max_instances = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ resource "google_vpc_access_connector" "connector" {
name = google_compute_subnetwork.custom_test.name
}
machine_type = "e2-standard-4"
min_instances = 2
max_instances = 3
}

resource "google_compute_subnetwork" "custom_test" {
Expand All @@ -16,4 +18,4 @@ resource "google_compute_subnetwork" "custom_test" {
resource "google_compute_network" "custom_test" {
name = "<%= ctx[:vars]['name'] %>"
auto_create_subnetworks = false
}
}
3 changes: 3 additions & 0 deletions mmv1/templates/terraform/resource_definition/connector.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CustomizeDiff: customdiff.All(
customdiff.ForceNewIfChange("min_instances", AreInstancesReduced),
customdiff.ForceNewIfChange("max_instances", AreInstancesReduced)),
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ resource "google_vpc_access_connector" "connector" {
ip_cidr_range = "10.8.0.0/28"
network = "default"
region = "us-central1"
min_instances = 2
max_instances = 3
}
data "google_vpc_access_connector" "connector" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ resource "google_vpc_access_connector" "bar" {
region = "us-central1"
ip_cidr_range = "10.8.0.0/28"
network = "default"
min_instances = 3
max_instances = 10
}
resource "google_app_engine_standard_app_version" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,8 @@ resource "google_vpc_access_connector" "%s" {
region = "us-central1"
ip_cidr_range = "%s"
network = google_compute_network.vpc.name
min_instances = 3
max_instances = 10
}

resource "google_storage_bucket" "bucket" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ func TestAccVPCAccessConnector_vpcAccessConnectorThroughput(t *testing.T) {
})
}

func TestAccVPCAccessConnector_vpcAccessConnectorMachineAndInstancesChanged(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": RandString(t, 10),
}

VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckVPCAccessConnectorDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccVPCAccessConnector_vpcAccessConnectorThroughput(context),
},
{
ResourceName: "google_vpc_access_connector.connector",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccVPCAccessConnector_vpcAccessConnectorMachineAndInstancesChanged(context),
},
{
ResourceName: "google_vpc_access_connector.connector",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccVPCAccessConnector_vpcAccessConnectorThroughput(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_vpc_access_connector" "connector" {
Expand All @@ -57,3 +89,30 @@ resource "google_compute_network" "custom_test" {
}
`, context)
}

func testAccVPCAccessConnector_vpcAccessConnectorMachineAndInstancesChanged(context map[string]interface{}) string {
return Nprintf(`
resource "google_vpc_access_connector" "connector" {
name = "tf-test-vpc-con%{random_suffix}"
subnet {
name = google_compute_subnetwork.custom_test.name
}
machine_type = "e2-micro"
min_instances = 3
max_instances = 5
region = "us-central1"
}
resource "google_compute_subnetwork" "custom_test" {
name = "tf-test-vpc-con%{random_suffix}"
ip_cidr_range = "10.2.0.0/28"
region = "us-central1"
network = google_compute_network.custom_test.id
}
resource "google_compute_network" "custom_test" {
name = "tf-test-vpc-con%{random_suffix}"
auto_create_subnetworks = false
}
`, context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ provider "google" {
}

resource "google_vpc_access_connector" "connector" {
name = "vpc-con"
ip_cidr_range = "10.8.0.0/28"
network = "default"
region = "us-central1"
name = "vpc-con"
ip_cidr_range = "10.8.0.0/28"
network = "default"
region = "us-central1"
max_throughput = 300
min_throughput = 200
}

0 comments on commit 85ff1c2

Please sign in to comment.