Skip to content

Commit 2a463cf

Browse files
committed
Implement Tencent Cloud regions and zones
1 parent c9e925f commit 2a463cf

File tree

3 files changed

+225
-105
lines changed

3 files changed

+225
-105
lines changed

Sources/TencentSCFEvents/AWSRegion.swift

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
// list all available regions and zones using tccli:
16+
// $ tccli cvm DescribeRegions
17+
// $ tccli cvm DescribeZones --region <Region>
18+
19+
/// Enumeration of the Tencent Cloud regions and zones.
20+
public enum TencentCloud {
21+
public struct Region: RawRepresentable, CustomStringConvertible, Equatable, Hashable {
22+
public typealias RawValue = String
23+
24+
public let rawValue: String
25+
26+
public init?(rawValue: String) {
27+
self.rawValue = rawValue
28+
}
29+
30+
public var description: String {
31+
self.rawValue
32+
}
33+
34+
static var regular: [Self] = [
35+
.ap_bangkok,
36+
.ap_beijing,
37+
.ap_chengdu,
38+
.ap_chongqing,
39+
.ap_guangzhou,
40+
.ap_guangzhou_open,
41+
.ap_hongkong,
42+
.ap_numbai,
43+
.ap_nanjing,
44+
.ap_seoul,
45+
.ap_shanghai,
46+
.ap_singapore,
47+
.ap_tokyo,
48+
.eu_frankfurt,
49+
.eu_moscow,
50+
.na_ashburn,
51+
.na_siliconvalley,
52+
.na_toronto,
53+
]
54+
55+
static var financial: [Self] = [
56+
.ap_shanghai_fsi,
57+
.ap_shenzhen_fsi,
58+
]
59+
60+
static var mainland: [Self] = [
61+
.ap_beijing,
62+
.ap_chengdu,
63+
.ap_chongqing,
64+
.ap_guangzhou,
65+
.ap_guangzhou_open,
66+
.ap_nanjing,
67+
.ap_shanghai,
68+
.ap_shanghai_fsi,
69+
.ap_shenzhen_fsi,
70+
]
71+
72+
static var overseas: [Self] = [
73+
.ap_bangkok,
74+
.ap_hongkong,
75+
.ap_numbai,
76+
.ap_seoul,
77+
.ap_singapore,
78+
.ap_tokyo,
79+
.eu_frankfurt,
80+
.eu_moscow,
81+
.na_ashburn,
82+
.na_siliconvalley,
83+
.na_toronto,
84+
]
85+
86+
public static var ap_bangkok: Self { Region(rawValue: "ap-bangkok")! } // Thailand
87+
public static var ap_beijing: Self { Region(rawValue: "ap-beijing")! } // Beijing, China
88+
public static var ap_chengdu: Self { Region(rawValue: "ap-chengdu")! } // Sichuan, China
89+
public static var ap_chongqing: Self { Region(rawValue: "ap-chongqing")! } // Chongqing, China
90+
public static var ap_guangzhou: Self { Region(rawValue: "ap-guangzhou")! } // Guangdong, China
91+
public static var ap_guangzhou_open: Self { Region(rawValue: "ap-guangzhou-open")! } // Guangdong, China (Open)
92+
public static var ap_hongkong: Self { Region(rawValue: "ap-hongkong")! } // Hong Kong, China
93+
public static var ap_numbai: Self { Region(rawValue: "ap-numbai")! } // India
94+
public static var ap_nanjing: Self { Region(rawValue: "ap-nanjing")! } // Jiangsu, China
95+
public static var ap_seoul: Self { Region(rawValue: "ap-seoul")! } // South Korea
96+
public static var ap_shanghai: Self { Region(rawValue: "ap-shanghai")! } // Shanghai, China
97+
public static var ap_shanghai_fsi: Self { Region(rawValue: "ap-shanghai-fsi")! } // Shanghai, China (Financial)
98+
public static var ap_shenzhen_fsi: Self { Region(rawValue: "ap-shenzhen-fsi")! } // Guangdong, China (Financial)
99+
public static var ap_singapore: Self { Region(rawValue: "ap-singapore")! } // Singapore
100+
public static var ap_tokyo: Self { Region(rawValue: "ap-tokyo")! } // Japan
101+
102+
public static var eu_frankfurt: Self { Region(rawValue: "eu-frankfurt")! } // German
103+
public static var eu_moscow: Self { Region(rawValue: "eu-moscow")! } // Russia
104+
public static var na_ashburn: Self { Region(rawValue: "na-ashburn")! } // Virginia, US
105+
public static var na_siliconvalley: Self { Region(rawValue: "na-siliconvalley")! } // California, US
106+
public static var na_toronto: Self { Region(rawValue: "na-toronto")! } // Canada
107+
}
108+
109+
public struct Zone: RawRepresentable, CustomStringConvertible, Equatable, Hashable {
110+
public typealias RawValue = String
111+
112+
public var rawValue: String {
113+
"\(self.region)-\(self.number)"
114+
}
115+
116+
public var description: String {
117+
self.rawValue
118+
}
119+
120+
public init?(rawValue: String) {
121+
guard let (region, number) = Self.parse(rawValue: rawValue) else {
122+
return nil
123+
}
124+
self.region = region
125+
self.number = number
126+
}
127+
128+
private static func parse(rawValue: String) -> (Region, UInt8)? {
129+
let components = rawValue.split(separator: "-")
130+
guard components.count > 2, let number = UInt8(components.last!) else {
131+
return nil
132+
}
133+
134+
let region = Region(rawValue: components.dropLast().joined(separator: "-"))!
135+
return (region, number)
136+
}
137+
138+
public let region: Region
139+
140+
public let number: UInt8
141+
}
142+
}
143+
144+
extension TencentCloud.Region: Codable {
145+
public init(from decoder: Decoder) throws {
146+
let container = try decoder.singleValueContainer()
147+
let region = try container.decode(String.self)
148+
self.init(rawValue: region)!
149+
}
150+
151+
public func encode(to encoder: Encoder) throws {
152+
var container = encoder.singleValueContainer()
153+
try container.encode(self.rawValue)
154+
}
155+
}
156+
157+
extension TencentCloud.Zone: Codable {
158+
public init(from decoder: Decoder) throws {
159+
let container = try decoder.singleValueContainer()
160+
let zone = try container.decode(String.self)
161+
guard let (region, number) = Self.parse(rawValue: zone) else {
162+
throw DecodingError.dataCorruptedError(in: container, debugDescription:
163+
"Expected zone format like `ap-shanghai-3` or `ap-shenzhen-fsi-1`, but `\(zone)` does not forfill format")
164+
}
165+
self.region = region
166+
self.number = number
167+
}
168+
169+
public func encode(to encoder: Encoder) throws {
170+
var container = encoder.singleValueContainer()
171+
try container.encode(self.rawValue)
172+
}
173+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
@testable import TencentSCFEvents
16+
import XCTest
17+
18+
class TencentCloudTests: XCTestCase {
19+
static let allRegions = Set(TencentCloud.Region.mainland + TencentCloud.Region.overseas)
20+
21+
func testRegionCountEqual() {
22+
XCTAssertEqual(TencentCloud.Region.mainland.count + TencentCloud.Region.overseas.count, TencentCloud.Region.regular.count + TencentCloud.Region.financial.count)
23+
XCTAssertEqual(Self.allRegions, Set(TencentCloud.Region.regular + TencentCloud.Region.financial))
24+
}
25+
26+
func testRegionCodable() throws {
27+
let encoder = JSONEncoder()
28+
let decoder = JSONDecoder()
29+
for region in Self.allRegions {
30+
let json = "\"\(region.rawValue)\""
31+
let encoded = try encoder.encode(region)
32+
let decoded = try decoder.decode(TencentCloud.Region.self, from: json.data(using: .utf8)!)
33+
XCTAssertEqual(String(data: encoded, encoding: .utf8), json)
34+
XCTAssertEqual(region, decoded)
35+
}
36+
}
37+
38+
func testZoneWithRawAndCodable() throws {
39+
let number = 23
40+
let encoder = JSONEncoder()
41+
let decoder = JSONDecoder()
42+
for region in Self.allRegions {
43+
let zone = TencentCloud.Zone(rawValue: "\(region)-\(number)")
44+
XCTAssertNotNil(zone)
45+
let json = "\"\(zone!)\""
46+
let encoded = try encoder.encode(zone)
47+
let decoded = try decoder.decode(TencentCloud.Zone.self, from: json.data(using: .utf8)!)
48+
XCTAssertEqual(String(data: encoded, encoding: .utf8), json)
49+
XCTAssertEqual(zone, decoded)
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)