Skip to content

Commit

Permalink
Complete fields of Event representation.
Browse files Browse the repository at this point in the history
Add AD Authentication.
  • Loading branch information
sjuarezgx committed Jul 20, 2023
1 parent 3b8aa35 commit 74ceb51
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
using Azure;
using Azure.Identity;
using Azure.Messaging;
using Azure.Messaging.EventGrid;
using GeneXus.Messaging.Common;
Expand All @@ -31,9 +31,23 @@ private void Initialize(GXService providerService)
ServiceSettings serviceSettings = new(PropertyConstants.EVENT_ROUTER, Name, providerService);
_endpoint = serviceSettings.GetEncryptedPropertyValue(PropertyConstants.URI_ENDPOINT);
_accessKey = serviceSettings.GetEncryptedPropertyValue(PropertyConstants.ACCESS_KEY);
_client = new EventGridPublisherClient(
new Uri(_endpoint),
new AzureKeyCredential(_accessKey));

if (!string.IsNullOrEmpty(_endpoint)) {
if (string.IsNullOrEmpty(_accessKey))

//Try using Active Directory authentication
_client = new EventGridPublisherClient(
new Uri(_endpoint),
new DefaultAzureCredential());

else

_client = new EventGridPublisherClient(
new Uri(_endpoint),
new AzureKeyCredential(_accessKey));
}
else
throw new Exception("Endpoint URI must be set.");
}
public override string GetName()
{
Expand All @@ -54,7 +68,7 @@ public bool SendEvent(GXCloudEvent gxCloudEvent, bool binaryData)
}
else
{
throw new Exception("SendEvent: There was an error at the Event Grid initialization.");
throw new Exception("There was an error at the Event Grid initialization.");
}
}
catch (AggregateException ae)
Expand All @@ -80,7 +94,7 @@ public bool SendEvents(IList<GXCloudEvent> gxCloudEvents, bool binaryData)
}
else
{
throw new Exception("SendEvents: There was an error at the Event Grid initialization.");
throw new Exception("There was an error at the Event Grid initialization.");
}
}
catch (AggregateException ae)
Expand Down Expand Up @@ -232,7 +246,7 @@ public bool GetMessageFromException(Exception ex, SdtMessages_Message msg)
{
try
{
HttpListenerException az_ex = (HttpListenerException)ex;
RequestFailedException az_ex = (RequestFailedException)ex;
msg.gxTpr_Id = az_ex.ErrorCode.ToString();
msg.gxTpr_Description = az_ex.Message;
return true;
Expand All @@ -255,7 +269,9 @@ private EventGridEvent ToEventGridSchema(GXEventGridSchema gxEventGridSchema, bo
evt.Id = gxEventGridSchema.id;
if (!string.IsNullOrEmpty(gxEventGridSchema.topic))
evt.Topic = gxEventGridSchema.topic;

if (gxEventGridSchema.eventtime != DateTime.MinValue)
evt.EventTime = gxEventGridSchema.eventtime;

return evt;
}
private CloudEvent ToCloudEvent(GXCloudEvent gxCloudEvent, bool isBinaryData)
Expand All @@ -264,9 +280,13 @@ private CloudEvent ToCloudEvent(GXCloudEvent gxCloudEvent, bool isBinaryData)
if (string.IsNullOrEmpty(gxCloudEvent.data))
evt = new CloudEvent(gxCloudEvent.source, gxCloudEvent.type, null);
else
{
{
if (!isBinaryData)
evt = new CloudEvent(gxCloudEvent.source, gxCloudEvent.type, gxCloudEvent.data);
{
if (string.IsNullOrEmpty(gxCloudEvent.datacontenttype))
gxCloudEvent.datacontenttype = "application/json";
evt = new CloudEvent(gxCloudEvent.source, gxCloudEvent.type, BinaryData.FromString(gxCloudEvent.data),gxCloudEvent.datacontenttype,CloudEventDataFormat.Json);
}
else
{
if (string.IsNullOrEmpty(gxCloudEvent.datacontenttype))
Expand All @@ -280,6 +300,8 @@ private CloudEvent ToCloudEvent(GXCloudEvent gxCloudEvent, bool isBinaryData)
evt.DataSchema = gxCloudEvent.dataschema;
if (!string.IsNullOrEmpty(gxCloudEvent.subject))
evt.Subject = gxCloudEvent.subject;
if (gxCloudEvent.time != DateTime.MinValue)
evt.Time = gxCloudEvent.time;
return evt;
}
#endregion
Expand All @@ -301,5 +323,12 @@ public class GXEventGridSchema
[DataMember]
public string dataversion { get; set; }

[DataMember]
public DateTime eventtime { get; set; }

[DataMember]
public string metadataversion { get; set; }


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace GeneXus.Messaging.GXAzureEventGrid
{
/// <summary>
/// Implementation of EventGridRouterProvider External Object.
/// </summary>
public class EventGridRouterProvider
{
public EventRouterProviderBase Connect(string endpoint, string accesskey, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
Expand All @@ -14,6 +17,19 @@ public EventRouterProviderBase Connect(string endpoint, string accesskey, out GX
{ PropertyConstants.EVENTROUTER_AZUREEG_ACCESS_KEY, accesskey }
};

EventRouterProviderBase evtRouterProvider = eventRouterProvider.Connect(PropertyConstants.AZUREEVENTGRID, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
errorMessages = errorMessagesConnect;
success = successConnect;
return evtRouterProvider;
}
public EventRouterProviderBase Connect(string endpoint, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
{
EventRouterProvider eventRouterProvider = new EventRouterProvider();
GXProperties properties = new GXProperties
{
{ PropertyConstants.EVENTROUTER_AZUREEG_ENDPOINT, endpoint },
};

EventRouterProviderBase evtRouterProvider = eventRouterProvider.Connect(PropertyConstants.AZUREEVENTGRID, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
errorMessages = errorMessagesConnect;
success = successConnect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.17.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GeneXus.Utils;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using GeneXus.Utils;

namespace GeneXus.Messaging.Common
{
Expand All @@ -20,6 +20,8 @@ public class GXCloudEvent : GxUserType
public string id { get; set; }
public string dataschema { get; set; }
public string subject { get; set; }
public string data_base64 { get; set; }
public DateTime time { get; set; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ private GXCloudEvent ToGXCloudEvent(GxUserType evt)
gxCloudEvent.id = evt.GetPropertyValue<string>("Id");
gxCloudEvent.subject = evt.GetPropertyValue<string>("Subject");
gxCloudEvent.dataschema = evt.GetPropertyValue<string>("Dataschema");
gxCloudEvent.data_base64 = evt.GetPropertyValue<string>("Data_base64");
gxCloudEvent.time = evt.GetPropertyValue<DateTime>("Time");
return gxCloudEvent;
}
return null;
Expand All @@ -187,7 +189,7 @@ private GXCloudEvent ToGXCloudEvent(GxUserType evt)
internal class ServiceFactory
{
private static IEventRouter eventRouter;
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GeneXus.Services.ServiceFactory));
private static readonly ILog log = LogManager.GetLogger(typeof(Services.ServiceFactory));

public static GXServices GetGXServices()
{
Expand Down

0 comments on commit 74ceb51

Please sign in to comment.