From 0e4210c1c3a9759e81514a7da06471def19c69e5 Mon Sep 17 00:00:00 2001 From: Cameron Thornton Date: Tue, 8 Nov 2022 15:28:26 -0600 Subject: [PATCH] AlloyDB Instance (#6792) --- mmv1/products/alloydb/api.yaml | 136 ++++++++++++++++++ mmv1/products/alloydb/terraform.yaml | 25 +++- .../custom_import/alloydb_instance.go.erb | 17 +++ .../examples/alloydb_instance_basic.tf.erb | 44 ++++++ .../resource_alloydb_instance_test.go.erb | 97 +++++++++++++ 5 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 mmv1/templates/terraform/custom_import/alloydb_instance.go.erb create mode 100644 mmv1/templates/terraform/examples/alloydb_instance_basic.tf.erb create mode 100644 mmv1/third_party/terraform/tests/resource_alloydb_instance_test.go.erb diff --git a/mmv1/products/alloydb/api.yaml b/mmv1/products/alloydb/api.yaml index 5eac7f80ea0f..0110feba387c 100644 --- a/mmv1/products/alloydb/api.yaml +++ b/mmv1/products/alloydb/api.yaml @@ -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." \ No newline at end of file diff --git a/mmv1/products/alloydb/terraform.yaml b/mmv1/products/alloydb/terraform.yaml index 5d31d7ddca1c..d7ee45d32484 100644 --- a/mmv1/products/alloydb/terraform.yaml +++ b/mmv1/products/alloydb/terraform.yaml @@ -35,4 +35,27 @@ overrides: !ruby/object:Overrides::ResourceOverrides min_version: beta primary_resource_id: "full" vars: - alloydb_cluster_name: "alloydb-cluster-full" \ No newline at end of file + 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" \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/alloydb_instance.go.erb b/mmv1/templates/terraform/custom_import/alloydb_instance.go.erb new file mode 100644 index 000000000000..4848b3471728 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/alloydb_instance.go.erb @@ -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.+)/instances/(?P[^/]+)", +}, 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 \ No newline at end of file diff --git a/mmv1/templates/terraform/examples/alloydb_instance_basic.tf.erb b/mmv1/templates/terraform/examples/alloydb_instance_basic.tf.erb new file mode 100644 index 000000000000..c1aa681580b0 --- /dev/null +++ b/mmv1/templates/terraform/examples/alloydb_instance_basic.tf.erb @@ -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] +} \ No newline at end of file diff --git a/mmv1/third_party/terraform/tests/resource_alloydb_instance_test.go.erb b/mmv1/third_party/terraform/tests/resource_alloydb_instance_test.go.erb new file mode 100644 index 000000000000..4fc71131a11c --- /dev/null +++ b/mmv1/third_party/terraform/tests/resource_alloydb_instance_test.go.erb @@ -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 -%> \ No newline at end of file