Skip to content

Commit 675a177

Browse files
committed
add support for SQS batchsize and enable properties
1 parent 29255ec commit 675a177

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

Sources/AWSLambdaDeploymentDescriptor/DeploymentDescriptor.swift

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -345,33 +345,34 @@ public enum HttpVerb: String, Encodable {
345345
-----------------------------------------------------------------------------------------*/
346346

347347
extension Resource {
348-
internal static func sqs(
349-
name: String = "SQSEvent",
350-
properties: SQSEventProperties
351-
) -> Resource<EventSourceType> {
348+
internal static func sqs(name: String = "SQSEvent", properties: SQSEventProperties) -> Resource<EventSourceType> {
352349

353350
return Resource<EventSourceType>(
354351
type: .sqs,
355352
properties: properties,
356353
name: name)
357354
}
358-
public static func sqs(
359-
name: String = "SQSEvent",
360-
queue queueRef: String
361-
) -> Resource<EventSourceType> {
355+
public static func sqs(name: String = "SQSEvent",
356+
queue queueRef: String,
357+
batchSize: Int = 10,
358+
enabled: Bool = true) -> Resource<EventSourceType> {
362359

363-
let properties = SQSEventProperties(byRef: queueRef)
360+
let properties = SQSEventProperties(byRef: queueRef,
361+
batchSize: batchSize,
362+
enabled: enabled)
364363
return Resource<EventSourceType>.sqs(
365364
name: name,
366365
properties: properties)
367366
}
368367

369-
public static func sqs(
370-
name: String = "SQSEvent",
371-
queue: Resource<ResourceType>
372-
) -> Resource<EventSourceType> {
368+
public static func sqs(name: String = "SQSEvent",
369+
queue: Resource<ResourceType>,
370+
batchSize: Int = 10,
371+
enabled: Bool = true) -> Resource<EventSourceType> {
373372

374-
let properties = SQSEventProperties(queue)
373+
let properties = SQSEventProperties(queue,
374+
batchSize: batchSize,
375+
enabled: enabled)
375376
return Resource<EventSourceType>.sqs(
376377
name: name,
377378
properties: properties)
@@ -384,26 +385,41 @@ public struct SQSEventProperties: SAMResourceProperties, Equatable {
384385

385386
public var queueByArn: String? = nil
386387
public var queue: Resource<ResourceType>? = nil
388+
public var batchSize : Int
389+
public var enabled : Bool
387390

388-
init(byRef ref: String) {
391+
init(byRef ref: String,
392+
batchSize: Int,
393+
enabled: Bool) {
389394

390395
// when the ref is an ARN, leave it as it, otherwise, create a queue resource and pass a reference to it
391396
if let arn = Arn(ref)?.arn {
392397
self.queueByArn = arn
393398
} else {
394399
let logicalName = Resource<EventSourceType>.logicalName(
395-
resourceType: "Queue",
396-
resourceName: ref)
400+
resourceType: "Queue",
401+
resourceName: ref)
397402
self.queue = Resource<ResourceType>.queue(
398-
name: logicalName,
399-
properties: SQSResourceProperties(queueName: ref))
400-
}
401-
403+
name: logicalName,
404+
properties: SQSResourceProperties(queueName: ref))
405+
}
406+
self.batchSize = batchSize
407+
self.enabled = enabled
408+
}
409+
410+
init(_ queue: Resource<ResourceType>,
411+
batchSize: Int,
412+
enabled: Bool) {
413+
414+
self.queue = queue
415+
self.batchSize = batchSize
416+
self.enabled = enabled
402417
}
403-
init(_ queue: Resource<ResourceType>) { self.queue = queue }
404418

405419
enum CodingKeys: String, CodingKey {
406420
case queue = "Queue"
421+
case batchSize = " BatchSize"
422+
case enabled = "Enabled"
407423
}
408424

409425
public func encode(to encoder: Encoder) throws {
@@ -418,6 +434,8 @@ public struct SQSEventProperties: SAMResourceProperties, Equatable {
418434
getAttIntrinsicFunction["Fn::GetAtt"] = [queue.name, "Arn"]
419435
try container.encode(getAttIntrinsicFunction, forKey: .queue)
420436
}
437+
try container.encode(batchSize, forKey: .batchSize)
438+
try container.encode(enabled, forKey: .enabled)
421439
}
422440
}
423441

Sources/AWSLambdaDeploymentDescriptor/DeploymentDescriptorBuilder.swift

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,45 @@ public struct Sqs {
251251
private let name: String
252252
private var queueRef: String? = nil
253253
private var queue: Queue? = nil
254+
public var batchSize : Int = 10
255+
public var enabled : Bool = true
256+
254257
public init(name: String = "SQSEvent") {
255258
self.name = name
256259
}
257-
public init(name: String = "SQSEvent", _ queue: String) {
260+
public init(name: String = "SQSEvent",
261+
_ queue: String,
262+
batchSize: Int = 10,
263+
enabled: Bool = true) {
258264
self.name = name
259265
self.queueRef = queue
266+
self.batchSize = batchSize
267+
self.enabled = enabled
260268
}
261-
public init(name: String = "SQSEvent", _ queue: Queue) {
269+
public init(name: String = "SQSEvent",
270+
_ queue: Queue,
271+
batchSize: Int = 10,
272+
enabled: Bool = true) {
262273
self.name = name
263274
self.queue = queue
275+
self.batchSize = batchSize
276+
self.enabled = enabled
264277
}
265278
public func queue(logicalName: String, physicalName: String) -> Sqs {
266279
let queue = Queue(logicalName: logicalName, physicalName: physicalName)
267280
return Sqs(name: self.name, queue)
268281
}
269282
internal func resource() -> Resource<EventSourceType> {
270283
if self.queue != nil {
271-
return Resource<EventSourceType>.sqs(name: self.name, queue: self.queue!.resource())
284+
return Resource<EventSourceType>.sqs(name: self.name,
285+
queue: self.queue!.resource(),
286+
batchSize: self.batchSize,
287+
enabled: self.enabled)
272288
} else if self.queueRef != nil {
273-
return Resource<EventSourceType>.sqs(name: self.name, queue: self.queueRef!)
289+
return Resource<EventSourceType>.sqs(name: self.name,
290+
queue: self.queueRef!,
291+
batchSize: self.batchSize,
292+
enabled: self.enabled)
274293
} else {
275294
fatalError("Either queue or queueRef muts have a value")
276295
}
@@ -376,6 +395,10 @@ private struct DeploymentDescriptorSerializer {
376395
format: SerializeFormat,
377396
to fileDesc: Int32 = 1
378397
) throws {
398+
399+
// do not output the deployment descriptor on stdout when running unit tests
400+
if Thread.current.isRunningXCTest { return }
401+
379402
guard let fd = fdopen(fileDesc, "w") else { return }
380403
switch format {
381404
case .json: fputs(deploymentDescriptor.toJSON(), fd)

Tests/AWSLambdaDeploymentDescriptorTests/DeploymentDescriptorTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ final class DeploymentDescriptorTest: XCTestCase {
2222
expected: String) -> Bool {
2323
// when
2424
let samJSON = deployment.toJSON()
25+
// print(samJSON)
26+
// print("===========")
27+
// print(expected)
2528

2629
// then
2730
return samJSON.contains(expected)
@@ -129,7 +132,7 @@ function","AWSTemplateFormatVersion":"2010-09-09","Resources":{"TestLambda":{"Ty
129132

130133
// given
131134
let expected = """
132-
"Resources":{"TestLambda":{"Type":"AWS::Serverless::Function","Properties":{"Runtime":"provided.al2","CodeUri":"\\/path\\/lambda.zip","Events":{"SQSEvent":{"Type":"SQS","Properties":{"Queue":"arn:aws:sqs:eu-central-1:012345678901:lambda-test"}}},"Handler":"Provided","AutoPublishAlias":"Live","Architectures":["\(Architectures.defaultArchitecture())"]}}}
135+
"Resources":{"TestLambda":{"Type":"AWS::Serverless::Function","Properties":{"Runtime":"provided.al2","CodeUri":"\\/path\\/lambda.zip","Events":{"SQSEvent":{"Type":"SQS","Properties":{"Queue":"arn:aws:sqs:eu-central-1:012345678901:lambda-test"," BatchSize":10,"Enabled":true}}},"Handler":"Provided","AutoPublishAlias":"Live","Architectures":["arm64"]}}}
133136
"""
134137

135138
let testDeployment = MockDeploymentDescriptor(withFunction: true,
@@ -146,7 +149,7 @@ function","AWSTemplateFormatVersion":"2010-09-09","Resources":{"TestLambda":{"Ty
146149
eventSource: [ .sqs(queue: "queue-lambda-test") ] )
147150

148151
let expected = """
149-
"Events":{"SQSEvent":{"Type":"SQS","Properties":{"Queue":{"Fn::GetAtt":["QueueQueueLambdaTest","Arn"]}}}}
152+
"Events":{"SQSEvent":{"Type":"SQS","Properties":{"Queue":{"Fn::GetAtt":["QueueQueueLambdaTest","Arn"]}," BatchSize":10,"Enabled":true}}}
150153
"""
151154
XCTAssertTrue(self.generateAndTestDeploymentDescriptor(deployment: testDeployment,
152155
expected: expected))

0 commit comments

Comments
 (0)