Skip to content

Commit

Permalink
Add support for secondary instance
Browse files Browse the repository at this point in the history
  • Loading branch information
199201shubhamsahu committed Oct 9, 2023
1 parent e6da0eb commit 1e28c54
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 1 deletion.
9 changes: 9 additions & 0 deletions mmv1/products/alloydb/Cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ examples:
alloydb_secondary_cluster_name: 'alloydb-secondary-cluster'
custom_code: !ruby/object:Provider::Terraform::CustomCode
pre_create: templates/terraform/pre_create/alloydb_cluster.go.erb
pre_delete: templates/terraform/pre_delete/alloydb_cluster.go.erb
pre_update: templates/terraform/pre_update/alloydb_cluster.go.erb
parameters:
- !ruby/object:Api::Type::String
Expand Down Expand Up @@ -479,3 +480,11 @@ properties:
description: |
Name of the primary cluster must be in the format
'projects/{project}/locations/{location}/clusters/{cluster_id}'
virtual_fields:
- !ruby/object:Api::Type::Enum
name: 'deletion_policy'
description: 'Policy to determine if the cluster should be deleted forecefully. Deleting a cluster forcefully, deletes the cluster and all its associated instances within the cluster.'
values:
- :DEFAULT
- :FORCE
default_value: :DEFAULT
21 changes: 20 additions & 1 deletion mmv1/products/alloydb/Instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ skip_sweeper: true
autogen_async: true
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_import: templates/terraform/custom_import/alloydb_instance.go.erb
pre_create: templates/terraform/pre_create/alloydb_instance.go.erb
pre_update: templates/terraform/pre_update/alloydb_instance.go.erb
pre_delete: templates/terraform/pre_delete/alloydb_instance.go.erb
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'alloydb_instance_basic'
Expand All @@ -60,6 +63,18 @@ examples:
ignore_read_extra:
- 'reconciling'
- 'update_time'
- !ruby/object:Provider::Terraform::Examples
name: 'alloydb_secondary_instance_basic'
primary_resource_id: 'secondary'
vars:
alloydb_primary_cluster_name: 'alloydb-primary-cluster'
alloydb_primary_instance_name: 'alloydb-primary-instance'
alloydb_secondary_cluster_name: 'alloydb-secondary-cluster'
alloydb_secondary_instance_name: 'alloydb-secondary-instance'
network_name: 'alloydb-secondary-network'
ignore_read_extra:
- 'reconciling'
- 'update_time'
parameters:
- !ruby/object:Api::Type::ResourceRef
name: 'cluster'
Expand Down Expand Up @@ -153,10 +168,14 @@ properties:
required: true
immutable: true
description: |
The type of the instance. If the instance type is READ_POOL, provide the associated PRIMARY instance in the `depends_on` meta-data attribute.
The type of the instance.
If the instance type is READ_POOL, provide the associated PRIMARY/SECONDARY instance in the `depends_on` meta-data attribute.
If the instance type is SECONDARY, point to the cluster_type of the associated secondary cluster instead of mentioning SECONDARY
Example: {instance_type = google_alloydb_cluster.<secondary_cluster_name>.cluster_type} instead of {instance_type = SECONDARY}
values:
- :PRIMARY
- :READ_POOL
- :SECONDARY
- !ruby/object:Api::Type::String
name: 'ipAddress'
output: true
Expand Down
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 mmv1/templates/terraform/pre_create/alloydb_instance.go.erb
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.")
}
}
4 changes: 4 additions & 0 deletions mmv1/templates/terraform/pre_delete/alloydb_cluster.go.erb
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 mmv1/templates/terraform/pre_delete/alloydb_instance.go.erb
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
}
6 changes: 6 additions & 0 deletions mmv1/templates/terraform/pre_update/alloydb_instance.go.erb
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.")
}

0 comments on commit 1e28c54

Please sign in to comment.