mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-10 06:29:52 +00:00
enhance: resolve error on tencent cdn dupe deployment
优化:腾讯云cdn重复部署报错的问题
This commit is contained in:
parent
9d4d14db06
commit
38dc8a63d9
@ -195,3 +195,13 @@ func getDeployVariables(conf domain.DeployConfig) map[string]string {
|
||||
|
||||
return rs
|
||||
}
|
||||
|
||||
// contains 检查字符串切片中是否包含指定的字符串
|
||||
func contains(slice []string, item string) bool {
|
||||
for _, s := range slice {
|
||||
if s == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@ -2,7 +2,6 @@ package deployer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
@ -100,16 +99,23 @@ func (d *TencentCDNDeployer) deploy(certId string) error {
|
||||
// 如果是泛域名就从cdn列表下获取SSL证书中的可用域名
|
||||
domain := getDeployString(d.option.DeployConfig, "domain")
|
||||
if strings.Contains(domain, "*") {
|
||||
list, errGetList := d.getDomainList()
|
||||
list, errGetList := d.getDomainList(certId)
|
||||
if errGetList != nil {
|
||||
return fmt.Errorf("failed to get certificate domain list: %w", errGetList)
|
||||
}
|
||||
if list == nil || len(list) == 0 {
|
||||
return fmt.Errorf("failed to get certificate domain list: empty list.")
|
||||
if len(list) == 0 {
|
||||
d.infos = append(d.infos, "没有需要部署的实例")
|
||||
return nil
|
||||
}
|
||||
request.InstanceIdList = common.StringPtrs(list)
|
||||
} else { // 否则直接使用传入的域名
|
||||
request.InstanceIdList = common.StringPtrs([]string{domain})
|
||||
if(isDomainDeployed(certId, domain)){
|
||||
d.infos = append(d.infos, "域名已部署")
|
||||
return nil
|
||||
}
|
||||
else{
|
||||
request.InstanceIdList = common.StringPtrs([]string{domain})
|
||||
}
|
||||
}
|
||||
|
||||
// 返回的resp是一个DeployCertificateInstanceResponse的实例,与请求对象对应
|
||||
@ -121,23 +127,61 @@ func (d *TencentCDNDeployer) deploy(certId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *TencentCDNDeployer) getDomainList() ([]string, error) {
|
||||
func (d *TencentCDNDeployer) getDomainList(certId string) ([]string, error) {
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.Endpoint = "cdn.tencentcloudapi.com"
|
||||
client, _ := cdn.NewClient(d.credential, "", cpf)
|
||||
|
||||
request := cdn.NewDescribeCertDomainsRequest()
|
||||
|
||||
cert := base64.StdEncoding.EncodeToString([]byte(d.option.Certificate.Certificate))
|
||||
request.Cert = &cert
|
||||
request.CertId = common.StringPtr(certId)
|
||||
|
||||
response, err := client.DescribeCertDomains(request)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get domain list: %w", err)
|
||||
}
|
||||
|
||||
deployedDomains, err := d.getDeployedDomainList(certId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get deployed domain list: %w", err)
|
||||
}
|
||||
|
||||
domains := make([]string, 0)
|
||||
for _, domain := range response.Response.Domains {
|
||||
domainStr := *domain
|
||||
if(contains(deployedDomains, domainStr)){
|
||||
domains = append(domains, domainStr)
|
||||
}
|
||||
}
|
||||
|
||||
return domains, nil
|
||||
}
|
||||
|
||||
func (d *TencentCDNDeployer) isDomainDeployed(certId, domain string) (bool, error) {
|
||||
deployedDomains, err := d.getDeployedDomainList(certId)
|
||||
if(err != nil){
|
||||
return false, err
|
||||
}
|
||||
|
||||
return contains(deployedDomains, domain), nil
|
||||
}
|
||||
|
||||
func (d *TencentCDNDeployer) getDeployedDomainList(certId string) ([]string, error) {
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.Endpoint = "ssl.tencentcloudapi.com"
|
||||
client, _ := ssl.NewClient(d.credential, "", cpf)
|
||||
|
||||
request := ssl.NewDescribeDeployedResourcesRequest()
|
||||
request.CertificateIds = common.StringPtrs([]string{certId})
|
||||
request.ResourceType = common.StringPtr("cdn")
|
||||
|
||||
response, err := client.DescribeDeployedResources(request)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get deployed domain list: %w", err)
|
||||
}
|
||||
|
||||
domains := make([]string, 0)
|
||||
for _, domain := range response.Response.DeployedResources[0].Resources {
|
||||
domains = append(domains, *domain)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user