-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6da0eb
commit 1e28c54
Showing
7 changed files
with
144 additions
and
1 deletion.
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
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
resource "google_alloydb_cluster" "primary" { | ||
cluster_id = "<%= ctx[:vars]['alloydb_primary_cluster_name'] %>" | ||
location = "us-central1" | ||
network = google_compute_network.default.id | ||
cluster_type = "PRIMARY" | ||
} | ||
|
||
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] | ||
} |
18 changes: 18 additions & 0 deletions
18
mmv1/templates/terraform/pre_create/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 call createsecondary api if instance_type is SECONDARY | ||
|
||
var instanceType interface{} | ||
var cluster interface{} | ||
|
||
if val, ok := obj["instanceType"]; ok { | ||
instanceType = val | ||
} | ||
|
||
cluster = d.Get("cluster") | ||
|
||
if instanceType == "SECONDARY" { | ||
if cluster != nil { | ||
url = strings.Replace(url, "instances?instanceId", "instances:createsecondary?instanceId", 1) | ||
} else { | ||
return fmt.Errorf("Ill defined cluster name when creating secondary instance.") | ||
} | ||
} |
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 | ||
} |
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,6 @@ | ||
// Avoid modifying instance type. No use case for this | ||
// Secondary instances can not be created in primary cluster and vice versa. | ||
// Default change is destroy before create, so modifcation from primary <-> secondary will only delete the instance with failure to create the instance. | ||
if d.HasChange("instance_type") { | ||
return fmt.Errorf("Can not change instance type after creation.") | ||
} |