-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add redis enterprise database datasource * Update documentation * Add datasource test case
- Loading branch information
Showing
6 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
azurerm/internal/services/redisenterprise/redis_enterprise_database_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
47 changes: 47 additions & 0 deletions
47
azurerm/internal/services/redisenterprise/redis_enterprise_database_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters