yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/cloudprovider/endpoints.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cloudprovider
    16  
    17  import (
    18  	"reflect"
    19  	"strings"
    20  
    21  	"yunion.io/x/pkg/utils"
    22  )
    23  
    24  // SHCSOEndpoints 华为私有云endpoints配置
    25  /*
    26  endpoint获取方式优先级:
    27  通过参数明确指定使用指定endpoint。否则,程序根据华为云endpoint命名规则自动拼接endpoint
    28  */
    29  type SHCSOEndpoints struct {
    30  	caches map[string]string
    31  
    32  	// 华为私有云Endpoint域名
    33  	// example: hcso.com.cn
    34  	// required:true
    35  	EndpointDomain string `default:"$HUAWEI_ENDPOINT_DOMAIN" metavar:"$HUAWEI_ENDPOINT_DOMAIN"`
    36  
    37  	// 默认DNS
    38  	// example: 10.125.0.26,10.125.0.27
    39  	// required: false
    40  	DefaultSubnetDns string `default:"$HUAWEI_DEFAULT_SUBNET_DNS" metavar:"$HUAWEI_DEFAULT_SUBNET_DNS"`
    41  
    42  	// 弹性云服务
    43  	Ecs string `default:"$HUAWEI_ECS_ENDPOINT"`
    44  	// 云容器服务
    45  	Cce string `default:"$HUAWEI_CCE_ENDPOINT"`
    46  	// 弹性伸缩服务
    47  	As string `default:"$HUAWEI_AS_ENDPOINT"`
    48  	// 统一身份认证服务
    49  	Iam string `default:"$HUAWEI_IAM_ENDPOINT"`
    50  	// 镜像服务
    51  	Ims string `default:"$HUAWEI_IMS_ENDPOINT"`
    52  	// 云服务器备份服务
    53  	Csbs string `default:"$HUAWEI_CSBS_ENDPOINT"`
    54  	// 云容器实例 CCI
    55  	Cci string `default:"$HUAWEI_CCI_ENDPOINT"`
    56  	// 裸金属服务器
    57  	Bms string `default:"$HUAWEI_BMS_ENDPOINT"`
    58  	// 云硬盘 EVS
    59  	Evs string `default:"$HUAWEI_EVS_ENDPOINT"`
    60  	// 云硬盘备份 VBS
    61  	Vbs string `default:"$HUAWEI_VBS_ENDPOINT"`
    62  	// 对象存储服务 OBS
    63  	Obs string `default:"$HUAWEI_OBS_ENDPOINT"`
    64  	// 虚拟私有云 VPC
    65  	Vpc string `default:"$HUAWEI_VPC_ENDPOINT"`
    66  	// 弹性负载均衡 ELB
    67  	Elb string `default:"$HUAWEI_ELB_ENDPOINT"`
    68  	// 合作伙伴运营能力
    69  	Bss string `default:"$HUAWEI_BSS_ENDPOINT"`
    70  	// Nat网关 NAT
    71  	Nat string `default:"$HUAWEI_NAT_ENDPOINT"`
    72  	// 分布式缓存服务
    73  	Dcs string `default:"$HUAWEI_DCS_ENDPOINT"`
    74  	// 关系型数据库 RDS
    75  	Rds string `default:"$HUAWEI_RDS_ENDPOINT"`
    76  	// 云审计服务
    77  	Cts string `default:"$HUAWEI_CTS_ENDPOINT"`
    78  	// 监控服务 CloudEye
    79  	Ces string `default:"$HUAWEI_CES_ENDPOINT"`
    80  	// 企业项目
    81  	Eps string `default:"$HUAWEI_EPS_ENDPOINT"`
    82  	// 文件系统
    83  	SfsTurbo string `default:"$HUAWEI_SFS_TURBO_ENDPOINT"`
    84  	// Modelarts
    85  	Modelarts string `default:"$HUAWEI_MODELARTS_ENDPOINT"`
    86  }
    87  
    88  func (self *SHCSOEndpoints) GetEndpoint(defaultRegion, serviceName string, region string) string {
    89  	sn := utils.Kebab2Camel(serviceName, "-")
    90  	if self.caches == nil {
    91  		self.caches = make(map[string]string, 0)
    92  	}
    93  
    94  	key := defaultRegion + "." + sn
    95  	if len(region) > 0 {
    96  		key = region + "." + sn
    97  	}
    98  
    99  	if endpoint, ok := self.caches[key]; ok && len(endpoint) > 0 {
   100  		return endpoint
   101  	}
   102  
   103  	var endpoint string
   104  	fileds := reflect.Indirect(reflect.ValueOf(self))
   105  	f := fileds.FieldByNameFunc(func(c string) bool {
   106  		return c == sn
   107  	})
   108  
   109  	if f.Kind() == reflect.String {
   110  		endpoint = f.String()
   111  	}
   112  
   113  	if len(endpoint) == 0 {
   114  		endpoint = strings.Join([]string{serviceName, defaultRegion, self.EndpointDomain}, ".")
   115  	}
   116  
   117  	if len(region) > 0 {
   118  		endpoint = strings.Replace(endpoint, defaultRegion, region, 1)
   119  	}
   120  
   121  	self.caches[key] = endpoint
   122  	return endpoint
   123  }