|
| 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 | +} |
0 commit comments