forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alloydb crr secondary instance (GoogleCloudPlatform#9203)
Co-authored-by: Riley Karson <[email protected]> Co-authored-by: Shubham Sahu <[email protected]>
- Loading branch information
Showing
8 changed files
with
830 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
mmv1/templates/terraform/examples/alloydb_secondary_instance_basic.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
resource "google_alloydb_cluster" "primary" { | ||
cluster_id = "<%= ctx[:vars]['alloydb_primary_cluster_name'] %>" | ||
location = "us-central1" | ||
network = google_compute_network.default.id | ||
} | ||
|
||
resource "google_alloydb_instance" "primary" { | ||
cluster = google_alloydb_cluster.primary.name | ||
instance_id = "<%= ctx[:vars]['alloydb_primary_instance_name'] %>" | ||
instance_type = "PRIMARY" | ||
|
||
machine_config { | ||
cpu_count = 2 | ||
} | ||
|
||
depends_on = [google_service_networking_connection.vpc_connection] | ||
} | ||
|
||
resource "google_alloydb_cluster" "secondary" { | ||
cluster_id = "<%= ctx[:vars]['alloydb_secondary_cluster_name'] %>" | ||
location = "us-east1" | ||
network = google_compute_network.default.id | ||
cluster_type = "SECONDARY" | ||
|
||
continuous_backup_config { | ||
enabled = false | ||
} | ||
|
||
secondary_config { | ||
primary_cluster_name = google_alloydb_cluster.primary.name | ||
} | ||
|
||
deletion_policy = "FORCE" | ||
|
||
depends_on = [google_alloydb_instance.primary] | ||
} | ||
|
||
resource "google_alloydb_instance" "<%= ctx[:primary_resource_id] %>" { | ||
cluster = google_alloydb_cluster.secondary.name | ||
instance_id = "<%= ctx[:vars]['alloydb_secondary_instance_name'] %>" | ||
instance_type = google_alloydb_cluster.secondary.cluster_type | ||
|
||
machine_config { | ||
cpu_count = 2 | ||
} | ||
|
||
depends_on = [google_service_networking_connection.vpc_connection] | ||
} | ||
|
||
data "google_project" "project" {} | ||
|
||
resource "google_compute_network" "default" { | ||
name = "<%= ctx[:vars]['network_name'] %>" | ||
} | ||
|
||
resource "google_compute_global_address" "private_ip_alloc" { | ||
name = "<%= ctx[:vars]['alloydb_secondary_instance_name'] %>" | ||
address_type = "INTERNAL" | ||
purpose = "VPC_PEERING" | ||
prefix_length = 16 | ||
network = google_compute_network.default.id | ||
} | ||
|
||
resource "google_service_networking_connection" "vpc_connection" { | ||
network = google_compute_network.default.id | ||
service = "servicenetworking.googleapis.com" | ||
reserved_peering_ranges = [google_compute_global_address.private_ip_alloc.name] | ||
} |
50 changes: 50 additions & 0 deletions
50
mmv1/templates/terraform/examples/alloydb_secondary_instance_basic_test.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
resource "google_alloydb_cluster" "primary" { | ||
cluster_id = "<%= ctx[:vars]['alloydb_primary_cluster_name'] %>" | ||
location = "us-central1" | ||
network = data.google_compute_network.default.id | ||
} | ||
|
||
resource "google_alloydb_instance" "primary" { | ||
cluster = google_alloydb_cluster.primary.name | ||
instance_id = "<%= ctx[:vars]['alloydb_primary_instance_name'] %>" | ||
instance_type = "PRIMARY" | ||
|
||
machine_config { | ||
cpu_count = 2 | ||
} | ||
} | ||
|
||
resource "google_alloydb_cluster" "secondary" { | ||
cluster_id = "<%= ctx[:vars]['alloydb_secondary_cluster_name'] %>" | ||
location = "us-east1" | ||
network = data.google_compute_network.default.id | ||
cluster_type = "SECONDARY" | ||
|
||
continuous_backup_config { | ||
enabled = false | ||
} | ||
|
||
secondary_config { | ||
primary_cluster_name = google_alloydb_cluster.primary.name | ||
} | ||
|
||
deletion_policy = "FORCE" | ||
|
||
depends_on = [google_alloydb_instance.primary] | ||
} | ||
|
||
resource "google_alloydb_instance" "<%= ctx[:primary_resource_id] %>" { | ||
cluster = google_alloydb_cluster.secondary.name | ||
instance_id = "<%= ctx[:vars]['alloydb_secondary_instance_name'] %>" | ||
instance_type = google_alloydb_cluster.secondary.cluster_type | ||
|
||
machine_config { | ||
cpu_count = 2 | ||
} | ||
} | ||
|
||
data "google_project" "project" {} | ||
|
||
data "google_compute_network" "default" { | ||
name = "<%= ctx[:vars]['network_name'] %>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Read the config and call createsecondary api if instance_type is SECONDARY | ||
|
||
if instanceType := d.Get("instance_type"); instanceType == "SECONDARY" { | ||
url = strings.Replace(url, "instances?instanceId", "instances:createsecondary?instanceId", 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Forcefully delete the secondary cluster and the dependent instances because deletion of secondary instance is not supported. | ||
if deletionPolicy := d.Get("deletion_policy"); deletionPolicy == "FORCE" { | ||
url = url + "?force=true" | ||
} |
18 changes: 18 additions & 0 deletions
18
mmv1/templates/terraform/pre_delete/alloydb_instance.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Read the config and avoid calling the delete API if the instance_type is SECONDARY and instead return nil | ||
// Returning nil is equivalent of returning a success message to the users | ||
// This is done because deletion of secondary instance is not supported | ||
// Instead users should be deleting the secondary cluster which will forcefully delete the associated secondary instance | ||
// A warning message prompts the user to delete the associated secondary cluster. | ||
// Users can always undo the delete secondary instance action by importing the deleted secondary instance by calling terraform import | ||
|
||
var instanceType interface{} | ||
instanceTypeProp, err := expandAlloydbInstanceInstanceType(d.Get("instance_type"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("instance_type"); !tpgresource.IsEmptyValue(reflect.ValueOf(instanceTypeProp)) && (ok || !reflect.DeepEqual(v, instanceTypeProp)) { | ||
instanceType = instanceTypeProp | ||
} | ||
if instanceType != nil && instanceType == "SECONDARY" { | ||
log.Printf("[WARNING] This operation didn't delete the Secondary Instance %q. Please delete the associated Secondary Cluster as well to delete the entire cluster and the secondary instance.\n", d.Id()) | ||
return nil | ||
} |
Oops, something went wrong.