private static EventGridAttribute EventGridAttributeFromNotifyRequest(string hookAddress, string commandName)
        {
            if (!string.IsNullOrWhiteSpace(hookAddress))
            {
                EventGridAttribute ret = new EventGridAttribute();
                ret.TopicEndpointUri = hookAddress;
                ret.TopicKeySetting  = EventStream.MakeEventStreamName(commandName);
            }

            // If we reach the end with not enough info to create an event grid target return null
            return(null);
        }
Esempio n. 2
0
        public static async Task <ActivityResponse> QueryOutputToEventGridActivity(
            [ActivityTrigger] DurableActivityContext context,
            Binder outputBinder,
            ILogger log)
        {
            ActivityResponse ret = new ActivityResponse()
            {
                FunctionName = "QueryOutputToEventGridActivity"
            };

            #region Logging
            if (null != log)
            {
                log.LogDebug($"Output  in {ret.FunctionName} ");
            }
            #endregion

            // Read the results
            QueryOutputRecord <object> results = context.GetInput <QueryOutputRecord <object> >();
            if (null != results)
            {
                EventGridAttribute egAttribute = EventGridAttributeFromTarget(results.Target, results.QueryName, results.QueryUniqueIdentifier);

                if (null != egAttribute)
                {
                    // split the target string into an event grid attribute

                    Microsoft.Azure.EventGrid.Models.EventGridEvent eventGridEvent = new Microsoft.Azure.EventGrid.Models.EventGridEvent()
                    {
                        Subject = results.QueryUniqueIdentifier.ToString(),
                        Data    = results.Results
                    };

                    IAsyncCollector <EventGridEvent> eventCollector = outputBinder.Bind <IAsyncCollector <EventGridEvent> >(egAttribute);
                    if (null != eventCollector)
                    {
                        await eventCollector.AddAsync(eventGridEvent);

                        await eventCollector.FlushAsync();
                    }
                }
                else
                {
                    ret.Message = $"Unable to determine the event grid target from {results.Target}";
                }
            }
            else
            {
                ret.Message = $"Unable to get query output record to send to event grid";
            }

            return(ret);
        }
Esempio n. 3
0
        /// <summary>
        /// Turn a query output target into the event grid attribute to which it will be sent
        /// </summary>
        public static EventGridAttribute EventGridAttributeFromTarget(string target,
                                                                      string queryName,
                                                                      Guid queryUniqueIdentifier)
        {
            if (!string.IsNullOrWhiteSpace(target))
            {
                EventGridAttribute ret = new EventGridAttribute();
                ret.TopicEndpointUri = target;
                ret.TopicKeySetting  = EventStream.MakeEventStreamName(queryName);
            }

            // If we reach the end with not enough info to create an event grid target return null
            return(null);
        }
        public static async Task <ActivityResponse> RunCustomEventGridTopicNotificationActivity(
            [ActivityTrigger] DurableActivityContext context,
            Binder outputBinder,
            ILogger log)
        {
            ActivityResponse response = new ActivityResponse()
            {
                FunctionName = "RunCustomEventGridTopicNotificationActivity"
            };

            Command_Notification_Request notifyRequest = context.GetInput <Command_Notification_Request>();

            if (null != notifyRequest)
            {
                EventGridAttribute egAttribute = EventGridAttributeFromNotifyRequest(notifyRequest.HookAddress, notifyRequest.CommandName);

                if (null != egAttribute)
                {
                    // split the target string into an event grid attribute

                    Microsoft.Azure.EventGrid.Models.EventGridEvent eventGridEvent = new Microsoft.Azure.EventGrid.Models.EventGridEvent()
                    {
                        Subject = $"/{EventStream.MakeEventStreamName(notifyRequest.ImpactedEntity.EntityType)}/{EventStream.MakeEventStreamName(notifyRequest.ImpactedEntity.InstanceUniqueIdentifier)}",
                        Data    = notifyRequest
                    };

                    IAsyncCollector <EventGridEvent> eventCollector = outputBinder.Bind <IAsyncCollector <EventGridEvent> >(egAttribute);
                    if (null != eventCollector)
                    {
                        await eventCollector.AddAsync(eventGridEvent);

                        await eventCollector.FlushAsync();
                    }
                    response.Message = $"Sent notification to {egAttribute.TopicEndpointUri} for {notifyRequest.ImpactedEntity.EntityType}:{notifyRequest.ImpactedEntity.InstanceUniqueIdentifier }  ";
                }
                else
                {
                    response.StepFailure = true;
                    response.Message     = "Unable to create an event grid attribute to send the notification to";
                }
            }
            else
            {
                response.StepFailure = true;
                response.Message     = "Unable to read command notification request from context";
            }


            return(response);
        }