Skip to content

Commit

Permalink
Adding nodeType field in the terraform (#10090) (#7174)
Browse files Browse the repository at this point in the history
[upstream:a9d6aed791af556fb2adbd28ea509a9fd838098e]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 2, 2024
1 parent 18530da commit 11163e6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .changelog/10090.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
redis: added `node_type` and `precise_size_gb` fields to `google_redis_cluster`
```
38 changes: 38 additions & 0 deletions google-beta/services/redis/resource_redis_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ projects/{network_project_id_or_number}/global/networks/{network_id}.`,
Description: `Optional. The authorization mode of the Redis cluster. If not provided, auth feature is disabled for the cluster. Default value: "AUTH_MODE_DISABLED" Possible values: ["AUTH_MODE_UNSPECIFIED", "AUTH_MODE_IAM_AUTH", "AUTH_MODE_DISABLED"]`,
Default: "AUTH_MODE_DISABLED",
},
"node_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"REDIS_SHARED_CORE_NANO", "REDIS_HIGHMEM_MEDIUM", "REDIS_HIGHMEM_XLARGE", "REDIS_STANDARD_SMALL", ""}),
Description: `The nodeType for the Redis cluster.
If not provided, REDIS_HIGHMEM_MEDIUM will be used as default Possible values: ["REDIS_SHARED_CORE_NANO", "REDIS_HIGHMEM_MEDIUM", "REDIS_HIGHMEM_XLARGE", "REDIS_STANDARD_SMALL"]`,
},
"region": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -161,6 +170,11 @@ projects/{network_project_id}/global/networks/{network_id}.`,
},
},
},
"precise_size_gb": {
Type: schema.TypeFloat,
Computed: true,
Description: `Output only. Redis memory precise size in GB for the entire cluster.`,
},
"psc_connections": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -270,6 +284,12 @@ func resourceRedisClusterCreate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("transit_encryption_mode"); !tpgresource.IsEmptyValue(reflect.ValueOf(transitEncryptionModeProp)) && (ok || !reflect.DeepEqual(v, transitEncryptionModeProp)) {
obj["transitEncryptionMode"] = transitEncryptionModeProp
}
nodeTypeProp, err := expandRedisClusterNodeType(d.Get("node_type"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("node_type"); !tpgresource.IsEmptyValue(reflect.ValueOf(nodeTypeProp)) && (ok || !reflect.DeepEqual(v, nodeTypeProp)) {
obj["nodeType"] = nodeTypeProp
}
pscConfigsProp, err := expandRedisClusterPscConfigs(d.Get("psc_configs"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -406,6 +426,9 @@ func resourceRedisClusterRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("transit_encryption_mode", flattenRedisClusterTransitEncryptionMode(res["transitEncryptionMode"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
if err := d.Set("node_type", flattenRedisClusterNodeType(res["nodeType"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
if err := d.Set("discovery_endpoints", flattenRedisClusterDiscoveryEndpoints(res["discoveryEndpoints"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
Expand All @@ -421,6 +444,9 @@ func resourceRedisClusterRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("size_gb", flattenRedisClusterSizeGb(res["sizeGb"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
if err := d.Set("precise_size_gb", flattenRedisClusterPreciseSizeGb(res["preciseSizeGb"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
if err := d.Set("shard_count", flattenRedisClusterShardCount(res["shardCount"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
Expand Down Expand Up @@ -618,6 +644,10 @@ func flattenRedisClusterTransitEncryptionMode(v interface{}, d *schema.ResourceD
return v
}

func flattenRedisClusterNodeType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenRedisClusterDiscoveryEndpoints(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -814,6 +844,10 @@ func flattenRedisClusterSizeGb(v interface{}, d *schema.ResourceData, config *tr
return v // let terraform core handle it otherwise
}

func flattenRedisClusterPreciseSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenRedisClusterShardCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
Expand All @@ -839,6 +873,10 @@ func expandRedisClusterTransitEncryptionMode(v interface{}, d tpgresource.Terraf
return v, nil
}

func expandRedisClusterNodeType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandRedisClusterPscConfigs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ resource "google_redis_cluster" "cluster-ha" {
}
region = "us-central1"
replica_count = 1
node_type = "REDIS_SHARED_CORE_NANO"
transit_encryption_mode = "TRANSIT_ENCRYPTION_MODE_DISABLED"
authorization_mode = "AUTH_MODE_DISABLED"
depends_on = [
Expand Down
49 changes: 39 additions & 10 deletions google-beta/services/redis/resource_redis_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ import (
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
)

func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
t.Parallel()

name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckRedisClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 3, true /*nodeType = */, "REDIS_STANDARD_SMALL"),
},
{
ResourceName: "google_redis_cluster.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"psc_configs"},
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 0 /* shardCount = */, 3, false /*nodeType = */, "REDIS_STANDARD_SMALL"),
},
},
})
}

// Validate that replica count is updated for the cluster
func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
t.Parallel()
Expand All @@ -23,7 +51,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 3, true),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 3, true /* nodeType = */, ""),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -33,7 +61,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// update replica count to 2
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 2 /* shardCount = */, 3, true),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 2 /* shardCount = */, 3, true /*nodeType = */, ""),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -43,11 +71,11 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 2 /* shardCount = */, 3, false),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 2 /* shardCount = */, 3, false /*nodeType = */, ""),
},
{
// update replica count to 0
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 0 /* shardCount = */, 3, true),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 0 /* shardCount = */, 3, true /*nodeType = */, ""),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -57,7 +85,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 0 /* shardCount = */, 3, false),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 0 /* shardCount = */, 3, false /*nodeType = */, ""),
},
},
})
Expand All @@ -76,7 +104,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with shard count 3
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 3, true),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 3, true /*nodeType = */, ""),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -86,7 +114,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
},
{
// update shard count to 5
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 5, true),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 5, true /*nodeType = */, ""),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -96,13 +124,13 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 5, false),
Config: createOrUpdateRedisCluster(name /* replicaCount = */, 1 /* shardCount = */, 5, false /* nodeType = */, ""),
},
},
})
}

func createOrUpdateRedisCluster(name string, replicaCount int, shardCount int, preventDestroy bool) string {
func createOrUpdateRedisCluster(name string, replicaCount int, shardCount int, preventDestroy bool, nodeType string) string {
lifecycleBlock := ""
if preventDestroy {
lifecycleBlock = `
Expand All @@ -116,6 +144,7 @@ resource "google_redis_cluster" "test" {
name = "%s"
replica_count = %d
shard_count = %d
node_type = "%s"
region = "us-central1"
psc_configs {
network = google_compute_network.producer_net.id
Expand Down Expand Up @@ -151,5 +180,5 @@ resource "google_compute_network" "producer_net" {
name = "%s"
auto_create_subnetworks = false
}
`, name, replicaCount, shardCount, lifecycleBlock, name, name, name)
`, name, replicaCount, shardCount, nodeType, lifecycleBlock, name, name, name)
}
10 changes: 10 additions & 0 deletions website/docs/r/redis_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ resource "google_redis_cluster" "cluster-ha" {
}
region = "us-central1"
replica_count = 1
node_type = "REDIS_SHARED_CORE_NANO"
transit_encryption_mode = "TRANSIT_ENCRYPTION_MODE_DISABLED"
authorization_mode = "AUTH_MODE_DISABLED"
depends_on = [
Expand Down Expand Up @@ -126,6 +127,12 @@ The following arguments are supported:
Default value is `TRANSIT_ENCRYPTION_MODE_DISABLED`.
Possible values are: `TRANSIT_ENCRYPTION_MODE_UNSPECIFIED`, `TRANSIT_ENCRYPTION_MODE_DISABLED`, `TRANSIT_ENCRYPTION_MODE_SERVER_AUTHENTICATION`.

* `node_type` -
(Optional)
The nodeType for the Redis cluster.
If not provided, REDIS_HIGHMEM_MEDIUM will be used as default
Possible values are: `REDIS_SHARED_CORE_NANO`, `REDIS_HIGHMEM_MEDIUM`, `REDIS_HIGHMEM_XLARGE`, `REDIS_STANDARD_SMALL`.

* `replica_count` -
(Optional)
Optional. The number of replica nodes per shard.
Expand Down Expand Up @@ -172,6 +179,9 @@ In addition to the arguments listed above, the following computed attributes are
* `size_gb` -
Output only. Redis memory size in GB for the entire cluster.

* `precise_size_gb` -
Output only. Redis memory precise size in GB for the entire cluster.


<a name="nested_discovery_endpoints"></a>The `discovery_endpoints` block contains:

Expand Down

0 comments on commit 11163e6

Please sign in to comment.