Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Resource: azurerm_eventgrid_event_subscription #6860

Merged
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/eventgrid/mgmt/2020-04-01-preview/eventgrid"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand All @@ -20,7 +21,7 @@ import (
)

func getEnpointTypes() []string {
return []string{"webhook_endpoint", "storage_queue_endpoint", "eventhub_endpoint", "hybrid_connection_endpoint"}
return []string{"webhook_endpoint", "storage_queue_endpoint", "eventhub_endpoint", "hybrid_connection_endpoint", "service_bus_queue_endpoint", "service_bus_topic_endpoint", "azure_function_endpoint"}
}

func resourceArmEventGridEventSubscription() *schema.Resource {
Expand Down Expand Up @@ -69,6 +70,13 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
}, false),
},

"expiration_time_utc": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"topic_name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -144,6 +152,54 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
},
},

"service_bus_queue_endpoint": {
jrauschenbusch marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: utils.RemoveFromStringArray(getEnpointTypes(), "service_bus_queue_endpoint"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_bus_queue_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
},
},
},

"service_bus_topic_endpoint": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: utils.RemoveFromStringArray(getEnpointTypes(), "service_bus_topic_endpoint"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_bus_queue_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
},
},
},

"azure_function_endpoint": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: utils.RemoveFromStringArray(getEnpointTypes(), "azure_function_endpoint"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"azure_function_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
},
},
},

"included_event_types": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -256,13 +312,21 @@ func resourceArmEventGridEventSubscriptionCreateUpdate(d *schema.ResourceData, m

filter := expandEventGridEventSubscriptionFilter(d)

parsedTime, err := date.ParseTime(time.RFC3339, d.Get("expiration_time_utc").(string))
if err != nil {
return fmt.Errorf("Error creating/updating EventGrid Event Subscription %q (Scope %q): %s", name, scope, err)
}

expirationTime := date.Time{Time: parsedTime}

eventSubscriptionProperties := eventgrid.EventSubscriptionProperties{
Destination: destination,
Filter: filter,
DeadLetterDestination: expandEventGridEventSubscriptionStorageBlobDeadLetterDestination(d),
RetryPolicy: expandEventGridEventSubscriptionRetryPolicy(d),
Labels: utils.ExpandStringSlice(d.Get("labels").([]interface{})),
EventDeliverySchema: eventgrid.EventDeliverySchema(d.Get("event_delivery_schema").(string)),
ExpirationTimeUtc: &expirationTime,
}

eventSubscription := eventgrid.EventSubscription{
Expand Down Expand Up @@ -318,6 +382,7 @@ func resourceArmEventGridEventSubscriptionRead(d *schema.ResourceData, meta inte
d.Set("scope", id.Scope)

if props := resp.EventSubscriptionProperties; props != nil {
d.Set("expiration_time_utc", props.ExpirationTimeUtc.Format(time.RFC3339))
d.Set("event_delivery_schema", string(props.EventDeliverySchema))

if props.Topic != nil && *props.Topic != "" {
Expand All @@ -339,6 +404,21 @@ func resourceArmEventGridEventSubscriptionRead(d *schema.ResourceData, meta inte
return fmt.Errorf("Error setting `hybrid_connection_endpoint` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
}
}
if serviceBusQueueEndpoint, ok := props.Destination.AsServiceBusQueueEventSubscriptionDestination(); ok {
if err := d.Set("service_bus_queue_endpoint", flattenEventGridEventSubscriptionServiceBusQueueEndpoint(serviceBusQueueEndpoint)); err != nil {
return fmt.Errorf("Error setting `service_bus_queue_endpoint` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
}
}
if serviceBusTopicEndpoint, ok := props.Destination.AsServiceBusTopicEventSubscriptionDestination(); ok {
if err := d.Set("service_bus_topic_endpoint", flattenEventGridEventSubscriptionServiceBusTopicEndpoint(serviceBusTopicEndpoint)); err != nil {
return fmt.Errorf("Error setting `service_bus_topic_endpoint` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
}
}
if azureFunctionEndpoint, ok := props.Destination.AsAzureFunctionEventSubscriptionDestination(); ok {
if err := d.Set("azure_function_endpoint", flattenEventGridEventSubscriptionAzureFunctionEndpoint(azureFunctionEndpoint)); err != nil {
return fmt.Errorf("Error setting `azure_function_endpoint` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
}
}
if _, ok := props.Destination.AsWebHookEventSubscriptionDestination(); ok {
fullURL, err := client.GetFullURL(ctx, id.Scope, id.Name)
if err != nil {
Expand Down Expand Up @@ -423,6 +503,18 @@ func expandEventGridEventSubscriptionDestination(d *schema.ResourceData) eventgr
return expandEventGridEventSubscriptionWebhookEndpoint(d)
}

if _, ok := d.GetOk("service_bus_queue_endpoint"); ok {
return expandEventGridEventSubscriptionServiceBusQueueEndpoint(d)
}

if _, ok := d.GetOk("service_bus_topic_endpoint"); ok {
return expandEventGridEventSubscriptionServiceBusTopicEndpoint(d)
}

if _, ok := d.GetOk("azure_function"); ok {
return expandEventGridEventSubscriptionAzureFunctionEndpoint(d)
}

return nil
}

Expand Down Expand Up @@ -480,6 +572,45 @@ func expandEventGridEventSubscriptionWebhookEndpoint(d *schema.ResourceData) eve
return webhookEndpoint
}

func expandEventGridEventSubscriptionServiceBusQueueEndpoint(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination {
props := d.Get("service_bus_queue_endpoint").([]interface{})[0].(map[string]interface{})
serviceBusQueueID := props["service_bus_queue_id"].(string)

serviceBusQueueEndpoint := eventgrid.ServiceBusQueueEventSubscriptionDestination{
EndpointType: eventgrid.EndpointTypeServiceBusQueue,
ServiceBusQueueEventSubscriptionDestinationProperties: &eventgrid.ServiceBusQueueEventSubscriptionDestinationProperties{
ResourceID: &serviceBusQueueID,
},
}
return serviceBusQueueEndpoint
}

func expandEventGridEventSubscriptionServiceBusTopicEndpoint(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination {
props := d.Get("service_bus_topic_endpoint").([]interface{})[0].(map[string]interface{})
serviceBusTopicID := props["service_bus_topic_id"].(string)

serviceBusTopicEndpoint := eventgrid.ServiceBusTopicEventSubscriptionDestination{
EndpointType: eventgrid.EndpointTypeServiceBusTopic,
ServiceBusTopicEventSubscriptionDestinationProperties: &eventgrid.ServiceBusTopicEventSubscriptionDestinationProperties{
ResourceID: &serviceBusTopicID,
},
}
return serviceBusTopicEndpoint
}

func expandEventGridEventSubscriptionAzureFunctionEndpoint(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination {
props := d.Get("azure_function_endpoint").([]interface{})[0].(map[string]interface{})
azureFunctionResourceID := props["azure_function_id"].(string)

azureFunctionEndpoint := eventgrid.AzureFunctionEventSubscriptionDestination{
EndpointType: eventgrid.EndpointTypeAzureFunction,
AzureFunctionEventSubscriptionDestinationProperties: &eventgrid.AzureFunctionEventSubscriptionDestinationProperties{
ResourceID: &azureFunctionResourceID,
},
}
return azureFunctionEndpoint
}

func expandEventGridEventSubscriptionFilter(d *schema.ResourceData) *eventgrid.EventSubscriptionFilter {
filter := &eventgrid.EventSubscriptionFilter{}

Expand Down Expand Up @@ -623,6 +754,45 @@ func flattenEventGridEventSubscriptionStorageBlobDeadLetterDestination(dest *eve
return []interface{}{result}
}

func flattenEventGridEventSubscriptionServiceBusQueueEndpoint(input *eventgrid.ServiceBusQueueEventSubscriptionDestination) []interface{} {
if input == nil {
return nil
}
result := make(map[string]interface{})

if input.ResourceID != nil {
result["service_bus_queue_id"] = *input.ResourceID
}

return []interface{}{result}
}

func flattenEventGridEventSubscriptionServiceBusTopicEndpoint(input *eventgrid.ServiceBusTopicEventSubscriptionDestination) []interface{} {
if input == nil {
return nil
}
result := make(map[string]interface{})

if input.ResourceID != nil {
result["service_bus_topic_id"] = *input.ResourceID
}

return []interface{}{result}
}

func flattenEventGridEventSubscriptionAzureFunctionEndpoint(input *eventgrid.AzureFunctionEventSubscriptionDestination) []interface{} {
if input == nil {
return nil
}
result := make(map[string]interface{})

if input.ResourceID != nil {
result["azure_function_id"] = *input.ResourceID
}

return []interface{}{result}
}

func flattenEventGridEventSubscriptionRetryPolicy(retryPolicy *eventgrid.RetryPolicy) []interface{} {
result := make(map[string]interface{})

Expand Down