Skip to content

Commit e253084

Browse files
committed
Finished scenario
1 parent 65e7944 commit e253084

File tree

6 files changed

+842
-0
lines changed

6 files changed

+842
-0
lines changed

swift/example_code/sfn/hello/Sources/entry.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct ExampleCommand: ParsableCommand {
3333
let sfnConfig = try await SFNClient.SFNClientConfiguration(region: awsRegion)
3434
let sfnClient = SFNClient(config: sfnConfig)
3535

36+
// snippet-start:[swift.sfn.hello.ListStateMachines]
3637
do {
3738
let output = try await sfnClient.listStateMachines(
3839
input: ListStateMachinesInput(
@@ -52,6 +53,7 @@ struct ExampleCommand: ParsableCommand {
5253
} catch {
5354
print("*** Error fetching state machine list: \(error.localizedDescription)")
5455
}
56+
// snippet-end:[swift.sfn.hello.ListStateMachines]
5557
}
5658
}
5759

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// (swift-tools-version has two lines here because it needs to be the first
6+
// line in the file, but it should also appear in the snippet below)
7+
//
8+
// snippet-start:[swift.sfn.hello.package]
9+
// swift-tools-version: 5.9
10+
//
11+
// The swift-tools-version declares the minimum version of Swift required to
12+
// build this package.
13+
14+
import PackageDescription
15+
16+
let package = Package(
17+
name: "sfn-scenario",
18+
// Let Xcode know the minimum Apple platforms supported.
19+
platforms: [
20+
.macOS(.v13),
21+
.iOS(.v15)
22+
],
23+
dependencies: [
24+
// Dependencies declare other packages that this package depends on.
25+
.package(
26+
url: "https://github.com/awslabs/aws-sdk-swift",
27+
from: "1.0.0"),
28+
.package(
29+
url: "https://github.com/apple/swift-argument-parser.git",
30+
branch: "main"
31+
)
32+
],
33+
targets: [
34+
// Targets are the basic building blocks of a package, defining a module or a test suite.
35+
// Targets can depend on other targets in this package and products
36+
// from dependencies.
37+
.executableTarget(
38+
name: "sfn-scenario",
39+
dependencies: [
40+
.product(name: "AWSIAM", package: "aws-sdk-swift"),
41+
.product(name: "AWSSFN", package: "aws-sdk-swift"),
42+
.product(name: "ArgumentParser", package: "swift-argument-parser")
43+
],
44+
path: "Sources")
45+
46+
]
47+
)
48+
// snippet-end:[swift.sfn.hello.package]
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import Foundation
5+
import AWSSFN
6+
7+
/// Describes errors that occur on Step Functions activities.
8+
enum ActivityError: Error {
9+
/// The ARN is missing from the returned activity.
10+
case missingArnError
11+
/// The activity list is missing from the response.
12+
case missingActivityListError
13+
/// No matching activity was found.
14+
case activityNotFoundError
15+
16+
var errorDescription: String {
17+
switch self {
18+
case .missingArnError:
19+
return "The ARN is missing from the returned activity"
20+
case .missingActivityListError:
21+
return "The activity list is missing from the response"
22+
case .activityNotFoundError:
23+
return "No activity with the specified name was found"
24+
}
25+
}
26+
}
27+
28+
/// Manage a Step Functions activity.
29+
class Activity {
30+
let sfnClient: SFNClient
31+
let activityName: String
32+
var activityArn = ""
33+
34+
init(client: SFNClient, name: String) async throws {
35+
sfnClient = client
36+
self.activityName = name
37+
38+
try await self.findOrCreateActivity()
39+
}
40+
41+
/// Create a new Step Functions activity.
42+
///
43+
/// - Throws: `ActivityError` and appropriate AWS errors.
44+
private func createActivity() async throws {
45+
let output = try await sfnClient.createActivity(
46+
input: CreateActivityInput(name: activityName)
47+
)
48+
49+
guard let arn = output.activityArn else {
50+
throw ActivityError.missingArnError
51+
}
52+
53+
activityArn = arn
54+
}
55+
56+
/// Find an activity with the name specified when initializing the
57+
/// `Activity` object.
58+
///
59+
/// - Throws: `ActivityError` and appropriate AWS errors.
60+
private func findActivity() async throws {
61+
let pages = sfnClient.listActivitiesPaginated(
62+
input: ListActivitiesInput()
63+
)
64+
65+
for try await page in pages {
66+
guard let activities = page.activities else {
67+
throw ActivityError.missingActivityListError
68+
}
69+
70+
for activity in activities {
71+
if activity.name == activityName {
72+
guard let arn = activity.activityArn else {
73+
throw ActivityError.missingArnError
74+
}
75+
self.activityArn = arn
76+
}
77+
}
78+
}
79+
80+
throw ActivityError.activityNotFoundError
81+
}
82+
83+
/// Finds an existing activity with the name given when initializing
84+
/// the `Activity`. If one isn't found, a new one is created.
85+
///
86+
/// - Throws: `ActivityError` and appropriate AWS errors.
87+
private func findOrCreateActivity() async throws {
88+
do {
89+
try await findActivity()
90+
} catch {
91+
try await createActivity()
92+
}
93+
}
94+
95+
/// Delete the activity described by this object.
96+
public func delete() async {
97+
do {
98+
_ = try await sfnClient.deleteActivity(
99+
input: DeleteActivityInput(activityArn: activityArn)
100+
)
101+
} catch {
102+
print("*** Error deleting the activity: \(error.localizedDescription)")
103+
}
104+
}
105+
106+
/// Sends a task success notification to the activity.
107+
///
108+
/// - Parameters:
109+
/// - taskToken: The task's token.
110+
/// - response: The task response.
111+
///
112+
/// - Returns: `ActivityError` and appropriate AWS errors.
113+
public func sendTaskSuccess(taskToken: String, response: String) async -> Bool {
114+
do {
115+
_ = try await sfnClient.sendTaskSuccess(
116+
input: SendTaskSuccessInput(output: response, taskToken: taskToken)
117+
)
118+
119+
return true
120+
} catch {
121+
print("*** Error sending task success: \(error.localizedDescription)")
122+
return false
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)