Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Import support to Redis Cache/Firewall Rule #1211

Merged
merged 7 commits into from
May 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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