diff --git a/.changelog/3246.txt b/.changelog/3246.txt new file mode 100644 index 0000000000..c4e001f58a --- /dev/null +++ b/.changelog/3246.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +redis: added `connect_mode` field to `google_redis_instance` resource +``` diff --git a/google-beta/resource_redis_instance.go b/google-beta/resource_redis_instance.go index e99465e492..d3b85c642f 100644 --- a/google-beta/resource_redis_instance.go +++ b/google-beta/resource_redis_instance.go @@ -75,6 +75,16 @@ If provided, it must be a different zone from the one provided in instance is connected. If left unspecified, the default network will be used.`, }, + "connect_mode": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"DIRECT_PEERING", "PRIVATE_SERVICE_ACCESS", ""}, false), + Description: `The connection mode of the Redis instance. Can be either +'DIRECT_PEERING' or 'PRIVATE_SERVICE_ACCESS'. The default +connect mode if not provided is 'DIRECT_PEERING'.`, + Default: "DIRECT_PEERING", + }, "display_name": { Type: schema.TypeString, Optional: true, @@ -197,6 +207,12 @@ func resourceRedisInstanceCreate(d *schema.ResourceData, meta interface{}) error } else if v, ok := d.GetOkExists("authorized_network"); !isEmptyValue(reflect.ValueOf(authorizedNetworkProp)) && (ok || !reflect.DeepEqual(v, authorizedNetworkProp)) { obj["authorizedNetwork"] = authorizedNetworkProp } + connectModeProp, err := expandRedisInstanceConnectMode(d.Get("connect_mode"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("connect_mode"); !isEmptyValue(reflect.ValueOf(connectModeProp)) && (ok || !reflect.DeepEqual(v, connectModeProp)) { + obj["connectMode"] = connectModeProp + } displayNameProp, err := expandRedisInstanceDisplayName(d.Get("display_name"), d, config) if err != nil { return err @@ -342,6 +358,9 @@ func resourceRedisInstanceRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("authorized_network", flattenRedisInstanceAuthorizedNetwork(res["authorizedNetwork"], d, config)); err != nil { return fmt.Errorf("Error reading Instance: %s", err) } + if err := d.Set("connect_mode", flattenRedisInstanceConnectMode(res["connectMode"], d, config)); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } if err := d.Set("create_time", flattenRedisInstanceCreateTime(res["createTime"], d, config)); err != nil { return fmt.Errorf("Error reading Instance: %s", err) } @@ -532,6 +551,10 @@ func flattenRedisInstanceAuthorizedNetwork(v interface{}, d *schema.ResourceData return v } +func flattenRedisInstanceConnectMode(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func flattenRedisInstanceCreateTime(v interface{}, d *schema.ResourceData, config *Config) interface{} { return v } @@ -611,6 +634,10 @@ func expandRedisInstanceAuthorizedNetwork(v interface{}, d TerraformResourceData return fv.RelativeLink(), nil } +func expandRedisInstanceConnectMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func expandRedisInstanceDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } diff --git a/google-beta/resource_redis_instance_generated_test.go b/google-beta/resource_redis_instance_generated_test.go index a33e88c390..1649bc296e 100644 --- a/google-beta/resource_redis_instance_generated_test.go +++ b/google-beta/resource_redis_instance_generated_test.go @@ -120,6 +120,71 @@ data "google_compute_network" "redis-network" { `, context) } +func TestAccRedisInstance_redisInstancePrivateServiceExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckRedisInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccRedisInstance_redisInstancePrivateServiceExample(context), + }, + { + ResourceName: "google_redis_instance.cache", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"region"}, + }, + }, + }) +} + +func testAccRedisInstance_redisInstancePrivateServiceExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_network" "network" { + name = "tf-test%{random_suffix}" +} + +resource "google_compute_global_address" "service_range" { + name = "tf-test%{random_suffix}" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 16 + network = google_compute_network.network.self_link +} + +resource "google_service_networking_connection" "private_service_connection" { + network = google_compute_network.network.self_link + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.service_range.name] +} + +resource "google_redis_instance" "cache" { + name = "tf-test%{random_suffix}" + tier = "STANDARD_HA" + memory_size_gb = 1 + + location_id = "us-central1-a" + alternative_location_id = "us-central1-f" + + authorized_network = google_compute_network.network.self_link + connect_mode = "PRIVATE_SERVICE_ACCESS" + + redis_version = "REDIS_3_2" + display_name = "Terraform Test Instance" + + depends_on = [ google_service_networking_connection.private_service_connection ] + +} +`, context) +} + func testAccCheckRedisInstanceDestroy(s *terraform.State) error { for name, rs := range s.RootModule().Resources { if rs.Type != "google_redis_instance" { diff --git a/website/docs/r/redis_instance.html.markdown b/website/docs/r/redis_instance.html.markdown index d60a40323a..1281501107 100644 --- a/website/docs/r/redis_instance.html.markdown +++ b/website/docs/r/redis_instance.html.markdown @@ -86,6 +86,51 @@ data "google_compute_network" "redis-network" { name = "redis-test-network" } ``` +
+## Example Usage - Redis Instance Private Service + + +```hcl +resource "google_compute_network" "network" { + name = "tf-test%{random_suffix}" +} + +resource "google_compute_global_address" "service_range" { + name = "tf-test%{random_suffix}" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 16 + network = google_compute_network.network.self_link +} + +resource "google_service_networking_connection" "private_service_connection" { + network = google_compute_network.network.self_link + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.service_range.name] +} + +resource "google_redis_instance" "cache" { + name = "tf-test%{random_suffix}" + tier = "STANDARD_HA" + memory_size_gb = 1 + + location_id = "us-central1-a" + alternative_location_id = "us-central1-f" + + authorized_network = google_compute_network.network.self_link + connect_mode = "PRIVATE_SERVICE_ACCESS" + + redis_version = "REDIS_3_2" + display_name = "Terraform Test Instance" + + depends_on = [ google_service_networking_connection.private_service_connection ] + +} +``` ## Argument Reference @@ -117,6 +162,12 @@ The following arguments are supported: instance is connected. If left unspecified, the default network will be used. +* `connect_mode` - + (Optional) + The connection mode of the Redis instance. Can be either + `DIRECT_PEERING` or `PRIVATE_SERVICE_ACCESS`. The default + connect mode if not provided is `DIRECT_PEERING`. + * `display_name` - (Optional) An arbitrary and optional user-provided name for the instance.