Skip to content

Commit c75c086

Browse files
google_vpc_access_connector missing GA args (hashicorp#6718) (hashicorp#12838)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 2e0bdef commit c75c086

5 files changed

+306
-10
lines changed

.changelog/6718.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
vpcaccess - promoted `machine_type`, `min_instances`, `max_instances`, and `subnet` in `google_vpc_access_connector` to GA
3+
```

google/resource_vpc_access_connector.go

+191-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ func resourceVPCAccessConnector() *schema.Resource {
5353
Description: `The range of internal addresses that follows RFC 4632 notation. Example: '10.132.0.0/28'.`,
5454
RequiredWith: []string{"network"},
5555
},
56+
"machine_type": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
ForceNew: true,
60+
Description: `Machine type of VM Instance underlying connector. Default is e2-micro`,
61+
Default: "e2-micro",
62+
},
63+
"max_instances": {
64+
Type: schema.TypeInt,
65+
Computed: true,
66+
Optional: true,
67+
ForceNew: true,
68+
Description: `Maximum value of instances in autoscaling group underlying the connector.`,
69+
},
5670
"max_throughput": {
5771
Type: schema.TypeInt,
5872
Optional: true,
@@ -61,6 +75,13 @@ func resourceVPCAccessConnector() *schema.Resource {
6175
Description: `Maximum throughput of the connector in Mbps, must be greater than 'min_throughput'. Default is 300.`,
6276
Default: 300,
6377
},
78+
"min_instances": {
79+
Type: schema.TypeInt,
80+
Computed: true,
81+
Optional: true,
82+
ForceNew: true,
83+
Description: `Minimum value of instances in autoscaling group underlying the connector.`,
84+
},
6485
"min_throughput": {
6586
Type: schema.TypeInt,
6687
Optional: true,
@@ -76,7 +97,7 @@ func resourceVPCAccessConnector() *schema.Resource {
7697
ForceNew: true,
7798
DiffSuppressFunc: compareResourceNames,
7899
Description: `Name or self_link of the VPC network. Required if 'ip_cidr_range' is set.`,
79-
ExactlyOneOf: []string{"network"},
100+
ExactlyOneOf: []string{"network", "subnet.0.name"},
80101
},
81102
"region": {
82103
Type: schema.TypeString,
@@ -85,6 +106,32 @@ func resourceVPCAccessConnector() *schema.Resource {
85106
ForceNew: true,
86107
Description: `Region where the VPC Access connector resides. If it is not provided, the provider region is used.`,
87108
},
109+
"subnet": {
110+
Type: schema.TypeList,
111+
Optional: true,
112+
ForceNew: true,
113+
Description: `The subnet in which to house the connector`,
114+
MaxItems: 1,
115+
Elem: &schema.Resource{
116+
Schema: map[string]*schema.Schema{
117+
"name": {
118+
Type: schema.TypeString,
119+
Optional: true,
120+
ForceNew: true,
121+
Description: `Subnet name (relative, not fully qualified). E.g. if the full subnet selfLink is
122+
https://compute.googleapis.com/compute/v1/projects/{project}/regions/{region}/subnetworks/{subnetName} the correct input for this field would be {subnetName}"`,
123+
ExactlyOneOf: []string{"network", "subnet.0.name"},
124+
},
125+
"project_id": {
126+
Type: schema.TypeString,
127+
Computed: true,
128+
Optional: true,
129+
ForceNew: true,
130+
Description: `Project in which the subnet exists. If not set, this project is assumed to be the project for which the connector create request was issued.`,
131+
},
132+
},
133+
},
134+
},
88135
"self_link": {
89136
Type: schema.TypeString,
90137
Computed: true,
@@ -132,18 +179,42 @@ func resourceVPCAccessConnectorCreate(d *schema.ResourceData, meta interface{})
132179
} else if v, ok := d.GetOkExists("ip_cidr_range"); !isEmptyValue(reflect.ValueOf(ipCidrRangeProp)) && (ok || !reflect.DeepEqual(v, ipCidrRangeProp)) {
133180
obj["ipCidrRange"] = ipCidrRangeProp
134181
}
182+
machineTypeProp, err := expandVPCAccessConnectorMachineType(d.Get("machine_type"), d, config)
183+
if err != nil {
184+
return err
185+
} else if v, ok := d.GetOkExists("machine_type"); !isEmptyValue(reflect.ValueOf(machineTypeProp)) && (ok || !reflect.DeepEqual(v, machineTypeProp)) {
186+
obj["machineType"] = machineTypeProp
187+
}
135188
minThroughputProp, err := expandVPCAccessConnectorMinThroughput(d.Get("min_throughput"), d, config)
136189
if err != nil {
137190
return err
138191
} else if v, ok := d.GetOkExists("min_throughput"); !isEmptyValue(reflect.ValueOf(minThroughputProp)) && (ok || !reflect.DeepEqual(v, minThroughputProp)) {
139192
obj["minThroughput"] = minThroughputProp
140193
}
194+
minInstancesProp, err := expandVPCAccessConnectorMinInstances(d.Get("min_instances"), d, config)
195+
if err != nil {
196+
return err
197+
} else if v, ok := d.GetOkExists("min_instances"); !isEmptyValue(reflect.ValueOf(minInstancesProp)) && (ok || !reflect.DeepEqual(v, minInstancesProp)) {
198+
obj["minInstances"] = minInstancesProp
199+
}
200+
maxInstancesProp, err := expandVPCAccessConnectorMaxInstances(d.Get("max_instances"), d, config)
201+
if err != nil {
202+
return err
203+
} else if v, ok := d.GetOkExists("max_instances"); !isEmptyValue(reflect.ValueOf(maxInstancesProp)) && (ok || !reflect.DeepEqual(v, maxInstancesProp)) {
204+
obj["maxInstances"] = maxInstancesProp
205+
}
141206
maxThroughputProp, err := expandVPCAccessConnectorMaxThroughput(d.Get("max_throughput"), d, config)
142207
if err != nil {
143208
return err
144209
} else if v, ok := d.GetOkExists("max_throughput"); !isEmptyValue(reflect.ValueOf(maxThroughputProp)) && (ok || !reflect.DeepEqual(v, maxThroughputProp)) {
145210
obj["maxThroughput"] = maxThroughputProp
146211
}
212+
subnetProp, err := expandVPCAccessConnectorSubnet(d.Get("subnet"), d, config)
213+
if err != nil {
214+
return err
215+
} else if v, ok := d.GetOkExists("subnet"); !isEmptyValue(reflect.ValueOf(subnetProp)) && (ok || !reflect.DeepEqual(v, subnetProp)) {
216+
obj["subnet"] = subnetProp
217+
}
147218

148219
obj, err = resourceVPCAccessConnectorEncoder(d, meta, obj)
149220
if err != nil {
@@ -281,12 +352,24 @@ func resourceVPCAccessConnectorRead(d *schema.ResourceData, meta interface{}) er
281352
if err := d.Set("state", flattenVPCAccessConnectorState(res["state"], d, config)); err != nil {
282353
return fmt.Errorf("Error reading Connector: %s", err)
283354
}
355+
if err := d.Set("machine_type", flattenVPCAccessConnectorMachineType(res["machineType"], d, config)); err != nil {
356+
return fmt.Errorf("Error reading Connector: %s", err)
357+
}
284358
if err := d.Set("min_throughput", flattenVPCAccessConnectorMinThroughput(res["minThroughput"], d, config)); err != nil {
285359
return fmt.Errorf("Error reading Connector: %s", err)
286360
}
361+
if err := d.Set("min_instances", flattenVPCAccessConnectorMinInstances(res["minInstances"], d, config)); err != nil {
362+
return fmt.Errorf("Error reading Connector: %s", err)
363+
}
364+
if err := d.Set("max_instances", flattenVPCAccessConnectorMaxInstances(res["maxInstances"], d, config)); err != nil {
365+
return fmt.Errorf("Error reading Connector: %s", err)
366+
}
287367
if err := d.Set("max_throughput", flattenVPCAccessConnectorMaxThroughput(res["maxThroughput"], d, config)); err != nil {
288368
return fmt.Errorf("Error reading Connector: %s", err)
289369
}
370+
if err := d.Set("subnet", flattenVPCAccessConnectorSubnet(res["subnet"], d, config)); err != nil {
371+
return fmt.Errorf("Error reading Connector: %s", err)
372+
}
290373

291374
return nil
292375
}
@@ -379,6 +462,10 @@ func flattenVPCAccessConnectorState(v interface{}, d *schema.ResourceData, confi
379462
return v
380463
}
381464

465+
func flattenVPCAccessConnectorMachineType(v interface{}, d *schema.ResourceData, config *Config) interface{} {
466+
return v
467+
}
468+
382469
func flattenVPCAccessConnectorMinThroughput(v interface{}, d *schema.ResourceData, config *Config) interface{} {
383470
// Handles the string fixed64 format
384471
if strVal, ok := v.(string); ok {
@@ -396,6 +483,40 @@ func flattenVPCAccessConnectorMinThroughput(v interface{}, d *schema.ResourceDat
396483
return v // let terraform core handle it otherwise
397484
}
398485

486+
func flattenVPCAccessConnectorMinInstances(v interface{}, d *schema.ResourceData, config *Config) interface{} {
487+
// Handles the string fixed64 format
488+
if strVal, ok := v.(string); ok {
489+
if intVal, err := stringToFixed64(strVal); err == nil {
490+
return intVal
491+
}
492+
}
493+
494+
// number values are represented as float64
495+
if floatVal, ok := v.(float64); ok {
496+
intVal := int(floatVal)
497+
return intVal
498+
}
499+
500+
return v // let terraform core handle it otherwise
501+
}
502+
503+
func flattenVPCAccessConnectorMaxInstances(v interface{}, d *schema.ResourceData, config *Config) interface{} {
504+
// Handles the string fixed64 format
505+
if strVal, ok := v.(string); ok {
506+
if intVal, err := stringToFixed64(strVal); err == nil {
507+
return intVal
508+
}
509+
}
510+
511+
// number values are represented as float64
512+
if floatVal, ok := v.(float64); ok {
513+
intVal := int(floatVal)
514+
return intVal
515+
}
516+
517+
return v // let terraform core handle it otherwise
518+
}
519+
399520
func flattenVPCAccessConnectorMaxThroughput(v interface{}, d *schema.ResourceData, config *Config) interface{} {
400521
// Handles the string fixed64 format
401522
if strVal, ok := v.(string); ok {
@@ -413,6 +534,29 @@ func flattenVPCAccessConnectorMaxThroughput(v interface{}, d *schema.ResourceDat
413534
return v // let terraform core handle it otherwise
414535
}
415536

537+
func flattenVPCAccessConnectorSubnet(v interface{}, d *schema.ResourceData, config *Config) interface{} {
538+
if v == nil {
539+
return nil
540+
}
541+
original := v.(map[string]interface{})
542+
if len(original) == 0 {
543+
return nil
544+
}
545+
transformed := make(map[string]interface{})
546+
transformed["name"] =
547+
flattenVPCAccessConnectorSubnetName(original["name"], d, config)
548+
transformed["project_id"] =
549+
flattenVPCAccessConnectorSubnetProjectId(original["projectId"], d, config)
550+
return []interface{}{transformed}
551+
}
552+
func flattenVPCAccessConnectorSubnetName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
553+
return v
554+
}
555+
556+
func flattenVPCAccessConnectorSubnetProjectId(v interface{}, d *schema.ResourceData, config *Config) interface{} {
557+
return v
558+
}
559+
416560
func expandVPCAccessConnectorName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
417561
return v, nil
418562
}
@@ -425,14 +569,60 @@ func expandVPCAccessConnectorIpCidrRange(v interface{}, d TerraformResourceData,
425569
return v, nil
426570
}
427571

572+
func expandVPCAccessConnectorMachineType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
573+
return v, nil
574+
}
575+
428576
func expandVPCAccessConnectorMinThroughput(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
429577
return v, nil
430578
}
431579

580+
func expandVPCAccessConnectorMinInstances(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
581+
return v, nil
582+
}
583+
584+
func expandVPCAccessConnectorMaxInstances(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
585+
return v, nil
586+
}
587+
432588
func expandVPCAccessConnectorMaxThroughput(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
433589
return v, nil
434590
}
435591

592+
func expandVPCAccessConnectorSubnet(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
593+
l := v.([]interface{})
594+
if len(l) == 0 || l[0] == nil {
595+
return nil, nil
596+
}
597+
raw := l[0]
598+
original := raw.(map[string]interface{})
599+
transformed := make(map[string]interface{})
600+
601+
transformedName, err := expandVPCAccessConnectorSubnetName(original["name"], d, config)
602+
if err != nil {
603+
return nil, err
604+
} else if val := reflect.ValueOf(transformedName); val.IsValid() && !isEmptyValue(val) {
605+
transformed["name"] = transformedName
606+
}
607+
608+
transformedProjectId, err := expandVPCAccessConnectorSubnetProjectId(original["project_id"], d, config)
609+
if err != nil {
610+
return nil, err
611+
} else if val := reflect.ValueOf(transformedProjectId); val.IsValid() && !isEmptyValue(val) {
612+
transformed["projectId"] = transformedProjectId
613+
}
614+
615+
return transformed, nil
616+
}
617+
618+
func expandVPCAccessConnectorSubnetName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
619+
return v, nil
620+
}
621+
622+
func expandVPCAccessConnectorSubnetProjectId(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
623+
return v, nil
624+
}
625+
436626
func resourceVPCAccessConnectorEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
437627
delete(obj, "name")
438628
return obj, nil

google/resource_vpc_access_connector_generated_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,55 @@ resource "google_vpc_access_connector" "connector" {
5858
`, context)
5959
}
6060

61+
func TestAccVPCAccessConnector_vpcAccessConnectorSharedVPCExample(t *testing.T) {
62+
t.Parallel()
63+
64+
context := map[string]interface{}{
65+
"random_suffix": randString(t, 10),
66+
}
67+
68+
vcrTest(t, resource.TestCase{
69+
PreCheck: func() { testAccPreCheck(t) },
70+
Providers: testAccProviders,
71+
CheckDestroy: testAccCheckVPCAccessConnectorDestroyProducer(t),
72+
Steps: []resource.TestStep{
73+
{
74+
Config: testAccVPCAccessConnector_vpcAccessConnectorSharedVPCExample(context),
75+
},
76+
{
77+
ResourceName: "google_vpc_access_connector.connector",
78+
ImportState: true,
79+
ImportStateVerify: true,
80+
ImportStateVerifyIgnore: []string{"self_link", "region"},
81+
},
82+
},
83+
})
84+
}
85+
86+
func testAccVPCAccessConnector_vpcAccessConnectorSharedVPCExample(context map[string]interface{}) string {
87+
return Nprintf(`
88+
resource "google_vpc_access_connector" "connector" {
89+
name = "tf-test-vpc-con%{random_suffix}"
90+
subnet {
91+
name = google_compute_subnetwork.custom_test.name
92+
}
93+
machine_type = "e2-standard-4"
94+
}
95+
96+
resource "google_compute_subnetwork" "custom_test" {
97+
name = "tf-test-vpc-con%{random_suffix}"
98+
ip_cidr_range = "10.2.0.0/28"
99+
region = "us-central1"
100+
network = google_compute_network.custom_test.id
101+
}
102+
103+
resource "google_compute_network" "custom_test" {
104+
name = "tf-test-vpc-con%{random_suffix}"
105+
auto_create_subnetworks = false
106+
}
107+
`, context)
108+
}
109+
61110
func testAccCheckVPCAccessConnectorDestroyProducer(t *testing.T) func(s *terraform.State) error {
62111
return func(s *terraform.State) error {
63112
for name, rs := range s.RootModule().Resources {

0 commit comments

Comments
 (0)