public EventSubscription CreateEventSubscription( string scope, string eventSubscriptionName, string endpoint, string endpointType, string subjectBeginsWith, string subjectEndsWith, bool isSubjectCaseSensitive, string[] includedEventTypes, string[] labels) { EventSubscription eventSubscription = new EventSubscription(); EventSubscriptionDestination destination = null; const string WebHookEventSubscriptionDestination = "webhook"; const string EventHubEventSubscriptionDestination = "eventhub"; if (string.IsNullOrEmpty(endpointType) || string.Equals(endpointType, WebHookEventSubscriptionDestination, StringComparison.OrdinalIgnoreCase)) { destination = new WebHookEventSubscriptionDestination() { EndpointUrl = endpoint }; } else if (string.Equals(endpointType, EventHubEventSubscriptionDestination, StringComparison.OrdinalIgnoreCase)) { destination = new EventHubEventSubscriptionDestination() { ResourceId = endpoint }; } else { throw new ArgumentNullException(nameof(endpointType), "EndpointType should be WebHook or EventHub"); } if (includedEventTypes == null) { includedEventTypes = new string[1]; includedEventTypes[0] = "All"; } var filter = new EventSubscriptionFilter() { SubjectBeginsWith = subjectBeginsWith, SubjectEndsWith = subjectEndsWith, IsSubjectCaseSensitive = isSubjectCaseSensitive, IncludedEventTypes = new List <string>(includedEventTypes) }; eventSubscription.Destination = destination; eventSubscription.Filter = filter; if (labels != null) { eventSubscription.Labels = new List <string>(labels); } return(this.Client.EventSubscriptions.Create(scope, eventSubscriptionName, eventSubscription)); }
public async Task <EventSubscription> SubscribeEvent( string credentialsAzureSubscriptionId, string credentialsTenantId, string credentialsClientId, string credentialsClientSecret, string scope, string eventSubscriptionName, string id, string name, string subscriptionType, string topic, string provisioningState, List <string> labels, string eventDeliverySchema, int maxDeliveryAttempts, int eventTimeToLiveInMinutes, double expirationTimeInHours, string endpointUrl, int?maxEventsPerBatch) { await Task.Delay(0); try { EventGridManagementClient managementClient = new EventGridManagementClient(credentials: new CustomLoginCredentials(credentialsTenantId, credentialsClientId, credentialsClientSecret)); managementClient.SubscriptionId = credentialsAzureSubscriptionId; EventSubscriptionFilter filter = null; DateTime? expirationTime = DateTime.UtcNow.AddHours(expirationTimeInHours); RetryPolicy retryPolicy = new RetryPolicy(maxDeliveryAttempts: maxDeliveryAttempts, eventTimeToLiveInMinutes: eventTimeToLiveInMinutes); DeadLetterDestination deadLetterDestination = null; WebHookEventSubscriptionDestination destination = new WebHookEventSubscriptionDestination(endpointUrl: endpointUrl, maxEventsPerBatch: maxEventsPerBatch); EventSubscription eventSubscriptionInfo = new EventSubscription( id: id, name: name, type: subscriptionType, topic: topic, provisioningState: provisioningState, destination: destination, filter: filter, labels: labels, expirationTimeUtc: expirationTime, eventDeliverySchema: eventDeliverySchema, retryPolicy: retryPolicy, deadLetterDestination: deadLetterDestination); EventSubscription createdSubscription = await managementClient.EventSubscriptions.CreateOrUpdateAsync( scope : scope, eventSubscriptionName : eventSubscriptionName, eventSubscriptionInfo : eventSubscriptionInfo); return(createdSubscription); } catch (Exception ex) { _logger?.LogError($"Unknown Exception. Type: {ex.GetType().ToString()} ; Message: {ex.Message} ; Details: {ex.ToString()}"); return(null); } }
public override void ExecuteCmdlet() { if (string.IsNullOrEmpty(this.EventSubscriptionName)) { // This can happen with the InputObject parameter set where the // event subscription name needs to be determined based on the piped in // EventSubscriptionObject if (this.InputObject == null) { throw new Exception("Unexpected condition: Event Subscription name cannot be determined."); } this.EventSubscriptionName = this.InputObject.EventSubscriptionName; } if (this.ShouldProcess(this.EventSubscriptionName, $"Update existing Event Grid subscription {this.EventSubscriptionName}")) { string scope; RetryPolicy retryPolicy = null; int? maxEventsPerBatch = null; int? preferredBatchSizeInKiloByte = null; string aadAppIdOrUri = string.Empty; string aadTenantId = string.Empty; if (!string.IsNullOrEmpty(this.ResourceId)) { scope = this.ResourceId; } else if (this.InputObject != null) { scope = this.InputObject.Topic; } else { // ResourceID not specified, build the scope for the event subscription for either the // subscription, or resource group, or custom topic depending on which of the parameters are provided. scope = EventGridUtils.GetScope( this.DefaultContext.Subscription.Id, this.ResourceGroupName, this.TopicName, this.DomainName, this.DomainTopicName); } EventSubscription existingEventSubscription = this.Client.GetEventSubscription(scope, this.EventSubscriptionName); if (existingEventSubscription == null) { throw new Exception($"Cannot find an existing event subscription with name {this.EventSubscriptionName}."); } if (this.IsParameterBound(c => c.MaxDeliveryAttempt) || this.IsParameterBound(c => c.EventTtl)) { retryPolicy = new RetryPolicy(existingEventSubscription.RetryPolicy?.MaxDeliveryAttempts, existingEventSubscription.RetryPolicy?.EventTimeToLiveInMinutes); // Only override the new values if any. if (this.IsParameterBound(c => c.MaxDeliveryAttempt)) { retryPolicy.MaxDeliveryAttempts = this.MaxDeliveryAttempt; } if (this.IsParameterBound(c => c.EventTtl)) { retryPolicy.EventTimeToLiveInMinutes = this.EventTtl; } } else { retryPolicy = existingEventSubscription.RetryPolicy; } if (string.Equals(this.EndpointType, EventGridConstants.Webhook, StringComparison.OrdinalIgnoreCase)) { WebHookEventSubscriptionDestination dest = existingEventSubscription.Destination as WebHookEventSubscriptionDestination; if (dest != null) { maxEventsPerBatch = this.IsParameterBound(c => c.MaxEventsPerBatch) ? (int?)this.MaxEventsPerBatch : dest.MaxEventsPerBatch.HasValue ? dest.MaxEventsPerBatch : null; preferredBatchSizeInKiloByte = this.IsParameterBound(c => c.PreferredBatchSizeInKiloByte) ? (int?)this.PreferredBatchSizeInKiloByte : dest.PreferredBatchSizeInKilobytes.HasValue ? dest.PreferredBatchSizeInKilobytes : null; aadAppIdOrUri = this.IsParameterBound(c => c.AzureActiveDirectoryApplicationIdOrUri) ? this.AzureActiveDirectoryApplicationIdOrUri : dest.AzureActiveDirectoryApplicationIdOrUri; aadTenantId = this.IsParameterBound(c => c.AzureActiveDirectoryTenantId) ? this.AzureActiveDirectoryTenantId : dest.AzureActiveDirectoryTenantId; } else { maxEventsPerBatch = this.IsParameterBound(c => c.MaxEventsPerBatch) ? (int?)this.MaxEventsPerBatch : null; preferredBatchSizeInKiloByte = this.IsParameterBound(c => c.PreferredBatchSizeInKiloByte) ? (int?)this.PreferredBatchSizeInKiloByte : null; aadAppIdOrUri = this.IsParameterBound(c => c.AzureActiveDirectoryApplicationIdOrUri) ? this.AzureActiveDirectoryApplicationIdOrUri : string.Empty; aadTenantId = this.IsParameterBound(c => c.AzureActiveDirectoryTenantId) ? this.AzureActiveDirectoryTenantId : string.Empty; } } else { if (this.IsParameterBound(c => c.MaxEventsPerBatch) || this.IsParameterBound(c => c.PreferredBatchSizeInKiloByte)) { throw new ArgumentException("MaxEventsPerBatch and PreferredBatchSizeInKiloByte are supported when EndpointType is webhook only."); } if (this.IsParameterBound(c => c.AzureActiveDirectoryApplicationIdOrUri) || this.IsParameterBound(c => c.AzureActiveDirectoryTenantId)) { throw new ArgumentException("AzureActiveDirectoryApplicationIdOrUri and AzureActiveDirectoryTenantId are supported when EndpointType is webhook only."); } } if (EventGridUtils.ShouldShowEventSubscriptionWarningMessage(this.Endpoint, this.EndpointType)) { WriteWarning(EventGridConstants.EventSubscriptionHandshakeValidationMessage); } if (this.IncludedEventType != null && this.IncludedEventType.Length == 1 && string.Equals(this.IncludedEventType[0], "All", StringComparison.OrdinalIgnoreCase)) { // Show Warning message for user this.IncludedEventType = null; WriteWarning(EventGridConstants.IncludedEventTypeDeprecationMessage); } EventSubscription eventSubscription = this.Client.UpdateEventSubscription( scope: scope, eventSubscriptionName: this.EventSubscriptionName, endpoint: this.Endpoint, endpointType: this.EndpointType, subjectBeginsWith: this.SubjectBeginsWith ?? existingEventSubscription.Filter.SubjectBeginsWith, subjectEndsWith: this.SubjectEndsWith ?? existingEventSubscription.Filter.SubjectEndsWith, isSubjectCaseSensitive: existingEventSubscription.Filter.IsSubjectCaseSensitive, includedEventTypes: this.IncludedEventType, labels: this.Label, retryPolicy: retryPolicy, deadLetterEndpoint: this.DeadLetterEndpoint, expirationDate: this.ExpirationDate, advancedFilter: this.AdvancedFilter, maxEventsPerBatch: maxEventsPerBatch, preferredBatchSizeInKiloByte: preferredBatchSizeInKiloByte, aadAppIdOrUri: aadAppIdOrUri, aadTenantId: aadTenantId); PSEventSubscription psEventSubscription = new PSEventSubscription(eventSubscription); this.WriteObject(psEventSubscription); } }
public EventSubscription CreateEventSubscription( string scope, string eventSubscriptionName, string endpoint, string endpointType, string subjectBeginsWith, string subjectEndsWith, bool isSubjectCaseSensitive, string[] includedEventTypes, string[] labels, RetryPolicy retryPolicy, string deadLetterEndpoint) { EventSubscription eventSubscription = new EventSubscription(); EventSubscriptionDestination destination = null; if (string.IsNullOrEmpty(endpointType) || string.Equals(endpointType, EventGridConstants.Webhook, StringComparison.OrdinalIgnoreCase)) { destination = new WebHookEventSubscriptionDestination() { EndpointUrl = endpoint }; } else if (string.Equals(endpointType, EventGridConstants.EventHub, StringComparison.OrdinalIgnoreCase)) { destination = new EventHubEventSubscriptionDestination() { ResourceId = endpoint }; } else if (string.Equals(endpointType, EventGridConstants.StorageQueue, StringComparison.OrdinalIgnoreCase)) { destination = this.GetStorageQueueEventSubscriptionDestinationFromEndpoint(endpoint); } else if (string.Equals(endpointType, EventGridConstants.HybridConnection, StringComparison.OrdinalIgnoreCase)) { destination = new HybridConnectionEventSubscriptionDestination() { ResourceId = endpoint }; } else { throw new ArgumentNullException(nameof(endpointType), "Invalid EndpointType. Allowed values are WebHook, EventHub, StorageQueue or HybridConnection"); } if (includedEventTypes == null) { includedEventTypes = new string[1]; includedEventTypes[0] = "All"; } eventSubscription.Destination = destination; var filter = new EventSubscriptionFilter() { SubjectBeginsWith = subjectBeginsWith, SubjectEndsWith = subjectEndsWith, IsSubjectCaseSensitive = isSubjectCaseSensitive, IncludedEventTypes = new List <string>(includedEventTypes) }; eventSubscription.Filter = filter; if (labels != null) { eventSubscription.Labels = new List <string>(labels); } if (retryPolicy != null) { eventSubscription.RetryPolicy = retryPolicy; } if (!string.IsNullOrEmpty(deadLetterEndpoint)) { eventSubscription.DeadLetterDestination = this.GetStorageBlobDeadLetterDestinationFromEndPoint(deadLetterEndpoint); } return(this.Client.EventSubscriptions.CreateOrUpdate(scope, eventSubscriptionName, eventSubscription)); }