private static LogItem GetLogItem(ResourceWriteSuccess evt)
 {
     return(new LogItem(evt.SubscriptionId, evt.Claims[ClaimTypes.Name])
     {
         ActionDate = DateTime.UtcNow,
         IpAddress = evt.HttpRequest.ClientIpAddress,
         OperationName = evt.OperationName,
         ResourceName = evt.ResourceUri,
         Status = evt.Status
     });
 }
 public static LogItem Process(ResourceWriteSuccess evt)
 {
     switch (evt.OperationName)
     {
     case OperationType.ResourceGroupWrite:
         /*
          * --do what you need to do here--
          * For example, grab properties, call into an HR system and determine a cost center for the creator's division,
          * then use a service principal here to update the resource group tags with the value
          */
         return(GetLogItem(evt));
     }
     return(null);
 }
Example #3
0
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");
            string requestBody = "";

            try
            {
                requestBody = new StreamReader(req.Body).ReadToEnd();
                IList <EventGridResponse> data = JsonConvert.DeserializeObject <IList <EventGridResponse> >(requestBody);

                if (data.Count == 1 && data[0].EventType == EventType.SubscriptionValidationEvent)
                {
                    log.Info(String.Format("Registering event grid..."));
                    return(new OkObjectResult(GetValidationResponse((string)data[0].Data.validationCode)));
                }

                foreach (EventGridResponse response in data)
                {
                    response.Data.authorization = JToken.Parse(response.Data.authorization.ToString());
                    response.Data.claims        = JToken.Parse(response.Data.claims.ToString());
                    response.Data.httpRequest   = JToken.Parse(response.Data.httpRequest.ToString());

                    ResourceWriteSuccess evt = JsonConvert.DeserializeObject <ResourceWriteSuccess>(JsonConvert.SerializeObject(response.Data));
                    var logItem = ProcessEvent.Process(evt);

                    log.Info(String.Format("New event - log data:\r\n{0}", JsonConvert.SerializeObject(logItem)));
                }

                return(new OkResult());
            }
            catch (Exception ex)
            {
                log.Error("Error processing event", ex, requestBody);
                log.Error("Err evt data", null, requestBody);
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }