Пример #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("AlertCreated Function Triggered Via Webhook.");

            // Get the remote execution context
            string jsonContext = await req.Content.ReadAsStringAsync();

            log.Info("Read context: " + jsonContext);

            jsonContext = FormatJson(jsonContext);
            log.Info("Formatted JSON Context string: " + jsonContext);

            Microsoft.Xrm.Sdk.RemoteExecutionContext context = DeserializeJsonString <Microsoft.Xrm.Sdk.RemoteExecutionContext>(jsonContext);

            //var context = await req.Content.ReadAsAsync<RemoteExecutionContext>();

            // Ensure this function was called on the create message of the scan_alert entity
            if (context.MessageName != "Create" && context.PrimaryEntityName != "scan_alert")
            {
                var err = "AlertCreated Context Execution Is Invalid";
                log.Error(err);
                return(req.CreateResponse(HttpStatusCode.BadRequest, err));
            }

            var alert = context.PostEntityImages.FirstOrDefault().Value.ToEntity <scan_alert>();

            if (alert == null)
            {
                var err = "Invalid Alert Post Entity Image";
                log.Error(err);
                return(req.CreateResponse(HttpStatusCode.BadRequest, err));
            }

            log.Info("Obtained Valid Context");
            log.Info("Processing Alert " + alert.Id.ToString());

            string orgUrl = ConfigurationManager.AppSettings["ScantegraOrgUrl"];
            Uri    orgUri = new Uri(orgUrl);

            log.Info(orgUri.ToString());
            //var proxyClient = new OrganizationWebProxyClient(orgUri, true);
            CrmServiceClient.AuthOverrideHook = new AuthHook();
            //CrmServiceClient test = new CrmServiceClient()
            CrmServiceClient crmSvc = new CrmServiceClient(orgUri, true);

            if (crmSvc.IsReady)
            {
                log.Info("Connected To Dynamics 365");
            }
            //crmSvc.OrganizationWebProxyClient.EnableProxyTypes();

            var scantegraContext = new ScantegraServiceContext(crmSvc.OrganizationWebProxyClient);


            // Pull the alert to get the full entity
            alert = scantegraContext.scan_alertSet.Where(a => a.Id == alert.Id).FirstOrDefault();


            // Pull the alert notification group from the rule
            var alertNotificationGroup = scantegraContext.scan_alertnotificationgroupSet.Where(ang => ang.Id == alert.scan_AlertNotificationGroup.Id).FirstOrDefault();

            log.Info("ANG ID " + alertNotificationGroup.Id.ToString());

            // Pull the alert notification actions from the intersection table
            var alertNotificationActionAssociations = scantegraContext.scan_alertgroupalertnotificationassociationSet.Where(a => a.scan_AlertNotificationGroup.Id == alertNotificationGroup.Id).ToList();

            // Iterate through the alert notification actions and process accordingly
            foreach (var association in alertNotificationActionAssociations)
            {
                var alertNotificationAction = scantegraContext.scan_alertnotificationactionSet.Where(aa => aa.Id == association.scan_AlertNotificationAction.Id).FirstOrDefault();
                log.Info("Processing action " + alertNotificationAction.Id.ToString());
                switch (alertNotificationAction.scan_NotificationActionType.Value)
                {
                case (int)scan_AlertNotificationActionType.PostToMicrosoftTeams:
                    var client = new TeamsClient(alertNotificationAction.scan_TeamsWebhookUrl);
                    var card   = new AlertCard()
                    {
                        Title = alert.scan_name,
                        Text  = $"{alert.FormattedValues["scan_alertseverity"]} - {alert.scan_name}"
                    };
                    client.PostMessage(card);
                    break;
                }
            }

            return(req.CreateResponse(HttpStatusCode.OK));
        }