#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Region {
CnQingdao,
CnBeijing,
CnZhangjiakou,
CnWulanchabu,
CnHangzhou,
CnShanghai,
CnShenzhen,
CnGuangzhou,
CnChengdu,
CnHongkong,
ApSoutheast1,
ApSoutheast2,
ApSoutheast5,
ApSouth1,
EuCentral1,
EuWest1,
UsEast1,
UsWest1,
CnNorth2Gov1,
Custom(String),
}
impl Region {
pub fn id(&self) -> &str {
match self {
Self::CnQingdao => "cn-qingdao",
Self::CnBeijing => "cn-beijing",
Self::CnZhangjiakou => "cn-zhangjiakou",
Self::CnWulanchabu => "cn-wulanchabu",
Self::CnHangzhou => "cn-hangzhou",
Self::CnShanghai => "cn-shanghai",
Self::CnShenzhen => "cn-shenzhen",
Self::CnGuangzhou => "cn-guangzhou",
Self::CnChengdu => "cn-chengdu",
Self::CnHongkong => "cn-hongkong",
Self::ApSoutheast1 => "ap-southeast-1",
Self::ApSoutheast2 => "ap-southeast-2",
Self::ApSoutheast5 => "ap-southeast-5",
Self::ApSouth1 => "ap-south-1",
Self::EuCentral1 => "eu-central-1",
Self::EuWest1 => "eu-west-1",
Self::UsEast1 => "us-east-1",
Self::UsWest1 => "us-west-1",
Self::CnNorth2Gov1 => "cn-north-2-gov-1",
Self::Custom(id) => id,
}
}
pub fn public_endpoint(&self) -> String {
format!("https://imm.{}.aliyuncs.com", self.id())
}
pub fn vpc_endpoint(&self) -> String {
format!("https://imm-vpc.{}.aliyuncs.com", self.id())
}
}
impl std::fmt::Display for Region {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.id())
}
}
impl AsRef<str> for Region {
fn as_ref(&self) -> &str {
self.id()
}
}
impl<T: AsRef<str> + ?Sized> From<&T> for Region {
fn from(s: &T) -> Self {
match s.as_ref() {
"cn-qingdao" => Self::CnQingdao,
"cn-beijing" => Self::CnBeijing,
"cn-zhangjiakou" => Self::CnZhangjiakou,
"cn-wulanchabu" => Self::CnWulanchabu,
"cn-hangzhou" => Self::CnHangzhou,
"cn-shanghai" => Self::CnShanghai,
"cn-shenzhen" => Self::CnShenzhen,
"cn-guangzhou" => Self::CnGuangzhou,
"cn-chengdu" => Self::CnChengdu,
"cn-hongkong" => Self::CnHongkong,
"ap-southeast-1" => Self::ApSoutheast1,
"ap-southeast-2" => Self::ApSoutheast2,
"ap-southeast-5" => Self::ApSoutheast5,
"ap-south-1" => Self::ApSouth1,
"eu-central-1" => Self::EuCentral1,
"eu-west-1" => Self::EuWest1,
"us-east-1" => Self::UsEast1,
"us-west-1" => Self::UsWest1,
"cn-north-2-gov-1" => Self::CnNorth2Gov1,
other => Self::Custom(other.to_string()),
}
}
}
impl From<String> for Region {
fn from(s: String) -> Self {
let region = Self::from(s.as_str());
if matches!(region, Self::Custom(_)) {
Self::Custom(s)
} else {
region
}
}
}