Skip to content

Commit 9539567

Browse files
Main (#13943) (#22818)
[upstream:7f199d95bafc97b9f2a7533f486f21e5f051ea96] Signed-off-by: Modular Magician <[email protected]>
1 parent affd517 commit 9539567

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

.changelog/13943.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
alloydb: added `activation_policy` field to `google_alloydb_instance` resource
3+
```

google/services/alloydb/resource_alloydb_instance.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ Example: {instance_type = google_alloydb_cluster.<secondary_cluster_name>.cluste
8484
If the instance type is SECONDARY, the terraform delete instance operation does not delete the secondary instance but abandons it instead.
8585
Use deletion_policy = "FORCE" in the associated secondary cluster and delete the cluster forcefully to delete the secondary cluster as well its associated secondary instance.
8686
Users can undo the delete secondary instance action by importing the deleted secondary instance by calling terraform import. Possible values: ["PRIMARY", "READ_POOL", "SECONDARY"]`,
87+
},
88+
"activation_policy": {
89+
Type: schema.TypeString,
90+
Computed: true,
91+
Optional: true,
92+
ValidateFunc: verify.ValidateEnum([]string{"ACTIVATION_POLICY_UNSPECIFIED", "ALWAYS", "NEVER", ""}),
93+
Description: `'Specifies whether an instance needs to spin up. Once the instance is
94+
active, the activation policy can be updated to the 'NEVER' to stop the
95+
instance. Likewise, the activation policy can be updated to 'ALWAYS' to
96+
start the instance.
97+
There are restrictions around when an instance can/cannot be activated (for
98+
example, a read pool instance should be stopped before stopping primary
99+
etc.). Please refer to the API documentation for more details.
100+
Possible values are: 'ACTIVATION_POLICY_UNSPECIFIED', 'ALWAYS', 'NEVER'.' Possible values: ["ACTIVATION_POLICY_UNSPECIFIED", "ALWAYS", "NEVER"]`,
87101
},
88102
"annotations": {
89103
Type: schema.TypeMap,
@@ -477,6 +491,12 @@ func resourceAlloydbInstanceCreate(d *schema.ResourceData, meta interface{}) err
477491
} else if v, ok := d.GetOkExists("availability_type"); !tpgresource.IsEmptyValue(reflect.ValueOf(availabilityTypeProp)) && (ok || !reflect.DeepEqual(v, availabilityTypeProp)) {
478492
obj["availabilityType"] = availabilityTypeProp
479493
}
494+
activationPolicyProp, err := expandAlloydbInstanceActivationPolicy(d.Get("activation_policy"), d, config)
495+
if err != nil {
496+
return err
497+
} else if v, ok := d.GetOkExists("activation_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(activationPolicyProp)) && (ok || !reflect.DeepEqual(v, activationPolicyProp)) {
498+
obj["activationPolicy"] = activationPolicyProp
499+
}
480500
instanceTypeProp, err := expandAlloydbInstanceInstanceType(d.Get("instance_type"), d, config)
481501
if err != nil {
482502
return err
@@ -652,6 +672,9 @@ func resourceAlloydbInstanceRead(d *schema.ResourceData, meta interface{}) error
652672
if err := d.Set("availability_type", flattenAlloydbInstanceAvailabilityType(res["availabilityType"], d, config)); err != nil {
653673
return fmt.Errorf("Error reading Instance: %s", err)
654674
}
675+
if err := d.Set("activation_policy", flattenAlloydbInstanceActivationPolicy(res["activationPolicy"], d, config)); err != nil {
676+
return fmt.Errorf("Error reading Instance: %s", err)
677+
}
655678
if err := d.Set("instance_type", flattenAlloydbInstanceInstanceType(res["instanceType"], d, config)); err != nil {
656679
return fmt.Errorf("Error reading Instance: %s", err)
657680
}
@@ -730,6 +753,12 @@ func resourceAlloydbInstanceUpdate(d *schema.ResourceData, meta interface{}) err
730753
} else if v, ok := d.GetOkExists("availability_type"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, availabilityTypeProp)) {
731754
obj["availabilityType"] = availabilityTypeProp
732755
}
756+
activationPolicyProp, err := expandAlloydbInstanceActivationPolicy(d.Get("activation_policy"), d, config)
757+
if err != nil {
758+
return err
759+
} else if v, ok := d.GetOkExists("activation_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, activationPolicyProp)) {
760+
obj["activationPolicy"] = activationPolicyProp
761+
}
733762
queryInsightsConfigProp, err := expandAlloydbInstanceQueryInsightsConfig(d.Get("query_insights_config"), d, config)
734763
if err != nil {
735764
return err
@@ -804,6 +833,10 @@ func resourceAlloydbInstanceUpdate(d *schema.ResourceData, meta interface{}) err
804833
updateMask = append(updateMask, "availabilityType")
805834
}
806835

836+
if d.HasChange("activation_policy") {
837+
updateMask = append(updateMask, "activationPolicy")
838+
}
839+
807840
if d.HasChange("query_insights_config") {
808841
updateMask = append(updateMask, "queryInsightsConfig")
809842
}
@@ -1033,6 +1066,10 @@ func flattenAlloydbInstanceAvailabilityType(v interface{}, d *schema.ResourceDat
10331066
return v
10341067
}
10351068

1069+
func flattenAlloydbInstanceActivationPolicy(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1070+
return v
1071+
}
1072+
10361073
func flattenAlloydbInstanceInstanceType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
10371074
return v
10381075
}
@@ -1402,6 +1439,10 @@ func expandAlloydbInstanceAvailabilityType(v interface{}, d tpgresource.Terrafor
14021439
return v, nil
14031440
}
14041441

1442+
func expandAlloydbInstanceActivationPolicy(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1443+
return v, nil
1444+
}
1445+
14051446
func expandAlloydbInstanceInstanceType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
14061447
return v, nil
14071448
}

google/services/alloydb/resource_alloydb_instance_generated_meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ api_service_name: 'alloydb.googleapis.com'
55
api_version: 'v1'
66
api_resource_type_kind: 'Instance'
77
fields:
8+
- field: 'activation_policy'
89
- field: 'annotations'
910
- field: 'availability_type'
1011
- field: 'client_connection_config.require_connectors'

google/services/alloydb/resource_alloydb_instance_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,99 @@ func TestAccAlloydbInstance_createInstanceWithMandatoryFields(t *testing.T) {
130130
})
131131
}
132132

133+
// This test passes if we are able to create a primary instance STOP it and then START it back again
134+
func TestAccAlloydbInstance_stopstart(t *testing.T) {
135+
t.Parallel()
136+
137+
suffix := acctest.RandString(t, 10)
138+
networkName := acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-clientconnectionconfig")
139+
140+
context := map[string]interface{}{
141+
"random_suffix": suffix,
142+
"network_name": networkName,
143+
}
144+
145+
contextStop := map[string]interface{}{
146+
"random_suffix": suffix,
147+
"network_name": networkName,
148+
"activation_policy": "NEVER",
149+
}
150+
151+
contextStart := map[string]interface{}{
152+
"random_suffix": suffix,
153+
"network_name": networkName,
154+
"activation_policy": "ALWAYS",
155+
}
156+
157+
acctest.VcrTest(t, resource.TestCase{
158+
PreCheck: func() { acctest.AccTestPreCheck(t) },
159+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
160+
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
161+
Steps: []resource.TestStep{
162+
{
163+
Config: testAccAlloydbInstance_createInstanceWithMandatoryFields(context),
164+
},
165+
{
166+
ResourceName: "google_alloydb_instance.default",
167+
ImportState: true,
168+
ImportStateVerify: true,
169+
ImportStateVerifyIgnore: []string{"cluster", "instance_id", "reconciling", "update_time"},
170+
},
171+
{
172+
Config: testAccAlloydbInstance_updateActivationPolicy(contextStop),
173+
Check: resource.ComposeTestCheckFunc(
174+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "activation_policy", "NEVER"),
175+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "state", "STOPPED"),
176+
),
177+
},
178+
{
179+
ResourceName: "google_alloydb_instance.default",
180+
ImportState: true,
181+
ImportStateVerify: true,
182+
ImportStateVerifyIgnore: []string{"cluster", "instance_id", "reconciling", "update_time", "labels", "terraform_labels"},
183+
},
184+
{
185+
Config: testAccAlloydbInstance_updateActivationPolicy(contextStart),
186+
Check: resource.ComposeTestCheckFunc(
187+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "activation_policy", "ALWAYS"),
188+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "state", "READY"),
189+
),
190+
},
191+
{
192+
ResourceName: "google_alloydb_instance.default",
193+
ImportState: true,
194+
ImportStateVerify: true,
195+
ImportStateVerifyIgnore: []string{"cluster", "instance_id", "reconciling", "update_time", "labels", "terraform_labels"},
196+
},
197+
},
198+
})
199+
}
200+
201+
func testAccAlloydbInstance_updateActivationPolicy(context map[string]interface{}) string {
202+
return acctest.Nprintf(`
203+
resource "google_alloydb_instance" "default" {
204+
cluster = google_alloydb_cluster.default.name
205+
instance_id = "tf-test-alloydb-instance%{random_suffix}"
206+
instance_type = "PRIMARY"
207+
activation_policy = "%{activation_policy}"
208+
}
209+
210+
resource "google_alloydb_cluster" "default" {
211+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
212+
location = "us-central1"
213+
network_config {
214+
network = data.google_compute_network.default.id
215+
}
216+
}
217+
218+
data "google_project" "project" {}
219+
220+
data "google_compute_network" "default" {
221+
name = "%{network_name}"
222+
}
223+
`, context)
224+
}
225+
133226
func testAccAlloydbInstance_createInstanceWithMandatoryFields(context map[string]interface{}) string {
134227
return acctest.Nprintf(`
135228
resource "google_alloydb_instance" "default" {

website/docs/r/alloydb_instance.html.markdown

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ The following arguments are supported:
230230
Possible values are: `AVAILABILITY_TYPE_UNSPECIFIED`, `ZONAL`, `REGIONAL`.'
231231
Possible values are: `AVAILABILITY_TYPE_UNSPECIFIED`, `ZONAL`, `REGIONAL`.
232232

233+
* `activation_policy` -
234+
(Optional)
235+
'Specifies whether an instance needs to spin up. Once the instance is
236+
active, the activation policy can be updated to the `NEVER` to stop the
237+
instance. Likewise, the activation policy can be updated to `ALWAYS` to
238+
start the instance.
239+
There are restrictions around when an instance can/cannot be activated (for
240+
example, a read pool instance should be stopped before stopping primary
241+
etc.). Please refer to the API documentation for more details.
242+
Possible values are: `ACTIVATION_POLICY_UNSPECIFIED`, `ALWAYS`, `NEVER`.'
243+
Possible values are: `ACTIVATION_POLICY_UNSPECIFIED`, `ALWAYS`, `NEVER`.
244+
233245
* `query_insights_config` -
234246
(Optional)
235247
Configuration for query insights.

0 commit comments

Comments
 (0)