Skip to content

Commit

Permalink
Add ability to purge SQS queue
Browse files Browse the repository at this point in the history
  • Loading branch information
normj committed Nov 19, 2018
1 parent 2931a7f commit 6873c9d
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 20 deletions.
Expand Up @@ -30,7 +30,7 @@ static void Main(string[] args)
command.HelpOption("-?|-h|--help");
var profileOption = command.Option("-p|--profile <profile>",
"The AWS profile identifing the AWS account to use.",
"The AWS profile identifying the AWS account to use.",
CommandOptionType.SingleValue);
var regionOption = command.Option("-r|--region <region>",
"The AWS region to use.",
Expand All @@ -54,7 +54,7 @@ static void Main(string[] args)
command.HelpOption("-?|-h|--help");
var profileOption = command.Option("-p|--profile <profile>",
"The AWS profile identifing the AWS account to use.",
"The AWS profile identifying the AWS account to use.",
CommandOptionType.SingleValue);
var regionOption = command.Option("-r|--region <region>",
"The AWS region to use.",
Expand All @@ -81,7 +81,7 @@ static void Main(string[] args)
command.HelpOption("-?|-h|--help");
var profileOption = command.Option("-p|--profile <profile>",
"The AWS profile identifing the AWS account to use.",
"The AWS profile identifying the AWS account to use.",
CommandOptionType.SingleValue);
var regionOption = command.Option("-r|--region <region>",
"The AWS region to use.",
Expand All @@ -104,7 +104,34 @@ static void Main(string[] args)
return -1;
}
});
});
});

app.Command("purge-queue", (command) => {
command.Description = "Purge messages from SQS queue.";
command.HelpOption("-?|-h|--help");
var profileOption = command.Option("-p|--profile <profile>",
"The AWS profile identifying the AWS account to use.",
CommandOptionType.SingleValue);
var regionOption = command.Option("-r|--region <region>",
"The AWS region to use.",
CommandOptionType.SingleValue);
var queueOption = command.Option("-q|--queue <queue-url>",
"The SQS queue url to read a message from.",
CommandOptionType.SingleValue);
command.OnExecute(() => {
try
{
new PurgeQueueCommand(profileOption.Value(), regionOption.Value(), queueOption.Value()).ExecuteAsync().Wait();
return 0;
}
catch (Exception)
{
return -1;
}
});
});

app.Execute(args);
}
Expand Down
@@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using Amazon.SQS;
using Amazon.SQS.Model;

namespace Amazon.Lambda.TestTool.ExternalCommands
{
public class PurgeQueueCommand
{

string Profile { get; }
string Region { get; }
private string QueueUrl { get; }

public PurgeQueueCommand(string profile, string region, string queueUrl)
{
this.Profile = profile;
this.Region = region;
this.QueueUrl = queueUrl;
}

public async Task ExecuteAsync()
{
try
{
var creds = Utils.GetCredentials(this.Profile);
using (var client = new AmazonSQSClient(creds, RegionEndpoint.GetBySystemName(this.Region)))
{
var request = new PurgeQueueRequest()
{
QueueUrl = this.QueueUrl
};
await client.PurgeQueueAsync(request);
}
}
catch (Exception e)
{
Console.Error.WriteLine("Error purging messages from queue: " + e.Message);
throw;
}
}
}
}
Expand Up @@ -95,5 +95,19 @@ public void DeleteMessage(string profile, string region, string queueUrl, string
throw new Exception(results.StandardError);
}
}

public void PurgeQueue(string profile, string region, string queueUrl)
{
var wrapper = new AppWrapper("purge-queue", new List<string>{"-p", profile, "-r", region, "-q", queueUrl});

var results = wrapper.Execute();

if (results.ExitCode != 0)
{
throw new Exception(results.StandardError);
}

}

}
}
Expand Up @@ -65,6 +65,13 @@ public void StopDlqMonitor()
}
}

[HttpPost("purge")]
public void PurgeDlq([FromBody] PurgeDlqModel model)
{
var manager = new ExternalCommandManager();
manager.PurgeQueue(model.Profile, model.Region, model.QueueUrl);
}

[HttpGet("is-running")]
public bool IsRunning()
{
Expand Down
@@ -0,0 +1,9 @@
namespace Amazon.Lambda.TestTool.WebTester.Models
{
public class PurgeDlqModel
{
public string Profile { get; set; }
public string Region { get; set; }
public string QueueUrl { get; set; }
}
}
Expand Up @@ -10,6 +10,11 @@
to how the AWS Lambda service would run .NET code but there are intended differences to make debugging simple, for example the host OS.
For that reason, this testing tool cannot help with diagnosing platform specific issues.
</p>
<p>
<b>Tip:</b> If a Lambda function using the default serializer, Amazon.Lambda.Serialization.Json, is deployed with the environment variable <b>LAMBDA_NET_SERIALIZER_DEBUG</b> set to <b>true</b>
the JSON input for the Lambda function will be written to CloudWatch Logs. The captured JSON can then be used in this tool to step
through the code.
</p>

<!-- Select Config File and Function Handler -->
<div class="row">
Expand Down
Expand Up @@ -13,16 +13,21 @@
</div>

<div class="row mt-3">
<div class="form-group" id="start-monitor-div">
<button class="btn btn-primary" onclick="onStartMonitoring()">Start Monitoring</button>
</div>
<div class="form-group collapse" id="stop-monitor-div">
<i class="fa fa-spinner fa-spin" style="font-size: 24px; margin: 5px; "></i>
<button class="btn btn-secondary" onclick="onStopMonitoring()">
Stop Monitoring
</button>
<div class="col-sm-9">
<div class="form-group" id="start-monitor-div">
<button class="btn btn-primary btn-sm" onclick="onStartMonitoring()">Start Monitoring</button>
</div>
<div class="form-group collapse" id="stop-monitor-div">
<i class="fa fa-spinner fa-spin" style="font-size: 24px; margin: 5px;"></i>
<button class="btn btn-secondary btn-sm" onclick="onStopMonitoring()">
Stop Monitoring
</button>
</div>
<div class="alert alert-warning collapse" style="margin-left: 10px" role="alert" id="monitor-error-msg-div">
</div>
</div>
<div class="alert alert-warning collapse" style="margin-left:10px" role="alert" id="monitor-error-msg-div">
<div class="form-group float-right col-sm-3" id="purge-dlq-div">
<button class="btn btn-danger float-right btn-sm" onclick="onPurgeDlqClick()">Purge Queue</button>
</div>
</div>
<div>
Expand Down Expand Up @@ -60,5 +65,44 @@
</div>
</div>
</div>

<div id="dlq-confirm-purge-dialog" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Purge</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete all messages from the SQS queue?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="onConfirmedPurgeDlqClick()">Ok</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>

<div id="dlq-no-queue-selected-dialog" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Error</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>There is no queue selected to perform this action.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Ok</button>
</div>
</div>
</div>
</div>
</div>

Expand Up @@ -121,6 +121,11 @@ function onStartMonitoring() {
config.Function = $("#functions-picker").val();
config.QueueUrl = $("#aws-dlq-queues").val();

if(!config.QueueUrl) {
$('#dlq-no-queue-selected-dialog').modal();
return;
}

$.post(
{
url: "webtester-api/MonitorDLQ/start",
Expand Down Expand Up @@ -198,3 +203,35 @@ function showMonitorErrorMessage(errorMessage) {
$("#monitor-error-msg-div").append(`<div>${errorMessage}</div>`);
$("#monitor-error-msg-div").show();
}

function onPurgeDlqClick() {

var queueUrl = $("#aws-dlq-queues").val();
if(!queueUrl) {
$('#dlq-no-queue-selected-dialog').modal();
return;
}

$('#dlq-confirm-purge-dialog').modal();
}

function onConfirmedPurgeDlqClick() {
$('#dlq-confirm-purge-dialog').modal('hide');

var config = {};
config.Profile = $("#aws-profile").val();
config.Region = $("#aws-region").val();
config.QueueUrl = $("#aws-dlq-queues").val();

$.post({
url: "webtester-api/MonitorDLQ/purge",
data: JSON.stringify(config),
contentType: "application/json",
success: function (data) {

}
})
.fail(function (data) {
alert('Error purging queue, is the .NET Lambda Test Tool been still running?');
});
}
9 changes: 2 additions & 7 deletions Tools/LambdaTestTool/aws-lambda-test-tool.sln
Expand Up @@ -15,14 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.TestTool.Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionSignatureExamples", "LambdaFunctions\FunctionSignatureExamples\FunctionSignatureExamples.csproj", "{4D8D6053-60E5-4AC3-8740-22E811452CD9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerlessTemplateExample", "LambdaFunctions\ServerlessTemplateExample\ServerlessTemplateExample.csproj", "{DAD2488C-F731-4719-A016-600BB5F25B48}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerlessTemplateExample", "LambdaFunctions\ServerlessTemplateExample\ServerlessTemplateExample.csproj", "{DAD2488C-F731-4719-A016-600BB5F25B48}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.TestTool.ExternalCommands", "Amazon.Lambda.TestTool.ExternalCommands\Amazon.Lambda.TestTool.ExternalCommands.csproj", "{AC05A239-3AFB-4694-9FF8-B9A0DAE19144}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F89015D0-DED5-4FFE-A37E-C1242D4DC792}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Amazon.Lambda.TestTool.ExternalCommands", "Amazon.Lambda.TestTool.ExternalCommands\Amazon.Lambda.TestTool.ExternalCommands.csproj", "{AC05A239-3AFB-4694-9FF8-B9A0DAE19144}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit 6873c9d

Please sign in to comment.