mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-13 07:59: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
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@ -100,17 +99,24 @@ func (d *TencentCDNDeployer) deploy(certId string) error {
|
|||||||
// 如果是泛域名就从cdn列表下获取SSL证书中的可用域名
|
// 如果是泛域名就从cdn列表下获取SSL证书中的可用域名
|
||||||
domain := getDeployString(d.option.DeployConfig, "domain")
|
domain := getDeployString(d.option.DeployConfig, "domain")
|
||||||
if strings.Contains(domain, "*") {
|
if strings.Contains(domain, "*") {
|
||||||
list, errGetList := d.getDomainList()
|
list, errGetList := d.getDomainList(certId)
|
||||||
if errGetList != nil {
|
if errGetList != nil {
|
||||||
return fmt.Errorf("failed to get certificate domain list: %w", errGetList)
|
return fmt.Errorf("failed to get certificate domain list: %w", errGetList)
|
||||||
}
|
}
|
||||||
if list == nil || len(list) == 0 {
|
if len(list) == 0 {
|
||||||
return fmt.Errorf("failed to get certificate domain list: empty list.")
|
d.infos = append(d.infos, "没有需要部署的实例")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
request.InstanceIdList = common.StringPtrs(list)
|
request.InstanceIdList = common.StringPtrs(list)
|
||||||
} else { // 否则直接使用传入的域名
|
} else { // 否则直接使用传入的域名
|
||||||
|
if(isDomainDeployed(certId, domain)){
|
||||||
|
d.infos = append(d.infos, "域名已部署")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
else{
|
||||||
request.InstanceIdList = common.StringPtrs([]string{domain})
|
request.InstanceIdList = common.StringPtrs([]string{domain})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 返回的resp是一个DeployCertificateInstanceResponse的实例,与请求对象对应
|
// 返回的resp是一个DeployCertificateInstanceResponse的实例,与请求对象对应
|
||||||
resp, err := client.DeployCertificateInstance(request)
|
resp, err := client.DeployCertificateInstance(request)
|
||||||
@ -121,23 +127,61 @@ func (d *TencentCDNDeployer) deploy(certId string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *TencentCDNDeployer) getDomainList() ([]string, error) {
|
func (d *TencentCDNDeployer) getDomainList(certId string) ([]string, error) {
|
||||||
cpf := profile.NewClientProfile()
|
cpf := profile.NewClientProfile()
|
||||||
cpf.HttpProfile.Endpoint = "cdn.tencentcloudapi.com"
|
cpf.HttpProfile.Endpoint = "cdn.tencentcloudapi.com"
|
||||||
client, _ := cdn.NewClient(d.credential, "", cpf)
|
client, _ := cdn.NewClient(d.credential, "", cpf)
|
||||||
|
|
||||||
request := cdn.NewDescribeCertDomainsRequest()
|
request := cdn.NewDescribeCertDomainsRequest()
|
||||||
|
|
||||||
cert := base64.StdEncoding.EncodeToString([]byte(d.option.Certificate.Certificate))
|
request.CertId = common.StringPtr(certId)
|
||||||
request.Cert = &cert
|
|
||||||
|
|
||||||
response, err := client.DescribeCertDomains(request)
|
response, err := client.DescribeCertDomains(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get domain list: %w", err)
|
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)
|
domains := make([]string, 0)
|
||||||
for _, domain := range response.Response.Domains {
|
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)
|
domains = append(domains, *domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user