Skip to content

Commit

Permalink
Fixes to IoTHub diff algorithm (#2951)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbog authored and katbyte committed Mar 21, 2019
1 parent 36e39b3 commit 8f47717
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 25 deletions.
64 changes: 45 additions & 19 deletions azurerm/resource_arm_iothub.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func suppressIfTypeIsNot(t string) schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
path := strings.Split(k, ".")
path[len(path)-1] = "type"
return d.Get(strings.Join(path, ".")).(string) != t
}
}

func supressWhenAll(fs ...schema.SchemaDiffSuppressFunc) schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
for _, f := range fs {
if !f(k, old, new, d) {
return false
}
}
return true
}
}

func resourceArmIotHub() *schema.Resource {
return &schema.Resource{
Create: resourceArmIotHubCreateUpdate,
Expand Down Expand Up @@ -158,12 +177,14 @@ func resourceArmIotHub() *schema.Resource {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// As Azure API masks the connection string key suppress diff for this property
if old != "" && strings.HasSuffix(old, "****") {
return true
}

return false
secretKeyRegex := regexp.MustCompile("(SharedAccessKey|AccountKey)=[^;]+")
sbProtocolRegex := regexp.MustCompile("sb://([^:]+)(:5671)?/;")

// Azure will always mask the Access Keys and will include the port number in the GET response
// 5671 is the default port for Azure Service Bus connections
maskedNew := sbProtocolRegex.ReplaceAllString(new, "sb://$1:5671/;")
maskedNew = secretKeyRegex.ReplaceAllString(maskedNew, "$1=****")
return (new == d.Get(k).(string)) && (maskedNew == old)
},
Sensitive: true,
},
Expand All @@ -173,25 +194,30 @@ func resourceArmIotHub() *schema.Resource {
ValidateFunc: validateIoTHubEndpointName,
},
"batch_frequency_in_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
ValidateFunc: validation.IntBetween(60, 720),
Type: schema.TypeInt,
Optional: true,
Default: 300,
DiffSuppressFunc: suppressIfTypeIsNot("AzureIotHub.StorageContainer"),
ValidateFunc: validation.IntBetween(60, 720),
},
"max_chunk_size_in_bytes": {
Type: schema.TypeInt,
Optional: true,
Default: 314572800,
ValidateFunc: validation.IntBetween(10485760, 524288000),
Type: schema.TypeInt,
Optional: true,
Default: 314572800,
DiffSuppressFunc: suppressIfTypeIsNot("AzureIotHub.StorageContainer"),
ValidateFunc: validation.IntBetween(10485760, 524288000),
},
"container_name": {
Type: schema.TypeString,
Optional: true,
},
"encoding": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppress.CaseDifference,
DiffSuppressFunc: suppressIfTypeIsNot("AzureIotHub.StorageContainer"),
},
"encoding": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: supressWhenAll(
suppressIfTypeIsNot("AzureIotHub.StorageContainer"),
suppress.CaseDifference),
ValidateFunc: validation.StringInSlice([]string{
string(devices.Avro),
string(devices.AvroDeflate),
Expand Down
53 changes: 47 additions & 6 deletions azurerm/resource_arm_iothub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func TestAccAzureRMIotHub_customRoutes(t *testing.T) {
Config: testAccAzureRMIotHub_customRoutes(rInt, rStr, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMIotHubExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "endpoint.#", "2"),
resource.TestCheckResourceAttr(resourceName, "endpoint.0.type", "AzureIotHub.StorageContainer"),
resource.TestCheckResourceAttr(resourceName, "endpoint.1.type", "AzureIotHub.EventHub"),
resource.TestCheckResourceAttr(resourceName, "route.#", "2"),
),
},
{
Expand Down Expand Up @@ -212,7 +216,7 @@ resource "azurerm_iothub" "test" {
capacity = "1"
}
tags = {
tags {
"purpose" = "testing"
}
}
Expand All @@ -235,7 +239,7 @@ resource "azurerm_iothub" "import" {
capacity = "1"
}
tags = {
tags {
"purpose" = "testing"
}
}
Expand All @@ -260,7 +264,7 @@ resource "azurerm_iothub" "test" {
capacity = "1"
}
tags = {
tags {
"purpose" = "testing"
}
}
Expand Down Expand Up @@ -289,6 +293,29 @@ resource "azurerm_storage_container" "test" {
container_access_type = "private"
}
resource "azurerm_eventhub_namespace" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
name = "acctest-%d"
sku = "Basic"
}
resource "azurerm_eventhub" "test" {
name = "acctest"
resource_group_name = "${azurerm_resource_group.test.name}"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
partition_count = 2
message_retention = 1
}
resource "azurerm_eventhub_authorization_rule" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
eventhub_name = "${azurerm_eventhub.test.name}"
name = "acctest"
send = true
}
resource "azurerm_iothub" "test" {
name = "acctestIoTHub-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
Expand All @@ -311,6 +338,12 @@ resource "azurerm_iothub" "test" {
file_name_format = "{iothub}/{partition}_{YYYY}_{MM}_{DD}_{HH}_{mm}"
}
endpoint {
type = "AzureIotHub.EventHub"
connection_string = "${azurerm_eventhub_authorization_rule.test.primary_connection_string}"
name = "export2"
}
route {
name = "export"
source = "DeviceMessages"
Expand All @@ -319,11 +352,19 @@ resource "azurerm_iothub" "test" {
enabled = true
}
tags = {
route {
name = "export2"
source = "DeviceMessages"
condition = "true"
endpoint_names = ["export2"]
enabled = true
}
tags {
"purpose" = "testing"
}
}
`, rInt, location, rStr, rInt)
`, rInt, location, rStr, rInt, rInt)
}

func testAccAzureRMIotHub_fallbackRoute(rInt int, location string) string {
Expand All @@ -350,7 +391,7 @@ resource "azurerm_iothub" "test" {
enabled = true
}
tags = {
tags {
"purpose" = "testing"
}
}
Expand Down

0 comments on commit 8f47717

Please sign in to comment.