Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduled events #329

Merged
merged 4 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Libraries/Libraries.sln
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerShellTests", "test\Pow
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerShellScriptsAsFunctions", "test\TestPowerShellFunctions\PowerShellScriptsAsFunctions\PowerShellScriptsAsFunctions.csproj", "{0AD1E5D6-AC23-47C1-97BF-227007021B6F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.CloudWatchEvents", "src\Amazon.Lambda.CloudWatchEvents\Amazon.Lambda.CloudWatchEvents.csproj", "{AD96AA48-2E1A-4BBB-9329-E1E484172FE3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -203,6 +205,10 @@ Global
{0AD1E5D6-AC23-47C1-97BF-227007021B6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AD1E5D6-AC23-47C1-97BF-227007021B6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AD1E5D6-AC23-47C1-97BF-227007021B6F}.Release|Any CPU.Build.0 = Release|Any CPU
{AD96AA48-2E1A-4BBB-9329-E1E484172FE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD96AA48-2E1A-4BBB-9329-E1E484172FE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD96AA48-2E1A-4BBB-9329-E1E484172FE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD96AA48-2E1A-4BBB-9329-E1E484172FE3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -240,6 +246,7 @@ Global
{ADEC039D-0C34-4DA7-802B-6204FFE3F1F5} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
{997B1047-4361-4E6D-9850-F130EC188141} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
{0AD1E5D6-AC23-47C1-97BF-227007021B6F} = {ADEC039D-0C34-4DA7-802B-6204FFE3F1F5}
{AD96AA48-2E1A-4BBB-9329-E1E484172FE3} = {AAB54E74-20B1-42ED-BC3D-CE9F7BC7FD12}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {503678A4-B8D1-4486-8915-405A3E9CF0EB}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\..\buildtools\common.props" />

<PropertyGroup>
<TargetFramework>netstandard1.3</TargetFramework>
<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>
<Description>Amazon Lambda .NET Core support - CloudWatchEvents package.</Description>
<AssemblyTitle>Amazon.Lambda.CloudWatchEvents</AssemblyTitle>
<VersionPrefix>1.0.0</VersionPrefix>
<AssemblyName>Amazon.Lambda.CloudWatchEvents</AssemblyName>
<PackageId>Amazon.Lambda.CloudWatchEvents</PackageId>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime" Version="4.1.0" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions Libraries/src/Amazon.Lambda.CloudWatchEvents/CloudWatchEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace Amazon.Lambda.CloudWatchEvents
{
using System;
using System.Collections.Generic;

/// <summary>
/// AWS CloudWatch event
/// The contents of the detail top-level field are different depending on which service generated the event and what the event is.
/// The combination of the source and detail-type fields serves to identify the fields and values found in the detail field.
/// Complete list of events that inherit this interface: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html
/// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html
/// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html
/// </summary>
public class CloudWatchEvent<T>
{
/// <summary>
/// By default, this is set to 0 (zero) in all events.
/// </summary>
public string Version { get; set; }

/// <summary>
/// The 12-digit number identifying an AWS account.
/// </summary>
public string Account { get; set; }

/// <summary>
/// Identifies the AWS region where the event originated.
/// </summary>
public string Region { get; set; }

/// <summary>
/// A JSON object, whose content is at the discretion of the service originating the event.
/// The detail content in the example above is very simple, just two fields.
/// AWS API call events have detail objects with around 50 fields nested several levels deep.
/// </summary>
public T Detail { get; set; }

/// <summary>
/// Identifies, in combination with the source field, the fields and values that appear in the detail field.
/// For example, ScheduledEvent will be null
/// For example, ECSEvent could be "ECS Container Instance State Change" or "ECS Task State Change"
/// </summary>
public string DetailType { get; set; }

/// <summary>
/// Identifies the service that sourced the event.
/// All events sourced from within AWS begin with "aws."
/// Customer-generated events can have any value here, as long as it doesn't begin with "aws."
/// We recommend the use of Java package-name style reverse domain-name strings.
/// For example, ScheduledEvent will be "aws.events"
/// For example, ECSEvent will be "aws.ecs"
/// </summary>
public string Source { get; set; }

/// <summary>
/// The event timestamp, which can be specified by the service originating the event.
/// If the event spans a time interval, the service might choose to report the start time,
/// so this value can be noticeably before the time the event is actually received.
/// </summary>
public DateTime Time { get; set; }

/// <summary>
/// A unique value is generated for every event.
/// This can be helpful in tracing events as they move through rules to targets, and are processed.
/// </summary>
public string Id { get; set; }

/// <summary>
/// This JSON array contains ARNs that identify resources that are involved in the event.
/// Inclusion of these ARNs is at the discretion of the service.
/// For example, Amazon EC2 instance state-changes include Amazon EC2 instance ARNs, Auto Scaling events
/// include ARNs for both instances and Auto Scaling groups, but API calls with AWS CloudTrail do not
/// include resource ARNs.
/// </summary>
public List<string> Resources { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Amazon.Lambda.CloudWatchEvents.ScheduledEvents
{

/// <summary>
/// AWS Scheduled event detail
/// https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-scheduled-event
/// </summary>
public class Detail
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Amazon.Lambda.CloudWatchEvents.ScheduledEvents
{

/// <summary>
/// AWS Scheduled event
/// http://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules.html
/// http://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_example-events.html
/// https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-scheduled-event
/// </summary>
public class ScheduledEvent : CloudWatchEvent<Detail>
{

}
}
13 changes: 13 additions & 0 deletions Libraries/src/Amazon.Lambda.Serialization.Json/AwsResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Amazon.Lambda.Serialization.Json
{
Expand Down Expand Up @@ -80,6 +81,18 @@ protected override IList<JsonProperty> CreateProperties(Type type, MemberSeriali
}
}
}
else if (type.FullName.StartsWith("Amazon.Lambda.CloudWatchEvents.")
&& (type.GetTypeInfo().BaseType?.FullName?.StartsWith("Amazon.Lambda.CloudWatchEvents.CloudWatchEvent`",
StringComparison.Ordinal) ?? false))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("DetailType", StringComparison.Ordinal))
{
property.PropertyName = "detail-type";
}
}
}

return properties;
}
Expand Down
28 changes: 28 additions & 0 deletions Libraries/test/EventsTests/EventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Amazon.Lambda.Tests
using Amazon.Lambda.LexEvents;
using Amazon.Lambda.KinesisFirehoseEvents;
using Amazon.Lambda.KinesisAnalyticsEvents;
using Amazon.Lambda.CloudWatchEvents.ScheduledEvents;

using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -780,6 +781,33 @@ private string MemoryStreamToBase64String(MemoryStream ms)
{
var data = ms.ToArray();
return Convert.ToBase64String(data);
}

[Fact]
public void ScheduledEventTest()
{
using (var fileStream = File.OpenRead("scheduled-event.json"))
{
var serializer = new JsonSerializer();
var scheduledEvent = serializer.Deserialize<ScheduledEvent>(fileStream);
Assert.Equal(scheduledEvent.Version, "0");
Assert.Equal(scheduledEvent.Id, "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c");
Assert.Equal(scheduledEvent.DetailType, "Scheduled Event");
Assert.Equal(scheduledEvent.Source, "aws.events");
Assert.Equal(scheduledEvent.Account, "123456789012");
Assert.Equal(scheduledEvent.Time.ToUniversalTime(), DateTime.Parse("1970-01-01T00:00:00Z").ToUniversalTime());
Assert.Equal(scheduledEvent.Region, "us-east-1");
Assert.Equal(scheduledEvent.Resources.Count, 1);
Assert.Equal(scheduledEvent.Resources[0], "arn:aws:events:us-east-1:123456789012:rule/my-schedule");
Assert.IsType(typeof(Detail), scheduledEvent.Detail);

Handle(scheduledEvent);
}
}

private void Handle(ScheduledEvent scheduledEvent)
{
Console.WriteLine($"[{scheduledEvent.Source} {scheduledEvent.Time}] {scheduledEvent.DetailType}");
}
}
}
1 change: 1 addition & 0 deletions Libraries/test/EventsTests/EventsTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Amazon.Lambda.CloudWatchEvents\Amazon.Lambda.CloudWatchEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.CloudWatchLogsEvents\Amazon.Lambda.CloudWatchLogsEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.Core\Amazon.Lambda.Core.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.KinesisAnalyticsEvents\Amazon.Lambda.KinesisAnalyticsEvents.csproj" />
Expand Down
13 changes: 13 additions & 0 deletions Libraries/test/EventsTests/scheduled-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0",
"account": "123456789012",
"region": "us-east-1",
"detail": {},
"detail-type": "Scheduled Event",
"source": "aws.events",
"time": "1970-01-01T00:00:00Z",
"id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c",
"resources": [
"arn:aws:events:us-east-1:123456789012:rule/my-schedule"
]
}