2018-05-23 18:25:48 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-06-06 17:55:04 +00:00
|
|
|
"strings"
|
2018-05-23 18:25:48 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2018-06-06 17:55:04 +00:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-05-23 18:25:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccRedisInstance_basic(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
name := acctest.RandomWithPrefix("tf-test")
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2018-06-06 17:55:04 +00:00
|
|
|
CheckDestroy: testAccCheckRedisInstanceDestroy,
|
2018-05-23 18:25:48 +00:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRedisInstance_basic(name),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_redis_instance.test",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-05 00:41:48 +00:00
|
|
|
func TestAccRedisInstance_update(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
name := acctest.RandomWithPrefix("tf-test")
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2018-06-06 17:55:04 +00:00
|
|
|
CheckDestroy: testAccCheckRedisInstanceDestroy,
|
2018-06-05 00:41:48 +00:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRedisInstance_update(name),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_redis_instance.test",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRedisInstance_update2(name),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_redis_instance.test",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:25:48 +00:00
|
|
|
func TestAccRedisInstance_full(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
name := acctest.RandomWithPrefix("tf-test")
|
|
|
|
network := acctest.RandomWithPrefix("tf-test")
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2018-06-06 17:55:04 +00:00
|
|
|
CheckDestroy: testAccCheckRedisInstanceDestroy,
|
2018-05-23 18:25:48 +00:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRedisInstance_full(name, network),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_redis_instance.test",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-06 17:55:04 +00:00
|
|
|
func testAccCheckRedisInstanceDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_redis_instance" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
redisIdParts := strings.Split(rs.Primary.ID, "/")
|
|
|
|
if len(redisIdParts) != 3 {
|
|
|
|
return fmt.Errorf("Unexpected resource ID %s, expected {project}/{region}/{name}", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
project, region, inst := redisIdParts[0], redisIdParts[1], redisIdParts[2]
|
|
|
|
|
|
|
|
name := fmt.Sprintf("projects/%s/locations/%s/instances/%s", project, region, inst)
|
|
|
|
_, err := config.clientRedis.Projects.Locations.Get(name).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Redis instance still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:25:48 +00:00
|
|
|
func testAccRedisInstance_basic(name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_redis_instance" "test" {
|
|
|
|
name = "%s"
|
|
|
|
memory_size_gb = 1
|
|
|
|
}`, name)
|
|
|
|
}
|
|
|
|
|
2018-06-05 00:41:48 +00:00
|
|
|
func testAccRedisInstance_update(name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_redis_instance" "test" {
|
|
|
|
name = "%s"
|
|
|
|
display_name = "pre-update"
|
|
|
|
memory_size_gb = 1
|
|
|
|
|
|
|
|
labels {
|
|
|
|
my_key = "my_val"
|
|
|
|
other_key = "other_val"
|
|
|
|
}
|
2018-07-02 17:16:38 +00:00
|
|
|
|
|
|
|
redis_configs {
|
|
|
|
maxmemory-policy = "allkeys-lru"
|
|
|
|
notify-keyspace-events = "KEA"
|
|
|
|
}
|
2018-06-05 00:41:48 +00:00
|
|
|
}`, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccRedisInstance_update2(name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_redis_instance" "test" {
|
|
|
|
name = "%s"
|
|
|
|
display_name = "post-update"
|
|
|
|
memory_size_gb = 1
|
|
|
|
|
|
|
|
labels {
|
|
|
|
my_key = "my_val"
|
|
|
|
other_key = "new_val"
|
|
|
|
}
|
2018-07-02 17:16:38 +00:00
|
|
|
|
|
|
|
redis_configs {
|
|
|
|
maxmemory-policy = "noeviction"
|
|
|
|
notify-keyspace-events = ""
|
|
|
|
}
|
2018-06-05 00:41:48 +00:00
|
|
|
}`, name)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:25:48 +00:00
|
|
|
func testAccRedisInstance_full(name, network string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_compute_network" "test" {
|
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_redis_instance" "test" {
|
|
|
|
name = "%s"
|
|
|
|
tier = "STANDARD_HA"
|
|
|
|
memory_size_gb = 1
|
|
|
|
|
2018-06-06 17:55:04 +00:00
|
|
|
authorized_network = "${google_compute_network.test.self_link}"
|
|
|
|
|
2018-05-23 18:25:48 +00:00
|
|
|
region = "us-central1"
|
|
|
|
location_id = "us-central1-a"
|
|
|
|
alternative_location_id = "us-central1-f"
|
|
|
|
|
|
|
|
redis_version = "REDIS_3_2"
|
|
|
|
display_name = "Terraform Test Instance"
|
|
|
|
reserved_ip_range = "192.168.0.0/29"
|
|
|
|
|
|
|
|
labels {
|
|
|
|
my_key = "my_val"
|
|
|
|
other_key = "other_val"
|
|
|
|
}
|
2018-07-02 17:16:38 +00:00
|
|
|
|
|
|
|
redis_configs {
|
|
|
|
maxmemory-policy = "allkeys-lru"
|
|
|
|
notify-keyspace-events = "KEA"
|
|
|
|
}
|
2018-06-06 17:55:04 +00:00
|
|
|
}`, network, name)
|
2018-05-23 18:25:48 +00:00
|
|
|
}
|