Skip to content

Commit fce99dd

Browse files
Implemented SNS topic information
1 parent 6360f20 commit fce99dd

File tree

5 files changed

+133
-5
lines changed

5 files changed

+133
-5
lines changed

GuiStack/Models/SNSCreateTopicModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public class SNSCreateTopicModel
1515
{
1616
public string TopicName { get; set; }
1717
public bool IsFifo { get; set; }
18+
public bool ContentBasedDeduplication { get; set; }
1819
}
1920
}

GuiStack/Models/SNSTopicInfo.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright © Vincent Bengtsson & Contributors 2022-2023
7+
* https://github.com/Visual-Vincent/GuiStack
8+
*/
9+
10+
using System;
11+
using Amazon;
12+
13+
namespace GuiStack.Models
14+
{
15+
public class SNSTopicInfo
16+
{
17+
public Arn TopicARN { get; set; }
18+
public int SubscriptionsConfirmed { get; set; }
19+
public int SubscriptionsDeleted { get; set; }
20+
public int SubscriptionsPending { get; set; }
21+
public bool FifoTopic { get; set; }
22+
public bool ContentBasedDeduplication { get; set; }
23+
}
24+
}

GuiStack/Pages/SNS/Index.cshtml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
@if(hasTopicArn)
2424
{
25-
<h1>@Model.Topic</h1>
25+
<h1>@Model.Topic.Split(':').Last()</h1>
2626
}
2727
else
2828
{
@@ -37,6 +37,10 @@ else
3737
<input type="checkbox" id="new-sns-topic-fifo-checkbox" class="fifo-checkbox" />
3838
<label for="new-sns-topic-fifo-checkbox">FIFO</label>
3939
</p>
40+
<p id="new-sns-topic-dedupe-checkbox-container" style="display: none">
41+
<input type="checkbox" id="new-sns-topic-dedupe-checkbox" class="fifo-checkbox" />
42+
<label for="new-sns-topic-dedupe-checkbox">Content-based deduplication</label>
43+
</p>
4044

4145
<div class="modal-buttons text-center">
4246
<button onclick="sns_CreateTopic()">Create</button>
@@ -63,8 +67,7 @@ else
6367
}
6468
else
6569
{
66-
// TODO:
67-
//await Html.RenderPartialAsync("~/Pages/SNS/_TopicInfo.cshtml", await Model.SNSRepository.GetTopicAttributesAsync(Model.Topic));
70+
await Html.RenderPartialAsync("~/Pages/SNS/_TopicInfo.cshtml", await Model.SNSRepository.GetTopicAttributesAsync(Model.Topic));
6871
}
6972
}
7073
catch(AmazonSimpleNotificationServiceException ex)
@@ -105,19 +108,29 @@ else
105108
@if(!hasTopicArn)
106109
{
107110
<script type="text/javascript">
111+
$("#new-sns-topic-fifo-checkbox").change(sns_NewTopicFifo_Change);
112+
113+
function sns_NewTopicFifo_Change(event)
114+
{
115+
var isFifo = event.currentTarget.checked;
116+
document.getElementById("new-sns-topic-dedupe-checkbox-container").style.display = isFifo ? "block" : "none";
117+
}
118+
108119
async function sns_CreateTopic()
109120
{
110121
try
111122
{
112123
var topicName = document.querySelector("#new-sns-topic-modal .name-textbox").value;
113124
var isFifo = document.getElementById("new-sns-topic-fifo-checkbox").checked;
125+
var dedupe = document.getElementById("new-sns-topic-dedupe-checkbox").checked;
114126
115127
var response = await fetch("@Url.Action("CreateTopic", "Topics")", {
116128
method: "PUT",
117129
headers: new Headers({ "Content-Type": "application/json" }),
118130
body: JSON.stringify({
119131
topicName: topicName,
120-
isFifo: isFifo
132+
isFifo: isFifo,
133+
contentBasedDeduplication: dedupe
121134
})
122135
});
123136
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
@*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright © Vincent Bengtsson & Contributors 2022-2023
7+
* https://github.com/Visual-Vincent/GuiStack
8+
*@
9+
10+
@using GuiStack.Models
11+
@using GuiStack.Extensions
12+
@model SNSTopicInfo
13+
14+
<table class="gs-info-table colored">
15+
<tbody>
16+
<tr>
17+
<td>Type</td>
18+
<td>@(Model.FifoTopic ? "FIFO" : "Standard")</td>
19+
</tr>
20+
<tr>
21+
<td>ARN</td>
22+
<td>@Model.TopicARN</td>
23+
</tr>
24+
<tr>
25+
<td>Content-based deduplication</td>
26+
<td>@(Model.FifoTopic ? (Model.ContentBasedDeduplication ? "Yes" : "No") : "N/A")</td>
27+
</tr>
28+
<tr>
29+
<td>Active subscriptions</td>
30+
<td>@Model.SubscriptionsConfirmed</td>
31+
</tr>
32+
</tbody>
33+
<tbody class="additional-info">
34+
<tr>
35+
<td>Pending subscriptions</td>
36+
<td>@Model.SubscriptionsPending</td>
37+
</tr>
38+
<tr>
39+
<td>Deleted subscriptions</td>
40+
<td>@Model.SubscriptionsDeleted</td>
41+
</tr>
42+
</tbody>
43+
<tbody>
44+
<tr class="expand-button text-center collapsed">
45+
<td colspan="2">
46+
<a no-href onclick="gsevent_InfoTable_ToggleButton_Click(event)">
47+
View more <i class="fa-solid fa-angles-down" style="font-size: 12px"></i>
48+
</a>
49+
</td>
50+
</tr>
51+
<tr class="expand-button text-center expanded">
52+
<td colspan="2">
53+
<a no-href onclick="gsevent_InfoTable_ToggleButton_Click(event)">
54+
View less <i class="fa-solid fa-angles-up" style="font-size: 12px"></i>
55+
</a>
56+
</td>
57+
</tr>
58+
</tbody>
59+
</table>

GuiStack/Repositories/ISNSRepository.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Collections.Generic;
1212
using System.Linq;
1313
using System.Threading.Tasks;
14+
using Amazon;
1415
using Amazon.SimpleNotificationService.Model;
1516
using GuiStack.Authentication.AWS;
1617
using GuiStack.Extensions;
@@ -22,6 +23,7 @@ public interface ISNSRepository
2223
{
2324
Task CreateTopicAsync(SNSCreateTopicModel model);
2425
Task<IEnumerable<SNSTopic>> GetTopicsAsync();
26+
Task<SNSTopicInfo> GetTopicAttributesAsync(string topicArn);
2527
Task DeleteTopicAsync(string topicArn);
2628
}
2729

@@ -43,7 +45,8 @@ public async Task CreateTopicAsync(SNSCreateTopicModel model)
4345
var response = await sns.CreateTopicAsync(new CreateTopicRequest() {
4446
Name = topicName,
4547
Attributes = new Dictionary<string, string>() {
46-
{ "FifoTopic", model.IsFifo.ToString().ToLower() }
48+
{ "FifoTopic", model.IsFifo.ToString().ToLower() },
49+
{ "ContentBasedDeduplication", model.IsFifo ? model.ContentBasedDeduplication.ToString().ToLower() : "false" }
4750
}
4851
});
4952

@@ -60,6 +63,34 @@ public async Task<IEnumerable<SNSTopic>> GetTopicsAsync()
6063
return response.Topics.Select(topic => new SNSTopic(topic.TopicArn));
6164
}
6265

66+
public async Task<SNSTopicInfo> GetTopicAttributesAsync(string topicName)
67+
{
68+
using var sns = authenticator.Authenticate();
69+
var topic = await sns.FindTopicAsync(topicName);
70+
71+
if(topic == null)
72+
throw new KeyNotFoundException($"Topic with name '{topicName}' was not found");
73+
74+
var response = await sns.GetTopicAttributesAsync(topic.TopicArn);
75+
76+
response.ThrowIfUnsuccessful("SNS");
77+
78+
var subscriptionsConfirmed = response.Attributes.GetValueOrDefault("SubscriptionsConfirmed");
79+
var subscriptionsDeleted = response.Attributes.GetValueOrDefault("SubscriptionsDeleted");
80+
var subscriptionsPending = response.Attributes.GetValueOrDefault("SubscriptionsPending");
81+
var fifoTopic = response.Attributes.GetValueOrDefault("FifoTopic") ?? "";
82+
var contentBasedDeduplication = response.Attributes.GetValueOrDefault("ContentBasedDeduplication") ?? "";
83+
84+
return new SNSTopicInfo() {
85+
TopicARN = Arn.Parse(topic.TopicArn),
86+
SubscriptionsConfirmed = int.TryParse(subscriptionsConfirmed ?? "", out var i) ? i : 0,
87+
SubscriptionsDeleted = int.TryParse(subscriptionsDeleted ?? "", out i) ? i : 0,
88+
SubscriptionsPending = int.TryParse(subscriptionsPending ?? "", out i) ? i : 0,
89+
FifoTopic = fifoTopic.Equals("true", StringComparison.OrdinalIgnoreCase),
90+
ContentBasedDeduplication = contentBasedDeduplication.Equals("true", StringComparison.OrdinalIgnoreCase)
91+
};
92+
}
93+
6394
public async Task DeleteTopicAsync(string topicArn)
6495
{
6596
if(string.IsNullOrWhiteSpace(topicArn))

0 commit comments

Comments
 (0)