Skip to content

Commit 884f8a0

Browse files
authored
Swift: Add Hello and Scenario examples for AWS Support (#7589)
Added AWS Support hello and scenario for Swift SDK Code examples written for this usecase look good and are merged into the main branch.
1 parent 3a9a4da commit 884f8a0

File tree

6 files changed

+1044
-0
lines changed

6 files changed

+1044
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Support code examples for the SDK for Swift
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Swift to work with AWS Support.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_Support provides support for users of Amazon Web Services._
11+
12+
## ⚠ Important
13+
14+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
15+
* Running the tests might result in charges to your AWS account.
16+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
17+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder.
27+
28+
29+
<!--custom.prerequisites.start-->
30+
<!--custom.prerequisites.end-->
31+
32+
### Get started
33+
34+
- [Hello Support](hello/Package.swift#L8) (`DescribeServices`)
35+
36+
37+
### Basics
38+
39+
Code examples that show you how to perform the essential operations within a service.
40+
41+
- [Learn the basics](scenario/Package.swift)
42+
43+
44+
### Single actions
45+
46+
Code excerpts that show you how to call individual service functions.
47+
48+
- [AddAttachmentsToSet](scenario/Sources/Scenario.swift#L463)
49+
- [AddCommunicationToCase](scenario/Sources/Scenario.swift#L512)
50+
- [CreateCase](scenario/Sources/Scenario.swift#L361)
51+
- [DescribeAttachment](scenario/Sources/Scenario.swift#L600)
52+
- [DescribeCases](scenario/Sources/Scenario.swift#L410)
53+
- [DescribeCommunications](scenario/Sources/Scenario.swift#L562)
54+
- [DescribeServices](scenario/Sources/Scenario.swift#L286)
55+
- [DescribeSeverityLevels](scenario/Sources/Scenario.swift#L322)
56+
- [ResolveCase](scenario/Sources/Scenario.swift#L640)
57+
58+
59+
<!--custom.examples.start-->
60+
<!--custom.examples.end-->
61+
62+
## Run the examples
63+
64+
### Instructions
65+
66+
To build any of these examples from a terminal window, navigate into its
67+
directory, then use the following command:
68+
69+
```
70+
$ swift build
71+
```
72+
73+
To build one of these examples in Xcode, navigate to the example's directory
74+
(such as the `ListUsers` directory, to build that example). Then type `xed.`
75+
to open the example directory in Xcode. You can then use standard Xcode build
76+
and run commands.
77+
78+
<!--custom.instructions.start-->
79+
<!--custom.instructions.end-->
80+
81+
#### Hello Support
82+
83+
This example shows you how to get started using Support.
84+
85+
86+
#### Learn the basics
87+
88+
This example shows you how to do the following:
89+
90+
- Get and display available services and severity levels for cases.
91+
- Create a support case using a selected service, category, and severity level.
92+
- Get and display a list of open cases for the current day.
93+
- Add an attachment set and a communication to the new case.
94+
- Describe the new attachment and communication for the case.
95+
- Resolve the case.
96+
- Get and display a list of resolved cases for the current day.
97+
98+
<!--custom.basic_prereqs.support_Scenario_GetStartedSupportCases.start-->
99+
<!--custom.basic_prereqs.support_Scenario_GetStartedSupportCases.end-->
100+
101+
102+
<!--custom.basics.support_Scenario_GetStartedSupportCases.start-->
103+
<!--custom.basics.support_Scenario_GetStartedSupportCases.end-->
104+
105+
106+
### Tests
107+
108+
⚠ Running tests might result in charges to your AWS account.
109+
110+
111+
To find instructions for running these tests, see the [README](../../README.md#Tests)
112+
in the `swift` folder.
113+
114+
115+
116+
<!--custom.tests.start-->
117+
<!--custom.tests.end-->
118+
119+
## Additional resources
120+
121+
- [Support User Guide](https://docs.aws.amazon.com/awssupport/latest/user/getting-started.html)
122+
- [Support API Reference](https://docs.aws.amazon.com/awssupport/latest/APIReference/welcome.html)
123+
- [SDK for Swift Support reference](https://sdk.amazonaws.com/swift/api/awssupport/latest/documentation/awssupport)
124+
125+
<!--custom.resources.start-->
126+
<!--custom.resources.end-->
127+
128+
---
129+
130+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
131+
132+
SPDX-License-Identifier: Apache-2.0
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.support.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: "hello-support",
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: "hello-support",
39+
dependencies: [
40+
.product(name: "AWSSupport", package: "aws-sdk-swift"),
41+
.product(name: "ArgumentParser", package: "swift-argument-parser")
42+
],
43+
path: "Sources")
44+
45+
]
46+
)
47+
// snippet-end:[swift.support.hello.package]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// snippet-start:[swift.support.hello]
5+
// An example that shows how to use the AWS SDK for Swift to perform a simple
6+
// operation using AWS Support.
7+
//
8+
9+
import ArgumentParser
10+
import AWSClientRuntime
11+
import Foundation
12+
13+
// snippet-start:[swift.support.import]
14+
import AWSSupport
15+
// snippet-end:[swift.support.import]
16+
17+
struct ExampleCommand: ParsableCommand {
18+
@Option(help: "The AWS Region to run AWS API calls in.")
19+
var awsRegion = "us-east-1"
20+
21+
static var configuration = CommandConfiguration(
22+
commandName: "hello-support",
23+
abstract: """
24+
Demonstrates a simple operation using Amazon Support.
25+
""",
26+
discussion: """
27+
An example showing how to make a call to Amazon Support using the AWS
28+
SDK for Swift.
29+
"""
30+
)
31+
32+
/// Return an array of the user's services.
33+
///
34+
/// - Parameter supportClient: The `SupportClient` to use when calling
35+
/// `describeServices()`.
36+
///
37+
/// - Returns: An array of services.
38+
func getSupportServices(supportClient: SupportClient) async -> [SupportClientTypes.Service] {
39+
do {
40+
let output = try await supportClient.describeServices(
41+
input: DescribeServicesInput()
42+
)
43+
44+
guard let services = output.services else {
45+
return []
46+
}
47+
48+
return services
49+
} catch let error as AWSServiceError {
50+
// SubscriptionRequiredException isn't a modeled error, so we
51+
// have to catch AWSServiceError and then look at its errorCode to
52+
// see if it's SubscriptionRequiredException.
53+
if error.errorCode == "SubscriptionRequiredException" {
54+
print("*** You need a subscription to use AWS Support.")
55+
return []
56+
} else {
57+
print("*** An unknown error occurred getting support information.")
58+
return []
59+
}
60+
} catch {
61+
print("*** Error getting service information: \(error.localizedDescription)")
62+
return []
63+
}
64+
}
65+
66+
/// Called by ``main()`` to run the bulk of the example.
67+
func runAsync() async throws {
68+
let supportConfig = try await SupportClient.SupportClientConfiguration(region: awsRegion)
69+
let supportClient = SupportClient(config: supportConfig)
70+
71+
let services = await getSupportServices(supportClient: supportClient)
72+
73+
print("Found \(services.count) security services")
74+
}
75+
}
76+
77+
/// The program's asynchronous entry point.
78+
@main
79+
struct Main {
80+
static func main() async {
81+
let args = Array(CommandLine.arguments.dropFirst())
82+
83+
do {
84+
let command = try ExampleCommand.parse(args)
85+
try await command.runAsync()
86+
} catch {
87+
ExampleCommand.exit(withError: error)
88+
}
89+
}
90+
}
91+
// snippet-end:[swift.support.hello]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.support.scenario.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: "support-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: "support-scenario",
39+
dependencies: [
40+
.product(name: "AWSSupport", package: "aws-sdk-swift"),
41+
.product(name: "ArgumentParser", package: "swift-argument-parser")
42+
],
43+
path: "Sources")
44+
45+
]
46+
)
47+
// snippet-end:[swift.support.scenario.package]

0 commit comments

Comments
 (0)