Skip to content

Commit

Permalink
Review changes + additional test for delivery property updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmorley committed Oct 9, 2021
1 parent 9dc8e7e commit ef89ef8
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 15 deletions.
33 changes: 23 additions & 10 deletions internal/services/eventgrid/event_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -1274,25 +1274,34 @@ func flattenEventGridEventSubscriptionEventhubEndpoint(input *eventgrid.EventHub
}

func flattenDeliveryProperties(d *pluginsdk.ResourceData, input *[]eventgrid.BasicDeliveryAttributeMapping) []interface{} {

if input == nil {
return nil
}
deliveryProperties := make([]interface{}, len(*input))

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

if staticMapping, ok := element.AsStaticDeliveryAttributeMapping(); ok {

attributeMapping["type"] = staticMapping.Type
attributeMapping["header_name"] = staticMapping.Name
attributeMapping["secret"] = staticMapping.IsSecret
if staticMapping.Name != nil {
attributeMapping["header_name"] = staticMapping.Name
}
if staticMapping.IsSecret != nil {
attributeMapping["secret"] = staticMapping.IsSecret
}

if *staticMapping.IsSecret {
// If this is a secret, the value doesn't get returned by the Azure API which just returns a value of 'Secret',
// so we need to lookup the value that was provided from config to return, so that we don't generate Diffs
// 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)
propertiesFromConfig := expandDeliveryProperties(d)
for _, v := range propertiesFromConfig {
if cofigMap, ok := v.AsStaticDeliveryAttributeMapping(); ok {
if *cofigMap.Name == *staticMapping.Name {
attributeMapping["value"] = cofigMap.Value
if configMap, ok := v.AsStaticDeliveryAttributeMapping(); ok {
if *configMap.Name == *staticMapping.Name {
if configMap.Value != nil {
attributeMapping["value"] = configMap.Value
}
break
}
}
Expand All @@ -1304,8 +1313,12 @@ func flattenDeliveryProperties(d *pluginsdk.ResourceData, input *[]eventgrid.Bas

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

deliveryProperties[i] = attributeMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
}

if azureFunctionEndpoint.DeliveryAttributeMappings != nil {
d.Set("delivery_property", flattenDeliveryProperties(d, azureFunctionEndpoint.DeliveryAttributeMappings))
if err := d.Set("delivery_property", flattenDeliveryProperties(d, azureFunctionEndpoint.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "azure_function_endpoint", id.Name, id.Scope, err)
}
}
}
if v, ok := destination.AsEventHubEventSubscriptionDestination(); ok {
Expand All @@ -316,7 +318,9 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
}

if v.DeliveryAttributeMappings != nil {
d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings))
if err := d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "eventhub_endpoint", id.Name, id.Scope, err)
}
}
}
if v, ok := destination.AsHybridConnectionEventSubscriptionDestination(); ok {
Expand All @@ -329,7 +333,9 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
}

if v.DeliveryAttributeMappings != nil {
d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings))
if err := d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "hybrid_connection_endpoint", id.Name, id.Scope, err)
}
}
}
if serviceBusQueueEndpoint, ok := destination.AsServiceBusQueueEventSubscriptionDestination(); ok {
Expand All @@ -342,7 +348,9 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "service_bus_topic_endpoint_id", id.Name, id.Scope, err)
}
if serviceBusTopicEndpoint.DeliveryAttributeMappings != nil {
d.Set("delivery_property", flattenDeliveryProperties(d, serviceBusTopicEndpoint.DeliveryAttributeMappings))
if err := d.Set("delivery_property", flattenDeliveryProperties(d, serviceBusTopicEndpoint.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "service_bus_topic_endpoint_id", id.Name, id.Scope, err)
}
}
}
if v, ok := destination.AsStorageQueueEventSubscriptionDestination(); ok {
Expand All @@ -360,7 +368,9 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
}

if v.DeliveryAttributeMappings != nil {
d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings))
if err := d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "webhook_endpoint", id.Name, id.Scope, err)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,48 @@ func TestAccEventGridEventSubscription_deliveryPropertiesForEventHubs(t *testing
})
}

func TestAccEventGridEventSubscription_deliveryPropertiesUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_eventgrid_event_subscription", "test")
r := EventGridEventSubscriptionResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.deliveryPropertiesWithMultipleTypes(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("delivery_property.0.header_name").HasValue("test-static-1"),
check.That(data.ResourceName).Key("delivery_property.0.type").HasValue("Static"),
check.That(data.ResourceName).Key("delivery_property.0.value").HasValue("1"),
check.That(data.ResourceName).Key("delivery_property.0.secret").HasValue("false"),
check.That(data.ResourceName).Key("delivery_property.1.header_name").HasValue("test-dynamic-1"),
check.That(data.ResourceName).Key("delivery_property.1.type").HasValue("Dynamic"),
check.That(data.ResourceName).Key("delivery_property.1.source_field").HasValue("data.system"),
check.That(data.ResourceName).Key("delivery_property.2.header_name").HasValue("test-secret-1"),
check.That(data.ResourceName).Key("delivery_property.2.type").HasValue("Static"),
check.That(data.ResourceName).Key("delivery_property.2.secret").HasValue("true"),
check.That(data.ResourceName).Key("delivery_property.2.value").HasValue("this-value-is-secret!"),
),
},
{
Config: r.deliveryPropertiesUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("delivery_property.0.header_name").HasValue("test-static-1"),
check.That(data.ResourceName).Key("delivery_property.0.type").HasValue("Static"),
check.That(data.ResourceName).Key("delivery_property.0.value").HasValue("2"),
check.That(data.ResourceName).Key("delivery_property.0.secret").HasValue("false"),
check.That(data.ResourceName).Key("delivery_property.1.header_name").HasValue("test-dynamic-1"),
check.That(data.ResourceName).Key("delivery_property.1.type").HasValue("Dynamic"),
check.That(data.ResourceName).Key("delivery_property.1.source_field").HasValue("data.topic"),
check.That(data.ResourceName).Key("delivery_property.2.header_name").HasValue("test-secret-1"),
check.That(data.ResourceName).Key("delivery_property.2.type").HasValue("Static"),
check.That(data.ResourceName).Key("delivery_property.2.secret").HasValue("true"),
check.That(data.ResourceName).Key("delivery_property.2.value").HasValue("this-value-is-still-secret!"),
),
},
})
}

func TestAccEventGridEventSubscription_identity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_eventgrid_event_subscription", "test")
r := EventGridEventSubscriptionResource{}
Expand Down Expand Up @@ -1095,6 +1137,65 @@ resource "azurerm_eventgrid_event_subscription" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (EventGridEventSubscriptionResource) deliveryPropertiesUpdate(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-eg-%[1]d"
location = "%[2]s"
}
resource "azurerm_servicebus_namespace" "example" {
name = "acctestservicebusnamespace-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Standard"
}
resource "azurerm_servicebus_topic" "test" {
name = "acctestservicebustopic-%[1]d"
resource_group_name = azurerm_resource_group.test.name
namespace_name = azurerm_servicebus_namespace.example.name
enable_partitioning = true
}
resource "azurerm_eventgrid_event_subscription" "test" {
name = "acctest-eg-%[1]d"
scope = azurerm_resource_group.test.id
service_bus_topic_endpoint_id = azurerm_servicebus_topic.test.id
advanced_filtering_on_arrays_enabled = true
subject_filter {
subject_begins_with = "test/test"
}
delivery_property {
header_name = "test-static-1"
type = "Static"
value = "2"
secret = false
}
delivery_property {
header_name = "test-dynamic-1"
type = "Dynamic"
source_field = "data.topic"
}
delivery_property {
header_name = "test-secret-1"
type = "Static"
value = "this-value-is-still-secret!"
secret = true
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (EventGridEventSubscriptionResource) deliveryPropertiesForEventHubs(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down

0 comments on commit ef89ef8

Please sign in to comment.