Skip to content

Commit

Permalink
Fixes to IoTHub diff algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksymilian Boguń committed Mar 6, 2019
1 parent c76a793 commit a4b3f25
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions azurerm/resource_arm_iothub.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ 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 {
return d.Get("type").(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 +175,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)
},
},
"name": {
Expand All @@ -172,25 +191,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

0 comments on commit a4b3f25

Please sign in to comment.