Skip to content

Commit 9db7498

Browse files
authored
feat: add ec2 region provider (#455)
1 parent 9472ff9 commit 9db7498

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

AWSClientRuntime/Sources/Regions/DefaultRegionResolver.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
//
55
// SPDX-License-Identifier: Apache-2.0
66
//
7+
import AwsCommonRuntimeKit
78

89
public struct DefaultRegionResolver: RegionResolver {
910
public let providers: [RegionProvider]
1011

11-
public init(providers: [RegionProvider] = [EnvironmentRegionProvider(), ProfileRegionProvider()]) {
12-
// TODO: add more region resolvers i.e. Imds, System Properties, etc
12+
public init(providers: [RegionProvider] = [EnvironmentRegionProvider(), ProfileRegionProvider(), IMDSRegionProvider()]) {
13+
// TODO: add more region resolvers i.e. System Properties, etc
1314
self.providers = providers
1415
}
1516

1617
public func resolveRegion() -> String? {
1718
for provider in providers {
18-
if let region = provider.resolveRegion() {
19-
return region
19+
do {
20+
// TODO: Verify that this does not block calling thread. If it does, we need to solve this similar to the way we solve this for the credentials provider
21+
if let region = try provider.resolveRegion().get() {
22+
return region
23+
}
24+
} catch {
25+
return nil
2026
}
2127
}
2228
return nil

AWSClientRuntime/Sources/Regions/EnvironmentRegionProvider.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77
import class Foundation.ProcessInfo
8+
import AwsCommonRuntimeKit
89

910
public struct EnvironmentRegionProvider: RegionProvider {
1011
private let AWS_ENVIRON_REGION = "AWS_REGION"
@@ -14,8 +15,10 @@ public struct EnvironmentRegionProvider: RegionProvider {
1415
self.env = env
1516
}
1617

17-
public func resolveRegion() -> String? {
18-
return env.environmentVariable(key: AWS_ENVIRON_REGION)
18+
public func resolveRegion() -> Future<String?> {
19+
let future = Future<String?>()
20+
future.fulfill(env.environmentVariable(key: AWS_ENVIRON_REGION))
21+
return future
1922
}
2023
}
2124

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
import AwsCommonRuntimeKit
8+
9+
public struct IMDSRegionProvider: RegionProvider {
10+
private let REGION_PATH = "/latest/meta-data/placement/region"
11+
let imdsClient: IMDSClient
12+
13+
public init() {
14+
self.imdsClient = IMDSClient()
15+
}
16+
17+
public func resolveRegion() -> Future<String?> {
18+
let future = Future<String?>()
19+
20+
imdsClient.get(path: REGION_PATH, completion: { result in
21+
switch result {
22+
case .success(let region):
23+
future.fulfill(region)
24+
case .failure:
25+
future.fulfill(nil)
26+
}
27+
})
28+
return future
29+
}
30+
}

AWSClientRuntime/Sources/Regions/ProfileRegionResolver.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77
import AwsCommonRuntimeKit
8+
import ClientRuntime
89

910
public struct ProfileRegionProvider: RegionProvider {
1011
let profileCollection: ProfileCollection
1112
let profileName: String
13+
let logger: SwiftLogger
1214

1315
init(profileCollection: ProfileCollection, profileName: String) {
1416
self.profileCollection = profileCollection
1517
self.profileName = profileName
18+
self.logger = SwiftLogger(label: "ProfileRegionResolver")
1619
}
1720

1821
//TODO: expose these config fields up to the sdk so customer can override path and profile name
@@ -22,10 +25,18 @@ public struct ProfileRegionProvider: RegionProvider {
2225
self.init(profileCollection: profileCollection, profileName: profileName)
2326
}
2427

25-
public func resolveRegion() -> String? {
28+
public func resolveRegion() -> Future<String?> {
29+
let future = Future<String?>()
2630
guard let profile = profileCollection.profile(for: profileName) else {
27-
return nil
31+
future.fulfill(nil)
32+
return future
2833
}
29-
return profile.getProperty(name: "region")
34+
guard let region = profile.getProperty(name: "region") else {
35+
logger.info("Attempting to use \(profileName), but no region was found")
36+
future.fulfill(nil)
37+
return future
38+
}
39+
future.fulfill(region)
40+
return future
3041
}
3142
}

AWSClientRuntime/Sources/Regions/RegionProvider.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//
55
// SPDX-License-Identifier: Apache-2.0
66
//
7+
import AwsCommonRuntimeKit
78

89
public protocol RegionProvider {
9-
func resolveRegion() -> String?
10+
func resolveRegion() -> Future<String?>
1011
}

0 commit comments

Comments
 (0)