2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"errors"
6
7
"fmt"
7
8
"log/slog"
9
+ "regexp"
10
+ "strings"
8
11
"time"
9
12
10
13
xerrors "github.com/pkg/errors"
@@ -75,6 +78,7 @@ func (d *DeployerProvider) Deploy(ctx context.Context, certPem string, privkeyPe
75
78
76
79
// 新增证书
77
80
// REF: https://portal.baishancloud.com/track/document/downloadPdf/1441
81
+ certificateId := ""
78
82
createCertificateReq := & bssdk.CreateCertificateRequest {
79
83
Certificate : certPem ,
80
84
Key : privkeyPem ,
@@ -83,7 +87,19 @@ func (d *DeployerProvider) Deploy(ctx context.Context, certPem string, privkeyPe
83
87
createCertificateResp , err := d .sdkClient .CreateCertificate (createCertificateReq )
84
88
d .logger .Debug ("sdk request 'baishan.CreateCertificate'" , slog .Any ("request" , createCertificateReq ), slog .Any ("response" , createCertificateResp ))
85
89
if err != nil {
86
- return nil , xerrors .Wrap (err , "failed to execute sdk request 'baishan.CreateCertificate'" )
90
+ if createCertificateResp != nil {
91
+ if createCertificateResp .GetCode () == 400699 && strings .Contains (createCertificateResp .GetMessage (), "this certificate is exists" ) {
92
+ // 证书已存在,忽略新增证书接口错误
93
+ re := regexp .MustCompile (`\d+` )
94
+ certificateId = re .FindString (createCertificateResp .GetMessage ())
95
+ }
96
+ }
97
+
98
+ if certificateId == "" {
99
+ return nil , xerrors .Wrap (err , "failed to execute sdk request 'baishan.CreateCertificate'" )
100
+ }
101
+ } else {
102
+ certificateId = createCertificateResp .Data .CertId .String ()
87
103
}
88
104
89
105
// 设置域名配置
@@ -92,7 +108,7 @@ func (d *DeployerProvider) Deploy(ctx context.Context, certPem string, privkeyPe
92
108
Domains : d .config .Domain ,
93
109
Config : & bssdk.DomainConfig {
94
110
Https : & bssdk.DomainConfigHttps {
95
- CertId : createCertificateResp . Data . CertId ,
111
+ CertId : json . Number ( certificateId ) ,
96
112
ForceHttps : getDomainConfigResp .Data [0 ].Config .Https .ForceHttps ,
97
113
EnableHttp2 : getDomainConfigResp .Data [0 ].Config .Https .EnableHttp2 ,
98
114
EnableOcsp : getDomainConfigResp .Data [0 ].Config .Https .EnableOcsp ,
0 commit comments