refactor: initialize sdk3rd clients using functional options pattern

This commit is contained in:
Fu Diwei
2026-06-09 22:58:34 +08:00
parent dac6fa3e65
commit 50ad1b9c6d
159 changed files with 1681 additions and 674 deletions
@@ -84,7 +84,13 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
return nil, fmt.Errorf("conohavpsv3: the configuration of the DNS provider is nil")
}
client, err := conohavpssdk.NewClient(config.UserID, config.UserName, config.Password, config.TenantID, config.TenantName)
client, err := conohavpssdk.NewClient(
conohavpssdk.WithUserId(config.UserID),
conohavpssdk.WithUserName(config.UserName),
conohavpssdk.WithUserPassword(config.Password),
conohavpssdk.WithTenantId(config.TenantID),
conohavpssdk.WithTenantName(config.TenantName),
)
if err != nil {
return nil, fmt.Errorf("conohavpsv3: %w", err)
} else {
@@ -72,7 +72,9 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
return nil, fmt.Errorf("ctyun: the configuration of the DNS provider is nil")
}
client, err := ctyundns.NewClient(config.AccessKeyId, config.SecretAccessKey)
client, err := ctyundns.NewClient(
ctyundns.WithAkSk(config.AccessKeyId, config.SecretAccessKey),
)
if err != nil {
return nil, fmt.Errorf("ctyun: %w", err)
} else {
@@ -72,7 +72,9 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
return nil, fmt.Errorf("dynv6: the configuration of the DNS provider is nil")
}
client, err := dynv6sdk.NewClient(config.HTTPToken)
client, err := dynv6sdk.NewClient(
dynv6sdk.WithHttpToken(config.HTTPToken),
)
if err != nil {
return nil, fmt.Errorf("dynv6: %w", err)
} else {
@@ -73,7 +73,9 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
return nil, fmt.Errorf("qingcloud: the configuration of the DNS provider is nil")
}
client, err := qingcloudsdk.NewClient(config.AccessKey, config.AccessSecret)
client, err := qingcloudsdk.NewClient(
qingcloudsdk.WithAkSk(config.AccessKey, config.AccessSecret),
)
if err != nil {
return nil, fmt.Errorf("qingcloud: %w", err)
} else {
+7 -9
View File
@@ -299,7 +299,9 @@ const (
func createSDKClient(serverUrl, apiVersion, apiKey string, skipTlsVerify bool, nodeName string) (any, error) {
if apiVersion == sdkVersionV1 {
client, err := onepanelsdk.NewClient(serverUrl, apiKey)
client, err := onepanelsdk.NewClient(serverUrl,
onepanelsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -310,14 +312,10 @@ func createSDKClient(serverUrl, apiVersion, apiKey string, skipTlsVerify bool, n
return client, nil
} else if apiVersion == sdkVersionV2 {
var client *onepanelsdk2.Client
var err error
if nodeName == "" {
client, err = onepanelsdk2.NewClient(serverUrl, apiKey)
} else {
client, err = onepanelsdk2.NewClientWithNode(serverUrl, apiKey, nodeName)
}
client, err := onepanelsdk2.NewClient(serverUrl,
onepanelsdk2.WithApiKey(apiKey),
onepanelsdk2.WithNode(nodeName),
)
if err != nil {
return nil, err
}
@@ -71,7 +71,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -145,7 +145,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
// REF: https://docs.aws.amazon.com/acm/latest/APIReference/API_ImportCertificate.html
importCertificateReq := &awsacm.ImportCertificateInput{
Certificate: ([]byte)(serverCertPEM),
CertificateChain: ([]byte)(intermediaCertPEM),
CertificateChain: ([]byte)(issuerCertPEM),
PrivateKey: ([]byte)(privkeyPEM),
}
importCertificateResp, err := c.sdkClient.ImportCertificate(ctx, importCertificateReq)
@@ -164,7 +164,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, privkeyPEM string) (*ReplaceResult, error) {
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -174,7 +174,7 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
importCertificateReq := &awsacm.ImportCertificateInput{
CertificateArn: aws.String(certIdOrName),
Certificate: ([]byte)(serverCertPEM),
CertificateChain: ([]byte)(intermediaCertPEM),
CertificateChain: ([]byte)(issuerCertPEM),
PrivateKey: ([]byte)(privkeyPEM),
}
importCertificateResp, err := c.sdkClient.ImportCertificate(ctx, importCertificateReq)
@@ -75,7 +75,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -156,7 +156,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
ServerCertificateName: aws.String(certName),
Path: aws.String(cmp.Or(c.config.CertificatePath, "/")),
CertificateBody: aws.String(serverCertPEM),
CertificateChain: aws.String(intermediaCertPEM),
CertificateChain: aws.String(issuerCertPEM),
PrivateKey: aws.String(privkeyPEM),
}
uploadServerCertificateResp, err := c.sdkClient.UploadServerCertificate(ctx, uploadServerCertificateReq)
@@ -113,5 +113,12 @@ func (d *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(apiToken string) (*baishansdk.Client, error) {
return baishansdk.NewClient(apiToken)
client, err := baishansdk.NewClient(
baishansdk.WithApiToken(apiToken),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -169,5 +169,12 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunao.Client, error) {
return ctyunao.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunao.NewClient(
ctyunao.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -169,5 +169,12 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyuncdn.Client, error) {
return ctyuncdn.NewClient(accessKeyId, secretAccessKey)
client, err := ctyuncdn.NewClient(
ctyuncdn.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -72,7 +72,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -85,7 +85,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
uploadCertificateReq := &ctyuncms.UploadCertificateRequest{
Name: lo.ToPtr(certName),
Certificate: lo.ToPtr(serverCertPEM),
CertificateChain: lo.ToPtr(intermediaCertPEM),
CertificateChain: lo.ToPtr(issuerCertPEM),
PrivateKey: lo.ToPtr(privkeyPEM),
EncryptionStandard: lo.ToPtr("INTERNATIONAL"),
}
@@ -195,5 +195,12 @@ func (c *Certmgr) tryGetResultIfCertExists(ctx context.Context, certPEM string)
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyuncms.Client, error) {
return ctyuncms.NewClient(accessKeyId, secretAccessKey)
client, err := ctyuncms.NewClient(
ctyuncms.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -116,5 +116,12 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunelb.Client, error) {
return ctyunelb.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunelb.NewClient(
ctyunelb.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -169,5 +169,12 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunicdn.Client, error) {
return ctyunicdn.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunicdn.NewClient(
ctyunicdn.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -169,5 +169,12 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunlvdn.Client, error) {
return ctyunlvdn.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunlvdn.NewClient(
ctyunlvdn.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -7,7 +7,7 @@ import (
"time"
"github.com/certimate-go/certimate/pkg/core"
dogesdk "github.com/certimate-go/certimate/pkg/sdk3rd/dogecloud"
dogecloudsdk "github.com/certimate-go/certimate/pkg/sdk3rd/dogecloud"
)
type (
@@ -26,7 +26,7 @@ type CertmgrConfig struct {
type Certmgr struct {
config *CertmgrConfig
logger *slog.Logger
sdkClient *dogesdk.Client
sdkClient *dogecloudsdk.Client
}
var _ Provider = (*Certmgr)(nil)
@@ -62,7 +62,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
// 上传新证书
// REF: https://docs.dogecloud.com/cdn/api-cert-upload
uploadSslCertReq := &dogesdk.UploadCdnCertRequest{
uploadSslCertReq := &dogecloudsdk.UploadCdnCertRequest{
Note: certName,
Certificate: certPEM,
PrivateKey: privkeyPEM,
@@ -83,6 +83,13 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
return nil, core.ErrUnsupported
}
func createSDKClient(accessKey, secretKey string) (*dogesdk.Client, error) {
return dogesdk.NewClient(accessKey, secretKey)
func createSDKClient(accessKey, secretKey string) (*dogecloudsdk.Client, error) {
client, err := dogecloudsdk.NewClient(
dogecloudsdk.WithAkSk(accessKey, secretKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -116,7 +116,9 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*dokploysdk.Client, error) {
client, err := dokploysdk.NewClient(serverUrl, apiKey)
client, err := dokploysdk.NewClient(serverUrl,
dokploysdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -71,7 +71,7 @@ func (c *Certmgr) SetLogger(logger *slog.Logger) {
func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*UploadResult, error) {
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -86,7 +86,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
for _, certItem := range *listCertificatesResp {
if certItem.Meta.Certificate == serverCertPEM &&
certItem.Meta.CertificateKey == privkeyPEM &&
certItem.Meta.IntermediateCertificate == intermediaCertPEM {
certItem.Meta.IntermediateCertificate == issuerCertPEM {
// 如果已存在相同证书,直接返回
c.logger.Info("ssl certificate already exists")
return &UploadResult{
@@ -113,7 +113,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
CertificateMeta: npmsdk.CertificateMeta{
Certificate: serverCertPEM,
CertificateKey: privkeyPEM,
IntermediateCertificate: intermediaCertPEM,
IntermediateCertificate: issuerCertPEM,
},
}
ngincxUploadCertificateResp, err := c.sdkClient.NginxUploadCertificateWithContext(ctx, nginxCreateCertificateResp.Id, ngincxUploadCertificateReq)
@@ -135,7 +135,7 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -145,7 +145,7 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
CertificateMeta: npmsdk.CertificateMeta{
Certificate: serverCertPEM,
CertificateKey: privkeyPEM,
IntermediateCertificate: intermediaCertPEM,
IntermediateCertificate: issuerCertPEM,
},
}
ngincxUploadCertificateResp, err := c.sdkClient.NginxUploadCertificateWithContext(ctx, certId, ngincxUploadCertificateReq)
@@ -164,12 +164,16 @@ func createSDKClient(serverUrl, authMethod, username, password, apiToken string,
switch authMethod {
case "", AUTH_METHOD_PASSWORD:
{
client, err = npmsdk.NewClient(serverUrl, username, password)
client, err = npmsdk.NewClient(serverUrl,
npmsdk.WithCredentials(username, password),
)
}
case AUTH_METHOD_TOKEN:
{
client, err = npmsdk.NewClientWithJwtToken(serverUrl, apiToken)
client, err = npmsdk.NewClient(serverUrl,
npmsdk.WithJwtToken(apiToken),
)
}
}
@@ -184,5 +184,12 @@ func (c *Certmgr) tryGetResultIfCertExists(ctx context.Context, certPEM string)
}
func createSDKClient(apiKey string) (*rainyunsdk.Client, error) {
return rainyunsdk.NewClient(apiKey)
client, err := rainyunsdk.NewClient(
rainyunsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -75,7 +75,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -89,7 +89,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
createSSLReq.SSLName = ucloud.String(certName)
createSSLReq.SSLType = ucloud.String("Pem")
createSSLReq.UserCert = ucloud.String(serverCertPEM)
createSSLReq.CaCert = ucloud.String(intermediaCertPEM)
createSSLReq.CaCert = ucloud.String(issuerCertPEM)
createSSLReq.PrivateKey = ucloud.String(privkeyPEM)
createSSLResp, err := c.sdkClient.CreateSSL(createSSLReq)
c.logger.Debug("sdk request 'ulb.CreateSSL'", slog.Any("request", createSSLReq), slog.Any("response", createSSLResp))
@@ -139,7 +139,7 @@ func (c *Certmgr) tryGetResultIfCertExists(ctx context.Context, certPEM, privkey
// 对比证书及私钥内容
// 按照“网站证书、私钥、中间证书”的方式拼接
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
continue
} else {
@@ -149,7 +149,7 @@ func (c *Certmgr) tryGetResultIfCertExists(ctx context.Context, certPEM, privkey
oldSSLContent = strings.ReplaceAll(oldSSLContent, "\t", "")
oldSSLContent = strings.ReplaceAll(oldSSLContent, " ", "")
newSSLContent := serverCertPEM + privkeyPEM + intermediaCertPEM
newSSLContent := serverCertPEM + privkeyPEM + issuerCertPEM
newSSLContent = strings.ReplaceAll(newSSLContent, "\r", "")
newSSLContent = strings.ReplaceAll(newSSLContent, "\n", "")
newSSLContent = strings.ReplaceAll(newSSLContent, "\t", "")
@@ -74,7 +74,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -88,7 +88,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
createPathXSSLReq.SSLName = ucloud.String(certName)
createPathXSSLReq.SSLType = ucloud.String("Pem")
createPathXSSLReq.UserCert = ucloud.String(serverCertPEM)
createPathXSSLReq.CACert = ucloud.String(intermediaCertPEM)
createPathXSSLReq.CACert = ucloud.String(issuerCertPEM)
createPathXSSLReq.PrivateKey = ucloud.String(privkeyPEM)
createPathXSSLResp, err := c.sdkClient.CreatePathXSSL(createPathXSSLReq)
c.logger.Debug("sdk request 'pathx.CreatePathXSSL'", slog.Any("request", createPathXSSLReq), slog.Any("response", createPathXSSLResp))
@@ -138,7 +138,7 @@ func (c *Certmgr) tryGetResultIfCertExists(ctx context.Context, certPEM, privkey
// 对比证书及私钥内容
// 按照“私钥、证书链”的方式拼接
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
continue
} else {
@@ -148,7 +148,7 @@ func (c *Certmgr) tryGetResultIfCertExists(ctx context.Context, certPEM, privkey
oldSSLContent = strings.ReplaceAll(oldSSLContent, "\t", "")
oldSSLContent = strings.ReplaceAll(oldSSLContent, " ", "")
newSSLContent := privkeyPEM + serverCertPEM + intermediaCertPEM
newSSLContent := privkeyPEM + serverCertPEM + issuerCertPEM
newSSLContent = strings.ReplaceAll(newSSLContent, "\r", "")
newSSLContent = strings.ReplaceAll(newSSLContent, "\n", "")
newSSLContent = strings.ReplaceAll(newSSLContent, "\t", "")
@@ -77,5 +77,12 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
}
func createSDKClient(username, password string) (*upyunsdk.Client, error) {
return upyunsdk.NewClient(username, password)
client, err := upyunsdk.NewClient(
upyunsdk.WithCredentials(username, password),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -11,7 +11,7 @@ import (
"github.com/samber/lo"
"github.com/certimate-go/certimate/pkg/core"
wangsusdk "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/certificate"
wangsucertificate "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/certificate"
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
)
@@ -31,7 +31,7 @@ type CertmgrConfig struct {
type Certmgr struct {
config *CertmgrConfig
logger *slog.Logger
sdkClient *wangsusdk.Client
sdkClient *wangsucertificate.Client
}
var _ Provider = (*Certmgr)(nil)
@@ -105,7 +105,7 @@ func (c *Certmgr) Upload(ctx context.Context, certPEM, privkeyPEM string) (*Uplo
// 新增证书
// REF: https://www.wangsu.com/document/api-doc/25199?productCode=certificatemanagement
createCertificateReq := &wangsusdk.CreateCertificateRequest{
createCertificateReq := &wangsucertificate.CreateCertificateRequest{
Name: lo.ToPtr(certName),
Certificate: lo.ToPtr(certPEM),
PrivateKey: lo.ToPtr(privkeyPEM),
@@ -137,7 +137,7 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
// 修改证书
// REF: https://www.wangsu.com/document/api-doc/25568?productCode=certificatemanagement
updateCertificateReq := &wangsusdk.UpdateCertificateRequest{
updateCertificateReq := &wangsucertificate.UpdateCertificateRequest{
Name: lo.ToPtr(certName),
Certificate: lo.ToPtr(certPEM),
PrivateKey: lo.ToPtr(privkeyPEM),
@@ -152,6 +152,13 @@ func (c *Certmgr) Replace(ctx context.Context, certIdOrName string, certPEM, pri
return &ReplaceResult{}, nil
}
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsusdk.Client, error) {
return wangsusdk.NewClient(accessKeyId, accessKeySecret)
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsucertificate.Client, error) {
client, err := wangsucertificate.NewClient(
wangsucertificate.WithAkSk(accessKeyId, accessKeySecret),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -113,7 +113,9 @@ const (
func createSDKClient(serverUrl, apiVersion, apiKey string, skipTlsVerify bool) (any, error) {
if apiVersion == sdkVersionV1 {
client, err := onepanelsdk.NewClient(serverUrl, apiKey)
client, err := onepanelsdk.NewClient(serverUrl,
onepanelsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -124,7 +126,9 @@ func createSDKClient(serverUrl, apiVersion, apiKey string, skipTlsVerify bool) (
return client, nil
} else if apiVersion == sdkVersionV2 {
client, err := onepanelsdk2.NewClient(serverUrl, apiKey)
client, err := onepanelsdk2.NewClient(serverUrl,
onepanelsdk2.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
+7 -9
View File
@@ -403,7 +403,9 @@ const (
func createSDKClient(serverUrl, apiVersion, apiKey string, skipTlsVerify bool, nodeName string) (any, error) {
if apiVersion == sdkVersionV1 {
client, err := onepanelsdk.NewClient(serverUrl, apiKey)
client, err := onepanelsdk.NewClient(serverUrl,
onepanelsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -414,14 +416,10 @@ func createSDKClient(serverUrl, apiVersion, apiKey string, skipTlsVerify bool, n
return client, nil
} else if apiVersion == sdkVersionV2 {
var client *onepanelsdk2.Client
var err error
if nodeName == "" {
client, err = onepanelsdk2.NewClient(serverUrl, apiKey)
} else {
client, err = onepanelsdk2.NewClientWithNode(serverUrl, apiKey, nodeName)
}
client, err := onepanelsdk2.NewClient(serverUrl,
onepanelsdk2.WithApiKey(apiKey),
onepanelsdk2.WithNode(nodeName),
)
if err != nil {
return nil, err
}
+3 -1
View File
@@ -111,7 +111,9 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*apisixsdk.Client, error) {
client, err := apisixsdk.NewClient(serverUrl, apiKey)
client, err := apisixsdk.NewClient(serverUrl,
apisixsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -161,5 +161,12 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
func createSDKClient(apiToken string) (*baishansdk.Client, error) {
return baishansdk.NewClient(apiToken)
client, err := baishansdk.NewClient(
baishansdk.WithApiToken(apiToken),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -7,7 +7,7 @@ import (
"log/slog"
"github.com/certimate-go/certimate/pkg/core"
btsdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanel"
btpanelsdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanel"
)
type (
@@ -29,7 +29,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *btsdk.Client
sdkClient *btpanelsdk.Client
}
var _ Provider = (*Deployer)(nil)
@@ -61,7 +61,7 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
// 设置面板 SSL 证书
configSavePanelSSLReq := &btsdk.ConfigSavePanelSSLRequest{
configSavePanelSSLReq := &btpanelsdk.ConfigSavePanelSSLRequest{
PrivateKey: privkeyPEM,
Certificate: certPEM,
}
@@ -73,7 +73,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
if d.config.AutoRestart {
// 重启面板(无需关心响应,因为宝塔重启时会断开连接产生 error)
systemServiceAdminReq := &btsdk.SystemServiceAdminRequest{
systemServiceAdminReq := &btpanelsdk.SystemServiceAdminRequest{
Name: "nginx",
Type: "restart",
}
@@ -84,8 +84,10 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
return &DeployResult{}, nil
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btsdk.Client, error) {
client, err := btsdk.NewClient(serverUrl, apiKey)
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btpanelsdk.Client, error) {
client, err := btpanelsdk.NewClient(serverUrl,
btpanelsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -11,7 +11,7 @@ import (
"github.com/samber/lo"
"github.com/certimate-go/certimate/pkg/core"
btsdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanel"
btpanelsdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanel"
xwait "github.com/certimate-go/certimate/pkg/utils/wait"
)
@@ -36,7 +36,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *btsdk.Client
sdkClient *btpanelsdk.Client
}
var _ Provider = (*Deployer)(nil)
@@ -77,7 +77,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
case "any":
{
// 上传证书
sslCertSaveCertReq := &btsdk.SSLCertSaveCertRequest{
sslCertSaveCertReq := &btpanelsdk.SSLCertSaveCertRequest{
Certificate: certPEM,
PrivateKey: privkeyPEM,
}
@@ -88,9 +88,9 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
// 设置站点证书
sslSetBatchCertToSiteReq := &btsdk.SSLSetBatchCertToSiteRequest{
BatchInfo: lo.Map(d.config.SiteNames, func(siteName string, _ int) *btsdk.SSLSetBatchCertToSiteRequestBatchInfo {
return &btsdk.SSLSetBatchCertToSiteRequestBatchInfo{
sslSetBatchCertToSiteReq := &btpanelsdk.SSLSetBatchCertToSiteRequest{
BatchInfo: lo.Map(d.config.SiteNames, func(siteName string, _ int) *btpanelsdk.SSLSetBatchCertToSiteRequestBatchInfo {
return &btpanelsdk.SSLSetBatchCertToSiteRequestBatchInfo{
SiteName: siteName,
SSLHash: sslCertSaveCertResp.SSLHash,
}
@@ -139,7 +139,7 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName
case "proxy":
{
// 设置代理 SSL 证书
modProxyComSetSSLReq := &btsdk.ModProxyComSetSSLRequest{
modProxyComSetSSLReq := &btpanelsdk.ModProxyComSetSSLRequest{
SiteName: siteName,
Certificate: certPEM,
PrivateKey: privkeyPEM,
@@ -154,7 +154,7 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName
default:
{
// 设置站点 SSL 证书
siteSetSSLReq := &btsdk.SiteSetSSLRequest{
siteSetSSLReq := &btpanelsdk.SiteSetSSLRequest{
Type: "0",
SiteName: siteName,
Certificate: certPEM,
@@ -171,8 +171,10 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName
return nil
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btsdk.Client, error) {
client, err := btsdk.NewClient(serverUrl, apiKey)
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btpanelsdk.Client, error) {
client, err := btpanelsdk.NewClient(serverUrl,
btpanelsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -9,7 +9,7 @@ import (
"github.com/samber/lo"
"github.com/certimate-go/certimate/pkg/core"
btsdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanelgo"
btpanelgosdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanelgo"
)
type (
@@ -29,7 +29,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *btsdk.Client
sdkClient *btpanelgosdk.Client
}
var _ Provider = (*Deployer)(nil)
@@ -61,7 +61,7 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
// 设置面板 SSL 证书
configSetPanelSSLReq := &btsdk.ConfigSetPanelSSLRequest{
configSetPanelSSLReq := &btpanelgosdk.ConfigSetPanelSSLRequest{
SSLStatus: lo.ToPtr(int32(1)),
SSLKey: lo.ToPtr(privkeyPEM),
SSLPem: lo.ToPtr(certPEM),
@@ -75,8 +75,10 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
return &DeployResult{}, nil
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btsdk.Client, error) {
client, err := btsdk.NewClient(serverUrl, apiKey)
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btpanelgosdk.Client, error) {
client, err := btpanelgosdk.NewClient(serverUrl,
btpanelgosdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -14,7 +14,7 @@ import (
"github.com/samber/lo"
"github.com/certimate-go/certimate/pkg/core"
btsdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanelgo"
btpanelgosdk "github.com/certimate-go/certimate/pkg/sdk3rd/btpanelgo"
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
xwait "github.com/certimate-go/certimate/pkg/utils/wait"
)
@@ -40,7 +40,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *btsdk.Client
sdkClient *btpanelgosdk.Client
}
var _ Provider = (*Deployer)(nil)
@@ -107,7 +107,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
return &DeployResult{}, nil
}
func (d *Deployer) findSiteByName(ctx context.Context, siteType, siteName string) (*btsdk.SiteData, error) {
func (d *Deployer) findSiteByName(ctx context.Context, siteType, siteName string) (*btpanelgosdk.SiteData, error) {
if siteType == "" || lo.Contains(btProjectTypesInIIS, siteType) {
// 查询网站列表
datalistGetDataListPage := 1
@@ -119,7 +119,7 @@ func (d *Deployer) findSiteByName(ctx context.Context, siteType, siteName string
default:
}
datalistGetDataListReq := &btsdk.DatalistGetDataListRequest{
datalistGetDataListReq := &btpanelgosdk.DatalistGetDataListRequest{
Table: lo.ToPtr("sites"),
SearchString: lo.ToPtr(siteName),
Page: lo.ToPtr(int32(datalistGetDataListPage)),
@@ -154,7 +154,7 @@ func (d *Deployer) findSiteByName(ctx context.Context, siteType, siteName string
default:
}
siteGetProjectListReq := &btsdk.SiteGetProjectListRequest{
siteGetProjectListReq := &btpanelgosdk.SiteGetProjectListRequest{
SearchType: lo.ToPtr(siteType),
SearchString: lo.ToPtr(siteName),
Page: lo.ToPtr(int32(siteGetProjectListPage)),
@@ -185,7 +185,7 @@ func (d *Deployer) findSiteByName(ctx context.Context, siteType, siteName string
func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName string, certPEM, privkeyPEM string) error {
// 获取面板配置
panelGetConfigReq := &btsdk.PanelGetConfigRequest{}
panelGetConfigReq := &btpanelgosdk.PanelGetConfigRequest{}
panelGetConfigResp, err := d.sdkClient.PanelGetConfigWithContext(ctx, panelGetConfigReq)
d.logger.Debug("sdk request 'panel.GetConfig'", slog.Any("request", panelGetConfigReq), slog.Any("response", panelGetConfigResp))
if err != nil {
@@ -213,7 +213,7 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName
certPFXHashHex := hex.EncodeToString(certPFXHash[:])
certPFXPath := panelGetConfigResp.Paths.Soft + "/temp/ssl/certimate"
certPFXFileName := fmt.Sprintf("%s.pfx", certPFXHashHex)
filesUploadReq := &btsdk.FilesUploadRequest{
filesUploadReq := &btpanelgosdk.FilesUploadRequest{
Path: lo.ToPtr(certPFXPath),
Name: lo.ToPtr(certPFXFileName),
Start: lo.ToPtr(int32(0)),
@@ -228,7 +228,7 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName
}
// 服务器为 IIS,设置网站 SSL
siteSetSitePFXSSLReq := &btsdk.SiteSetSitePFXSSLRequest{
siteSetSitePFXSSLReq := &btpanelgosdk.SiteSetSitePFXSSLRequest{
SiteId: lo.ToPtr(siteData.Id),
PFX: lo.ToPtr(fmt.Sprintf("%s/%s", certPFXPath, certPFXFileName)),
Password: lo.ToPtr(certPFXPassword),
@@ -240,7 +240,7 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName
}
} else {
// 服务器非 IIS,设置网站 SSL
siteSetSiteSSLReq := &btsdk.SiteSetSiteSSLRequest{
siteSetSiteSSLReq := &btpanelgosdk.SiteSetSiteSSLRequest{
SiteId: lo.ToPtr(siteData.Id),
Status: lo.ToPtr(true),
Key: lo.ToPtr(privkeyPEM),
@@ -256,8 +256,10 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteType, siteName
return nil
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btsdk.Client, error) {
client, err := btsdk.NewClient(serverUrl, apiKey)
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btpanelgosdk.Client, error) {
client, err := btpanelgosdk.NewClient(serverUrl,
btpanelgosdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -75,7 +75,9 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btwafsdk.Client, error) {
client, err := btwafsdk.NewClient(serverUrl, apiKey)
client, err := btwafsdk.NewClient(serverUrl,
btwafsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -169,7 +169,9 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteName string, s
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*btwafsdk.Client, error) {
client, err := btwafsdk.NewClient(serverUrl, apiKey)
client, err := btwafsdk.NewClient(serverUrl,
btwafsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
@@ -81,5 +81,12 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(apiKey string) (*bunnysdk.Client, error) {
return bunnysdk.NewClient(apiKey)
client, err := bunnysdk.NewClient(
bunnysdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
return client, err
}
@@ -71,5 +71,12 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(apiToken string) (*cacheflysdk.Client, error) {
return cacheflysdk.NewClient(apiToken)
client, err := cacheflysdk.NewClient(
cacheflysdk.WithApiToken(apiToken),
)
if err != nil {
return nil, err
}
return client, nil
}
+3 -1
View File
@@ -157,7 +157,9 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
func createSDKClient(serverUrl, apiKey, apiSecret string, skipTlsVerify bool) (*cdnflysdk.Client, error) {
client, err := cdnflysdk.NewClient(serverUrl, apiKey, apiSecret)
client, err := cdnflysdk.NewClient(serverUrl,
cdnflysdk.WithApiKey(apiKey, apiSecret),
)
if err != nil {
return nil, err
}
+6 -3
View File
@@ -88,7 +88,7 @@ func (d *Deployer) deployToWebsite(ctx context.Context, certPEM, privkeyPEM stri
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return fmt.Errorf("failed to extract certs: %w", err)
}
@@ -99,7 +99,7 @@ func (d *Deployer) deployToWebsite(ctx context.Context, certPEM, privkeyPEM stri
Domain: lo.ToPtr(d.config.Domain),
Cert: lo.ToPtr(serverCertPEM),
Key: lo.ToPtr(privkeyPEM),
CABundle: lo.ToPtr(intermediaCertPEM),
CABundle: lo.ToPtr(issuerCertPEM),
}
sslInstallSSLResp, err := d.sdkClient.SSLInstallSSL(sslInstallSSLReq)
d.logger.Debug("sdk request 'SSL.install_ssl'", slog.Any("request", sslInstallSSLReq), slog.Any("response", sslInstallSSLResp))
@@ -111,7 +111,10 @@ func (d *Deployer) deployToWebsite(ctx context.Context, certPEM, privkeyPEM stri
}
func createSDKClient(serverUrl, username, apiToken string, skipTlsVerify bool) (*cpanelsdk.Client, error) {
client, err := cpanelsdk.NewClient(serverUrl, username, apiToken)
client, err := cpanelsdk.NewClient(serverUrl,
cpanelsdk.WithUsername(username),
cpanelsdk.WithApiToken(apiToken),
)
if err != nil {
return nil, err
}
@@ -255,5 +255,12 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunao.Client, error) {
return ctyunao.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunao.NewClient(
ctyunao.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -245,5 +245,12 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyuncdn.Client, error) {
return ctyuncdn.NewClient(accessKeyId, secretAccessKey)
client, err := ctyuncdn.NewClient(
ctyuncdn.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -191,5 +191,12 @@ func (d *Deployer) updateListenerCertificate(ctx context.Context, cloudListenerI
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunelb.Client, error) {
return ctyunelb.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunelb.NewClient(
ctyunelb.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -124,5 +124,12 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunfaas.Client, error) {
return ctyunfaas.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunfaas.NewClient(
ctyunfaas.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -241,5 +241,12 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunicdn.Client, error) {
return ctyunicdn.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunicdn.NewClient(
ctyunicdn.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -219,5 +219,12 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
}
func createSDKClient(accessKeyId, secretAccessKey string) (*ctyunlvdn.Client, error) {
return ctyunlvdn.NewClient(accessKeyId, secretAccessKey)
client, err := ctyunlvdn.NewClient(
ctyunlvdn.WithAkSk(accessKeyId, secretAccessKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -11,7 +11,7 @@ import (
"github.com/certimate-go/certimate/pkg/core"
cmgrimpl "github.com/certimate-go/certimate/pkg/core/certmgr/providers/dogecloud"
dogesdk "github.com/certimate-go/certimate/pkg/sdk3rd/dogecloud"
dogecloudsdk "github.com/certimate-go/certimate/pkg/sdk3rd/dogecloud"
xcerthostname "github.com/certimate-go/certimate/pkg/utils/cert/hostname"
)
@@ -35,7 +35,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *dogesdk.Client
sdkClient *dogecloudsdk.Client
sdkCertmgr core.Certmgr
}
@@ -172,7 +172,7 @@ func (d *Deployer) getAllDomains(ctx context.Context) ([]string, error) {
func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, cloudCertId int64) error {
// 绑定证书
// REF: https://docs.dogecloud.com/cdn/api-cert-bind
bindCdnCertReq := &dogesdk.BindCdnCertRequest{
bindCdnCertReq := &dogecloudsdk.BindCdnCertRequest{
CertId: cloudCertId,
Domain: domain,
}
@@ -185,6 +185,13 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
return nil
}
func createSDKClient(accessKey, secretKey string) (*dogesdk.Client, error) {
return dogesdk.NewClient(accessKey, secretKey)
func createSDKClient(accessKey, secretKey string) (*dogecloudsdk.Client, error) {
client, err := dogecloudsdk.NewClient(
dogecloudsdk.WithAkSk(accessKey, secretKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -122,7 +122,10 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
func createSDKClient(serverUrl, apiRole, accessKeyId, accessKey string, skipTlsVerify bool) (*flexcdnsdk.Client, error) {
client, err := flexcdnsdk.NewClient(serverUrl, apiRole, accessKeyId, accessKey)
client, err := flexcdnsdk.NewClient(serverUrl,
flexcdnsdk.WithRole(apiRole),
flexcdnsdk.WithAccessKey(accessKeyId, accessKey),
)
if err != nil {
return nil, err
}
+8 -1
View File
@@ -82,5 +82,12 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(apiToken string) (*flyiosdk.Client, error) {
return flyiosdk.NewClient(apiToken)
client, err := flyiosdk.NewClient(
flyiosdk.WithApiToken(apiToken),
)
if err != nil {
return nil, err
}
return client, nil
}
+2 -2
View File
@@ -84,7 +84,7 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -147,7 +147,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
if err := ftpClient.ChangeDir(ctx, filepath.Dir(d.config.FilePathForCrtOnlyIntermedia)); err != nil {
return nil, fmt.Errorf("failed to upload intermedia certificate file: %w", err)
}
if err := ftpClient.StoreString(ctx, filepath.Base(d.config.FilePathForCrtOnlyIntermedia), intermediaCertPEM); err != nil {
if err := ftpClient.StoreString(ctx, filepath.Base(d.config.FilePathForCrtOnlyIntermedia), issuerCertPEM); err != nil {
return nil, fmt.Errorf("failed to upload intermedia certificate file: %w", err)
}
d.logger.Info("ssl intermedia certificate file uploaded", slog.String("path", d.config.FilePathForCrtOnlyIntermedia))
+4 -1
View File
@@ -122,7 +122,10 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
func createSDKClient(serverUrl, apiRole, accessKeyId, accessKey string, skipTlsVerify bool) (*goedgesdk.Client, error) {
client, err := goedgesdk.NewClient(serverUrl, apiRole, accessKeyId, accessKey)
client, err := goedgesdk.NewClient(serverUrl,
goedgesdk.WithRole(apiRole),
goedgesdk.WithAccessKey(accessKeyId, accessKey),
)
if err != nil {
return nil, err
}
@@ -92,7 +92,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -174,7 +174,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
secretPayload.Data[d.config.SecretDataKeyForCrtOnlyServer] = []byte(serverCertPEM)
}
if d.config.SecretDataKeyForCrtOnlyIntermedia != "" {
secretPayload.Data[d.config.SecretDataKeyForCrtOnlyIntermedia] = []byte(intermediaCertPEM)
secretPayload.Data[d.config.SecretDataKeyForCrtOnlyIntermedia] = []byte(issuerCertPEM)
}
// 创建或更新 Secret 实例
+12 -8
View File
@@ -8,8 +8,8 @@ import (
"time"
"github.com/certimate-go/certimate/pkg/core"
leclientsdkv3 "github.com/certimate-go/certimate/pkg/sdk3rd/lecdn/v3/client"
lemastersdkv3 "github.com/certimate-go/certimate/pkg/sdk3rd/lecdn/v3/master"
lecdnclientv3 "github.com/certimate-go/certimate/pkg/sdk3rd/lecdn/v3/client"
lecdnmasterv3 "github.com/certimate-go/certimate/pkg/sdk3rd/lecdn/v3/master"
)
type (
@@ -98,9 +98,9 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
// 修改证书
// REF: https://wdk0pwf8ul.feishu.cn/wiki/YE1XwCRIHiLYeKkPupgcXrlgnDd
switch sdkClient := d.sdkClient.(type) {
case *leclientsdkv3.Client:
case *lecdnclientv3.Client:
{
updateSSLCertReq := &leclientsdkv3.UpdateCertificateRequest{
updateSSLCertReq := &lecdnclientv3.UpdateCertificateRequest{
Name: fmt.Sprintf("certimate-%d", time.Now().UnixMilli()),
Description: "upload from certimate",
Type: "upload",
@@ -115,9 +115,9 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
}
case *lemastersdkv3.Client:
case *lecdnmasterv3.Client:
{
updateSSLCertReq := &lemastersdkv3.UpdateCertificateRequest{
updateSSLCertReq := &lecdnmasterv3.UpdateCertificateRequest{
ClientId: d.config.ClientId,
Name: fmt.Sprintf("certimate-%d", time.Now().UnixMilli()),
Description: "upload from certimate",
@@ -150,7 +150,9 @@ const (
func createSDKClient(serverUrl, apiVersion, apiRole, username, password string, skipTlsVerify bool) (any, error) {
if apiVersion == sdkVersionV3 && apiRole == sdkRoleClient {
// v3 版客户端
client, err := leclientsdkv3.NewClient(serverUrl, username, password)
client, err := lecdnclientv3.NewClient(serverUrl,
lecdnclientv3.WithCredentials(username, password),
)
if err != nil {
return nil, err
}
@@ -162,7 +164,9 @@ func createSDKClient(serverUrl, apiVersion, apiRole, username, password string,
return client, nil
} else if apiVersion == sdkVersionV3 && apiRole == sdkRoleMaster {
// v3 版主控端
client, err := lemastersdkv3.NewClient(serverUrl, username, password)
client, err := lecdnmasterv3.NewClient(serverUrl,
lecdnmasterv3.WithCredentials(username, password),
)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -85,7 +85,7 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -135,7 +135,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
if d.config.FilePathForCrtOnlyIntermedia != "" {
if err := xfile.WriteString(d.config.FilePathForCrtOnlyIntermedia, intermediaCertPEM); err != nil {
if err := xfile.WriteString(d.config.FilePathForCrtOnlyIntermedia, issuerCertPEM); err != nil {
return nil, fmt.Errorf("failed to save intermedia certificate file: %w", err)
}
d.logger.Info("ssl intermedia certificate file saved", slog.String("path", d.config.FilePathForCrtOnlyIntermedia))
+10 -3
View File
@@ -79,7 +79,7 @@ func (d *Deployer) deployToWebsite(ctx context.Context, certPEM, privkeyPEM stri
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return fmt.Errorf("failed to extract certs: %w", err)
}
@@ -88,7 +88,7 @@ func (d *Deployer) deployToWebsite(ctx context.Context, certPEM, privkeyPEM stri
// REF: https://open-api.netlify.com/#tag/sniCertificate/operation/provisionSiteTLSCertificate
provisionSiteTLSCertificateReq := &netlifysdk.ProvisionSiteTLSCertificateRequest{
Certificate: serverCertPEM,
CACertificates: intermediaCertPEM,
CACertificates: issuerCertPEM,
Key: privkeyPEM,
}
provisionSiteTLSCertificateResp, err := d.sdkClient.ProvisionSiteTLSCertificateWithContext(ctx, d.config.SiteId, provisionSiteTLSCertificateReq)
@@ -101,5 +101,12 @@ func (d *Deployer) deployToWebsite(ctx context.Context, certPEM, privkeyPEM stri
}
func createSDKClient(apiToken string) (*netlifysdk.Client, error) {
return netlifysdk.NewClient(apiToken)
client, err := netlifysdk.NewClient(
netlifysdk.WithApiToken(apiToken),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -386,12 +386,16 @@ func createSDKClient(serverUrl, authMethod, username, password, apiToken string,
switch authMethod {
case "", AUTH_METHOD_PASSWORD:
{
client, err = npmsdk.NewClient(serverUrl, username, password)
client, err = npmsdk.NewClient(serverUrl,
npmsdk.WithCredentials(username, password),
)
}
case AUTH_METHOD_TOKEN:
{
client, err = npmsdk.NewClientWithJwtToken(serverUrl, apiToken)
client, err = npmsdk.NewClient(serverUrl,
npmsdk.WithJwtToken(apiToken),
)
}
}
@@ -105,5 +105,12 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(apiKey string) (*rainyunsdk.Client, error) {
return rainyunsdk.NewClient(apiKey)
client, err := rainyunsdk.NewClient(
rainyunsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -75,7 +75,9 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(serverUrl string, accessTokenId int64, accessToken string, skipTlsVerify bool) (*ratpanelsdk.Client, error) {
client, err := ratpanelsdk.NewClient(serverUrl, accessTokenId, accessToken)
client, err := ratpanelsdk.NewClient(serverUrl,
ratpanelsdk.WithAccessToken(accessTokenId, accessToken),
)
if err != nil {
return nil, err
}
@@ -158,7 +158,9 @@ func (d *Deployer) updateSiteCertificate(ctx context.Context, siteName string, c
}
func createSDKClient(serverUrl string, accessTokenId int64, accessToken string, skipTlsVerify bool) (*ratpanelsdk.Client, error) {
client, err := ratpanelsdk.NewClient(serverUrl, accessTokenId, accessToken)
client, err := ratpanelsdk.NewClient(serverUrl,
ratpanelsdk.WithAccessToken(accessTokenId, accessToken),
)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -92,7 +92,7 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -129,7 +129,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
if d.config.ObjectKeyForCrtOnlyIntermedia != "" {
if err := s3Client.PutObjectString(ctx, d.config.Bucket, d.config.ObjectKeyForCrtOnlyIntermedia, intermediaCertPEM); err != nil {
if err := s3Client.PutObjectString(ctx, d.config.Bucket, d.config.ObjectKeyForCrtOnlyIntermedia, issuerCertPEM); err != nil {
return nil, fmt.Errorf("failed to upload intermedia certificate file: %w", err)
}
d.logger.Info("ssl intermedia certificate file uploaded", slog.String("bucket", d.config.Bucket), slog.String("object", d.config.ObjectKeyForCrtOnlyIntermedia))
@@ -101,7 +101,9 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
func createSDKClient(serverUrl, apiToken string, skipTlsVerify bool) (*safelinesdk.Client, error) {
client, err := safelinesdk.NewClient(serverUrl, apiToken)
client, err := safelinesdk.NewClient(serverUrl,
safelinesdk.WithApiToken(apiToken),
)
if err != nil {
return nil, err
}
+3 -1
View File
@@ -109,7 +109,9 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*samwafsdk.Client, error) {
client, err := samwafsdk.NewClient(serverUrl, apiKey)
client, err := samwafsdk.NewClient(serverUrl,
samwafsdk.WithApiKey(apiKey),
)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -107,7 +107,7 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -165,7 +165,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
if d.config.FilePathForCrtOnlyIntermedia != "" {
if err := xssh.WriteRemoteString(sshClient.RawClient(), d.config.FilePathForCrtOnlyIntermedia, intermediaCertPEM, d.config.UseSCP); err != nil {
if err := xssh.WriteRemoteString(sshClient.RawClient(), d.config.FilePathForCrtOnlyIntermedia, issuerCertPEM, d.config.UseSCP); err != nil {
return nil, fmt.Errorf("failed to save intermedia certificate file: %w", err)
}
d.logger.Info("ssl intermedia certificate file uploaded", slog.String("path", d.config.FilePathForCrtOnlyIntermedia))
@@ -91,5 +91,12 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(username, password string) (*unicloudsdk.Client, error) {
return unicloudsdk.NewClient(username, password)
client, err := unicloudsdk.NewClient(
unicloudsdk.WithCredentials(username, password),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -261,5 +261,12 @@ func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, c
}
func createSDKClient(username, password string) (*upyunsdk.Client, error) {
return upyunsdk.NewClient(username, password)
client, err := upyunsdk.NewClient(
upyunsdk.WithCredentials(username, password),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -122,5 +122,12 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(username, password string) (*upyunsdk.Client, error) {
return upyunsdk.NewClient(username, password)
client, err := upyunsdk.NewClient(
upyunsdk.WithCredentials(username, password),
)
if err != nil {
return nil, err
}
return client, nil
}
+11 -3
View File
@@ -57,7 +57,7 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -65,7 +65,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
// 上传证书
// REF: https://vercel.com/docs/rest-api/certs/upload-a-cert
uploadCertReq := &vercelsdk.UploadCertRequest{
CA: intermediaCertPEM,
CA: issuerCertPEM,
Cert: serverCertPEM,
Key: privkeyPEM,
SkipValidation: true,
@@ -80,5 +80,13 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
func createSDKClient(apiToken, teamId string) (*vercelsdk.Client, error) {
return vercelsdk.NewClientWithTeam(apiToken, teamId)
client, err := vercelsdk.NewClient(
vercelsdk.WithApiToken(apiToken),
vercelsdk.WithTeamId(teamId),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -11,7 +11,7 @@ import (
"github.com/certimate-go/certimate/pkg/core"
cmgrimpl "github.com/certimate-go/certimate/pkg/core/certmgr/providers/wangsu-certificate"
wangsusdk "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/cdn"
wangsucdn "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/cdn"
)
type (
@@ -34,7 +34,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *wangsusdk.Client
sdkClient *wangsucdn.Client
sdkCertmgr core.Certmgr
}
@@ -105,7 +105,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
// 批量修改域名证书配置
// REF: https://www.wangsu.com/document/api-doc/37447
certId, _ := strconv.ParseInt(upres.CertId, 10, 64)
batchUpdateCertificateConfigReq := &wangsusdk.BatchUpdateCertificateConfigRequest{
batchUpdateCertificateConfigReq := &wangsucdn.BatchUpdateCertificateConfigRequest{
CertificateId: certId,
DomainNames: domains,
}
@@ -118,6 +118,13 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
return &DeployResult{}, nil
}
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsusdk.Client, error) {
return wangsusdk.NewClient(accessKeyId, accessKeySecret)
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsucdn.Client, error) {
client, err := wangsucdn.NewClient(
wangsucdn.WithAkSk(accessKeyId, accessKeySecret),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -18,7 +18,7 @@ import (
"github.com/samber/lo"
"github.com/certimate-go/certimate/pkg/core"
wangsucdn "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/cdnpro"
wangsucdnpro "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/cdnpro"
xwait "github.com/certimate-go/certimate/pkg/utils/wait"
)
@@ -52,7 +52,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *wangsucdn.Client
sdkClient *wangsucdnpro.Client
}
var _ Provider = (*Deployer)(nil)
@@ -99,7 +99,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
if err != nil {
return nil, fmt.Errorf("failed to encrypt private key: %w", err)
}
certificateNewVersionInfo := &wangsucdn.CertificateVersionInfo{
certificateNewVersionInfo := &wangsucdnpro.CertificateVersionInfo{
PrivateKey: lo.ToPtr(encryptedPrivateKey),
Certificate: lo.ToPtr(certPEM),
}
@@ -116,7 +116,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
timestamp := time.Now().Unix()
if d.config.CertificateId == "" {
// 创建证书
createCertificateReq := &wangsucdn.CreateCertificateRequest{
createCertificateReq := &wangsucdnpro.CreateCertificateRequest{
Timestamp: timestamp,
Name: lo.ToPtr(fmt.Sprintf("certimate_%d", time.Now().UnixMilli())),
AutoRenew: lo.ToPtr("Off"),
@@ -139,7 +139,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
wangsuCertVer = 1
} else {
// 更新证书
updateCertificateReq := &wangsucdn.UpdateCertificateRequest{
updateCertificateReq := &wangsucdnpro.UpdateCertificateRequest{
Timestamp: timestamp,
Name: lo.ToPtr(fmt.Sprintf("certimate_%d", time.Now().UnixMilli())),
AutoRenew: lo.ToPtr("Off"),
@@ -169,10 +169,10 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
// 创建部署任务
// REF: https://www.wangsu.com/document/api-doc/27034
var wangsuTaskId string
createDeploymentTaskReq := &wangsucdn.CreateDeploymentTaskRequest{
createDeploymentTaskReq := &wangsucdnpro.CreateDeploymentTaskRequest{
Name: lo.ToPtr(fmt.Sprintf("certimate_%d", time.Now().UnixMilli())),
Target: lo.ToPtr(d.config.Environment),
Actions: &[]wangsucdn.DeploymentTaskActionInfo{
Actions: &[]wangsucdnpro.DeploymentTaskActionInfo{
{
Action: lo.ToPtr("deploy_cert"),
CertificateId: lo.ToPtr(wangsuCertId),
@@ -218,8 +218,15 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
return &DeployResult{}, nil
}
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsucdn.Client, error) {
return wangsucdn.NewClient(accessKeyId, accessKeySecret)
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsucdnpro.Client, error) {
client, err := wangsucdnpro.NewClient(
wangsucdnpro.WithAkSk(accessKeyId, accessKeySecret),
)
if err != nil {
return nil, err
}
return client, nil
}
func encryptPrivateKey(privkeyPEM string, apiKey string, timestamp int64) (string, error) {
@@ -7,7 +7,7 @@ import (
"github.com/certimate-go/certimate/pkg/core"
cmgrimpl "github.com/certimate-go/certimate/pkg/core/certmgr/providers/wangsu-certificate"
wangsusdk "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/certificate"
wangsucertificate "github.com/certimate-go/certimate/pkg/sdk3rd/wangsu/certificate"
)
type (
@@ -28,7 +28,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *wangsusdk.Client
sdkClient *wangsucertificate.Client
sdkCertmgr core.Certmgr
}
@@ -90,6 +90,13 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
return &DeployResult{}, nil
}
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsusdk.Client, error) {
return wangsusdk.NewClient(accessKeyId, accessKeySecret)
func createSDKClient(accessKeyId, accessKeySecret string) (*wangsucertificate.Client, error) {
client, err := wangsucertificate.NewClient(
wangsucertificate.WithAkSk(accessKeyId, accessKeySecret),
)
if err != nil {
return nil, err
}
return client, nil
}
@@ -87,7 +87,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
}
// 提取服务器证书和中间证书
serverCertPEM, intermediaCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
serverCertPEM, issuerCertPEM, err := xcert.ExtractCertificatesFromPEM(certPEM)
if err != nil {
return nil, fmt.Errorf("failed to extract certs: %w", err)
}
@@ -165,7 +165,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
replaceJsonValueRecursively(webhookData, "${CERTIMATE_DEPLOYER_SUBJECTALTNAMES}", strings.Join(xcertx509.GetSubjectAltNames(certX509), ";"))
replaceJsonValueRecursively(webhookData, "${CERTIMATE_DEPLOYER_CERTIFICATE}", certPEM)
replaceJsonValueRecursively(webhookData, "${CERTIMATE_DEPLOYER_CERTIFICATE_SERVER}", serverCertPEM)
replaceJsonValueRecursively(webhookData, "${CERTIMATE_DEPLOYER_CERTIFICATE_INTERMEDIA}", intermediaCertPEM)
replaceJsonValueRecursively(webhookData, "${CERTIMATE_DEPLOYER_CERTIFICATE_INTERMEDIA}", issuerCertPEM)
replaceJsonValueRecursively(webhookData, "${CERTIMATE_DEPLOYER_PRIVATEKEY}", privkeyPEM)
// 兼容旧版变量
@@ -174,7 +174,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
replaceJsonValueRecursively(webhookData, "${DOMAINS}", strings.Join(certX509.DNSNames, ";"))
replaceJsonValueRecursively(webhookData, "${CERTIFICATE}", certPEM)
replaceJsonValueRecursively(webhookData, "${SERVER_CERTIFICATE}", serverCertPEM)
replaceJsonValueRecursively(webhookData, "${INTERMEDIA_CERTIFICATE}", intermediaCertPEM)
replaceJsonValueRecursively(webhookData, "${INTERMEDIA_CERTIFICATE}", issuerCertPEM)
replaceJsonValueRecursively(webhookData, "${PRIVATE_KEY}", privkeyPEM)
// 生成请求
@@ -37,7 +37,7 @@ func NewNotifier(config *NotifierConfig) (*Notifier, error) {
}
client := resty.New().
SetHeader("Authorization", fmt.Sprintf("Bot %s", config.BotToken)).
SetHeader("Authorization", "Bot "+config.BotToken).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent)
@@ -37,7 +37,7 @@ func NewNotifier(config *NotifierConfig) (*Notifier, error) {
}
client := resty.New().
SetHeader("Authorization", fmt.Sprintf("Bearer %s", config.BotToken)).
SetHeader("Authorization", "Bearer "+config.BotToken).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent)
+16 -11
View File
@@ -17,28 +17,33 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl, apiKey string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/api/v1").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
timestamp := fmt.Sprintf("%d", time.Now().Unix())
tokenMd5 := md5.Sum([]byte("1panel" + apiKey + timestamp))
tokenMd5 := md5.Sum([]byte("1panel" + opts.ApiKey + timestamp))
tokenMd5Hex := hex.EncodeToString(tokenMd5[:])
req.Header.Set("1Panel-Timestamp", timestamp)
req.Header.Set("1Panel-Token", tokenMd5Hex)
@@ -46,16 +51,16 @@ func NewClient(serverUrl, apiKey string) (*Client, error) {
return nil
})
return &Client{client}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -67,7 +72,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
@@ -108,8 +113,8 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tcode := res.GetCode(); tcode/100 != 2 {
return resp, fmt.Errorf("sdkerr: api error: code='%d', message='%s'", tcode, res.GetMessage())
if rCode := res.GetCode(); rCode/100 != 2 {
return resp, fmt.Errorf("sdkerr: api error: code='%d', message='%s'", rCode, res.GetMessage())
}
}
}
+13
View File
@@ -0,0 +1,13 @@
package onepanel
type Options struct {
ApiKey string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
}
}
+17 -25
View File
@@ -17,28 +17,34 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl, apiKey string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/api/v2").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("CurrentNode", opts.CurrentNode).
SetHeader("User-Agent", app.AppUserAgent).
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
timestamp := fmt.Sprintf("%d", time.Now().Unix())
tokenMd5 := md5.Sum([]byte("1panel" + apiKey + timestamp))
tokenMd5 := md5.Sum([]byte("1panel" + opts.ApiKey + timestamp))
tokenMd5Hex := hex.EncodeToString(tokenMd5[:])
req.Header.Set("1Panel-Timestamp", timestamp)
req.Header.Set("1Panel-Token", tokenMd5Hex)
@@ -46,30 +52,16 @@ func NewClient(serverUrl, apiKey string) (*Client, error) {
return nil
})
return &Client{client}, nil
}
func NewClientWithNode(serverUrl, apiKey, node string) (*Client, error) {
client, err := NewClient(serverUrl, apiKey)
if err != nil {
return nil, err
}
if node == "" {
node = "local"
}
client.client.SetHeader("CurrentNode", node)
return client, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -81,7 +73,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
@@ -122,8 +114,8 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tcode := res.GetCode(); tcode/100 != 2 {
return resp, fmt.Errorf("sdkerr: api error: code='%d', message='%s'", tcode, res.GetMessage())
if rCode := res.GetCode(); rCode/100 != 2 {
return resp, fmt.Errorf("sdkerr: api error: code='%d', message='%s'", rCode, res.GetMessage())
}
}
}
+24
View File
@@ -0,0 +1,24 @@
package v2
type Options struct {
ApiKey string
CurrentNode string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
}
}
func WithNode(node string) OptionsFunc {
if node == "" {
node = "local"
}
return func(o *Options) {
o.CurrentNode = node
}
}
+14 -11
View File
@@ -14,39 +14,42 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl, apiKey string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/apisix/admin").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetHeader("X-Api-Key", apiKey)
SetHeader("X-Api-Key", opts.ApiKey)
return &Client{
client: client,
}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -58,7 +61,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
+13
View File
@@ -0,0 +1,13 @@
package apisix
type Options struct {
ApiKey string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
}
}
+15 -10
View File
@@ -11,26 +11,31 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(apiToken string) (*Client, error) {
if apiToken == "" {
func NewClient(optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if opts.ApiToken == "" {
return nil, fmt.Errorf("sdkerr: unset apiToken")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL("https://cdn.api.baishan.com").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetQueryParam("token", apiToken)
SetQueryParam("token", opts.ApiToken)
return &Client{client}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
@@ -42,7 +47,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
@@ -83,8 +88,8 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tcode := res.GetCode(); tcode != 0 {
return resp, fmt.Errorf("sdkerr: code='%d', message='%s'", tcode, res.GetMessage())
if rCode := res.GetCode(); rCode != 0 {
return resp, fmt.Errorf("sdkerr: code='%d', message='%s'", rCode, res.GetMessage())
}
}
}
+13
View File
@@ -0,0 +1,13 @@
package baishan
type Options struct {
ApiToken string
}
type OptionsFunc func(*Options)
func WithApiToken(apiToken string) OptionsFunc {
return func(o *Options) {
o.ApiToken = apiToken
}
}
+15 -10
View File
@@ -19,39 +19,44 @@ import (
type Client struct {
apiKey string
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl, apiKey string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")).
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetHeader("User-Agent", app.AppUserAgent)
return &Client{
apiKey: apiKey,
client: client,
apiKey: opts.ApiKey,
rc: restyClient,
}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -95,7 +100,7 @@ func (c *Client) newRequest(method string, path string, params any) (*resty.Requ
data["request_time"] = fmt.Sprintf("%d", timestamp)
data["request_token"] = generateSignature(fmt.Sprintf("%d", timestamp), c.apiKey)
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
req.SetFormData(data)
@@ -138,7 +143,7 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tstatus := res.GetStatus(); tstatus != nil && !*tstatus {
if rStatus := res.GetStatus(); rStatus != nil && !*rStatus {
if res.GetMessage() == nil {
return resp, fmt.Errorf("sdkerr: api error: unknown error")
} else {
+13
View File
@@ -0,0 +1,13 @@
package btpanel
type Options struct {
ApiKey string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
}
}
+21 -16
View File
@@ -20,39 +20,44 @@ import (
type Client struct {
apiKey string
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl, apiKey string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")).
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetHeader("User-Agent", app.AppUserAgent)
return &Client{
apiKey: apiKey,
client: client,
apiKey: opts.ApiKey,
rc: restyClient,
}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -96,7 +101,7 @@ func (c *Client) newRequest(method string, path string, params any, multipart bo
data["request_time"] = fmt.Sprintf("%d", timestamp)
data["request_token"] = generateSignature(fmt.Sprintf("%d", timestamp), c.apiKey)
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
if multipart {
@@ -175,17 +180,17 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tstatus := res.GetStatus(); tstatus != nil {
if rStatus := res.GetStatus(); rStatus != nil {
var errored bool
var bstatus bool
if err := json.Unmarshal(tstatus, &bstatus); err == nil {
errored = !bstatus
var rStatusAsBool bool
if err := json.Unmarshal(rStatus, &rStatusAsBool); err == nil {
errored = !rStatusAsBool
}
var istatus int
if err := json.Unmarshal(tstatus, &istatus); err == nil {
errored = istatus != 0
var rStatusAsInt int
if err := json.Unmarshal(rStatus, &rStatusAsInt); err == nil {
errored = rStatusAsInt != 0
}
if errored {
+13
View File
@@ -0,0 +1,13 @@
package btpanelgo
type Options struct {
ApiKey string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
}
}
+16 -11
View File
@@ -17,28 +17,33 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl, apiKey string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/api").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
timestamp := fmt.Sprintf("%d", time.Now().Unix())
keyMd5 := md5.Sum([]byte(apiKey))
keyMd5 := md5.Sum([]byte(opts.ApiKey))
keyMd5Hex := strings.ToLower(hex.EncodeToString(keyMd5[:]))
signMd5 := md5.Sum([]byte(timestamp + keyMd5Hex))
signMd5Hex := strings.ToLower(hex.EncodeToString(signMd5[:]))
@@ -48,16 +53,16 @@ func NewClient(serverUrl, apiKey string) (*Client, error) {
return nil
})
return &Client{client}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -69,7 +74,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
@@ -110,8 +115,8 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if code := res.GetCode(); code != 0 {
return resp, fmt.Errorf("sdkerr: api error: code='%d'", code)
if rCode := res.GetCode(); rCode != 0 {
return resp, fmt.Errorf("sdkerr: api error: code='%d'", rCode)
}
}
}
+13
View File
@@ -0,0 +1,13 @@
package btwaf
type Options struct {
ApiKey string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
}
}
+15 -10
View File
@@ -10,26 +10,31 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(apiToken string) (*Client, error) {
if apiToken == "" {
return nil, fmt.Errorf("sdkerr: unset apiToken")
func NewClient(optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
client := resty.New().
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
restyClient := resty.New().
SetBaseURL("https://api.bunny.net").
SetHeader("Accept", "application/json").
SetHeader("AccessKey", opts.ApiKey).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetHeader("AccessKey", apiToken)
SetHeader("User-Agent", app.AppUserAgent)
return &Client{client}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
@@ -41,7 +46,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
+13
View File
@@ -0,0 +1,13 @@
package bunny
type Options struct {
ApiKey string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
}
}
+13 -8
View File
@@ -11,26 +11,31 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(apiToken string) (*Client, error) {
if apiToken == "" {
func NewClient(optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if opts.ApiToken == "" {
return nil, fmt.Errorf("sdkerr: unset apiToken")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL("https://api.cachefly.com/api/2.5").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetHeader("X-CF-Authorization", "Bearer "+apiToken)
SetHeader("X-CF-Authorization", "Bearer "+opts.ApiToken)
return &Client{client}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
@@ -42,7 +47,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
+13
View File
@@ -0,0 +1,13 @@
package cachefly
type Options struct {
ApiToken string
}
type OptionsFunc func(*Options)
func WithApiToken(apiToken string) OptionsFunc {
return func(o *Options) {
o.ApiToken = apiToken
}
}
+19 -14
View File
@@ -14,41 +14,46 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl, apiKey, apiSecret string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
if opts.ApiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
if apiSecret == "" {
if opts.ApiSecret == "" {
return nil, fmt.Errorf("sdkerr: unset apiSecret")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/v1").
SetHeader("Accept", "application/json").
SetHeader("API-Key", opts.ApiKey).
SetHeader("API-Secret", opts.ApiSecret).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetHeader("API-Key", apiKey).
SetHeader("API-Secret", apiSecret)
SetHeader("User-Agent", app.AppUserAgent)
return &Client{client}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -60,7 +65,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
@@ -101,8 +106,8 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tcode := res.GetCode(); tcode != "" && tcode != "0" {
return resp, fmt.Errorf("sdkerr: code='%s', message='%s'", tcode, res.GetMessage())
if rCode := res.GetCode(); rCode != "" && rCode != "0" {
return resp, fmt.Errorf("sdkerr: code='%s', message='%s'", rCode, res.GetMessage())
}
}
}
+15
View File
@@ -0,0 +1,15 @@
package cdnfly
type Options struct {
ApiKey string
ApiSecret string
}
type OptionsFunc func(*Options)
func WithApiKey(apiKey, apiSecret string) OptionsFunc {
return func(o *Options) {
o.ApiKey = apiKey
o.ApiSecret = apiSecret
}
}
+23 -18
View File
@@ -24,28 +24,33 @@ type Client struct {
accessTokenExp time.Time
accessTokenMtx sync.Mutex
client *resty.Client
rc *resty.Client
}
func NewClient(userId, userName, userPassword, tenantId, tenantName string) (*Client, error) {
if userId == "" && userName == "" {
func NewClient(optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if opts.UserId == "" && opts.UserName == "" {
return nil, fmt.Errorf("sdkerr: unset userId or userName")
}
if userPassword == "" {
if opts.UserPassword == "" {
return nil, fmt.Errorf("sdkerr: unset userPassword")
}
if tenantId == "" || tenantName == "" {
if opts.TenantId == "" || opts.TenantName == "" {
return nil, fmt.Errorf("sdkerr: unset tenantId or tenantName")
}
client := &Client{
userId: userId,
userName: userName,
userPassword: userPassword,
tenantId: tenantId,
tenantName: tenantName,
userId: opts.UserId,
userName: opts.UserName,
userPassword: opts.UserPassword,
tenantId: opts.TenantId,
tenantName: opts.TenantName,
}
client.client = resty.New().
client.rc = resty.New().
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
@@ -61,12 +66,12 @@ func NewClient(userId, userName, userPassword, tenantId, tenantName string) (*Cl
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -78,7 +83,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
@@ -119,8 +124,8 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tcode := res.GetCode(); tcode == 0 {
return resp, fmt.Errorf("sdkerr: code='%d', error='%s'", tcode, res.GetError())
if rCode := res.GetCode(); rCode == 0 {
return resp, fmt.Errorf("sdkerr: code='%d', error='%s'", rCode, res.GetError())
}
}
}
@@ -181,8 +186,8 @@ func (c *Client) ensureAccessTokenExists() error {
result := &createAuthTokenResponse{}
if httpresp, err := c.doRequestWithResult(httpreq, result); err != nil {
return err
} else if code := result.GetCode(); code != 0 {
return fmt.Errorf("sdkerr: failed to get conoha access token: code='%d', error='%s'", code, result.GetError())
} else if rCode := result.GetCode(); rCode != 0 {
return fmt.Errorf("sdkerr: failed to get conoha access token: code='%d', error='%s'", rCode, result.GetError())
} else {
token := httpresp.Header().Get("X-Subject-Token")
if token == "" {
+41
View File
@@ -0,0 +1,41 @@
package v3
type Options struct {
UserId string
UserName string
UserPassword string
TenantId string
TenantName string
}
type OptionsFunc func(*Options)
func WithUserId(userId string) OptionsFunc {
return func(o *Options) {
o.UserId = userId
}
}
func WithUserName(userName string) OptionsFunc {
return func(o *Options) {
o.UserName = userName
}
}
func WithUserPassword(userPassword string) OptionsFunc {
return func(o *Options) {
o.UserPassword = userPassword
}
}
func WithTenantId(tenantId string) OptionsFunc {
return func(o *Options) {
o.TenantId = tenantId
}
}
func WithTenantName(tenantName string) OptionsFunc {
return func(o *Options) {
o.TenantName = tenantName
}
}
+17 -12
View File
@@ -14,39 +14,44 @@ import (
)
type Client struct {
client *resty.Client
rc *resty.Client
}
func NewClient(serverUrl string, username, apiToken string) (*Client, error) {
func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
opts := &Options{}
for _, fn := range optFns {
fn(opts)
}
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if username == "" {
if opts.Username == "" {
return nil, fmt.Errorf("sdkerr: unset username")
}
if apiToken == "" {
if opts.ApiToken == "" {
return nil, fmt.Errorf("sdkerr: unset apiToken")
}
client := resty.New().
restyClient := resty.New().
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/execute").
SetHeader("Accept", "application/json").
SetHeader("Authorization", fmt.Sprintf("cpanel %s:%s", username, apiToken)).
SetHeader("Authorization", "cpanel "+opts.Username+":"+opts.ApiToken).
SetHeader("User-Agent", app.AppUserAgent)
return &Client{client}, nil
return &Client{rc: restyClient}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
c.rc.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
c.rc.SetTLSClientConfig(config)
return c
}
@@ -58,7 +63,7 @@ func (c *Client) newRequest(method string, path string) (*resty.Request, error)
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req := c.rc.R()
req.Method = method
req.URL = path
return req, nil
@@ -99,8 +104,8 @@ func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*rest
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
} else {
if tstatus := res.GetStatus(); tstatus == 0 {
return resp, fmt.Errorf("sdkerr: status='%d', messages='%s', warnings='%s', errors='%s'", tstatus, strings.Join(res.GetMessages(), ", "), strings.Join(res.GetWarnings(), ", "), strings.Join(res.GetErrors(), ", "))
if rStatus := res.GetStatus(); rStatus == 0 {
return resp, fmt.Errorf("sdkerr: status='%d', messages='%s', warnings='%s', errors='%s'", rStatus, strings.Join(res.GetMessages(), ", "), strings.Join(res.GetWarnings(), ", "), strings.Join(res.GetErrors(), ", "))
}
}
}
+20
View File
@@ -0,0 +1,20 @@
package cpanel
type Options struct {
Username string
ApiToken string
}
type OptionsFunc func(*Options)
func WithUsername(username string) OptionsFunc {
return func(o *Options) {
o.Username = username
}
}
func WithApiToken(apiToken string) OptionsFunc {
return func(o *Options) {
o.ApiToken = apiToken
}
}
+6 -4
View File
@@ -14,8 +14,8 @@ type Client struct {
client *openapi.Client
}
func NewClient(accessKeyId, secretAccessKey string) (*Client, error) {
client, err := openapi.NewClient(endpoint, accessKeyId, secretAccessKey)
func NewClient(optFns ...openapi.OptionsFunc) (*Client, error) {
client, err := openapi.NewClient(endpoint, optFns...)
if err != nil {
return nil, err
}
@@ -39,8 +39,10 @@ func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*resty.Response, error) {
resp, err := c.client.DoRequestWithResult(req, res)
if err == nil {
if tcode := res.GetStatusCode(); tcode != "" && tcode != "100000" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', errorCode='%s', errorMessage='%s'", tcode, res.GetMessage(), res.GetMessage(), res.GetErrorMessage())
rStatusCode := res.GetStatusCode()
rErrorCode := res.GetError()
if rStatusCode != "" && rStatusCode != "100000" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', error='%s', errorMessage='%s'", rStatusCode, res.GetMessage(), rErrorCode, res.GetErrorMessage())
}
}
+9
View File
@@ -0,0 +1,9 @@
package ao
import (
"github.com/certimate-go/certimate/pkg/sdk3rd/ctyun/openapi"
)
func WithAkSk(ak, sk string) openapi.OptionsFunc {
return openapi.WithAkSk(ak, sk)
}
+6 -4
View File
@@ -14,8 +14,8 @@ type Client struct {
client *openapi.Client
}
func NewClient(accessKeyId, secretAccessKey string) (*Client, error) {
client, err := openapi.NewClient(endpoint, accessKeyId, secretAccessKey)
func NewClient(optFns ...openapi.OptionsFunc) (*Client, error) {
client, err := openapi.NewClient(endpoint, optFns...)
if err != nil {
return nil, err
}
@@ -39,8 +39,10 @@ func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*resty.Response, error) {
resp, err := c.client.DoRequestWithResult(req, res)
if err == nil {
if tcode := res.GetStatusCode(); tcode != "" && tcode != "100000" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', errorCode='%s', errorMessage='%s'", tcode, res.GetMessage(), res.GetMessage(), res.GetErrorMessage())
rStatusCode := res.GetStatusCode()
rErrorCode := res.GetError()
if rStatusCode != "" && rStatusCode != "100000" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', error='%s', errorMessage='%s'", rStatusCode, res.GetMessage(), rErrorCode, res.GetErrorMessage())
}
}
+9
View File
@@ -0,0 +1,9 @@
package cdn
import (
"github.com/certimate-go/certimate/pkg/sdk3rd/ctyun/openapi"
)
func WithAkSk(ak, sk string) openapi.OptionsFunc {
return openapi.WithAkSk(ak, sk)
}
+6 -6
View File
@@ -14,8 +14,8 @@ type Client struct {
client *openapi.Client
}
func NewClient(accessKeyId, secretAccessKey string) (*Client, error) {
client, err := openapi.NewClient(endpoint, accessKeyId, secretAccessKey)
func NewClient(optFns ...openapi.OptionsFunc) (*Client, error) {
client, err := openapi.NewClient(endpoint, optFns...)
if err != nil {
return nil, err
}
@@ -39,10 +39,10 @@ func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*resty.Response, error) {
resp, err := c.client.DoRequestWithResult(req, res)
if err == nil {
statusCode := res.GetStatusCode()
errorCode := res.GetError()
if (statusCode != "" && statusCode != "200") || errorCode != "" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', errorCode='%s', errorMessage='%s'", statusCode, res.GetMessage(), res.GetMessage(), res.GetErrorMessage())
rStatusCode := res.GetStatusCode()
rError := res.GetError()
if (rStatusCode != "" && rStatusCode != "200") || rError != "" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', error='%s', errorMessage='%s'", rStatusCode, res.GetMessage(), rError, res.GetErrorMessage())
}
}
+9
View File
@@ -0,0 +1,9 @@
package cms
import (
"github.com/certimate-go/certimate/pkg/sdk3rd/ctyun/openapi"
)
func WithAkSk(ak, sk string) openapi.OptionsFunc {
return openapi.WithAkSk(ak, sk)
}
+6 -6
View File
@@ -14,8 +14,8 @@ type Client struct {
client *openapi.Client
}
func NewClient(accessKeyId, secretAccessKey string) (*Client, error) {
client, err := openapi.NewClient(endpoint, accessKeyId, secretAccessKey)
func NewClient(optFns ...openapi.OptionsFunc) (*Client, error) {
client, err := openapi.NewClient(endpoint, optFns...)
if err != nil {
return nil, err
}
@@ -39,10 +39,10 @@ func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
func (c *Client) doRequestWithResult(req *resty.Request, res sdkResponse) (*resty.Response, error) {
resp, err := c.client.DoRequestWithResult(req, res)
if err == nil {
statusCode := res.GetStatusCode()
errorCode := res.GetError()
if (statusCode != "" && statusCode != "200") || errorCode != "" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', errorCode='%s', errorMessage='%s'", statusCode, res.GetMessage(), res.GetMessage(), res.GetErrorMessage())
rStatusCode := res.GetStatusCode()
rError := res.GetError()
if (rStatusCode != "" && rStatusCode != "200") || rError != "" {
return resp, fmt.Errorf("sdkerr: api error: code='%s', message='%s', error='%s', errorMessage='%s'", rStatusCode, res.GetMessage(), rError, res.GetErrorMessage())
}
}

Some files were not shown because too many files have changed in this diff Show More