mirror of
https://github.com/certimate-go/certimate.git
synced 2026-09-24 23:10:13 +08:00
refactor: clean code
This commit is contained in:
@@ -2,7 +2,6 @@ package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -73,11 +72,11 @@ func NewDNSProvider() (*DNSProvider, error) {
|
||||
|
||||
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("byteplus: the configuration of the DNS provider is nil")
|
||||
return nil, fmt.Errorf("byteplus: the configuration of the DNS provider is nil")
|
||||
}
|
||||
|
||||
if config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, errors.New("byteplus: missing credentials")
|
||||
return nil, fmt.Errorf("byteplus: missing credentials")
|
||||
}
|
||||
|
||||
client := bpdns.InitDNSBytePlusClient()
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -79,7 +78,7 @@ func NewDNSProvider() (*DNSProvider, error) {
|
||||
|
||||
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("linode: the configuration of the DNS provider is nil")
|
||||
return nil, fmt.Errorf("linode: the configuration of the DNS provider is nil")
|
||||
}
|
||||
|
||||
client, err := linodesdk.NewClient(
|
||||
|
||||
@@ -117,7 +117,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
|
||||
},
|
||||
}
|
||||
ngincxUploadCertificateResp, err := c.sdkClient.NginxUploadCertificateWithContext(ctx, nginxCreateCertificateResp.Id, ngincxUploadCertificateReq)
|
||||
c.logger.Debug("sdk request 'nginx.UploadCertificate'", slog.Int64("request.certId", nginxCreateCertificateResp.Id), slog.Any("request", ngincxUploadCertificateReq), slog.Any("response", ngincxUploadCertificateResp))
|
||||
c.logger.Debug("sdk request 'nginx.UploadCertificate'", slog.Int64("params.certId", nginxCreateCertificateResp.Id), slog.Any("request", ngincxUploadCertificateReq), slog.Any("response", ngincxUploadCertificateResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'nginx.UploadCertificate': %w", err)
|
||||
}
|
||||
@@ -149,7 +149,7 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
|
||||
},
|
||||
}
|
||||
ngincxUploadCertificateResp, err := c.sdkClient.NginxUploadCertificateWithContext(ctx, certId, ngincxUploadCertificateReq)
|
||||
c.logger.Debug("sdk request 'nginx.UploadCertificate'", slog.Int64("request.certId", certId), slog.Any("request", ngincxUploadCertificateReq), slog.Any("response", ngincxUploadCertificateResp))
|
||||
c.logger.Debug("sdk request 'nginx.UploadCertificate'", slog.Int64("params.certId", certId), slog.Any("request", ngincxUploadCertificateReq), slog.Any("response", ngincxUploadCertificateResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'nginx.UploadCertificate': %w", err)
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
|
||||
}
|
||||
|
||||
getSslCertListResp, err := c.sdkClient.GetSslCertList(ctx, getSslCertListMarker, 200)
|
||||
c.logger.Debug("sdk request 'sslcert.GetList'", slog.Any("request.marker", getSslCertListMarker), slog.Any("response", getSslCertListResp))
|
||||
c.logger.Debug("sdk request 'sslcert.GetList'", slog.Any("params.marker", getSslCertListMarker), slog.Any("response", getSslCertListResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'sslcert.GetList': %w", err)
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
|
||||
// 上传新证书
|
||||
// REF: https://cloud.tencent.com/document/api/400/41665
|
||||
uploadCertificateReq := tcssl.NewUploadCertificateRequest()
|
||||
uploadCertificateReq.ProjectId = lo.IfF(c.config.ProjectId != 0, func() *uint64 { return common.Uint64Ptr(uint64(c.config.ProjectId)) }).Else(nil)
|
||||
uploadCertificateReq.ProjectId = lo.EmptyableToPtr(uint64(c.config.ProjectId))
|
||||
uploadCertificateReq.CertificatePublicKey = common.StringPtr(certPEM)
|
||||
uploadCertificateReq.CertificatePrivateKey = common.StringPtr(privkeyPEM)
|
||||
uploadCertificateReq.Repeatable = common.BoolPtr(false)
|
||||
|
||||
@@ -97,7 +97,7 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
|
||||
ID: lo.ToPtr(d.config.CertificateId),
|
||||
Certificate: lo.ToPtr(certPEM),
|
||||
PrivateKey: lo.ToPtr(privkeyPEM),
|
||||
SNIs: lo.Map(certX509.DNSNames, func(s string, _ int) *string { return lo.ToPtr(s) }),
|
||||
SNIs: lo.ToSlicePtr(certX509.DNSNames),
|
||||
Type: lo.ToPtr("server"),
|
||||
Status: lo.ToPtr(int32(1)),
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
|
||||
Id: lo.ToPtr(d.config.CertificateId),
|
||||
Cert: lo.ToPtr(certPEM),
|
||||
Key: lo.ToPtr(privkeyPEM),
|
||||
SNIs: lo.Map(certX509.DNSNames, func(s string, _ int) *string { return lo.ToPtr(s) }),
|
||||
SNIs: lo.ToSlicePtr(certX509.DNSNames),
|
||||
}
|
||||
upsertCertificateResp, err := d.sdkClient.UpsertCertificateWithContext(ctx, d.config.CertificateId, upsertCertificateReq)
|
||||
d.logger.Debug("sdk request 'UpsertCertificate'", slog.String("params.certificateId", d.config.CertificateId), slog.Any("request", upsertCertificateReq), slog.Any("response", upsertCertificateResp))
|
||||
|
||||
@@ -85,7 +85,7 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
|
||||
// 获取 SSL 证书 详情
|
||||
// REF: https://doc.samwaf.com/api/
|
||||
sslConfigDetailResp, err := d.sdkClient.SslConfigDetailWithContext(ctx, d.config.CertificateId)
|
||||
d.logger.Debug("sdk request 'sslconfig.Detail'", slog.Any("request.sslId", d.config.CertificateId), slog.Any("response", sslConfigDetailResp))
|
||||
d.logger.Debug("sdk request 'sslconfig.Detail'", slog.Any("params.sslId", d.config.CertificateId), slog.Any("response", sslConfigDetailResp))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute sdk request 'sslconfig.Detail': %w", err)
|
||||
} else if sslConfigDetailResp.Data == nil || sslConfigDetailResp.Data.Id == "" {
|
||||
|
||||
@@ -271,7 +271,7 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
|
||||
// 更新加速域名配置
|
||||
// REF: https://cloud.tencent.com/document/api/228/41116
|
||||
updateDomainConfigReq := tccdn.NewUpdateDomainConfigRequest()
|
||||
updateDomainConfigReq.ProjectId = lo.IfF(d.config.ProjectId != 0, func() *int64 { return common.Int64Ptr(d.config.ProjectId) }).Else(nil)
|
||||
updateDomainConfigReq.ProjectId = lo.EmptyableToPtr(d.config.ProjectId)
|
||||
updateDomainConfigReq.Domain = common.StringPtr(domain)
|
||||
updateDomainConfigReq.Https = domainConfig.Https
|
||||
if updateDomainConfigReq.Https == nil {
|
||||
|
||||
@@ -268,7 +268,7 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
|
||||
// 更新加速域名配置
|
||||
// REF: https://cloud.tencent.com/document/api/228/41116
|
||||
updateDomainConfigReq := tccdn.NewUpdateDomainConfigRequest()
|
||||
updateDomainConfigReq.ProjectId = lo.IfF(d.config.ProjectId != 0, func() *int64 { return common.Int64Ptr(d.config.ProjectId) }).Else(nil)
|
||||
updateDomainConfigReq.ProjectId = lo.EmptyableToPtr(d.config.ProjectId)
|
||||
updateDomainConfigReq.Domain = common.StringPtr(domain)
|
||||
updateDomainConfigReq.Https = domainConfig.Https
|
||||
if updateDomainConfigReq.Https == nil {
|
||||
|
||||
@@ -124,7 +124,7 @@ func (d *Deployer) executeUpdateCertificateInstance(ctx context.Context, certPEM
|
||||
var deployRecordId string
|
||||
if _, err := xwait.UntilWithContext(ctx, func(_ context.Context, _ int) (bool, error) {
|
||||
updateCertificateInstanceReq := tcssl.NewUpdateCertificateInstanceRequest()
|
||||
updateCertificateInstanceReq.ProjectId = lo.IfF(d.config.ProjectId != 0, func() *uint64 { return common.Uint64Ptr(uint64(d.config.ProjectId)) }).Else(nil)
|
||||
updateCertificateInstanceReq.ProjectId = lo.EmptyableToPtr(uint64(d.config.ProjectId))
|
||||
updateCertificateInstanceReq.OldCertificateId = common.StringPtr(d.config.CertificateId)
|
||||
updateCertificateInstanceReq.CertificateId = common.StringPtr(upres.CertId)
|
||||
updateCertificateInstanceReq.ResourceTypes = common.StringPtrs(d.config.ResourceProducts)
|
||||
|
||||
Reference in New Issue
Block a user