Skip to content

Commit

Permalink
Added Import support to Redis Cache/Firewall Rule (#1211)
Browse files Browse the repository at this point in the history
* `azurerm_redis_cache` - support for importing

* `azurerm_firewall_rule` - support for import

* Setting the redis_configuration using the correct types

* maxclients becomes computed

* Documenting a known bug in the API

* Documenting the behaviour of the storage_connection_string field

* Working around broken API's
  • Loading branch information
tombuildsstuff authored May 13, 2018
1 parent 150e426 commit 95a820c
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 31 deletions.
219 changes: 219 additions & 0 deletions azurerm/import_arm_redis_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMRedisCache_importBasic(t *testing.T) {
resourceName := "azurerm_redis_cache.test"

ri := acctest.RandInt()
config := testAccAzureRMRedisCache_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRedisCache_importStandard(t *testing.T) {
resourceName := "azurerm_redis_cache.test"

ri := acctest.RandInt()
config := testAccAzureRMRedisCache_standard(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRedisCache_importPremium(t *testing.T) {
resourceName := "azurerm_redis_cache.test"

ri := acctest.RandInt()
config := testAccAzureRMRedisCache_premium(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRedisCache_importPremiumSharded(t *testing.T) {
resourceName := "azurerm_redis_cache.test"

ri := acctest.RandInt()
config := testAccAzureRMRedisCache_premiumSharded(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRedisCache_importNonStandardCasing(t *testing.T) {
resourceName := "azurerm_redis_cache.test"

ri := acctest.RandInt()
config := testAccAzureRMRedisCacheNonStandardCasing(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRedisCache_importBackupEnabled(t *testing.T) {
resourceName := "azurerm_redis_cache.test"

ri := acctest.RandInt()
rs := acctest.RandString(4)
config := testAccAzureRMRedisCacheBackupEnabled(ri, rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
// `redis_configuration.0.rdb_storage_connection_string` is returned as:
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf"
// TODO: remove this once the Bug's been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/3037
ExpectNonEmptyPlan: true,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"redis_configuration.0.rdb_storage_connection_string"},
},
},
})
}

func TestAccAzureRMRedisCache_importPatchSchedule(t *testing.T) {
resourceName := "azurerm_redis_cache.test"
ri := acctest.RandInt()
location := testLocation()
config := testAccAzureRMRedisCachePatchSchedule(ri, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccAzureRMRedisCache_importInternalSubnet(t *testing.T) {
resourceName := "azurerm_redis_cache.test"
ri := acctest.RandInt()
config := testAccAzureRMRedisCache_internalSubnet(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRedisCache_importInternalSubnetStaticIP(t *testing.T) {
resourceName := "azurerm_redis_cache.test"
ri := acctest.RandInt()
config := testAccAzureRMRedisCache_internalSubnetStaticIP(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisCacheDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
31 changes: 31 additions & 0 deletions azurerm/import_arm_redis_firewall_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMRedisFirewallRule_importBasic(t *testing.T) {
resourceName := "azurerm_redis_firewall_rule.test"

ri := acctest.RandInt()
config := testAccAzureRMRedisFirewallRule_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRedisFirewallRuleDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
80 changes: 65 additions & 15 deletions azurerm/resource_arm_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func resourceArmRedisCache() *schema.Resource {
Read: resourceArmRedisCacheRead,
Update: resourceArmRedisCacheUpdate,
Delete: resourceArmRedisCacheDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -95,7 +98,6 @@ func resourceArmRedisCache() *schema.Resource {
Schema: map[string]*schema.Schema{
"maxclients": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},

Expand Down Expand Up @@ -444,8 +446,13 @@ func resourceArmRedisCacheRead(d *schema.ResourceData, meta interface{}) error {
d.Set("subnet_id", props.SubnetID)
}

redisConfiguration := flattenRedisConfiguration(resp.RedisConfiguration)
d.Set("redis_configuration", &redisConfiguration)
redisConfiguration, err := flattenRedisConfiguration(resp.RedisConfiguration)
if err != nil {
return fmt.Errorf("Error flattening `redis_configuration`: %+v", err)
}
if err := d.Set("redis_configuration", redisConfiguration); err != nil {
return fmt.Errorf("Error setting `redis_configuration`: %+v", err)
}

d.Set("primary_access_key", keysResp.PrimaryKey)
d.Set("secondary_access_key", keysResp.SecondaryKey)
Expand Down Expand Up @@ -574,21 +581,64 @@ func expandRedisPatchSchedule(d *schema.ResourceData) (*redis.PatchSchedule, err
return &schedule, nil
}

func flattenRedisConfiguration(input map[string]*string) map[string]*string {
redisConfiguration := make(map[string]*string, len(input))
func flattenRedisConfiguration(input map[string]*string) ([]interface{}, error) {
outputs := make(map[string]interface{}, len(input))

redisConfiguration["maxclients"] = input["maxclients"]
redisConfiguration["maxmemory_delta"] = input["maxmemory-delta"]
redisConfiguration["maxmemory_reserved"] = input["maxmemory-reserved"]
redisConfiguration["maxmemory_policy"] = input["maxmemory-policy"]
if v := input["maxclients"]; v != nil {
i, err := strconv.Atoi(*v)
if err != nil {
return nil, fmt.Errorf("Error parsing `maxclients` %q: %+v", v, err)
}
outputs["maxclients"] = i
}
if v := input["maxmemory-delta"]; v != nil {
i, err := strconv.Atoi(*v)
if err != nil {
return nil, fmt.Errorf("Error parsing `maxmemory-delta` %q: %+v", v, err)
}
outputs["maxmemory_delta"] = i
}
if v := input["maxmemory-reserved"]; v != nil {
i, err := strconv.Atoi(*v)
if err != nil {
return nil, fmt.Errorf("Error parsing `maxmemory-reserved` %q: %+v", v, err)
}
outputs["maxmemory_reserved"] = i
}
if v := input["maxmemory-policy"]; v != nil {
outputs["maxmemory_policy"] = *v
}

redisConfiguration["rdb_backup_enabled"] = input["rdb-backup-enabled"]
redisConfiguration["rdb_backup_frequency"] = input["rdb-backup-frequency"]
redisConfiguration["rdb_backup_max_snapshot_count"] = input["rdb-backup-max-snapshot-count"]
redisConfiguration["rdb_storage_connection_string"] = input["rdb-storage-connection-string"]
redisConfiguration["notify_keyspace_events"] = input["notify-keyspace-events"]
// delta, reserved, enabled, frequency,, count,
if v := input["rdb-backup-enabled"]; v != nil {
b, err := strconv.ParseBool(*v)
if err != nil {
return nil, fmt.Errorf("Error parsing `rdb-backup-enabled` %q: %+v", v, err)
}
outputs["rdb_backup_enabled"] = b
}
if v := input["rdb-backup-frequency"]; v != nil {
i, err := strconv.Atoi(*v)
if err != nil {
return nil, fmt.Errorf("Error parsing `rdb-backup-frequency` %q: %+v", v, err)
}
outputs["rdb_backup_frequency"] = i
}
if v := input["rdb-backup-max-snapshot-count"]; v != nil {
i, err := strconv.Atoi(*v)
if err != nil {
return nil, fmt.Errorf("Error parsing `rdb-backup-max-snapshot-count` %q: %+v", v, err)
}
outputs["rdb_backup_max_snapshot_count"] = i
}
if v := input["rdb-storage-connection-string"]; v != nil {
outputs["rdb_storage_connection_string"] = *v
}
if v := input["notify-keyspace-events"]; v != nil {
outputs["notify_keyspace_events"] = *v
}

return redisConfiguration
return []interface{}{outputs}, nil
}

func flattenRedisPatchSchedules(schedule redis.PatchSchedule) []interface{} {
Expand Down
Loading

0 comments on commit 95a820c

Please sign in to comment.