18
18
package vpcaccess
19
19
20
20
import (
21
+ "context"
21
22
"fmt"
22
23
"log"
23
24
"reflect"
25
+ "strings"
24
26
"time"
25
27
28
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
26
29
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
27
30
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
28
31
29
32
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
30
33
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
31
34
)
32
35
36
+ // Are the number of min/max instances reduced?
37
+ func AreInstancesReduced (_ context.Context , old , new , _ interface {}) bool {
38
+ return new .(int ) < old .(int )
39
+ }
40
+
33
41
func ResourceVPCAccessConnector () * schema.Resource {
34
42
return & schema.Resource {
35
43
Create : resourceVPCAccessConnectorCreate ,
36
44
Read : resourceVPCAccessConnectorRead ,
45
+ Update : resourceVPCAccessConnectorUpdate ,
37
46
Delete : resourceVPCAccessConnectorDelete ,
38
47
39
48
Importer : & schema.ResourceImporter {
@@ -42,9 +51,14 @@ func ResourceVPCAccessConnector() *schema.Resource {
42
51
43
52
Timeouts : & schema.ResourceTimeout {
44
53
Create : schema .DefaultTimeout (20 * time .Minute ),
54
+ Update : schema .DefaultTimeout (20 * time .Minute ),
45
55
Delete : schema .DefaultTimeout (20 * time .Minute ),
46
56
},
47
57
58
+ CustomizeDiff : customdiff .All (
59
+ customdiff .ForceNewIfChange ("min_instances" , AreInstancesReduced ),
60
+ customdiff .ForceNewIfChange ("max_instances" , AreInstancesReduced )),
61
+
48
62
Schema : map [string ]* schema.Schema {
49
63
"name" : {
50
64
Type : schema .TypeString ,
@@ -62,39 +76,36 @@ func ResourceVPCAccessConnector() *schema.Resource {
62
76
"machine_type" : {
63
77
Type : schema .TypeString ,
64
78
Optional : true ,
65
- ForceNew : true ,
66
79
Description : `Machine type of VM Instance underlying connector. Default is e2-micro` ,
67
80
Default : "e2-micro" ,
68
81
},
69
82
"max_instances" : {
70
83
Type : schema .TypeInt ,
71
84
Computed : true ,
72
85
Optional : true ,
73
- ForceNew : true ,
74
86
Description : `Maximum value of instances in autoscaling group underlying the connector.` ,
75
87
},
76
88
"max_throughput" : {
77
89
Type : schema .TypeInt ,
90
+ Computed : true ,
78
91
Optional : true ,
79
92
ForceNew : true ,
80
- ValidateFunc : validation .IntBetween (200 , 1000 ),
81
- Description : `Maximum throughput of the connector in Mbps, must be greater than 'min_throughput'. Default is 300.` ,
82
- Default : 300 ,
93
+ ValidateFunc : validation .IntBetween (300 , 1000 ),
94
+ Description : `Maximum throughput of the connector in Mbps, must be greater than 'min_throughput'. Default is 1000.` ,
83
95
},
84
96
"min_instances" : {
85
97
Type : schema .TypeInt ,
86
98
Computed : true ,
87
99
Optional : true ,
88
- ForceNew : true ,
89
100
Description : `Minimum value of instances in autoscaling group underlying the connector.` ,
90
101
},
91
102
"min_throughput" : {
92
103
Type : schema .TypeInt ,
104
+ Computed : true ,
93
105
Optional : true ,
94
106
ForceNew : true ,
95
- ValidateFunc : validation .IntBetween (200 , 1000 ),
107
+ ValidateFunc : validation .IntBetween (200 , 900 ),
96
108
Description : `Minimum throughput of the connector in Mbps. Default and min is 200.` ,
97
- Default : 200 ,
98
109
},
99
110
"network" : {
100
111
Type : schema .TypeString ,
@@ -405,6 +416,104 @@ func resourceVPCAccessConnectorRead(d *schema.ResourceData, meta interface{}) er
405
416
return nil
406
417
}
407
418
419
+ func resourceVPCAccessConnectorUpdate (d * schema.ResourceData , meta interface {}) error {
420
+ config := meta .(* transport_tpg.Config )
421
+ userAgent , err := tpgresource .GenerateUserAgentString (d , config .UserAgent )
422
+ if err != nil {
423
+ return err
424
+ }
425
+
426
+ billingProject := ""
427
+
428
+ project , err := tpgresource .GetProject (d , config )
429
+ if err != nil {
430
+ return fmt .Errorf ("Error fetching project for Connector: %s" , err )
431
+ }
432
+ billingProject = project
433
+
434
+ obj := make (map [string ]interface {})
435
+ machineTypeProp , err := expandVPCAccessConnectorMachineType (d .Get ("machine_type" ), d , config )
436
+ if err != nil {
437
+ return err
438
+ } else if v , ok := d .GetOkExists ("machine_type" ); ! tpgresource .IsEmptyValue (reflect .ValueOf (v )) && (ok || ! reflect .DeepEqual (v , machineTypeProp )) {
439
+ obj ["machineType" ] = machineTypeProp
440
+ }
441
+ minInstancesProp , err := expandVPCAccessConnectorMinInstances (d .Get ("min_instances" ), d , config )
442
+ if err != nil {
443
+ return err
444
+ } else if v , ok := d .GetOkExists ("min_instances" ); ! tpgresource .IsEmptyValue (reflect .ValueOf (v )) && (ok || ! reflect .DeepEqual (v , minInstancesProp )) {
445
+ obj ["minInstances" ] = minInstancesProp
446
+ }
447
+ maxInstancesProp , err := expandVPCAccessConnectorMaxInstances (d .Get ("max_instances" ), d , config )
448
+ if err != nil {
449
+ return err
450
+ } else if v , ok := d .GetOkExists ("max_instances" ); ! tpgresource .IsEmptyValue (reflect .ValueOf (v )) && (ok || ! reflect .DeepEqual (v , maxInstancesProp )) {
451
+ obj ["maxInstances" ] = maxInstancesProp
452
+ }
453
+
454
+ obj , err = resourceVPCAccessConnectorEncoder (d , meta , obj )
455
+ if err != nil {
456
+ return err
457
+ }
458
+
459
+ url , err := tpgresource .ReplaceVars (d , config , "{{VPCAccessBasePath}}projects/{{project}}/locations/{{region}}/connectors/{{name}}" )
460
+ if err != nil {
461
+ return err
462
+ }
463
+
464
+ log .Printf ("[DEBUG] Updating Connector %q: %#v" , d .Id (), obj )
465
+ updateMask := []string {}
466
+
467
+ if d .HasChange ("machine_type" ) {
468
+ updateMask = append (updateMask , "machineType" )
469
+ }
470
+
471
+ if d .HasChange ("min_instances" ) {
472
+ updateMask = append (updateMask , "minInstances" )
473
+ }
474
+
475
+ if d .HasChange ("max_instances" ) {
476
+ updateMask = append (updateMask , "maxInstances" )
477
+ }
478
+ // updateMask is a URL parameter but not present in the schema, so ReplaceVars
479
+ // won't set it
480
+ url , err = transport_tpg .AddQueryParams (url , map [string ]string {"updateMask" : strings .Join (updateMask , "," )})
481
+ if err != nil {
482
+ return err
483
+ }
484
+
485
+ // err == nil indicates that the billing_project value was found
486
+ if bp , err := tpgresource .GetBillingProject (d , config ); err == nil {
487
+ billingProject = bp
488
+ }
489
+
490
+ res , err := transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
491
+ Config : config ,
492
+ Method : "PATCH" ,
493
+ Project : billingProject ,
494
+ RawURL : url ,
495
+ UserAgent : userAgent ,
496
+ Body : obj ,
497
+ Timeout : d .Timeout (schema .TimeoutUpdate ),
498
+ })
499
+
500
+ if err != nil {
501
+ return fmt .Errorf ("Error updating Connector %q: %s" , d .Id (), err )
502
+ } else {
503
+ log .Printf ("[DEBUG] Finished updating Connector %q: %#v" , d .Id (), res )
504
+ }
505
+
506
+ err = VPCAccessOperationWaitTime (
507
+ config , res , project , "Updating Connector" , userAgent ,
508
+ d .Timeout (schema .TimeoutUpdate ))
509
+
510
+ if err != nil {
511
+ return err
512
+ }
513
+
514
+ return resourceVPCAccessConnectorRead (d , meta )
515
+ }
516
+
408
517
func resourceVPCAccessConnectorDelete (d * schema.ResourceData , meta interface {}) error {
409
518
config := meta .(* transport_tpg.Config )
410
519
userAgent , err := tpgresource .GenerateUserAgentString (d , config .UserAgent )
0 commit comments