Skip to content

Commit

Permalink
Merge pull request #7182 from jrauschenbusch/r-eventgrid-event-subscr…
Browse files Browse the repository at this point in the history
…iption-azure-function-support

Azure Function support for Event Grid event subscription
  • Loading branch information
tombuildsstuff committed Jun 5, 2020
2 parents 940169a + 15eec7d commit e63745a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Expand Up @@ -22,6 +22,7 @@ import (

func enpointPropertyNames() []string {
return []string{
"azure_function_endpoint",
"eventhub_endpoint",
"eventhub_endpoint_id",
"hybrid_connection_endpoint",
Expand Down Expand Up @@ -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{
"function_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,
Expand Down Expand Up @@ -630,6 +655,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)
Expand Down Expand Up @@ -751,6 +781,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,
Expand Down Expand Up @@ -840,6 +874,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["function_id"]; ok && v != "" {
props.ResourceID = 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)))
}

return azureFunctionDestination
}

func expandEventGridEventSubscriptionWebhookEndpoint(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination {
props := d.Get("webhook_endpoint").([]interface{})[0].(map[string]interface{})
url := props["url"].(string)
Expand Down Expand Up @@ -1011,6 +1075,35 @@ func flattenEventGridEventSubscriptionStorageQueueEndpoint(input *eventgrid.Stor
return []interface{}{result}
}

func flattenEventGridEventSubscriptionAzureFunctionEndpoint(input *eventgrid.AzureFunctionEventSubscriptionDestination) []interface{} {
results := make([]interface{}, 0)

if input == nil {
return results
}

functionID := ""
if input.ResourceID != nil {
functionID = *input.ResourceID
}

maxEventsPerBatch := 0
if input.MaxEventsPerBatch != nil {
maxEventsPerBatch = int(*input.MaxEventsPerBatch)
}

preferredBatchSize := 0
if input.PreferredBatchSizeInKilobytes != nil {
preferredBatchSize = int(*input.PreferredBatchSizeInKilobytes)
}

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{} {
if input == nil {
return nil
Expand Down
12 changes: 12 additions & 0 deletions website/docs/r/eventgrid_event_subscription.html.markdown
Expand Up @@ -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.
Expand Down Expand Up @@ -99,6 +101,16 @@ A `storage_queue_endpoint` supports the following:

---

An `azure_function_endpoint` supports the following:

* `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.

* `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.
Expand Down

0 comments on commit e63745a

Please sign in to comment.