From cedcffe3a25f48125b3217ab63e7baaa535c5f49 Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Thu, 4 Jun 2020 12:03:15 +0200 Subject: [PATCH 1/2] Extended EventGrid webhook support --- .../eventgrid_event_subscription_resource.go | 116 +++++++++++++++--- ...eventgrid_event_subscription.html.markdown | 8 ++ 2 files changed, 107 insertions(+), 17 deletions(-) diff --git a/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go b/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go index 40f6ddd12ff7..a2f764e2acdc 100644 --- a/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go +++ b/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go @@ -217,6 +217,28 @@ func resourceArmEventGridEventSubscription() *schema.Resource { Required: true, ValidateFunc: validation.IsURLWithHTTPS, }, + "base_url": { + Type: schema.TypeString, + Computed: true, + }, + "max_events_per_batch": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 5000), + }, + "preferred_batch_size_in_kilobytes": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 1024), + }, + "active_directory_tenant_id": { + Type: schema.TypeString, + Optional: true, + }, + "active_directory_app_id_or_uri": { + Type: schema.TypeString, + Optional: true, + }, }, }, }, @@ -693,12 +715,12 @@ func resourceArmEventGridEventSubscriptionRead(d *schema.ResourceData, meta inte return fmt.Errorf("Error setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "storage_queue_endpoint", id.Name, id.Scope, err) } } - if _, ok := props.Destination.AsWebHookEventSubscriptionDestination(); ok { + if v, ok := props.Destination.AsWebHookEventSubscriptionDestination(); ok { fullURL, err := client.GetFullURL(ctx, id.Scope, id.Name) if err != nil { return fmt.Errorf("Error making Read request on EventGrid Event Subscription full URL '%s': %+v", id.Name, err) } - if err := d.Set("webhook_endpoint", flattenEventGridEventSubscriptionWebhookEndpoint(&fullURL)); err != nil { + if err := d.Set("webhook_endpoint", flattenEventGridEventSubscriptionWebhookEndpoint(v, &fullURL)); err != nil { return fmt.Errorf("Error setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "webhook_endpoint", id.Name, id.Scope, err) } } @@ -829,8 +851,8 @@ func expandEventGridEventSubscriptionDestination(d *schema.ResourceData) eventgr return expandEventGridEventSubscriptionStorageQueueEndpoint(d) } - if _, ok := d.GetOk("webhook_endpoint"); ok { - return expandEventGridEventSubscriptionWebhookEndpoint(d) + if v, ok := d.GetOk("webhook_endpoint"); ok { + return expandEventGridEventSubscriptionWebhookEndpoint(v) } return nil @@ -904,16 +926,42 @@ func expandEventGridEventSubscriptionAzureFunctionEndpoint(input interface{}) ev return azureFunctionDestination } -func expandEventGridEventSubscriptionWebhookEndpoint(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination { - props := d.Get("webhook_endpoint").([]interface{})[0].(map[string]interface{}) - url := props["url"].(string) +func expandEventGridEventSubscriptionWebhookEndpoint(input interface{}) eventgrid.BasicEventSubscriptionDestination { + configs := input.([]interface{}) - return eventgrid.WebHookEventSubscriptionDestination{ + props := eventgrid.WebHookEventSubscriptionDestinationProperties{} + webhookDestination := &eventgrid.WebHookEventSubscriptionDestination{ EndpointType: eventgrid.EndpointTypeWebHook, - WebHookEventSubscriptionDestinationProperties: &eventgrid.WebHookEventSubscriptionDestinationProperties{ - EndpointURL: &url, - }, + WebHookEventSubscriptionDestinationProperties: &props, + } + + if len(configs) == 0 { + return webhookDestination } + + config := configs[0].(map[string]interface{}) + + if v, ok := config["url"]; ok && v != "" { + props.EndpointURL = utils.String(v.(string)) + } + + if v, ok := config["max_events_per_batch"]; ok && v != 0 { + props.MaxEventsPerBatch = utils.Int32(int32(v.(int))) + } + + if v, ok := config["preferred_batch_size_in_kilobytes"]; ok && v != 0 { + props.PreferredBatchSizeInKilobytes = utils.Int32(int32(v.(int))) + } + + if v, ok := config["active_directory_tenant_id"]; ok && v != "" { + props.AzureActiveDirectoryTenantID = utils.String(v.(string)) + } + + if v, ok := config["active_directory_app_id_or_uri"]; ok && v != "" { + props.AzureActiveDirectoryApplicationIDOrURI = utils.String(v.(string)) + } + + return webhookDestination } func expandEventGridEventSubscriptionFilter(d *schema.ResourceData) (*eventgrid.EventSubscriptionFilter, error) { @@ -1104,17 +1152,51 @@ func flattenEventGridEventSubscriptionAzureFunctionEndpoint(input *eventgrid.Azu }) } -func flattenEventGridEventSubscriptionWebhookEndpoint(input *eventgrid.EventSubscriptionFullURL) []interface{} { +func flattenEventGridEventSubscriptionWebhookEndpoint(input *eventgrid.WebHookEventSubscriptionDestination, fullURL *eventgrid.EventSubscriptionFullURL) []interface{} { + results := make([]interface{}, 0) + if input == nil { - return nil + return results } - result := make(map[string]interface{}) - if input.EndpointURL != nil { - result["url"] = *input.EndpointURL + webhookURL := "" + if fullURL != nil { + webhookURL = *fullURL.EndpointURL } - return []interface{}{result} + webhookBaseURL := "" + if input.EndpointBaseURL != nil { + webhookBaseURL = *input.EndpointBaseURL + } + + maxEventsPerBatch := 0 + if input.MaxEventsPerBatch != nil { + maxEventsPerBatch = int(*input.MaxEventsPerBatch) + } + + preferredBatchSizeInKilobytes := 0 + if input.PreferredBatchSizeInKilobytes != nil { + preferredBatchSizeInKilobytes = int(*input.PreferredBatchSizeInKilobytes) + } + + azureActiveDirectoryTenantID := "" + if input.AzureActiveDirectoryTenantID != nil { + azureActiveDirectoryTenantID = *input.AzureActiveDirectoryTenantID + } + + azureActiveDirectoryApplicationIDOrURI := "" + if input.AzureActiveDirectoryApplicationIDOrURI != nil { + azureActiveDirectoryApplicationIDOrURI = *input.AzureActiveDirectoryApplicationIDOrURI + } + + return append(results, map[string]interface{}{ + "url": webhookURL, + "base_url": webhookBaseURL, + "max_events_per_batch": maxEventsPerBatch, + "preferred_batch_size_in_kilobytes": preferredBatchSizeInKilobytes, + "active_directory_tenant_id": azureActiveDirectoryTenantID, + "active_directory_app_id_or_uri": azureActiveDirectoryApplicationIDOrURI, + }) } func flattenEventGridEventSubscriptionSubjectFilter(filter *eventgrid.EventSubscriptionFilter) []interface{} { diff --git a/website/docs/r/eventgrid_event_subscription.html.markdown b/website/docs/r/eventgrid_event_subscription.html.markdown index d42d267db95d..30fc054708d6 100644 --- a/website/docs/r/eventgrid_event_subscription.html.markdown +++ b/website/docs/r/eventgrid_event_subscription.html.markdown @@ -127,6 +127,14 @@ A `webhook_endpoint` supports the following: * `url` - (Required) Specifies the url of the webhook where the Event Subscription will receive events. +* `max_events_per_batch` - (Optional) Maximum number of events per batch. + +* `preferred_batch_size_in_kilobytes` - (Optional) Preferred batch size in Kilobytes. + +* `active_directory_tenant_id` - (Optional) The Azure Active Directory Tenant ID to get the access token that will be included as the bearer token in delivery requests. + +* `active_directory_app_id_or_uri` - (Optional) The Azure Active Directory Application ID or URI to get the access token that will be included as the bearer token in delivery requests. + --- A `subject_filter` supports the following: From 123da0c43c7fdf1988a952e61344c64891eb143e Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Wed, 10 Jun 2020 10:44:44 +0200 Subject: [PATCH 2/2] add documentation for base_url --- website/docs/r/eventgrid_event_subscription.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/eventgrid_event_subscription.html.markdown b/website/docs/r/eventgrid_event_subscription.html.markdown index 30fc054708d6..881d1cb47b62 100644 --- a/website/docs/r/eventgrid_event_subscription.html.markdown +++ b/website/docs/r/eventgrid_event_subscription.html.markdown @@ -127,6 +127,8 @@ A `webhook_endpoint` supports the following: * `url` - (Required) Specifies the url of the webhook where the Event Subscription will receive events. +* `base_url` - (Computed) The base url of the webhook where the Event Subscription will receive events. + * `max_events_per_batch` - (Optional) Maximum number of events per batch. * `preferred_batch_size_in_kilobytes` - (Optional) Preferred batch size in Kilobytes.