Skip to content

Commit 93abfc7

Browse files
committed
Introduce TencentCloud.Credential
1 parent 84eed8f commit 93abfc7

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===------------------------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftTencentSCFRuntime open source project
4+
//
5+
// Copyright (c) 2020 stevapple and the SwiftTencentSCFRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftTencentSCFRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===------------------------------------------------------------------------------------===//
14+
15+
/// A `struct` representing a Tencent Cloud credential.
16+
extension TencentCloud {
17+
public struct Credential {
18+
let secretId: String
19+
let secretKey: String
20+
let sessionToken: String?
21+
22+
enum CodingKeys: String, CodingKey {
23+
case secretId = "SecretId"
24+
case secretKey = "SecretKey"
25+
case sessionToken = "SessionToken"
26+
}
27+
28+
public init(secretId: String,
29+
secretKey: String,
30+
sessionToken: String? = nil)
31+
{
32+
self.secretId = secretId
33+
self.secretKey = secretKey
34+
self.sessionToken = sessionToken
35+
}
36+
}
37+
}

Sources/TencentSCFRuntimeCore/SCFContext.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ extension SCF {
106106
.init(stringLiteral: SCF.Env["SCF_FUNCTIONVERSION"] ?? "")
107107
}
108108

109+
public var credential: TencentCloud.Credential {
110+
.init(secretId: SCF.Env["TENCENTCLOUD_SECRETID"] ?? "",
111+
secretKey: SCF.Env["TENCENTCLOUD_SECRETKEY"] ?? "",
112+
sessionToken: SCF.Env["TENCENTCLOUD_SESSIONTOKEN"] ?? "")
113+
}
114+
109115
/// `Logger` to log with.
110116
///
111117
/// - Note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.

Tests/TencentSCFRuntimeCoreTests/SCFContextTest.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class SCFContextTest: XCTestCase {
3434
XCTAssertEqual(context.name, "my-swift-function")
3535
XCTAssertEqual(context.namespace, "default")
3636
XCTAssertEqual(context.version, .latest)
37+
XCTAssertEqual(context.credential.secretId, "")
38+
XCTAssertEqual(context.credential.secretKey, "")
39+
XCTAssertEqual(context.credential.sessionToken, "")
3740
}
3841

3942
func testEnvUpdateWithDict() {
@@ -44,6 +47,9 @@ class SCFContextTest: XCTestCase {
4447
"SCF_FUNCTIONNAME": "another-swift-function",
4548
"SCF_NAMESPACE": "custom",
4649
"SCF_FUNCTIONVERSION": "2",
50+
"TENCENTCLOUD_SECRETID": "SECRET_ID",
51+
"TENCENTCLOUD_SECRETKEY": "SECRET_KEY",
52+
"TENCENTCLOUD_SESSIONTOKEN": "SESSION_TOKEN",
4753
]
4854

4955
SCF.Env.update(with: customEnvironment)
@@ -62,6 +68,9 @@ class SCFContextTest: XCTestCase {
6268
XCTAssertEqual(context.name, "another-swift-function")
6369
XCTAssertEqual(context.namespace, "custom")
6470
XCTAssertEqual(context.version, .version(2))
71+
XCTAssertEqual(context.credential.secretId, "SECRET_ID")
72+
XCTAssertEqual(context.credential.secretKey, "SECRET_KEY")
73+
XCTAssertEqual(context.credential.sessionToken, "SESSION_TOKEN")
6574

6675
SCF.Env.reset()
6776
}
@@ -74,6 +83,9 @@ class SCFContextTest: XCTestCase {
7483
"SCF_FUNCTIONNAME": "another-swift-function",
7584
"SCF_NAMESPACE": "custom",
7685
"SCF_FUNCTIONVERSION": "2",
86+
"TENCENTCLOUD_SECRETID": "SECRET_ID",
87+
"TENCENTCLOUD_SECRETKEY": "SECRET_KEY",
88+
"TENCENTCLOUD_SESSIONTOKEN": "SESSION_TOKEN",
7789
]
7890

7991
for (key, value) in customEnvironment {
@@ -94,6 +106,9 @@ class SCFContextTest: XCTestCase {
94106
XCTAssertEqual(context.name, "another-swift-function")
95107
XCTAssertEqual(context.namespace, "custom")
96108
XCTAssertEqual(context.version, .version(2))
109+
XCTAssertEqual(context.credential.secretId, "SECRET_ID")
110+
XCTAssertEqual(context.credential.secretKey, "SECRET_KEY")
111+
XCTAssertEqual(context.credential.sessionToken, "SESSION_TOKEN")
97112

98113
SCF.Env.reset()
99114
}

0 commit comments

Comments
 (0)