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 Feb 22, 2019
1 parent c76a793 commit 98bd1df
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions azurerm/resource_arm_iothub.go
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,39 +175,45 @@ 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
}
secretKeyRegex := regexp.MustCompile("(SharedAccessKey|AccountKey)=[^;]+")
sbProtocolRegex := regexp.MustCompile("sb://([^:]+)(:5671)?/;")

return false
betterNew := sbProtocolRegex.ReplaceAllString(new, "sb://$1:5671/;")
betterNew = secretKeyRegex.ReplaceAllString(betterNew, "$1=****")
return old == betterNew
},
Sensitive: true,
},
"name": {
Type: schema.TypeString,
Required: true,
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 Expand Up @@ -745,6 +768,8 @@ func flattenIoTHubEndpoint(input *devices.RoutingProperties) []interface{} {
output["name"] = *name
}

output["type"] = "AzureIotHub.ServiceBusQueue"

results = append(results, output)
}
}
Expand All @@ -760,6 +785,8 @@ func flattenIoTHubEndpoint(input *devices.RoutingProperties) []interface{} {
output["name"] = *name
}

output["type"] = "AzureIotHub.ServiceBusTopic"

results = append(results, output)
}
}
Expand All @@ -775,6 +802,8 @@ func flattenIoTHubEndpoint(input *devices.RoutingProperties) []interface{} {
output["name"] = *name
}

output["type"] = "AzureIotHub.EventHub"

results = append(results, output)
}
}
Expand Down

0 comments on commit 98bd1df

Please sign in to comment.