Skip to content

Commit

Permalink
Redis Cache: support for clusters on the internal network (#1086)
Browse files Browse the repository at this point in the history
* Support for Redis Caches being hosted on an Internal Network

* Vendoring version `2018-03-01` of the Redis SDK

* SDK changes for the 2018 version

* Code changes required for the 2018 API

* Removing the update use-case since it's no longer valid

* Making the network fields ForceNew in the docs
  • Loading branch information
tombuildsstuff authored Apr 11, 2018
1 parent 9f20039 commit a60ad1a
Show file tree
Hide file tree
Showing 16 changed files with 1,789 additions and 751 deletions.
6 changes: 3 additions & 3 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/operationalinsights/mgmt/2015-11-01-preview/operationalinsights"
"github.com/Azure/azure-sdk-for-go/services/operationsmanagement/mgmt/2015-11-01-preview/operationsmanagement"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-04-30-preview/postgresql"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2016-04-01/redis"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2018-03-01/redis"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-12-01/policy"
Expand Down Expand Up @@ -89,7 +89,7 @@ type ArmClient struct {
solutionsClient operationsmanagement.SolutionsClient

redisClient redis.Client
redisFirewallClient redis.FirewallRuleClient
redisFirewallClient redis.FirewallRulesClient
redisPatchSchedulesClient redis.PatchSchedulesClient

// Application Insights
Expand Down Expand Up @@ -791,7 +791,7 @@ func (c *ArmClient) registerRedisClients(endpoint, subscriptionId string, auth a
c.configureClient(&redisClient.Client, auth)
c.redisClient = redisClient

firewallRuleClient := redis.NewFirewallRuleClientWithBaseURI(endpoint, subscriptionId)
firewallRuleClient := redis.NewFirewallRulesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&firewallRuleClient.Client, auth)
c.redisFirewallClient = firewallRuleClient

Expand Down
53 changes: 38 additions & 15 deletions azurerm/resource_arm_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2016-04-01/redis"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2018-03-01/redis"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -74,6 +74,19 @@ func resourceArmRedisCache() *schema.Resource {
Optional: true,
},

"subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"private_static_ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"redis_configuration": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -211,12 +224,11 @@ func resourceArmRedisCacheCreate(d *schema.ResourceData, meta interface{}) error
}

parameters := redis.CreateParameters{
Name: &name,
Location: &location,
Location: utils.String(location),
CreateProperties: &redis.CreateProperties{
EnableNonSslPort: &enableNonSSLPort,
EnableNonSslPort: utils.Bool(enableNonSSLPort),
Sku: &redis.Sku{
Capacity: &capacity,
Capacity: utils.Int32(capacity),
Family: family,
Name: sku,
},
Expand All @@ -230,6 +242,14 @@ func resourceArmRedisCacheCreate(d *schema.ResourceData, meta interface{}) error
parameters.ShardCount = &shardCount
}

if v, ok := d.GetOk("private_static_ip_address"); ok {
parameters.StaticIP = utils.String(v.(string))
}

if v, ok := d.GetOk("subnet_id"); ok {
parameters.SubnetID = utils.String(v.(string))
}

future, err := client.Create(ctx, resGroup, name, parameters)
if err != nil {
return err
Expand Down Expand Up @@ -292,9 +312,9 @@ func resourceArmRedisCacheUpdate(d *schema.ResourceData, meta interface{}) error

parameters := redis.UpdateParameters{
UpdateProperties: &redis.UpdateProperties{
EnableNonSslPort: &enableNonSSLPort,
EnableNonSslPort: utils.Bool(enableNonSSLPort),
Sku: &redis.Sku{
Capacity: &capacity,
Capacity: utils.Int32(capacity),
Family: family,
Name: sku,
},
Expand Down Expand Up @@ -406,19 +426,22 @@ func resourceArmRedisCacheRead(d *schema.ResourceData, meta interface{}) error {
d.Set("location", azureRMNormalizeLocation(*location))
}

d.Set("ssl_port", resp.SslPort)
d.Set("hostname", resp.HostName)
d.Set("port", resp.Port)
d.Set("enable_non_ssl_port", resp.EnableNonSslPort)

if sku := resp.Sku; sku != nil {
d.Set("capacity", sku.Capacity)
d.Set("family", sku.Family)
d.Set("sku_name", sku.Name)
}

if resp.ShardCount != nil {
d.Set("shard_count", resp.ShardCount)
if props := resp.Properties; props != nil {
d.Set("ssl_port", props.SslPort)
d.Set("hostname", props.HostName)
d.Set("port", props.Port)
d.Set("enable_non_ssl_port", props.EnableNonSslPort)
if props.ShardCount != nil {
d.Set("shard_count", props.ShardCount)
}
d.Set("private_static_ip_address", props.StaticIP)
d.Set("subnet_id", props.SubnetID)
}

redisConfiguration := flattenRedisConfiguration(resp.RedisConfiguration)
Expand Down Expand Up @@ -470,7 +493,7 @@ func redisStateRefreshFunc(ctx context.Context, client redis.Client, resourceGro
return nil, "", fmt.Errorf("Error issuing read request in redisStateRefreshFunc to Azure ARM for Redis Cache Instance '%s' (RG: '%s'): %s", sgName, resourceGroupName, err)
}

return res, *res.ProvisioningState, nil
return res, string(res.ProvisioningState), nil
}
}

Expand Down
112 changes: 112 additions & 0 deletions azurerm/resource_arm_redis_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,44 @@ func TestAccAzureRMRedisCache_PatchScheduleUpdated(t *testing.T) {
})
}

func TestAccAzureRMRedisCache_InternalSubnet(t *testing.T) {
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,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRedisCacheExists("azurerm_redis_cache.test"),
),
},
},
})
}

func TestAccAzureRMRedisCache_InternalSubnetStaticIP(t *testing.T) {
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,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRedisCacheExists("azurerm_redis_cache.test"),
),
},
},
})
}

func testCheckAzureRMRedisCacheExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
Expand Down Expand Up @@ -635,3 +673,77 @@ resource "azurerm_redis_cache" "test" {
}
`, rInt, location, rString, rInt)
}
func testAccAzureRMRedisCache_internalSubnet(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctestnw-%d"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "testsubnet"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.1.0/24"
}
resource "azurerm_redis_cache" "test" {
name = "acctestRedis-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
capacity = 1
family = "P"
sku_name = "Premium"
enable_non_ssl_port = false
subnet_id = "${azurerm_subnet.test.id}"
redis_configuration {
maxclients = "256"
}
}
`, ri, location, ri, ri)
}

func testAccAzureRMRedisCache_internalSubnetStaticIP(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctestnw-%d"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "testsubnet"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.1.0/24"
}
resource "azurerm_redis_cache" "test" {
name = "acctestRedis-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
capacity = 1
family = "P"
sku_name = "Premium"
enable_non_ssl_port = false
subnet_id = "${azurerm_subnet.test.id}"
private_static_ip_address = "10.0.1.20"
redis_configuration {
maxclients = "256"
}
}
`, ri, location, ri, ri)
}
5 changes: 2 additions & 3 deletions azurerm/resource_arm_redis_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"regexp"

"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2016-04-01/redis"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2018-03-01/redis"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -58,8 +58,7 @@ func resourceArmRedisFirewallRuleCreateUpdate(d *schema.ResourceData, meta inter
startIP := d.Get("start_ip").(string)
endIP := d.Get("end_ip").(string)

parameters := redis.FirewallRule{
Name: &name,
parameters := redis.FirewallRuleCreateParameters{
FirewallRuleProperties: &redis.FirewallRuleProperties{
StartIP: utils.String(startIP),
EndIP: utils.String(endIP),
Expand Down
Loading

0 comments on commit a60ad1a

Please sign in to comment.