Skip to content

Commit

Permalink
AlloyDB Instance (#6792)
Browse files Browse the repository at this point in the history
  • Loading branch information
c2thorn authored Nov 8, 2022
1 parent 50fa3ab commit 0e4210c
Show file tree
Hide file tree
Showing 5 changed files with 318 additions and 1 deletion.
136 changes: 136 additions & 0 deletions mmv1/products/alloydb/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,139 @@ objects:
- !ruby/object:Api::Type::String
name: "sourceType"
description: "Type of migration source."
- !ruby/object:Api::Resource
name: "Instance"
self_link: "{{cluster}}/instances/{{instance_id}}"
base_url: "{{cluster}}/instances?instanceId={{instance_id}}"
update_verb: :PATCH
update_mask: true
min_version: beta
description: "A managed alloydb cluster instance."
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
"AlloyDB": "https://cloud.google.com/alloydb/docs/"
api: "https://cloud.google.com/alloydb/docs/reference/rest/v1beta/projects.locations.clusters.instances/create"
async: !ruby/object:Api::OpAsync
operation: !ruby/object:Api::OpAsync::Operation
path: "name"
base_url: "{{op_id}}"
wait_ms: 1000
timeouts: !ruby/object:Api::Timeouts
insert_minutes: 10
update_minutes: 10
delete_minutes: 10
result: !ruby/object:Api::OpAsync::Result
path: "response"
status: !ruby/object:Api::OpAsync::Status
path: "done"
complete: true
allowed:
- true
- false
error: !ruby/object:Api::OpAsync::Error
path: "error"
message: "message"
include_project: true
parameters:
- !ruby/object:Api::Type::ResourceRef
name: 'cluster'
description: |
Identifies the alloydb cluster. Must be in the format
'projects/{project}/locations/{location}/clusters/{cluster_id}'
required: true
input: true
resource: 'Cluster'
imports: 'name'
url_param_only: true
- !ruby/object:Api::Type::String
name: "instanceId"
required: true
input: true
url_param_only: true
description: |
The ID of the alloydb instance.
properties:
- !ruby/object:Api::Type::String
name: "name"
output: true
description: |
The name of the instance resource.
- !ruby/object:Api::Type::Time
name: "createTime"
description: |
Time the Instance was created in UTC.
output: true
- !ruby/object:Api::Type::Time
name: "updateTime"
description: |
Time the Instance was updated in UTC.
output: true
- !ruby/object:Api::Type::String
name: "uid"
output: true
description: |
The system-generated UID of the resource.
- !ruby/object:Api::Type::KeyValuePairs
name: "labels"
description: "User-defined labels for the alloydb instance."
- !ruby/object:Api::Type::KeyValuePairs
name: "annotations"
description: "Annotations to allow client tools to store small amount of arbitrary data. This is distinct from labels."
- !ruby/object:Api::Type::String
name: "state"
output: true
description: |
The current state of the alloydb instance.
- !ruby/object:Api::Type::String
name: "displayName"
description: |
User-settable and human-readable display name for the Instance.
- !ruby/object:Api::Type::String
name: "gceZone"
description: |
The Compute Engine zone that the instance should serve from, per https://cloud.google.com/compute/docs/regions-zones This can ONLY be specified for ZONAL instances. If present for a REGIONAL instance, an error will be thrown. If this is absent for a ZONAL instance, instance is created in a random zone with available capacity.
- !ruby/object:Api::Type::Boolean
name: "reconciling"
output: true
description: |
Set to true if the current state of Instance does not match the user's intended state, and the service is actively updating the resource to reconcile them. This can happen due to user-triggered updates or system actions like failover or maintenance.
- !ruby/object:Api::Type::KeyValuePairs
name: "databaseFlags"
description: "Database flags. Set at instance level. * They are copied from primary instance on read instance creation. * Read instances can set new or override existing flags that are relevant for reads, e.g. for enabling columnar cache on a read instance. Flags set on read instance may or may not be present on primary."
- !ruby/object:Api::Type::Enum
name: "availabilityType"
description: |
Availability type of an Instance. Defaults to REGIONAL for both primary and read instances. Note that primary and read instances can have different availability types.
values:
- :AVAILABILITY_TYPE_UNSPECIFIED
- :ZONAL
- :REGIONAL
- !ruby/object:Api::Type::Enum
name: "instanceType"
required: true
input: true
description: |
The type of the instance.
values:
- :PRIMARY
- :READ_POOL
- !ruby/object:Api::Type::String
name: "ipAddress"
output: true
description: |
The IP address for the Instance. This is the connection endpoint for an end-user application.
- !ruby/object:Api::Type::NestedObject
name: "readPoolConfig"
description: "Read pool specific config."
properties:
- !ruby/object:Api::Type::Integer
name: "nodeCount"
description: "Read capacity, i.e. number of nodes in a read pool instance."
- !ruby/object:Api::Type::NestedObject
name: "machineConfig"
description: "Configurations for the machines that host the underlying database engine."
properties:
- !ruby/object:Api::Type::Integer
name: "cpuCount"
input: true
description: "The number of CPU's in the VM instance."
25 changes: 24 additions & 1 deletion mmv1/products/alloydb/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,27 @@ overrides: !ruby/object:Overrides::ResourceOverrides
min_version: beta
primary_resource_id: "full"
vars:
alloydb_cluster_name: "alloydb-cluster-full"
alloydb_cluster_name: "alloydb-cluster-full"
Instance: !ruby/object:Overrides::Terraform::ResourceOverride
import_format: ["{{cluster}}/instances/{{instance_id}}"]
autogen_async: true
properties:
availabilityType: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
machineConfig.cpuCount: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
machineConfig: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_import: templates/terraform/custom_import/alloydb_instance.go.erb
examples:
- !ruby/object:Provider::Terraform::Examples
name: "alloydb_instance_basic"
min_version: beta
primary_resource_id: "default"
vars:
alloydb_cluster_name: "alloydb-cluster"
alloydb_instance_name: "alloydb-instance"
ignore_read_extra:
- "reconciling"
- "update_time"
17 changes: 17 additions & 0 deletions mmv1/templates/terraform/custom_import/alloydb_instance.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
config := meta.(*Config)

// current import_formats can't import fields with forward slashes in their value
if err := parseImportId([]string{
"(?P<cluster>.+)/instances/(?P<instance_id>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{cluster}}/instances/{{instance_id}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
44 changes: 44 additions & 0 deletions mmv1/templates/terraform/examples/alloydb_instance_basic.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
resource "google_alloydb_instance" "<%= ctx[:primary_resource_id] %>" {
provider = google-beta
cluster = google_alloydb_cluster.<%= ctx[:primary_resource_id] %>.name
instance_id = "<%= ctx[:vars]['alloydb_instance_name'] %>"
instance_type = "PRIMARY"

depends_on = [google_service_networking_connection.vpc_connection]
}

resource "google_alloydb_cluster" "<%= ctx[:primary_resource_id] %>" {
provider = google-beta
cluster_id = "<%= ctx[:vars]['alloydb_cluster_name'] %>"
location = "us-central1"
network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"

initial_user {
password = "<%= ctx[:vars]['alloydb_cluster_name'] %>"
}
}

data "google_project" "project" {
provider = google-beta
}

resource "google_compute_network" "default" {
provider = google-beta
name = "<%= ctx[:vars]['alloydb_cluster_name'] %>"
}

resource "google_compute_global_address" "private_ip_alloc" {
provider = google-beta
name = "<%= ctx[:vars]['alloydb_cluster_name'] %>"
address_type = "INTERNAL"
purpose = "VPC_PEERING"
prefix_length = 16
network = google_compute_network.default.id
}

resource "google_service_networking_connection" "vpc_connection" {
provider = google-beta
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
@@ -0,0 +1,97 @@
<% autogen_exception -%>
package google
<% unless version == 'ga' -%>

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAlloydbInstance_update(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccAlloydbInstance_alloydbInstanceBasicExample(context),
},
{
ResourceName: "google_alloydb_instance.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"cluster", "instance_id", "reconciling", "update_time"},
},
{
Config: testAccAlloydbInstance_update(context),
},
{
ResourceName: "google_alloydb_instance.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"cluster", "instance_id", "reconciling", "update_time"},
},
},
})
}

func testAccAlloydbInstance_update(context map[string]interface{}) string {
return Nprintf(`
resource "google_alloydb_instance" "default" {
provider = google-beta
cluster = google_alloydb_cluster.default.name
instance_id = "tf-test-alloydb-instance%{random_suffix}"
instance_type = "PRIMARY"

labels = {
test = "tf-test-alloydb-instance%{random_suffix}"
}

depends_on = [google_service_networking_connection.vpc_connection]
}

resource "google_alloydb_cluster" "default" {
provider = google-beta
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
location = "us-central1"
network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"

initial_user {
password = "tf-test-alloydb-cluster%{random_suffix}"
}
}

data "google_project" "project" {
provider = google-beta
}

resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-alloydb-cluster%{random_suffix}"
}

resource "google_compute_global_address" "private_ip_alloc" {
provider = google-beta
name = "tf-test-alloydb-cluster%{random_suffix}"
address_type = "INTERNAL"
purpose = "VPC_PEERING"
prefix_length = 16
network = google_compute_network.default.id
}

resource "google_service_networking_connection" "vpc_connection" {
provider = google-beta
network = google_compute_network.default.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.private_ip_alloc.name]
}
`, context)
}
<% end -%>

0 comments on commit 0e4210c

Please sign in to comment.