From 4a6e6c02daffd0843319ac957d8560a3b4ac8df0 Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Sun, 31 May 2020 14:19:33 +0200 Subject: [PATCH 1/3] Azure Function Support for Event Grid Subscription --- .../eventgrid_event_subscription_resource.go | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go b/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go index b133082a6639..bbae76ffbc4f 100644 --- a/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go +++ b/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go @@ -22,6 +22,7 @@ import ( func enpointPropertyNames() []string { return []string{ + "azure_function_endpoint", "eventhub_endpoint", "eventhub_endpoint_id", "hybrid_connection_endpoint", @@ -91,6 +92,30 @@ func resourceArmEventGridEventSubscription() *schema.Resource { Computed: true, }, + "azure_function_endpoint": { + Type: schema.TypeList, + MaxItems: 1, + Optional: true, + ConflictsWith: utils.RemoveFromStringArray(enpointPropertyNames(), "azure_function_endpoint"), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Required: true, + ValidateFunc: azure.ValidateResourceID, + }, + "max_events_per_batch": { + Type: schema.TypeInt, + Optional: true, + }, + "preferred_batch_size_in_kilobytes": { + Type: schema.TypeInt, + Optional: true, + }, + }, + }, + }, + "eventhub_endpoint_id": { Type: schema.TypeString, Optional: true, @@ -386,6 +411,11 @@ func resourceArmEventGridEventSubscriptionRead(d *schema.ResourceData, meta inte d.Set("topic_name", props.Topic) } + if azureFunctionEndpoint, ok := props.Destination.AsAzureFunctionEventSubscriptionDestination(); ok { + if err := d.Set("azure_function_endpoint", flattenEventGridEventSubscriptionAzureFunctionEndpoint(azureFunctionEndpoint)); err != nil { + return fmt.Errorf("Error setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "azure_function_endpoint", id.Name, id.Scope, err) + } + } if v, ok := props.Destination.AsEventHubEventSubscriptionDestination(); ok { if err := d.Set("eventhub_endpoint_id", v.ResourceID); err != nil { return fmt.Errorf("Error setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "eventhub_endpoint_id", id.Name, id.Scope, err) @@ -504,6 +534,10 @@ func expandEventGridExpirationTime(d *schema.ResourceData) (*date.Time, error) { } func expandEventGridEventSubscriptionDestination(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination { + if v, ok := d.GetOk("azure_function_endpoint"); ok { + return expandEventGridEventSubscriptionAzureFunctionEndpoint(v) + } + if v, ok := d.GetOk("eventhub_endpoint_id"); ok { return &eventgrid.EventHubEventSubscriptionDestination{ EndpointType: eventgrid.EndpointTypeEventHub, @@ -593,6 +627,36 @@ func expandEventGridEventSubscriptionHybridConnectionEndpoint(d *schema.Resource } } +func expandEventGridEventSubscriptionAzureFunctionEndpoint(input interface{}) eventgrid.BasicEventSubscriptionDestination { + configs := input.([]interface{}) + + props := eventgrid.AzureFunctionEventSubscriptionDestinationProperties{} + azureFunctionDestination := &eventgrid.AzureFunctionEventSubscriptionDestination{ + EndpointType: eventgrid.EndpointTypeAzureFunction, + AzureFunctionEventSubscriptionDestinationProperties: &props, + } + + if len(configs) == 0 { + return azureFunctionDestination + } + + config := configs[0].(map[string]interface{}) + + if v, ok := config["id"]; ok { + props.ResourceID = utils.String(v.(string)) + } + + if v, ok := config["max_events_per_batch"]; ok { + props.MaxEventsPerBatch = utils.Int32(int32(v.(int))) + } + + if v, ok := config["preferred_batch_size_in_kilobytes"]; ok { + props.PreferredBatchSizeInKilobytes = utils.Int32(int32(v.(int))) + } + + return azureFunctionDestination +} + func expandEventGridEventSubscriptionWebhookEndpoint(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination { props := d.Get("webhook_endpoint").([]interface{})[0].(map[string]interface{}) url := props["url"].(string) @@ -699,6 +763,30 @@ func flattenEventGridEventSubscriptionStorageQueueEndpoint(input *eventgrid.Stor return []interface{}{result} } +func flattenEventGridEventSubscriptionAzureFunctionEndpoint(input *eventgrid.AzureFunctionEventSubscriptionDestination) []interface{} { + results := make([]interface{}, 0) + + if input == nil { + return results + } + + result := make(map[string]interface{}) + + if input.ResourceID != nil { + result["id"] = *input.ResourceID + } + + if input.MaxEventsPerBatch != nil { + result["max_events_per_batch"] = *input.MaxEventsPerBatch + } + + if input.PreferredBatchSizeInKilobytes != nil { + result["preferred_batch_size_in_kilobytes"] = *input.PreferredBatchSizeInKilobytes + } + + return append(results, result) +} + func flattenEventGridEventSubscriptionWebhookEndpoint(input *eventgrid.EventSubscriptionFullURL) []interface{} { if input == nil { return nil From f8fc97591f4aef4ef8058b3a7a120bf7c8e1871a Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Mon, 1 Jun 2020 19:45:38 +0200 Subject: [PATCH 2/3] update docs --- .../r/eventgrid_event_subscription.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/website/docs/r/eventgrid_event_subscription.html.markdown b/website/docs/r/eventgrid_event_subscription.html.markdown index 8df480c0222b..5c568c705b82 100644 --- a/website/docs/r/eventgrid_event_subscription.html.markdown +++ b/website/docs/r/eventgrid_event_subscription.html.markdown @@ -59,6 +59,8 @@ The following arguments are supported: * `event_delivery_schema` - (Optional) Specifies the event delivery schema for the event subscription. Possible values include: `EventGridSchema`, `CloudEventSchemaV1_0`, `CustomInputSchema`. Defaults to `EventGridSchema`. Changing this forces a new resource to be created. +* `azure_function_endpoint` - (Optional) An `azure_function_endpoint` block as defined below. + * `eventhub_endpoint` - (Optional / **Deprecated in favour of `eventhub_endpoint_id`**) A `eventhub_endpoint` block as defined below. * `eventhub_endpoint_id` - (Optional) Specifies the id where the Event Hub is located. @@ -97,6 +99,16 @@ A `storage_queue_endpoint` supports the following: --- +An `azure_function_endpoint` supports the following: + +* `id` - (Required) Specifies the Azure Resource ID that represents the endpoint of the Azure function 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. + +--- + A `eventhub_endpoint` supports the following: * `eventhub_id` - (Required) Specifies the id of the eventhub where the Event Subscription will receive events. From 15eec7d8f4b51179d77a60b2dc41107d240b207a Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Thu, 4 Jun 2020 13:20:59 +0200 Subject: [PATCH 3/3] fixed PR notes and optionals --- .../eventgrid_event_subscription_resource.go | 25 +++++++++++-------- ...eventgrid_event_subscription.html.markdown | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go b/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go index bbae76ffbc4f..408d75ca1c5c 100644 --- a/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go +++ b/azurerm/internal/services/eventgrid/eventgrid_event_subscription_resource.go @@ -99,7 +99,7 @@ func resourceArmEventGridEventSubscription() *schema.Resource { ConflictsWith: utils.RemoveFromStringArray(enpointPropertyNames(), "azure_function_endpoint"), Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "id": { + "function_id": { Type: schema.TypeString, Required: true, ValidateFunc: azure.ValidateResourceID, @@ -642,15 +642,15 @@ func expandEventGridEventSubscriptionAzureFunctionEndpoint(input interface{}) ev config := configs[0].(map[string]interface{}) - if v, ok := config["id"]; ok { + if v, ok := config["function_id"]; ok && v != "" { props.ResourceID = utils.String(v.(string)) } - if v, ok := config["max_events_per_batch"]; ok { + 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 { + if v, ok := config["preferred_batch_size_in_kilobytes"]; ok && v != 0 { props.PreferredBatchSizeInKilobytes = utils.Int32(int32(v.(int))) } @@ -770,21 +770,26 @@ func flattenEventGridEventSubscriptionAzureFunctionEndpoint(input *eventgrid.Azu return results } - result := make(map[string]interface{}) - + functionID := "" if input.ResourceID != nil { - result["id"] = *input.ResourceID + functionID = *input.ResourceID } + maxEventsPerBatch := 0 if input.MaxEventsPerBatch != nil { - result["max_events_per_batch"] = *input.MaxEventsPerBatch + maxEventsPerBatch = int(*input.MaxEventsPerBatch) } + preferredBatchSize := 0 if input.PreferredBatchSizeInKilobytes != nil { - result["preferred_batch_size_in_kilobytes"] = *input.PreferredBatchSizeInKilobytes + preferredBatchSize = int(*input.PreferredBatchSizeInKilobytes) } - return append(results, result) + return append(results, map[string]interface{}{ + "function_id": functionID, + "max_events_per_batch": maxEventsPerBatch, + "preferred_batch_size_in_kilobytes": preferredBatchSize, + }) } func flattenEventGridEventSubscriptionWebhookEndpoint(input *eventgrid.EventSubscriptionFullURL) []interface{} { diff --git a/website/docs/r/eventgrid_event_subscription.html.markdown b/website/docs/r/eventgrid_event_subscription.html.markdown index 5c568c705b82..7669c208a5f9 100644 --- a/website/docs/r/eventgrid_event_subscription.html.markdown +++ b/website/docs/r/eventgrid_event_subscription.html.markdown @@ -101,7 +101,7 @@ A `storage_queue_endpoint` supports the following: An `azure_function_endpoint` supports the following: -* `id` - (Required) Specifies the Azure Resource ID that represents the endpoint of the Azure function where the Event Subscription will receive events. +* `function_id` - (Required) Specifies the ID of the Function where the Event Subscription will receive events. * `max_events_per_batch` - (Optional) Maximum number of events per batch.