Skip to content
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
132 changes: 132 additions & 0 deletions swift/example_code/support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Support code examples for the SDK for Swift

## Overview

Shows how to use the AWS SDK for Swift to work with AWS Support.

<!--custom.overview.start-->
<!--custom.overview.end-->

_Support provides support for users of Amazon Web Services._

## ⚠ Important

* 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/).
* Running the tests might result in charges to your AWS account.
* 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).
* 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).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder.


<!--custom.prerequisites.start-->
<!--custom.prerequisites.end-->

### Get started

- [Hello Support](hello/Package.swift#L8) (`DescribeServices`)


### Basics

Code examples that show you how to perform the essential operations within a service.

- [Learn the basics](scenario/Package.swift)


### Single actions

Code excerpts that show you how to call individual service functions.

- [AddAttachmentsToSet](scenario/Sources/Scenario.swift#L463)
- [AddCommunicationToCase](scenario/Sources/Scenario.swift#L512)
- [CreateCase](scenario/Sources/Scenario.swift#L361)
- [DescribeAttachment](scenario/Sources/Scenario.swift#L600)
- [DescribeCases](scenario/Sources/Scenario.swift#L410)
- [DescribeCommunications](scenario/Sources/Scenario.swift#L562)
- [DescribeServices](scenario/Sources/Scenario.swift#L286)
- [DescribeSeverityLevels](scenario/Sources/Scenario.swift#L322)
- [ResolveCase](scenario/Sources/Scenario.swift#L640)


<!--custom.examples.start-->
<!--custom.examples.end-->

## Run the examples

### Instructions

To build any of these examples from a terminal window, navigate into its
directory, then use the following command:

```
$ swift build
```

To build one of these examples in Xcode, navigate to the example's directory
(such as the `ListUsers` directory, to build that example). Then type `xed.`
to open the example directory in Xcode. You can then use standard Xcode build
and run commands.

<!--custom.instructions.start-->
<!--custom.instructions.end-->

#### Hello Support

This example shows you how to get started using Support.


#### Learn the basics

This example shows you how to do the following:

- Get and display available services and severity levels for cases.
- Create a support case using a selected service, category, and severity level.
- Get and display a list of open cases for the current day.
- Add an attachment set and a communication to the new case.
- Describe the new attachment and communication for the case.
- Resolve the case.
- Get and display a list of resolved cases for the current day.

<!--custom.basic_prereqs.support_Scenario_GetStartedSupportCases.start-->
<!--custom.basic_prereqs.support_Scenario_GetStartedSupportCases.end-->


<!--custom.basics.support_Scenario_GetStartedSupportCases.start-->
<!--custom.basics.support_Scenario_GetStartedSupportCases.end-->


### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../../README.md#Tests)
in the `swift` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

- [Support User Guide](https://docs.aws.amazon.com/awssupport/latest/user/getting-started.html)
- [Support API Reference](https://docs.aws.amazon.com/awssupport/latest/APIReference/welcome.html)
- [SDK for Swift Support reference](https://sdk.amazonaws.com/swift/api/awssupport/latest/documentation/awssupport)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
47 changes: 47 additions & 0 deletions swift/example_code/support/hello/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// swift-tools-version: 5.9
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// (swift-tools-version has two lines here because it needs to be the first
// line in the file, but it should also appear in the snippet below)
//
// snippet-start:[swift.support.hello.package]
// swift-tools-version: 5.9
//
// The swift-tools-version declares the minimum version of Swift required to
// build this package.

import PackageDescription

let package = Package(
name: "hello-support",
// Let Xcode know the minimum Apple platforms supported.
platforms: [
.macOS(.v13),
.iOS(.v15)
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(
url: "https://github.com/awslabs/aws-sdk-swift",
from: "1.0.0"),
.package(
url: "https://github.com/apple/swift-argument-parser.git",
branch: "main"
)
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products
// from dependencies.
.executableTarget(
name: "hello-support",
dependencies: [
.product(name: "AWSSupport", package: "aws-sdk-swift"),
.product(name: "ArgumentParser", package: "swift-argument-parser")
],
path: "Sources")

]
)
// snippet-end:[swift.support.hello.package]
91 changes: 91 additions & 0 deletions swift/example_code/support/hello/Sources/entry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// snippet-start:[swift.support.hello]
// An example that shows how to use the AWS SDK for Swift to perform a simple
// operation using AWS Support.
//

import ArgumentParser
import AWSClientRuntime
import Foundation

// snippet-start:[swift.support.import]
import AWSSupport
// snippet-end:[swift.support.import]

struct ExampleCommand: ParsableCommand {
@Option(help: "The AWS Region to run AWS API calls in.")
var awsRegion = "us-east-1"

static var configuration = CommandConfiguration(
commandName: "hello-support",
abstract: """
Demonstrates a simple operation using Amazon Support.
""",
discussion: """
An example showing how to make a call to Amazon Support using the AWS
SDK for Swift.
"""
)

/// Return an array of the user's services.
///
/// - Parameter supportClient: The `SupportClient` to use when calling
/// `describeServices()`.
///
/// - Returns: An array of services.
func getSupportServices(supportClient: SupportClient) async -> [SupportClientTypes.Service] {
do {
let output = try await supportClient.describeServices(
input: DescribeServicesInput()
)

guard let services = output.services else {
return []
}

return services
} catch let error as AWSServiceError {
// SubscriptionRequiredException isn't a modeled error, so we
// have to catch AWSServiceError and then look at its errorCode to
// see if it's SubscriptionRequiredException.
if error.errorCode == "SubscriptionRequiredException" {
print("*** You need a subscription to use AWS Support.")
return []
} else {
print("*** An unknown error occurred getting support information.")
return []
}
} catch {
print("*** Error getting service information: \(error.localizedDescription)")
return []
}
}

/// Called by ``main()`` to run the bulk of the example.
func runAsync() async throws {
let supportConfig = try await SupportClient.SupportClientConfiguration(region: awsRegion)
let supportClient = SupportClient(config: supportConfig)

let services = await getSupportServices(supportClient: supportClient)

print("Found \(services.count) security services")
}
}

/// The program's asynchronous entry point.
@main
struct Main {
static func main() async {
let args = Array(CommandLine.arguments.dropFirst())

do {
let command = try ExampleCommand.parse(args)
try await command.runAsync()
} catch {
ExampleCommand.exit(withError: error)
}
}
}
// snippet-end:[swift.support.hello]
47 changes: 47 additions & 0 deletions swift/example_code/support/scenario/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// swift-tools-version: 5.9
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// (swift-tools-version has two lines here because it needs to be the first
// line in the file, but it should also appear in the snippet below)
//
// snippet-start:[swift.support.scenario.package]
// swift-tools-version: 5.9
//
// The swift-tools-version declares the minimum version of Swift required to
// build this package.

import PackageDescription

let package = Package(
name: "support-scenario",
// Let Xcode know the minimum Apple platforms supported.
platforms: [
.macOS(.v13),
.iOS(.v15)
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(
url: "https://github.com/awslabs/aws-sdk-swift",
from: "1.0.0"),
.package(
url: "https://github.com/apple/swift-argument-parser.git",
branch: "main"
)
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products
// from dependencies.
.executableTarget(
name: "support-scenario",
dependencies: [
.product(name: "AWSSupport", package: "aws-sdk-swift"),
.product(name: "ArgumentParser", package: "swift-argument-parser")
],
path: "Sources")

]
)
// snippet-end:[swift.support.scenario.package]
Loading
Loading