Skip to content

Commit

Permalink
Updating the IAM swift examples
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws committed Sep 17, 2024
1 parent f2684a7 commit 08b5da6
Show file tree
Hide file tree
Showing 40 changed files with 495 additions and 356 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ struct ExampleCommand: ParsableCommand {
/// example.
// snippet-start:[iam.swift.attachrolepolicy.command.runasync]
func runAsync() async throws {
let serviceHandler = await ServiceHandler()

do {
let serviceHandler = try await ServiceHandler()

_ = try await serviceHandler.attachRolePolicy(role: rolename, policyArn: policyArn)
} catch {
print("ERROR: runAsync:", dump(error))
throw error
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ public class ServiceHandler {
public let client: IAMClient

/// Initialize and return a new ``ServiceHandler`` object, which is used
/// to drive the AWS calls used for the example. The Region string
/// `AWS_GLOBAL` is used because users are shared across all Regions.
/// to drive the AWS calls used for the example.
///
/// - Returns: A new ``ServiceHandler`` object, ready to be called to
/// execute AWS operations.
// snippet-start:[iam.swift.attachrolepolicy.handler.init]
public init() async {
public init() async throws {
do {
client = try IAMClient(region: "AWS_GLOBAL")
client = try await IAMClient()
} catch {
print("ERROR: ", dump(error, name: "Initializing Amazon IAM client"))
exit(1)
throw error
}
}
// snippet-end:[iam.swift.attachrolepolicy.handler.init]
Expand All @@ -48,7 +47,7 @@ public class ServiceHandler {
/// - role: The name of the role to attach the policy to.
/// - policyArn: The ARN of the policy to attach.
///
// snippet-start:[iam.swift.attachrolepolicy.handler.attachrolepolicy]
// snippet-start:[iam.swift.attachrolepolicy.handler.AttachRolePolicy]
public func attachRolePolicy(role: String, policyArn: String) async throws {
let input = AttachRolePolicyInput(
policyArn: policyArn,
Expand All @@ -57,9 +56,10 @@ public class ServiceHandler {
do {
_ = try await client.attachRolePolicy(input: input)
} catch {
print("ERROR: Attaching a role policy:", dump(error))
throw error
}
}
// snippet-end:[iam.swift.attachrolepolicy.handler.attachrolepolicy]
// snippet-end:[iam.swift.attachrolepolicy.handler.AttachRolePolicy]
}
// snippet-end:[iam.swift.attachrolepolicy.handler]
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class AttachRolePolicyTests: XCTestCase {
super.setUp()

Task() {
AttachRolePolicyTests.serviceHandler = await ServiceHandler()
AttachRolePolicyTests.serviceHandler = try await ServiceHandler()
tdSem.signal()
}
tdSem.wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ struct ExampleCommand: ParsableCommand {
/// example.
// snippet-start:[iam.swift.createrole.command.runasync]
func runAsync() async throws {
let serviceHandler = await ServiceHandler()

do {
let serviceHandler = try await ServiceHandler()

// Get information about the user running this example. This user
// will be granted the new role.
let user = try await serviceHandler.getUser(name: nil)
Expand Down Expand Up @@ -63,6 +63,7 @@ struct ExampleCommand: ParsableCommand {
let roleID = try await serviceHandler.createRole(name: rolename, policyDocument: policyDocument)
print("Created new role \(rolename) with ID \(roleID)")
} catch {
print("ERROR: CreateRole runAsync:", dump(error))
throw error
}
}
Expand All @@ -88,4 +89,4 @@ struct Main {
}
}
// snippet-end:[iam.swift.createrole.main]
// snippet-end:[iam.swift.createrole.example]
// snippet-end:[iam.swift.createrole.example]
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ public class ServiceHandler {
public let client: IAMClient

/// Initialize and return a new ``ServiceHandler`` object, which is used
/// to drive the AWS calls used for the example. The Region string
/// `AWS_GLOBAL` is used because users are shared across all Regions.
/// to drive the AWS calls used for the example.
///
/// - Returns: A new ``ServiceHandler`` object, ready to be called to
/// execute AWS operations.
// snippet-start:[iam.swift.createrole.handler.init]
public init() async {
public init() async throws {
do {
client = try IAMClient(region: "AWS_GLOBAL")
client = try await IAMClient()
} catch {
print("ERROR: ", dump(error, name: "Initializing Amazon IAM client"))
exit(1)
throw error
}
}
// snippet-end:[iam.swift.createrole.handler.init]
Expand All @@ -48,7 +47,7 @@ public class ServiceHandler {
/// - Parameter name: The name of the new IAM role.
///
/// - Returns: The ID of the newly created role.
// snippet-start:[iam.swift.createrole.handler.createrole]
// snippet-start:[iam.swift.createrole.handler.CreateRole]
public func createRole(name: String, policyDocument: String) async throws -> String {
let input = CreateRoleInput(
assumeRolePolicyDocument: policyDocument,
Expand All @@ -64,10 +63,11 @@ public class ServiceHandler {
}
return id
} catch {
print("ERROR: createRole:", dump(error))
throw error
}
}
// snippet-end:[iam.swift.createrole.handler.createrole]
// snippet-end:[iam.swift.createrole.handler.CreateRole]

/// Get information about the specified user
///
Expand All @@ -87,9 +87,10 @@ public class ServiceHandler {
}
return user
} catch {
print("ERROR: getUser:", dump(error))
throw error
}
}
// snippet-end:[iam.swift.createrole.handler.getuser]
}
// snippet-end:[iam.swift.createrole.handler]
// snippet-end:[iam.swift.createrole.handler]
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ final class CreateRoleTests: XCTestCase {
override class func setUp() {
let tdSem = TestWaiter(name: "Setup")
super.setUp()

Task() {
CreateRoleTests.serviceHandler = await ServiceHandler()
CreateRoleTests.serviceHandler = try await ServiceHandler()
tdSem.signal()
}
tdSem.wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

// snippet-start:[iam.swift.createservicelinkedrole.example]
// snippet-start:[iam.swift.createservicelinkedrole.main.imports]
import ArgumentParser
import Foundation
import ServiceHandler
import ArgumentParser

// snippet-end:[iam.swift.createservicelinkedrole.main.imports]

/// The command line arguments and options available for this
Expand All @@ -37,13 +38,13 @@ struct ExampleCommand: ParsableCommand {
/// example.
// snippet-start:[iam.swift.createservicelinkedrole.command.runasync]
func runAsync() async throws {
let serviceHandler = await ServiceHandler()

do {
let serviceHandler = try await ServiceHandler()
// Create the role and output information about it.
let role = try await serviceHandler.createServiceLinkedRole(
service: servicename, suffix: suffix, description: description)

service: servicename, suffix: suffix, description: description
)

let roleID = role.roleId ?? "<unknown>"
let roleARN = role.arn ?? "<unknown>"
let roleDesc = role.description ?? "<none>"
Expand All @@ -62,11 +63,13 @@ struct ExampleCommand: ParsableCommand {
print(" Created: \(dateFormatter.string(from: roleCreateDate))")
print("Description: \(roleDesc)")
} catch {
print("ERROR: CreateServiceLinkedRole runAsync:", dump(error))
throw error
}
}
// snippet-end:[iam.swift.createservicelinkedrole.command.runasync]
}

// snippet-end:[iam.swift.createservicelinkedrole.command]

//
Expand All @@ -84,7 +87,8 @@ struct Main {
} catch {
ExampleCommand.exit(withError: error)
}
}
}
}

// snippet-end:[iam.swift.createservicelinkedrole.main]
// snippet-end:[iam.swift.createservicelinkedrole.example]
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ public class ServiceHandler {
public let client: IAMClient

/// Initialize and return a new ``ServiceHandler`` object, which is used
/// to drive the AWS calls used for the example. The Region string
/// `AWS_GLOBAL` is used because users are shared across all Regions.
/// to drive the AWS calls used for the example.
///
/// - Returns: A new ``ServiceHandler`` object, ready to be called to
/// execute AWS operations.
// snippet-start:[iam.swift.createservicelinkedrole.handler.init]
public init() async {
public init() async throws {
do {
client = try IAMClient(region: "AWS_GLOBAL")
client = try await IAMClient()
} catch {
print("ERROR: ", dump(error, name: "Initializing Amazon IAM client"))
exit(1)
throw error
}
}
// snippet-end:[iam.swift.createservicelinkedrole.handler.init]
Expand All @@ -56,7 +55,7 @@ public class ServiceHandler {
/// The `service` parameter should be a string derived that looks like a
/// URL but has no `http://` at the beginning, such as
/// `elasticbeanstalk.amazonaws.com`.
// snippet-start:[iam.swift.createservicelinkedrole.handler.createservicelinkedrole]
// snippet-start:[iam.swift.createservicelinkedrole.handler.CreateServiceLinkedRole]
public func createServiceLinkedRole(service: String, suffix: String? = nil, description: String?)
async throws -> IAMClientTypes.Role {
let input = CreateServiceLinkedRoleInput(
Expand All @@ -71,9 +70,10 @@ public class ServiceHandler {
}
return role
} catch {
print("ERROR: createServiceLinkedRole:", dump(error))
throw error
}
}
// snippet-end:[iam.swift.createservicelinkedrole.handler.createservicelinkedrole]
// snippet-end:[iam.swift.createservicelinkedrole.handler.CreateServiceLinkedRole]
}
// snippet-end:[iam.swift.createservicelinkedrole.handler]
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ final class CreateServiceLinkedRoleTests: XCTestCase {
override class func setUp() {
let tdSem = TestWaiter(name: "Setup")
super.setUp()

Task() {
CreateServiceLinkedRoleTests.serviceHandler = await ServiceHandler()
CreateServiceLinkedRoleTests.serviceHandler = try await ServiceHandler()
tdSem.signal()
}
tdSem.wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ struct ExampleCommand: ParsableCommand {
/// example.
// snippet-start:[iam.swift.createuser.command.runasync]
func runAsync() async throws {
let serviceHandler = await ServiceHandler()


do {
let serviceHandler = try await ServiceHandler()
let userID = try await serviceHandler.createUser(name: username)
print("Created new user \(username) with ID \(userID)")
} catch {
print("ERROR: CreateUser runAsync:", dump(error))
throw error
}
}
Expand All @@ -62,4 +63,4 @@ struct Main {
}
}
// snippet-end:[iam.swift.createuser.main]
// snippet-end:[iam.swift.createuser.example]
// snippet-end:[iam.swift.createuser.example]
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ public class ServiceHandler {
public let client: IAMClient

/// Initialize and return a new ``ServiceHandler`` object, which is used
/// to drive the AWS calls used for the example. The Region string
/// `AWS_GLOBAL` is used because users are shared across all Regions.
/// to drive the AWS calls used for the example.
///
/// - Returns: A new ``ServiceHandler`` object, ready to be called to
/// execute AWS operations.
// snippet-start:[iam.swift.createuser.handler.init]
public init() async {
public init() async throws {
do {
client = try IAMClient(region: "AWS_GLOBAL")
client = try await IAMClient()
} catch {
print("ERROR: ", dump(error, name: "Initializing Amazon IAM client"))
exit(1)
throw error
}
}
// snippet-end:[iam.swift.createuser.handler.init]
Expand All @@ -47,7 +46,7 @@ public class ServiceHandler {
/// - Parameter name: The user's name.
///
/// - Returns: The ID of the newly created user.
// snippet-start:[iam.swift.createuser.handler.createuser]
// snippet-start:[iam.swift.createuser.handler.CreateUser]
public func createUser(name: String) async throws -> String {
let input = CreateUserInput(
userName: name
Expand All @@ -62,9 +61,10 @@ public class ServiceHandler {
}
return id
} catch {
print("ERROR: createUser:", dump(error))
throw error
}
}
// snippet-end:[iam.swift.createuser.handler.createuser]
// snippet-end:[iam.swift.createuser.handler.CreateUser]
}
// snippet-end:[iam.swift.createuser.handler]
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class CreateUserTests: XCTestCase {
super.setUp()

Task() {
CreateUserTests.serviceHandler = await ServiceHandler()
CreateUserTests.serviceHandler = try await ServiceHandler()
tdSem.signal()
}
tdSem.wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ struct ExampleCommand: ParsableCommand {
/// example.
// snippet-start:[iam.swift.getpolicy.command.runasync]
func runAsync() async throws {
let serviceHandler = await ServiceHandler()

do {
let serviceHandler = try await ServiceHandler()
let policy = try await serviceHandler.getPolicy(arn: arn)

guard let policyName = policy.policyName,
Expand All @@ -59,6 +58,7 @@ struct ExampleCommand: ParsableCommand {
}

} catch {
print("ERROR: CreateRole runAsync:", dump(error))
throw error
}
}
Expand All @@ -84,4 +84,4 @@ struct Main {
}
}
// snippet-end:[iam.swift.getpolicy.main]
// snippet-end:[iam.swift.getpolicy.example]
// snippet-end:[iam.swift.getpolicy.example]
Loading

0 comments on commit 08b5da6

Please sign in to comment.