Skip to content

Commit

Permalink
Fixes #11691
Browse files Browse the repository at this point in the history
* Add redis enterprise database datasource

* Update documentation

* Add datasource test case
  • Loading branch information
WodansSon authored May 17, 2021
1 parent fc8e4ef commit f5faf44
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package redisenterprise

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/redisenterprise/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/redisenterprise/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
)

func dataSourceRedisEnterpriseDatabase() *schema.Resource {
return &schema.Resource{
Read: dataSourceRedisEnterpriseDatabaseRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"cluster_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.RedisEnterpriseClusterID,
},

"primary_access_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_access_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func dataSourceRedisEnterpriseDatabaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).RedisEnterprise.DatabaseClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

clusterId, err := parse.RedisEnterpriseClusterID(d.Get("cluster_id").(string))
if err != nil {
return err
}

id := parse.NewRedisEnterpriseDatabaseID(subscriptionId, d.Get("resource_group_name").(string), clusterId.RedisEnterpriseName, d.Get("name").(string))

keysResp, err := client.ListKeys(ctx, id.ResourceGroup, id.RedisEnterpriseName, id.DatabaseName)
if err != nil {
return fmt.Errorf("listing keys for Redis Enterprise Database %q (Resource Group %q / Cluster Name %q): %+v", id.DatabaseName, id.ResourceGroup, id.RedisEnterpriseName, err)
}

d.SetId(id.ID())
d.Set("name", id.DatabaseName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("cluster_id", clusterId.ID())
d.Set("primary_access_key", keysResp.PrimaryKey)
d.Set("secondary_access_key", keysResp.SecondaryKey)

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package redisenterprise_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check"
)

type RedisEnterpriseDatabaseDataSource struct {
}

func TestAccRedisEnterpriseDatabaseDataSource_standard(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_redis_enterprise_database", "test")
r := RedisEnterpriseDatabaseDataSource{}

resourceGroupName := fmt.Sprintf("acctestRG-redisEnterprise-%d", data.RandomInteger)

data.DataSourceTest(t, []resource.TestStep{
{
Config: r.dataSource(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").HasValue("default"),
check.That(data.ResourceName).Key("resource_group_name").HasValue(resourceGroupName),
check.That(data.ResourceName).Key("cluster_id").Exists(),
check.That(data.ResourceName).Key("primary_access_key").Exists(),
check.That(data.ResourceName).Key("secondary_access_key").Exists(),
),
},
})
}

func (r RedisEnterpriseDatabaseDataSource) dataSource(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_redis_enterprise_database" "test" {
depends_on = [azurerm_redis_enterprise_database.test]
name = "default"
resource_group_name = azurerm_resource_group.test.name
cluster_id = azurerm_redis_enterprise_cluster.test.id
}
`, RedisenterpriseDatabaseResource{}.basic(data))
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ func resourceRedisEnterpriseDatabase() *schema.Resource {
Default: 10000,
ValidateFunc: validation.IntBetween(0, 65353),
},

"primary_access_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_access_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
Expand Down Expand Up @@ -259,6 +271,11 @@ func resourceRedisEnterpriseDatabaseRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("retrieving Redis Enterprise Database %q (Resource Group %q / Cluster Name %q): %+v", id.DatabaseName, id.ResourceGroup, id.RedisEnterpriseName, err)
}

keysResp, err := client.ListKeys(ctx, id.ResourceGroup, id.RedisEnterpriseName, id.DatabaseName)
if err != nil {
return fmt.Errorf("listing keys for Redis Enterprise Database %q (Resource Group %q / Cluster Name %q): %+v", id.DatabaseName, id.ResourceGroup, id.RedisEnterpriseName, err)
}

d.Set("name", id.DatabaseName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("cluster_id", parse.NewRedisEnterpriseClusterID(id.SubscriptionId, id.ResourceGroup, id.RedisEnterpriseName).ID())
Expand All @@ -276,6 +293,9 @@ func resourceRedisEnterpriseDatabaseRead(d *schema.ResourceData, meta interface{
d.Set("port", props.Port)
}

d.Set("primary_access_key", keysResp.PrimaryKey)
d.Set("secondary_access_key", keysResp.SecondaryKey)

return nil
}

Expand Down Expand Up @@ -342,6 +362,14 @@ func flattenArmDatabaseModuleArray(input *[]redisenterprise.Module) []interface{
args := ""
if item.Args != nil {
args = *item.Args
// new behavior if you do not pass args the RP sets the args to "PARTITIONS AUTO" by default
// (for RediSearch) which causes the the database to be force new on every plan after creation
// feels like an RP bug, but I added this workaround...
// NOTE: You also cannot set the args to PARTITIONS AUTO by default else you will get an error on create:
// Code="InvalidRequestBody" Message="The value of the parameter 'properties.modules' is invalid."
if strings.EqualFold(args, "PARTITIONS AUTO") {
args = ""
}
}

var version string
Expand Down
4 changes: 3 additions & 1 deletion azurerm/internal/services/redisenterprise/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func (r Registration) WebsiteCategories() []string {

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return nil
return map[string]*schema.Resource{
"azurerm_redis_enterprise_database": dataSourceRedisEnterpriseDatabase(),
}
}

// SupportedResources returns the supported Resources supported by this Service
Expand Down
60 changes: 60 additions & 0 deletions website/docs/d/redis_enterprise_database.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
subcategory: "Redis Enterprise"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_redis_enterprise_database"
description: |-
Gets information about an existing Redis Enterprise Database.
---

# Data Source: azurerm_redis_enterprise_database

Use this data source to access information about an existing Redis Enterprise Database

## Example Usage

```hcl
data "azurerm_redis_enterprise_database" "example" {
name = "default"
resource_group_name = azurerm_resource_group.example.name
cluster_id = azurerm_redis_enterprise_cluster.example.id
}
output "redis_enterprise_database_primary_key" {
value = data.azurerm_redis_enterprise_database.example.primary_access_key
description = "The Redis Enterprise DB primary key."
}
output "redis_enterprise_database_secondary_key" {
value = data.azurerm_redis_enterprise_database.example.secondary_access_key
description = "The Redis Enterprise DB secondary key."
}
```

## Argument Reference

* `name` - The name of the Redis Enterprise Database.

* `resource_group_name` - The name of the resource group the Redis Enterprise Database instance is located in.

* `cluster_id` - The resource ID of Redis Enterprise Cluster which hosts the Redis Enterprise Database instance.

## Attribute Reference

* `id` - The Redis Enterprise Database ID.

* `name` - The Redis Enterprise Database name.

* `cluster_id` - The Redis Enterprise Cluster ID that is hosting the Redis Enterprise Database.

* `primary_access_key` - The Primary Access Key for the Redis Enterprise Database instance.

* `secondary_access_key` - The Secondary Access Key for the Redis Enterprise Database instance.

---

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Redis Enterprise Database.
4 changes: 4 additions & 0 deletions website/docs/r/redis_enterprise_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ In addition to the Arguments listed above - the following Attributes are exporte

* `id` - The ID of the Redis Enterprise Database.

* `primary_access_key` - The Primary Access Key for the Redis Enterprise Database Instance.

* `secondary_access_key` - The Secondary Access Key for the Redis Enterprise Database Instance.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:
Expand Down

0 comments on commit f5faf44

Please sign in to comment.