Skip to content

Commit

Permalink
Slight optimisation and reduce indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmorley committed Oct 12, 2021
1 parent ef89ef8 commit 010aa80
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions internal/services/eventgrid/event_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func eventSubscriptionSchemaDeliveryProperty() *pluginsdk.Schema {
"value": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true, // must mark as Sensitive, as it's possible for this to be 'Secret'
Sensitive: true,
},

"source_field": {
Expand Down Expand Up @@ -1029,36 +1029,36 @@ func expandDeliveryProperties(d *pluginsdk.ResourceData) []eventgrid.BasicDelive

var basicDeliveryAttributeMapping []eventgrid.BasicDeliveryAttributeMapping

if deliveryMappingsConfig, ok := d.GetOk("delivery_property"); ok {
input := deliveryMappingsConfig.([]interface{})
deliveryMappingsConfig, deliveryMappingsExists := d.GetOk("delivery_property")
if !deliveryMappingsExists {
return basicDeliveryAttributeMapping
}

if len(input) == 0 {
return nil
}
input := deliveryMappingsConfig.([]interface{})
if len(input) == 0 {
return basicDeliveryAttributeMapping
}

for _, r := range input {
mappingBlock := r.(map[string]interface{})
for _, r := range input {
mappingBlock := r.(map[string]interface{})

if mappingBlock["type"].(string) == "Static" {
basicDeliveryAttributeMapping = append(basicDeliveryAttributeMapping, eventgrid.StaticDeliveryAttributeMapping{
Name: utils.String(mappingBlock["header_name"].(string)),
Type: eventgrid.TypeStatic,
StaticDeliveryAttributeMappingProperties: &eventgrid.StaticDeliveryAttributeMappingProperties{
Value: utils.String(mappingBlock["value"].(string)),
IsSecret: utils.Bool(mappingBlock["secret"].(bool)),
},
})
}

if mappingBlock["type"].(string) == "Dynamic" {
basicDeliveryAttributeMapping = append(basicDeliveryAttributeMapping, eventgrid.DynamicDeliveryAttributeMapping{
Name: utils.String(mappingBlock["header_name"].(string)),
Type: eventgrid.TypeDynamic,
DynamicDeliveryAttributeMappingProperties: &eventgrid.DynamicDeliveryAttributeMappingProperties{
SourceField: utils.String(mappingBlock["source_field"].(string)),
},
})
}
if mappingBlock["type"].(string) == "Static" {
basicDeliveryAttributeMapping = append(basicDeliveryAttributeMapping, eventgrid.StaticDeliveryAttributeMapping{
Name: utils.String(mappingBlock["header_name"].(string)),
Type: eventgrid.TypeStatic,
StaticDeliveryAttributeMappingProperties: &eventgrid.StaticDeliveryAttributeMappingProperties{
Value: utils.String(mappingBlock["value"].(string)),
IsSecret: utils.Bool(mappingBlock["secret"].(bool)),
},
})
} else if mappingBlock["type"].(string) == "Dynamic" {
basicDeliveryAttributeMapping = append(basicDeliveryAttributeMapping, eventgrid.DynamicDeliveryAttributeMapping{
Name: utils.String(mappingBlock["header_name"].(string)),
Type: eventgrid.TypeDynamic,
DynamicDeliveryAttributeMappingProperties: &eventgrid.DynamicDeliveryAttributeMappingProperties{
SourceField: utils.String(mappingBlock["source_field"].(string)),
},
})
}
}

Expand Down Expand Up @@ -1277,8 +1277,8 @@ func flattenDeliveryProperties(d *pluginsdk.ResourceData, input *[]eventgrid.Bas
if input == nil {
return nil
}
deliveryProperties := make([]interface{}, len(*input))

deliveryProperties := make([]interface{}, len(*input))
for i, element := range *input {
attributeMapping := make(map[string]interface{})

Expand All @@ -1294,7 +1294,7 @@ func flattenDeliveryProperties(d *pluginsdk.ResourceData, input *[]eventgrid.Bas

if *staticMapping.IsSecret {
// If this is a secret, the Azure API just returns a value of 'Hidden',
// so we need to lookup the value that was provided from config to return (so that we don't generate Diffs)
// so we need to lookup the value that was provided from config to return
propertiesFromConfig := expandDeliveryProperties(d)
for _, v := range propertiesFromConfig {
if configMap, ok := v.AsStaticDeliveryAttributeMapping(); ok {
Expand All @@ -1309,9 +1309,7 @@ func flattenDeliveryProperties(d *pluginsdk.ResourceData, input *[]eventgrid.Bas
} else {
attributeMapping["value"] = staticMapping.Value
}
}

if dynamicMapping, ok := element.AsDynamicDeliveryAttributeMapping(); ok {
} else if dynamicMapping, ok := element.AsDynamicDeliveryAttributeMapping(); ok {
attributeMapping["type"] = dynamicMapping.Type
if dynamicMapping.Name != nil {
attributeMapping["header_name"] = dynamicMapping.Name
Expand Down

0 comments on commit 010aa80

Please sign in to comment.