|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/RedisLabs/rediscloud-go-api/redis" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 15 | +) |
| 16 | + |
| 17 | +// Generates the base Terraform config for a Pro Subscription with QPF |
| 18 | +func proSubscriptionQPFBoilerplate(name, cloudAccountName string) string { |
| 19 | + return fmt.Sprintf(` |
| 20 | +data "rediscloud_payment_method" "card" { |
| 21 | + card_type = "Visa" |
| 22 | +} |
| 23 | +
|
| 24 | +data "rediscloud_cloud_account" "account" { |
| 25 | + exclude_internal_account = true |
| 26 | + provider_type = "AWS" |
| 27 | + name = "%s" |
| 28 | +} |
| 29 | +
|
| 30 | +resource "rediscloud_subscription" "example" { |
| 31 | + name = "%s" |
| 32 | + payment_method_id = data.rediscloud_payment_method.card.id |
| 33 | + memory_storage = "ram" |
| 34 | +
|
| 35 | + allowlist { |
| 36 | + cidrs = ["192.168.0.0/16"] |
| 37 | + security_group_ids = [] |
| 38 | + } |
| 39 | +
|
| 40 | + cloud_provider { |
| 41 | + provider = data.rediscloud_cloud_account.account.provider_type |
| 42 | + cloud_account_id = data.rediscloud_cloud_account.account.id |
| 43 | + region { |
| 44 | + region = "eu-west-1" |
| 45 | + networking_deployment_cidr = "10.0.0.0/24" |
| 46 | + preferred_availability_zones = ["eu-west-1a"] |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + creation_plan { |
| 51 | + dataset_size_in_gb = 1 |
| 52 | + throughput_measurement_by = "operations-per-second" |
| 53 | + throughput_measurement_value = 1000 |
| 54 | + quantity = 1 |
| 55 | + replication = false |
| 56 | + support_oss_cluster_api = false |
| 57 | + query_performance_factor = "2x" |
| 58 | + modules = ["RediSearch"] |
| 59 | + } |
| 60 | +}`, cloudAccountName, name) |
| 61 | +} |
| 62 | + |
| 63 | +// Generates Terraform configuration for the database |
| 64 | +func formatDatabaseConfig(name, cloudAccountName, password string, extraConfig string) string { |
| 65 | + return proSubscriptionQPFBoilerplate(name, cloudAccountName) + fmt.Sprintf(` |
| 66 | +resource "rediscloud_subscription_database" "example" { |
| 67 | + subscription_id = rediscloud_subscription.example.id |
| 68 | + name = "example" |
| 69 | + protocol = "redis" |
| 70 | + dataset_size_in_gb = 3 |
| 71 | + data_persistence = "none" |
| 72 | + data_eviction = "allkeys-random" |
| 73 | + throughput_measurement_by = "operations-per-second" |
| 74 | + throughput_measurement_value = 1000 |
| 75 | + password = "%s" |
| 76 | + support_oss_cluster_api = false |
| 77 | + external_endpoint_for_oss_cluster_api = false |
| 78 | + replication = false |
| 79 | + average_item_size_in_bytes = 0 |
| 80 | + client_ssl_certificate = "" |
| 81 | + periodic_backup_path = "" |
| 82 | + enable_default_user = true |
| 83 | + query_performance_factor = "2x" |
| 84 | +
|
| 85 | + alert { |
| 86 | + name = "dataset-size" |
| 87 | + value = 40 |
| 88 | + } |
| 89 | +
|
| 90 | + tags = { |
| 91 | + "market" = "emea" |
| 92 | + "material" = "cardboard" |
| 93 | + } |
| 94 | +
|
| 95 | + %s |
| 96 | +}`, password, extraConfig) |
| 97 | +} |
| 98 | + |
| 99 | +// Generic test helper for error cases |
| 100 | +func testErrorCase(t *testing.T, config string, expectedError *regexp.Regexp) { |
| 101 | + resource.ParallelTest(t, resource.TestCase{ |
| 102 | + PreCheck: func() { testAccPreCheck(t); testAccAwsPreExistingCloudAccountPreCheck(t) }, |
| 103 | + ProviderFactories: providerFactories, |
| 104 | + CheckDestroy: testAccCheckProSubscriptionDestroy, |
| 105 | + Steps: []resource.TestStep{ |
| 106 | + { |
| 107 | + Config: config, |
| 108 | + ExpectError: expectedError, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +func TestAccResourceRedisCloudProDatabase_qpf(t *testing.T) { |
| 115 | + name := acctest.RandomWithPrefix(testResourcePrefix) |
| 116 | + password := acctest.RandString(20) |
| 117 | + testCloudAccountName := os.Getenv("AWS_TEST_CLOUD_ACCOUNT_NAME") |
| 118 | + |
| 119 | + var subId int |
| 120 | + |
| 121 | + resource.ParallelTest(t, resource.TestCase{ |
| 122 | + PreCheck: func() { testAccPreCheck(t); testAccAwsPreExistingCloudAccountPreCheck(t) }, |
| 123 | + ProviderFactories: providerFactories, |
| 124 | + CheckDestroy: testAccCheckProSubscriptionDestroy, |
| 125 | + Steps: []resource.TestStep{ |
| 126 | + { |
| 127 | + Config: formatDatabaseConfig(name, testCloudAccountName, password, `modules = [{ name = "RediSearch" }]`), |
| 128 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 129 | + resource.TestCheckResourceAttr("rediscloud_subscription_database.example", "name", "example"), |
| 130 | + resource.TestCheckResourceAttr("rediscloud_subscription_database.example", "protocol", "redis"), |
| 131 | + resource.TestCheckResourceAttr("rediscloud_subscription_database.example", "dataset_size_in_gb", "3"), |
| 132 | + resource.TestCheckResourceAttr("rediscloud_subscription_database.example", "query_performance_factor", "2x"), |
| 133 | + resource.TestCheckResourceAttr("rediscloud_subscription_database.example", "tags.market", "emea"), |
| 134 | + resource.TestCheckResourceAttr("rediscloud_subscription_database.example", "tags.material", "cardboard"), |
| 135 | + |
| 136 | + func(s *terraform.State) error { |
| 137 | + r := s.RootModule().Resources["rediscloud_subscription.example"] |
| 138 | + |
| 139 | + var err error |
| 140 | + subId, err = strconv.Atoi(r.Primary.ID) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("couldn't parse the subscription ID: %s", redis.StringValue(&r.Primary.ID)) |
| 143 | + } |
| 144 | + |
| 145 | + client := testProvider.Meta().(*apiClient) |
| 146 | + sub, err := client.client.Subscription.Get(context.TODO(), subId) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + if redis.StringValue(sub.Name) != name { |
| 152 | + return fmt.Errorf("unexpected name value: %s", redis.StringValue(sub.Name)) |
| 153 | + } |
| 154 | + |
| 155 | + listDb := client.client.Database.List(context.TODO(), subId) |
| 156 | + if !listDb.Next() { |
| 157 | + return fmt.Errorf("no database found: %s", listDb.Err()) |
| 158 | + } |
| 159 | + if listDb.Err() != nil { |
| 160 | + return listDb.Err() |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
| 164 | + }, |
| 165 | + ), |
| 166 | + }, |
| 167 | + }, |
| 168 | + }) |
| 169 | +} |
| 170 | + |
| 171 | +func TestAccResourceRedisCloudProDatabase_missingModule(t *testing.T) { |
| 172 | + name := acctest.RandomWithPrefix(testResourcePrefix) |
| 173 | + password := acctest.RandString(20) |
| 174 | + testCloudAccountName := os.Getenv("AWS_TEST_CLOUD_ACCOUNT_NAME") |
| 175 | + |
| 176 | + config := formatDatabaseConfig(name, testCloudAccountName, password, "") |
| 177 | + |
| 178 | + testErrorCase(t, config, regexp.MustCompile("query_performance_factor\" requires the \"modules\" key to be explicitly defined in HCL")) |
| 179 | +} |
| 180 | + |
| 181 | +func TestAccResourceRedisCloudProDatabase_missingRediSearchModule(t *testing.T) { |
| 182 | + name := acctest.RandomWithPrefix(testResourcePrefix) |
| 183 | + password := acctest.RandString(20) |
| 184 | + testCloudAccountName := os.Getenv("AWS_TEST_CLOUD_ACCOUNT_NAME") |
| 185 | + |
| 186 | + config := formatDatabaseConfig(name, testCloudAccountName, password, `modules = [{ name = "RediBloom" }]`) |
| 187 | + |
| 188 | + testErrorCase(t, config, regexp.MustCompile("query_performance_factor\" requires the \"modules\" list to contain \"RediSearch")) |
| 189 | +} |
0 commit comments