Skip to content

Commit

Permalink
Memorystore Redis connectMode support (#3246) (#1854)
Browse files Browse the repository at this point in the history
* Memorystore Redis connectMode support

* Redis connectMode explicit default

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Mar 13, 2020
1 parent f712d28 commit feef617
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3246.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
redis: added `connect_mode` field to `google_redis_instance` resource
```
27 changes: 27 additions & 0 deletions google-beta/resource_redis_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
65 changes: 65 additions & 0 deletions google-beta/resource_redis_instance_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
51 changes: 51 additions & 0 deletions website/docs/r/redis_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,51 @@ data "google_compute_network" "redis-network" {
name = "redis-test-network"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgit.luolix.top%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=redis_instance_private_service&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit feef617

Please sign in to comment.