Пример #1
0
 protected void InitializeClients(MockContext context)
 {
     if (!m_initialized)
     {
         lock (m_lock)
         {
             try
             {
                 if (!m_initialized)
                 {
                     resourceManagementClient = EventGridManagementHelper.GetResourceManagementClient(context, new RecordedDelegatingHandler {
                         StatusCodeToReturn = HttpStatusCode.OK
                     });
                     eventGridManagementClient = EventGridManagementHelper.GetEventGridManagementClient(context, new RecordedDelegatingHandler {
                         StatusCodeToReturn = HttpStatusCode.OK
                     });
                 }
             }
             catch (Exception ex)
             {
                 Console.Write(ex);
                 throw;
             }
         }
     }
 }
        public void PublishEventsToTopic()
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                this.InitializeClients(context);

                var location = this.ResourceManagementClient.GetLocationFromProvider();

                var resourceGroup = this.ResourceManagementClient.TryGetResourceGroup(location);
                if (string.IsNullOrWhiteSpace(resourceGroup))
                {
                    resourceGroup = TestUtilities.GenerateName(EventGridManagementHelper.ResourceGroupPrefix);
                    this.ResourceManagementClient.TryRegisterResourceGroup(location, resourceGroup);
                }

                var topicName = TestUtilities.GenerateName(EventGridManagementHelper.TopicPrefix);

                var originalTagsDictionary = new Dictionary <string, string>()
                {
                    { "originalTag1", "originalValue1" },
                    { "originalTag2", "originalValue2" }
                };

                Topic topic = new Topic()
                {
                    Location = location,
                    Tags     = originalTagsDictionary
                };

                var createTopicResponse = this.EventGridManagementClient.Topics.CreateOrUpdate(resourceGroup, topicName, topic);

                Assert.NotNull(createTopicResponse);
                Assert.Equal(createTopicResponse.Name, topicName);

                TestUtilities.Wait(TimeSpan.FromSeconds(60));

                // Get the topic key
                TopicSharedAccessKeys keys = this.EventGridManagementClient.Topics.ListSharedAccessKeys(resourceGroup, topicName);

                // Publish events to topic
                string           topicHostname    = new Uri(createTopicResponse.Endpoint).Host;
                TopicCredentials topicCredentials = new TopicCredentials(keys.Key1);

                EventGridClient client = EventGridManagementHelper.GetEventGridClient(
                    context,
                    topicCredentials,
                    new RecordedDelegatingHandler {
                    StatusCodeToReturn = HttpStatusCode.OK
                });

                Console.WriteLine("Publishing to Azure Event Grid");
                client.PublishEventsAsync(topicHostname, GetEventsList()).GetAwaiter().GetResult();
                Console.WriteLine("Published successfully!");

                // Delete topic
                this.EventGridManagementClient.Topics.Delete(resourceGroup, topicName);
            }
        }
Пример #3
0
 protected void InitializeClients(MockContext context)
 {
     if (!m_initialized)
     {
         lock (m_lock)
         {
             if (!m_initialized)
             {
                 resourceManagementClient = EventGridManagementHelper.GetResourceManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
                 eventGridManagementClient = EventGridManagementHelper.GetEventGridManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
             }
         }
     }
 }