Skip to content

Commit 7d5747e

Browse files
committed
Add SqsCommand class and enhance IntegrationManager to handle SQS commands
1 parent bd7bc86 commit 7d5747e

File tree

3 files changed

+160
-3
lines changed

3 files changed

+160
-3
lines changed

Modules/Intent.Modules.Integration.IaC.Shared.AwsSqs/IntegrationManager.cs

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public static IntegrationManager Instance
3131

3232
private readonly List<MessageInfo> _publishedMessages;
3333
private readonly List<MessageInfo> _subscribedMessages;
34+
private readonly List<CommandInfo> _sentCommands;
35+
private readonly List<CommandInfo> _receivedCommands;
3436

3537
private IntegrationManager(IApplication application)
3638
{
@@ -65,6 +67,31 @@ private IntegrationManager(IApplication application)
6567
.Select(sub => new MessageInfo(app.Id, app.Name, sub.Message, sub.Handler)))
6668
.Distinct()
6769
.ToList();
70+
71+
// Collect sent commands
72+
_sentCommands = applications
73+
.Where(app => app.Modules.Any(x => x.ModuleId == awsSqsModule))
74+
.SelectMany(app => application.MetadataManager
75+
.GetExplicitlySentIntegrationCommandModels(app.Id)
76+
.Select(command => new CommandInfo(app.Id, app.Name, command, null)))
77+
.Distinct()
78+
.ToList();
79+
80+
// Collect received commands
81+
_receivedCommands = applications
82+
.Where(app => app.Modules.Any(x => x.ModuleId == awsSqsModule))
83+
.SelectMany(app => application.MetadataManager
84+
.Services(app.Id)
85+
.GetIntegrationEventHandlerModels()
86+
.SelectMany(handler => handler.IntegrationCommandSubscriptions()
87+
.Select(sub => new
88+
{
89+
Command = sub.Element.AsIntegrationCommandModel(),
90+
Handler = handler
91+
}))
92+
.Select(sub => new CommandInfo(app.Id, app.Name, sub.Command, sub.Handler)))
93+
.Distinct()
94+
.ToList();
6895
}
6996

7097
public IReadOnlyList<SqsMessage> GetPublishedSqsMessages(string applicationId)
@@ -92,18 +119,51 @@ public IReadOnlyList<SqsMessage> GetSubscribedSqsMessages(string applicationId)
92119
.ToList();
93120
}
94121

122+
public IReadOnlyList<SqsCommand> GetPublishedSqsCommands(string applicationId)
123+
{
124+
return _sentCommands
125+
.Where(p => p.ApplicationId == applicationId)
126+
.Select(s => new SqsCommand(
127+
s.ApplicationId,
128+
s.ApplicationName,
129+
s.Command,
130+
SqsMethodType.Publish))
131+
.ToList();
132+
}
133+
134+
public IReadOnlyList<SqsCommand> GetSubscribedSqsCommands(string applicationId)
135+
{
136+
return _receivedCommands
137+
.Where(p => p.ApplicationId == applicationId)
138+
.DistinctBy(s => s.Command.Id)
139+
.Select(s => new SqsCommand(
140+
s.ApplicationId,
141+
s.ApplicationName,
142+
s.Command,
143+
SqsMethodType.Subscribe))
144+
.ToList();
145+
}
146+
95147
public IReadOnlyList<SqsItemBase> GetAggregatedPublishedSqsItems(string applicationId)
96148
{
97-
return GetPublishedSqsMessages(applicationId)
149+
var messages = GetPublishedSqsMessages(applicationId)
98150
.Cast<SqsItemBase>()
99151
.ToList();
152+
var commands = GetPublishedSqsCommands(applicationId)
153+
.Cast<SqsItemBase>()
154+
.ToList();
155+
return messages.Concat(commands).ToList();
100156
}
101157

102158
public IReadOnlyList<SqsItemBase> GetAggregatedSubscribedSqsItems(string applicationId)
103159
{
104-
return GetSubscribedSqsMessages(applicationId)
160+
var messages = GetSubscribedSqsMessages(applicationId)
105161
.Cast<SqsItemBase>()
106162
.ToList();
163+
var commands = GetSubscribedSqsCommands(applicationId)
164+
.Cast<SqsItemBase>()
165+
.ToList();
166+
return messages.Concat(commands).ToList();
107167
}
108168

109169
public IReadOnlyList<SqsItemBase> GetAggregatedSqsItems(string applicationId)
@@ -117,7 +177,7 @@ public IReadOnlyList<SqsItemBase> GetAggregatedSqsItems(string applicationId)
117177

118178
public IReadOnlyList<Subscription<SqsItemBase>> GetAggregatedSqsSubscriptions(string applicationId)
119179
{
120-
return _subscribedMessages
180+
var messageSubscriptions = _subscribedMessages
121181
.Where(message => message.ApplicationId == applicationId)
122182
.Select(message => new Subscription<SqsItemBase>(
123183
message.EventHandlerModel!,
@@ -127,6 +187,19 @@ public IReadOnlyList<Subscription<SqsItemBase>> GetAggregatedSqsSubscriptions(st
127187
message.Message,
128188
SqsMethodType.Subscribe)))
129189
.ToList();
190+
191+
var commandSubscriptions = _receivedCommands
192+
.Where(command => command.ApplicationId == applicationId)
193+
.Select(command => new Subscription<SqsItemBase>(
194+
command.EventHandlerModel!,
195+
new SqsCommand(
196+
applicationId,
197+
command.ApplicationName,
198+
command.Command,
199+
SqsMethodType.Subscribe)))
200+
.ToList();
201+
202+
return messageSubscriptions.Concat(commandSubscriptions).ToList();
130203
}
131204

132205
public record Subscription<TSubscriptionItem>(
@@ -138,4 +211,10 @@ private record MessageInfo(
138211
string ApplicationName,
139212
MessageModel Message,
140213
IntegrationEventHandlerModel? EventHandlerModel);
214+
215+
private record CommandInfo(
216+
string ApplicationId,
217+
string ApplicationName,
218+
IntegrationCommandModel Command,
219+
IntegrationEventHandlerModel? EventHandlerModel);
141220
}

Modules/Intent.Modules.Integration.IaC.Shared.AwsSqs/Intent.Modules.Integration.IaC.Shared.AwsSqs.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<Compile Include="$(MSBuildThisFileDirectory)IntegrationManager.cs" />
1313
<Compile Include="$(MSBuildThisFileDirectory)SqsItemBase.cs" />
1414
<Compile Include="$(MSBuildThisFileDirectory)SqsMessage.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)SqsCommand.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)SqsMethodType.cs" />
1617
</ItemGroup>
1718
</Project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using Intent.Aws.Sqs.Api;
3+
using Intent.Modelers.Eventing.Api;
4+
using Intent.Modules.Common.Templates;
5+
6+
namespace Intent.Modules.Integration.IaC.Shared.AwsSqs;
7+
8+
internal record SqsCommand : SqsItemBase
9+
{
10+
public SqsCommand(
11+
string applicationId,
12+
string applicationName,
13+
IntegrationCommandModel commandModel,
14+
SqsMethodType methodType)
15+
{
16+
ApplicationId = applicationId;
17+
ApplicationName = applicationName;
18+
CommandModel = commandModel;
19+
MethodType = methodType;
20+
QueueName = GetQueueName(commandModel);
21+
QueueConfigurationName = GetQueueConfigurationName(commandModel);
22+
}
23+
24+
public IntegrationCommandModel CommandModel { get; init; }
25+
26+
private static string GetQueueName(IntegrationCommandModel command)
27+
{
28+
// Check if AWS SQS stereotype is applied
29+
if (command.HasAwsSqs())
30+
{
31+
var stereotypeName = command.GetAwsSqs().QueueName();
32+
// Only use stereotype value if it's not empty
33+
if (!string.IsNullOrWhiteSpace(stereotypeName))
34+
{
35+
return stereotypeName;
36+
}
37+
}
38+
39+
// Default naming convention: kebab-case, remove common suffixes
40+
var resolvedName = command.Name;
41+
resolvedName = resolvedName.RemoveSuffix("IntegrationCommand", "Command");
42+
resolvedName = resolvedName.ToKebabCase();
43+
return resolvedName;
44+
}
45+
46+
private static string GetQueueConfigurationName(IntegrationCommandModel command)
47+
{
48+
const string prefix = "AwsSqs:";
49+
50+
if (command.HasAwsSqs())
51+
{
52+
var name = command.GetAwsSqs().QueueName();
53+
// Only use stereotype value if it's not empty
54+
if (!string.IsNullOrWhiteSpace(name))
55+
{
56+
return prefix + name.ToPascalCase() + ":QueueUrl";
57+
}
58+
}
59+
60+
// Default: PascalCase name
61+
var resolvedName = command.Name;
62+
resolvedName = resolvedName.RemoveSuffix("IntegrationCommand", "Command");
63+
resolvedName = resolvedName.ToPascalCase();
64+
resolvedName = resolvedName + ":QueueUrl";
65+
return prefix + resolvedName;
66+
}
67+
68+
public override string GetModelTypeName(IntentTemplateBase template)
69+
{
70+
return template.GetTypeName("Intent.Eventing.Contracts.IntegrationCommand", CommandModel);
71+
}
72+
73+
public override string GetSubscriberTypeName<T>(IntentTemplateBase<T> template)
74+
{
75+
return $"{template.GetTypeName("Intent.Eventing.Contracts.IntegrationEventHandlerInterface")}<{GetModelTypeName(template)}>";
76+
}
77+
}

0 commit comments

Comments
 (0)