nirmalkumarv
4/22/2018 - 11:46 AM

Add Webhooks to Azure Data Factory

Add Webhooks to Azure Data Factory

/*

Create a Resource Alert Rule
Map the Rule to a Webhook
Create/Update the Rule 

*/


Rule resourceAlertRule = new Rule
    {
        Name = alertName,
        Description = String.Format("This alert will be triggered when an {0} is failed", datafactoryName),
        IsEnabled = true,

        Condition = new ManagementEventRuleCondition
        {
            Aggregation = null,
            DataSource = new RuleManagementEventDataSource
            {
                //  ResourceProviderName = "Microsoft.DataFactory",
                Status = "Failed",
                // SubStatus = "FailedExecution",
                ResourceUri = resourceUriFinal,
                OperationName = "RunFinished"
            }
        }
    };
            
            
    var webhookProperties = new Dictionary<string, string>();

    //Custom Properties to be received from Webhooks.
    webhookProperties.Add("resourceGroupName", _resourceGroupName);
    webhookProperties.Add("datafactoryName", datafactoryName);
    webhookProperties.Add("alertName", alertName);
    webhookProperties.Add("customEmailIds", String.Join(",", emailIds));

    RuleAction resourceAlertRuleWebHookAction = new RuleWebhookAction
    {
        ServiceUri = ControlCenter.Common.ControlCenterConfiguration.Adf_AlertWebHookUri,
        Properties = webhookProperties
    };
    //Associate the RuleAction with the Rule  
    resourceAlertRule.Actions = new List<RuleAction>() { };// resourceAlertRuleAction };
    resourceAlertRule.Actions.Add(resourceAlertRuleWebHookAction);
    resourceAlertRule.LastUpdatedTime = DateTime.UtcNow;

    //Commit the Alert configuration   
  AzureOperationResponse alertResponse = managementClient.AlertOperations.CreateOrUpdateRule(_resourceGroupName, new RuleCreateOrUpdateParameters() { Location = _credential.DefaultLocation, Properties = resourceAlertRule });