Skip to content

Commit

Permalink
Add cross-region replication support to AlloyDB (GoogleCloudPlatform#…
Browse files Browse the repository at this point in the history
…9012)

Co-authored-by: Riley Karson <[email protected]>
Co-authored-by: Shubham Sahu <[email protected]>
  • Loading branch information
3 people authored Oct 10, 2023
1 parent bb7db4f commit 79d5d56
Show file tree
Hide file tree
Showing 4 changed files with 949 additions and 5 deletions.
36 changes: 32 additions & 4 deletions mmv1/products/alloydb/Cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ async: !ruby/object:Api::OpAsync
base_url: '{{op_id}}'
wait_ms: 1000
timeouts: !ruby/object:Api::Timeouts
insert_minutes: 10
update_minutes: 10
delete_minutes: 10
insert_minutes: 30
update_minutes: 30
delete_minutes: 30
result: !ruby/object:Api::OpAsync::Result
path: 'response'
status: !ruby/object:Api::OpAsync::Status
Expand Down Expand Up @@ -78,8 +78,15 @@ examples:
ignore_read_extra:
- 'reconciling'
- 'update_time'
- !ruby/object:Provider::Terraform::Examples
name: 'alloydb_secondary_cluster_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'
custom_code: !ruby/object:Provider::Terraform::CustomCode
pre_create: templates/terraform/pre_create/alloydb_restore_cluster.go.erb
pre_create: templates/terraform/pre_create/alloydb_cluster.go.erb
parameters:
- !ruby/object:Api::Type::String
name: 'clusterId'
Expand Down Expand Up @@ -452,3 +459,24 @@ properties:
- !ruby/object:Api::Type::String
name: 'sourceType'
description: 'Type of migration source.'
- !ruby/object:Api::Type::Enum
name: clusterType
values:
- :PRIMARY
- :SECONDARY
default_value: :PRIMARY
immutable: true
description: |
The type of cluster. If not set, defaults to PRIMARY.
- !ruby/object:Api::Type::NestedObject
name: "secondaryConfig"
description: |
Configuration of the secondary cluster for Cross Region Replication. This should be set if and only if the cluster is of type SECONDARY.
properties:
- !ruby/object:Api::Type::String
name: "primaryClusterName"
immutable: true
required: true
description: |
Name of the primary cluster must be in the format
'projects/{project}/locations/{location}/clusters/{cluster_id}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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" "<%= ctx[:primary_resource_id] %>" {
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
}

depends_on = [google_alloydb_instance.primary]
}

data "google_project" "project" {}

resource "google_compute_network" "default" {
name = "<%= ctx[:vars]['alloydb_secondary_cluster_name'] %>"
}

resource "google_compute_global_address" "private_ip_alloc" {
name = "<%= ctx[:vars]['alloydb_secondary_cluster_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]
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,36 @@ if backupSource != nil || continuousBackupSource != nil {
}
restoreClusterRequestBody["cluster"] = cluster
obj = restoreClusterRequestBody
}
}


// Read the secondary cluster config to call the api for creating secondary cluster

var secondaryConfig interface{}
var clusterType interface{}

if val, ok := obj["secondaryConfig"]; ok {
secondaryConfig = val
}

if val, ok := obj["clusterType"]; ok {
clusterType = val
}

if clusterType == "SECONDARY" {
if secondaryConfig != nil {
// Use createsecondary API if this is a secondary cluster
url = strings.Replace(url, "clusters?clusterId", "clusters:createsecondary?cluster_id", 1)

// Validation error if secondary_config is not defined
} else {
return fmt.Errorf("Error creating cluster. Can not create secondary cluster without secondary_config field.")
}
}

// Validation error if secondary_config is defined but, cluster type is not secondary
if secondaryConfig != nil {
if clusterType != "SECONDARY" {
return fmt.Errorf("Error creating cluster. Add {cluster_type: \"SECONDARY\"} if attempting to create a secondary cluster, otherwise remove the secondary_config.")
}
}
Loading

0 comments on commit 79d5d56

Please sign in to comment.