feat: new deployment provider: ctcccloud cdn
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package ctcccloudcdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/core/deployer"
|
||||
"github.com/usual2970/certimate/internal/pkg/core/uploader"
|
||||
uploadersp "github.com/usual2970/certimate/internal/pkg/core/uploader/providers/ctcccloud-cdn"
|
||||
ctyuncdn "github.com/usual2970/certimate/internal/pkg/sdk3rd/ctyun/cdn"
|
||||
typeutil "github.com/usual2970/certimate/internal/pkg/utils/type"
|
||||
)
|
||||
|
||||
type DeployerConfig struct {
|
||||
// 天翼云 AccessKeyId。
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
// 天翼云 SecretAccessKey。
|
||||
SecretAccessKey string `json:"secretAccessKey"`
|
||||
// 加速域名(支持泛域名)。
|
||||
Domain string `json:"domain"`
|
||||
}
|
||||
|
||||
type DeployerProvider struct {
|
||||
config *DeployerConfig
|
||||
logger *slog.Logger
|
||||
sdkClient *ctyuncdn.Client
|
||||
sslUploader uploader.Uploader
|
||||
}
|
||||
|
||||
var _ deployer.Deployer = (*DeployerProvider)(nil)
|
||||
|
||||
func NewDeployer(config *DeployerConfig) (*DeployerProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
client, err := createSdkClient(config.AccessKeyId, config.SecretAccessKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create sdk client: %w", err)
|
||||
}
|
||||
|
||||
uploader, err := uploadersp.NewUploader(&uploadersp.UploaderConfig{
|
||||
AccessKeyId: config.AccessKeyId,
|
||||
SecretAccessKey: config.SecretAccessKey,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create ssl uploader: %w", err)
|
||||
}
|
||||
|
||||
return &DeployerProvider{
|
||||
config: config,
|
||||
logger: slog.Default(),
|
||||
sdkClient: client,
|
||||
sslUploader: uploader,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *DeployerProvider) WithLogger(logger *slog.Logger) deployer.Deployer {
|
||||
if logger == nil {
|
||||
d.logger = slog.New(slog.DiscardHandler)
|
||||
} else {
|
||||
d.logger = logger
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *DeployerProvider) Deploy(ctx context.Context, certPEM string, privkeyPEM string) (*deployer.DeployResult, error) {
|
||||
if d.config.Domain == "" {
|
||||
return nil, errors.New("config `domain` is required")
|
||||
}
|
||||
|
||||
// 上传证书到 CDN
|
||||
upres, err := d.sslUploader.Upload(ctx, certPEM, privkeyPEM)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to upload certificate file: %w", err)
|
||||
} else {
|
||||
d.logger.Info("ssl certificate uploaded", slog.Any("result", upres))
|
||||
}
|
||||
|
||||
// 查询域名配置信息
|
||||
// REF: https://eop.ctyun.cn/ebp/ctapiDocument/search?sid=108&api=11304&data=161&isNormal=1&vid=154
|
||||
queryDomainDetailReq := &ctyuncdn.QueryDomainDetailRequest{
|
||||
Domain: typeutil.ToPtr(d.config.Domain),
|
||||
}
|
||||
queryDomainDetailResp, err := d.sdkClient.QueryDomainDetail(queryDomainDetailReq)
|
||||
d.logger.Debug("sdk request 'cdn.QueryDomainDetail'", slog.Any("request", queryDomainDetailReq), slog.Any("response", queryDomainDetailResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cdn.QueryDomainDetail': %w", err)
|
||||
}
|
||||
|
||||
// 修改域名配置
|
||||
// REF: https://eop.ctyun.cn/ebp/ctapiDocument/search?sid=108&api=11308&data=161&isNormal=1&vid=154
|
||||
updateDomainReq := &ctyuncdn.UpdateDomainRequest{
|
||||
Domain: typeutil.ToPtr(d.config.Domain),
|
||||
HttpsStatus: typeutil.ToPtr("on"),
|
||||
CertName: typeutil.ToPtr(upres.CertName),
|
||||
}
|
||||
updateDomainResp, err := d.sdkClient.UpdateDomain(updateDomainReq)
|
||||
d.logger.Debug("sdk request 'cdn.UpdateDomain'", slog.Any("request", updateDomainReq), slog.Any("response", updateDomainResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cdn.UpdateDomain': %w", err)
|
||||
}
|
||||
|
||||
return &deployer.DeployResult{}, nil
|
||||
}
|
||||
|
||||
func createSdkClient(accessKeyId, secretAccessKey string) (*ctyuncdn.Client, error) {
|
||||
return ctyuncdn.NewClient(accessKeyId, secretAccessKey)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package ctcccloudcdn_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
provider "github.com/usual2970/certimate/internal/pkg/core/deployer/providers/ctcccloud-cdn"
|
||||
)
|
||||
|
||||
var (
|
||||
fInputCertPath string
|
||||
fInputKeyPath string
|
||||
fAccessKeyId string
|
||||
fSecretAccessKey string
|
||||
fDomain string
|
||||
)
|
||||
|
||||
func init() {
|
||||
argsPrefix := "CERTIMATE_DEPLOYER_CTCCCLOUDCDN_"
|
||||
|
||||
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
|
||||
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
|
||||
flag.StringVar(&fAccessKeyId, argsPrefix+"ACCESSKEYID", "", "")
|
||||
flag.StringVar(&fSecretAccessKey, argsPrefix+"SECRETACCESSKEY", "", "")
|
||||
flag.StringVar(&fDomain, argsPrefix+"DOMAIN", "", "")
|
||||
}
|
||||
|
||||
/*
|
||||
Shell command to run this test:
|
||||
|
||||
go test -v ./ctcccloud_cdn_test.go -args \
|
||||
--CERTIMATE_DEPLOYER_CTCCCLOUDCDN_INPUTCERTPATH="/path/to/your-input-cert.pem" \
|
||||
--CERTIMATE_DEPLOYER_CTCCCLOUDCDN_INPUTKEYPATH="/path/to/your-input-key.pem" \
|
||||
--CERTIMATE_DEPLOYER_CTCCCLOUDCDN_ACCESSKEYID="your-access-key-id" \
|
||||
--CERTIMATE_DEPLOYER_CTCCCLOUDCDN_SECRETACCESSKEY="your-secret-access-key" \
|
||||
--CERTIMATE_DEPLOYER_CTCCCLOUDCDN_DOMAIN="example.com"
|
||||
*/
|
||||
func TestDeploy(t *testing.T) {
|
||||
flag.Parse()
|
||||
|
||||
t.Run("Deploy", func(t *testing.T) {
|
||||
t.Log(strings.Join([]string{
|
||||
"args:",
|
||||
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||
fmt.Sprintf("ACCESSKEYID: %v", fAccessKeyId),
|
||||
fmt.Sprintf("SECRETACCESSKEY: %v", fSecretAccessKey),
|
||||
fmt.Sprintf("DOMAIN: %v", fDomain),
|
||||
}, "\n"))
|
||||
|
||||
deployer, err := provider.NewDeployer(&provider.DeployerConfig{
|
||||
AccessKeyId: fAccessKeyId,
|
||||
SecretAccessKey: fSecretAccessKey,
|
||||
Domain: fDomain,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fInputCertData, _ := os.ReadFile(fInputCertPath)
|
||||
fInputKeyData, _ := os.ReadFile(fInputKeyPath)
|
||||
res, err := deployer.Deploy(context.Background(), string(fInputCertData), string(fInputKeyData))
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Logf("ok: %v", res)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package ctcccloudcdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/core/uploader"
|
||||
ctyuncdn "github.com/usual2970/certimate/internal/pkg/sdk3rd/ctyun/cdn"
|
||||
certutil "github.com/usual2970/certimate/internal/pkg/utils/cert"
|
||||
typeutil "github.com/usual2970/certimate/internal/pkg/utils/type"
|
||||
)
|
||||
|
||||
type UploaderConfig struct {
|
||||
// 天翼云 AccessKeyId。
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
// 天翼云 SecretAccessKey。
|
||||
SecretAccessKey string `json:"secretAccessKey"`
|
||||
}
|
||||
|
||||
type UploaderProvider struct {
|
||||
config *UploaderConfig
|
||||
logger *slog.Logger
|
||||
sdkClient *ctyuncdn.Client
|
||||
}
|
||||
|
||||
var _ uploader.Uploader = (*UploaderProvider)(nil)
|
||||
|
||||
func NewUploader(config *UploaderConfig) (*UploaderProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
client, err := createSdkClient(config.AccessKeyId, config.SecretAccessKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create sdk client: %w", err)
|
||||
}
|
||||
|
||||
return &UploaderProvider{
|
||||
config: config,
|
||||
logger: slog.Default(),
|
||||
sdkClient: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *UploaderProvider) WithLogger(logger *slog.Logger) uploader.Uploader {
|
||||
if logger == nil {
|
||||
u.logger = slog.New(slog.DiscardHandler)
|
||||
} else {
|
||||
u.logger = logger
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *UploaderProvider) Upload(ctx context.Context, certPEM string, privkeyPEM string) (*uploader.UploadResult, error) {
|
||||
// 解析证书内容
|
||||
certX509, err := certutil.ParseCertificateFromPEM(certPEM)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 查询证书列表,避免重复上传
|
||||
// REF: https://eop.ctyun.cn/ebp/ctapiDocument/search?sid=108&api=10901&data=161&isNormal=1&vid=154
|
||||
queryCertListPage := int32(1)
|
||||
queryCertListPerPage := int32(1000)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
queryCertListReq := &ctyuncdn.QueryCertListRequest{
|
||||
Page: typeutil.ToPtr(queryCertListPage),
|
||||
PerPage: typeutil.ToPtr(queryCertListPerPage),
|
||||
UsageMode: typeutil.ToPtr(int32(0)),
|
||||
}
|
||||
queryCertListResp, err := u.sdkClient.QueryCertList(queryCertListReq)
|
||||
u.logger.Debug("sdk request 'cdn.QueryCertList'", slog.Any("request", queryCertListReq), slog.Any("response", queryCertListResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cdn.QueryCertList': %w", err)
|
||||
}
|
||||
|
||||
if queryCertListResp.ReturnObj != nil {
|
||||
for _, certRecord := range queryCertListResp.ReturnObj.Results {
|
||||
// 对比证书通用名称
|
||||
if !strings.EqualFold(certX509.Subject.CommonName, certRecord.CN) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 对比证书扩展名称
|
||||
if !slices.Equal(certX509.DNSNames, certRecord.SANs) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 对比证书有效期
|
||||
if !certX509.NotBefore.Equal(time.Unix(certRecord.IssueTime, 0).UTC()) {
|
||||
continue
|
||||
} else if !certX509.NotAfter.Equal(time.Unix(certRecord.ExpiresTime, 0).UTC()) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 查询证书详情
|
||||
// REF: https://eop.ctyun.cn/ebp/ctapiDocument/search?sid=108&api=10899&data=161&isNormal=1&vid=154
|
||||
queryCertDetailReq := &ctyuncdn.QueryCertDetailRequest{
|
||||
Id: typeutil.ToPtr(certRecord.Id),
|
||||
}
|
||||
queryCertDetailResp, err := u.sdkClient.QueryCertDetail(queryCertDetailReq)
|
||||
u.logger.Debug("sdk request 'cdn.QueryCertDetail'", slog.Any("request", queryCertDetailReq), slog.Any("response", queryCertDetailResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cdn.QueryCertDetail': %w", err)
|
||||
} else if queryCertDetailResp.ReturnObj != nil && queryCertDetailResp.ReturnObj.Result != nil {
|
||||
var isSameCert bool
|
||||
if queryCertDetailResp.ReturnObj.Result.Certs == certPEM {
|
||||
isSameCert = true
|
||||
} else {
|
||||
oldCertX509, err := certutil.ParseCertificateFromPEM(queryCertDetailResp.ReturnObj.Result.Certs)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
isSameCert = certutil.EqualCertificate(certX509, oldCertX509)
|
||||
}
|
||||
|
||||
// 如果已存在相同证书,直接返回
|
||||
if isSameCert {
|
||||
u.logger.Info("ssl certificate already exists")
|
||||
return &uploader.UploadResult{
|
||||
CertId: fmt.Sprintf("%d", queryCertDetailResp.ReturnObj.Result.Id),
|
||||
CertName: queryCertDetailResp.ReturnObj.Result.Name,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if queryCertListResp.ReturnObj == nil || len(queryCertListResp.ReturnObj.Results) < int(queryCertListPerPage) {
|
||||
break
|
||||
} else {
|
||||
queryCertListPage++
|
||||
}
|
||||
}
|
||||
|
||||
// 生成新证书名(需符合天翼云命名规则)
|
||||
certName := fmt.Sprintf("certimate-%d", time.Now().UnixMilli())
|
||||
|
||||
// 创建证书
|
||||
// REF: https://eop.ctyun.cn/ebp/ctapiDocument/search?sid=108&api=10893&data=161&isNormal=1&vid=154
|
||||
createCertReq := &ctyuncdn.CreateCertRequest{
|
||||
Name: typeutil.ToPtr(certName),
|
||||
Certs: typeutil.ToPtr(certPEM),
|
||||
Key: typeutil.ToPtr(privkeyPEM),
|
||||
}
|
||||
createCertResp, err := u.sdkClient.CreateCert(createCertReq)
|
||||
u.logger.Debug("sdk request 'cdn.CreateCert'", slog.Any("request", createCertReq), slog.Any("response", createCertResp))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cdn.CreateCert': %w", err)
|
||||
}
|
||||
|
||||
return &uploader.UploadResult{
|
||||
CertId: fmt.Sprintf("%d", createCertResp.ReturnObj.Id),
|
||||
CertName: certName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func createSdkClient(accessKeyId, secretAccessKey string) (*ctyuncdn.Client, error) {
|
||||
return ctyuncdn.NewClient(accessKeyId, secretAccessKey)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ctcccloudcdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
provider "github.com/usual2970/certimate/internal/pkg/core/uploader/providers/ctcccloud-cdn"
|
||||
)
|
||||
|
||||
var (
|
||||
fInputCertPath string
|
||||
fInputKeyPath string
|
||||
fAccessKeyId string
|
||||
fSecretAccessKey string
|
||||
)
|
||||
|
||||
func init() {
|
||||
argsPrefix := "CERTIMATE_UPLOADER_CTCCCLOUDCDN_"
|
||||
|
||||
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
|
||||
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
|
||||
flag.StringVar(&fAccessKeyId, argsPrefix+"ACCESSKEYID", "", "")
|
||||
flag.StringVar(&fSecretAccessKey, argsPrefix+"SECRETACCESSKEY", "", "")
|
||||
}
|
||||
|
||||
/*
|
||||
Shell command to run this test:
|
||||
|
||||
go test -v ./ctcccloud_cdn_test.go -args \
|
||||
--CERTIMATE_UPLOADER_CTCCCLOUDCDN_INPUTCERTPATH="/path/to/your-input-cert.pem" \
|
||||
--CERTIMATE_UPLOADER_CTCCCLOUDCDN_INPUTKEYPATH="/path/to/your-input-key.pem" \
|
||||
--CERTIMATE_DEPLOYER_CTCCCLOUDCDN_ACCESSKEYID="your-access-key-id" \
|
||||
--CERTIMATE_DEPLOYER_CTCCCLOUDCDN_SECRETACCESSKEY="your-secret-access-key"
|
||||
*/
|
||||
func TestDeploy(t *testing.T) {
|
||||
flag.Parse()
|
||||
|
||||
t.Run("Deploy", func(t *testing.T) {
|
||||
t.Log(strings.Join([]string{
|
||||
"args:",
|
||||
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||
fmt.Sprintf("ACCESSKEYID: %v", fAccessKeyId),
|
||||
fmt.Sprintf("SECRETACCESSKEY: %v", fSecretAccessKey),
|
||||
}, "\n"))
|
||||
|
||||
uploader, err := provider.NewUploader(&provider.UploaderConfig{
|
||||
AccessKeyId: fAccessKeyId,
|
||||
SecretAccessKey: fSecretAccessKey,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fInputCertData, _ := os.ReadFile(fInputCertPath)
|
||||
fInputKeyData, _ := os.ReadFile(fInputKeyPath)
|
||||
res, err := uploader.Upload(context.Background(), string(fInputCertData), string(fInputKeyData))
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
sres, _ := json.Marshal(res)
|
||||
t.Logf("ok: %s", string(sres))
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package jdcloudssl
|
||||
package wangsucertificate
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,9 +9,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
wangsusdk "github.com/usual2970/certimate/internal/pkg/sdk3rd/wangsu/certificate"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/core/uploader"
|
||||
wangsusdk "github.com/usual2970/certimate/internal/pkg/sdk3rd/wangsu/certificate"
|
||||
certutil "github.com/usual2970/certimate/internal/pkg/utils/cert"
|
||||
typeutil "github.com/usual2970/certimate/internal/pkg/utils/type"
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package jdcloudssl_test
|
||||
package wangsucertificate_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -20,7 +20,7 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
argsPrefix := "CERTIMATE_UPLOADER_JDCLOUDSSL_"
|
||||
argsPrefix := "CERTIMATE_UPLOADER_WANGSUCERTIFICATE_"
|
||||
|
||||
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
|
||||
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
|
||||
|
||||
41
internal/pkg/sdk3rd/ctyun/cdn/api_create_cert.go
Normal file
41
internal/pkg/sdk3rd/ctyun/cdn/api_create_cert.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CreateCertRequest struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
Certs *string `json:"certs,omitempty"`
|
||||
Key *string `json:"key,omitempty"`
|
||||
}
|
||||
|
||||
type CreateCertResponse struct {
|
||||
baseResult
|
||||
|
||||
ReturnObj *struct {
|
||||
Id int32 `json:"id"`
|
||||
} `json:"returnObj,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) CreateCert(req *CreateCertRequest) (*CreateCertResponse, error) {
|
||||
return c.CreateCertWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) CreateCertWithContext(ctx context.Context, req *CreateCertRequest) (*CreateCertResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/v1/cert/creat-cert")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &CreateCertResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
51
internal/pkg/sdk3rd/ctyun/cdn/api_query_cert_detail.go
Normal file
51
internal/pkg/sdk3rd/ctyun/cdn/api_query_cert_detail.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type QueryCertDetailRequest struct {
|
||||
Id *int32 `json:"id,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
UsageMode *int32 `json:"usage_mode,omitempty"`
|
||||
}
|
||||
|
||||
type QueryCertDetailResponse struct {
|
||||
baseResult
|
||||
|
||||
ReturnObj *struct {
|
||||
Result *CertDetail `json:"result,omitempty"`
|
||||
} `json:"returnObj,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) QueryCertDetail(req *QueryCertDetailRequest) (*QueryCertDetailResponse, error) {
|
||||
return c.QueryCertDetailWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) QueryCertDetailWithContext(ctx context.Context, req *QueryCertDetailRequest) (*QueryCertDetailResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodGet, "/v1/cert/query-cert-detail")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if req.Id != nil {
|
||||
httpreq.SetQueryParam("id", strconv.Itoa(int(*req.Id)))
|
||||
}
|
||||
if req.Name != nil {
|
||||
httpreq.SetQueryParam("name", *req.Name)
|
||||
}
|
||||
if req.UsageMode != nil {
|
||||
httpreq.SetQueryParam("usage_mode", strconv.Itoa(int(*req.UsageMode)))
|
||||
}
|
||||
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &QueryCertDetailResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
55
internal/pkg/sdk3rd/ctyun/cdn/api_query_cert_list.go
Normal file
55
internal/pkg/sdk3rd/ctyun/cdn/api_query_cert_list.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type QueryCertListRequest struct {
|
||||
Page *int32 `json:"page,omitempty"`
|
||||
PerPage *int32 `json:"per_page,omitempty"`
|
||||
UsageMode *int32 `json:"usage_mode,omitempty"`
|
||||
}
|
||||
|
||||
type QueryCertListResponse struct {
|
||||
baseResult
|
||||
|
||||
ReturnObj *struct {
|
||||
Results []*CertRecord `json:"result,omitempty"`
|
||||
Page int32 `json:"page,omitempty"`
|
||||
PerPage int32 `json:"per_page,omitempty"`
|
||||
TotalPage int32 `json:"total_page,omitempty"`
|
||||
TotalRecords int32 `json:"total_records,omitempty"`
|
||||
} `json:"returnObj,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) QueryCertList(req *QueryCertListRequest) (*QueryCertListResponse, error) {
|
||||
return c.QueryCertListWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) QueryCertListWithContext(ctx context.Context, req *QueryCertListRequest) (*QueryCertListResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodGet, "/v1/cert/query-cert-list")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if req.Page != nil {
|
||||
httpreq.SetQueryParam("page", strconv.Itoa(int(*req.Page)))
|
||||
}
|
||||
if req.PerPage != nil {
|
||||
httpreq.SetQueryParam("per_page", strconv.Itoa(int(*req.PerPage)))
|
||||
}
|
||||
if req.UsageMode != nil {
|
||||
httpreq.SetQueryParam("usage_mode", strconv.Itoa(int(*req.UsageMode)))
|
||||
}
|
||||
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &QueryCertListResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
64
internal/pkg/sdk3rd/ctyun/cdn/api_query_domain_detail.go
Normal file
64
internal/pkg/sdk3rd/ctyun/cdn/api_query_domain_detail.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type QueryDomainDetailRequest struct {
|
||||
Domain *string `json:"domain,omitempty"`
|
||||
ProductCode *string `json:"product_code,omitempty"`
|
||||
FunctionNames *string `json:"function_names,omitempty"`
|
||||
}
|
||||
|
||||
type QueryDomainDetailResponse struct {
|
||||
baseResult
|
||||
|
||||
ReturnObj *struct {
|
||||
Domain string `json:"domain"`
|
||||
ProductCode string `json:"product_code"`
|
||||
Status int32 `json:"status"`
|
||||
AreaScope int32 `json:"area_scope"`
|
||||
Cname string `json:"cname"`
|
||||
HttpsStatus string `json:"https_status"`
|
||||
HttpsBasic *struct {
|
||||
HttpsForce string `json:"https_force"`
|
||||
HttpForce string `json:"http_force"`
|
||||
ForceStatus string `json:"force_status"`
|
||||
OriginProtocol string `json:"origin_protocol"`
|
||||
} `json:"https_basic,omitempty"`
|
||||
CertName string `json:"cert_name"`
|
||||
Ssl string `json:"ssl"`
|
||||
SslStapling string `json:"ssl_stapling"`
|
||||
} `json:"returnObj,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) QueryDomainDetail(req *QueryDomainDetailRequest) (*QueryDomainDetailResponse, error) {
|
||||
return c.QueryDomainDetailWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) QueryDomainDetailWithContext(ctx context.Context, req *QueryDomainDetailRequest) (*QueryDomainDetailResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodGet, "/v1/domain/query-domain-detail")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if req.Domain != nil {
|
||||
httpreq.SetQueryParam("domain", *req.Domain)
|
||||
}
|
||||
if req.ProductCode != nil {
|
||||
httpreq.SetQueryParam("product_code", *req.ProductCode)
|
||||
}
|
||||
if req.FunctionNames != nil {
|
||||
httpreq.SetQueryParam("function_names", *req.FunctionNames)
|
||||
}
|
||||
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &QueryDomainDetailResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
37
internal/pkg/sdk3rd/ctyun/cdn/api_update_domain.go
Normal file
37
internal/pkg/sdk3rd/ctyun/cdn/api_update_domain.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UpdateDomainRequest struct {
|
||||
Domain *string `json:"domain,omitempty"`
|
||||
HttpsStatus *string `json:"https_status,omitempty"`
|
||||
CertName *string `json:"cert_name,omitempty"`
|
||||
}
|
||||
|
||||
type UpdateDomainResponse struct {
|
||||
baseResult
|
||||
}
|
||||
|
||||
func (c *Client) UpdateDomain(req *UpdateDomainRequest) (*UpdateDomainResponse, error) {
|
||||
return c.UpdateDomainWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) UpdateDomainWithContext(ctx context.Context, req *UpdateDomainRequest) (*UpdateDomainResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/v1/domain/update-domain")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &UpdateDomainResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
40
internal/pkg/sdk3rd/ctyun/cdn/client.go
Normal file
40
internal/pkg/sdk3rd/ctyun/cdn/client.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/usual2970/certimate/internal/pkg/sdk3rd/ctyun/openapi"
|
||||
)
|
||||
|
||||
const endpoint = "https://ctcdn-global.ctapi.ctyun.cn"
|
||||
|
||||
type Client struct {
|
||||
client *openapi.Client
|
||||
}
|
||||
|
||||
func NewClient(accessKeyId, secretAccessKey string) (*Client, error) {
|
||||
client, err := openapi.NewClient(endpoint, accessKeyId, secretAccessKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{client: client}, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetTimeout(timeout time.Duration) *Client {
|
||||
c.client.SetTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) newRequest(method string, path string) (*resty.Request, error) {
|
||||
return c.client.NewRequest(method, path)
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(request *resty.Request) (*resty.Response, error) {
|
||||
return c.client.DoRequest(request)
|
||||
}
|
||||
|
||||
func (c *Client) doRequestWithResult(request *resty.Request, result any) (*resty.Response, error) {
|
||||
return c.client.DoRequestWithResult(request, result)
|
||||
}
|
||||
30
internal/pkg/sdk3rd/ctyun/cdn/types.go
Normal file
30
internal/pkg/sdk3rd/ctyun/cdn/types.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package cdn
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type baseResult struct {
|
||||
StatusCode json.RawMessage `json:"statusCode,omitempty"`
|
||||
Message *string `json:"message,omitempty"`
|
||||
Error *string `json:"error,omitempty"`
|
||||
ErrorMessage *string `json:"errorMessage,omitempty"`
|
||||
RequestId *string `json:"requestId,omitempty"`
|
||||
}
|
||||
|
||||
type CertRecord struct {
|
||||
Id int32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CN string `json:"cn"`
|
||||
SANs []string `json:"sans"`
|
||||
UsageMode int32 `json:"usage_mode"`
|
||||
State int32 `json:"state"`
|
||||
ExpiresTime int64 `json:"expires"`
|
||||
IssueTime int64 `json:"issue"`
|
||||
Issuer string `json:"issuer"`
|
||||
CreatedTime int64 `json:"created"`
|
||||
}
|
||||
|
||||
type CertDetail struct {
|
||||
CertRecord
|
||||
Certs string `json:"certs"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
@@ -29,16 +29,16 @@ func (c *Client) AddRecord(req *AddRecordRequest) (*AddRecordResponse, error) {
|
||||
}
|
||||
|
||||
func (c *Client) AddRecordWithContext(ctx context.Context, req *AddRecordRequest) (*AddRecordResponse, error) {
|
||||
request, err := c.newRequest(http.MethodPost, "/v2/addRecord")
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/v2/addRecord")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
request.SetContext(ctx)
|
||||
request.SetBody(req)
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &AddRecordResponse{}
|
||||
if _, err := c.doRequestWithResult(request, result); err != nil {
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
||||
@@ -18,16 +18,16 @@ func (c *Client) DeleteRecord(req *DeleteRecordRequest) (*DeleteRecordResponse,
|
||||
}
|
||||
|
||||
func (c *Client) DeleteRecordWithContext(ctx context.Context, req *DeleteRecordRequest) (*DeleteRecordResponse, error) {
|
||||
request, err := c.newRequest(http.MethodPost, "/v2/deleteRecord")
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/v2/deleteRecord")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
request.SetContext(ctx)
|
||||
request.SetBody(req)
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &DeleteRecordResponse{}
|
||||
if _, err := c.doRequestWithResult(request, result); err != nil {
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
||||
@@ -18,16 +18,7 @@ type QueryRecordListResponse struct {
|
||||
baseResult
|
||||
|
||||
ReturnObj *struct {
|
||||
Records []*struct {
|
||||
RecordId int32 `json:"recordId"`
|
||||
Host string `json:"host"`
|
||||
Type string `json:"type"`
|
||||
LineCode string `json:"lineCode"`
|
||||
Value string `json:"value"`
|
||||
TTL int32 `json:"ttl"`
|
||||
State int32 `json:"state"`
|
||||
Remark string `json:"remark"`
|
||||
} `json:"records,omitempty"`
|
||||
Records []*DnsRecord `json:"records,omitempty"`
|
||||
} `json:"returnObj,omitempty"`
|
||||
}
|
||||
|
||||
@@ -36,16 +27,16 @@ func (c *Client) QueryRecordList(req *QueryRecordListRequest) (*QueryRecordListR
|
||||
}
|
||||
|
||||
func (c *Client) QueryRecordListWithContext(ctx context.Context, req *QueryRecordListRequest) (*QueryRecordListResponse, error) {
|
||||
request, err := c.newRequest(http.MethodGet, "/v2/queryRecordList")
|
||||
httpreq, err := c.newRequest(http.MethodGet, "/v2/queryRecordList")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
request.SetContext(ctx)
|
||||
request.SetBody(req)
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &QueryRecordListResponse{}
|
||||
if _, err := c.doRequestWithResult(request, result); err != nil {
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
||||
@@ -30,16 +30,16 @@ func (c *Client) UpdateRecord(req *UpdateRecordRequest) (*UpdateRecordResponse,
|
||||
}
|
||||
|
||||
func (c *Client) UpdateRecordWithContext(ctx context.Context, req *UpdateRecordRequest) (*UpdateRecordResponse, error) {
|
||||
request, err := c.newRequest(http.MethodPost, "/v2/updateRecord")
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/v2/updateRecord")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
request.SetContext(ctx)
|
||||
request.SetBody(req)
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &UpdateRecordResponse{}
|
||||
if _, err := c.doRequestWithResult(request, result); err != nil {
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
||||
@@ -9,3 +9,14 @@ type baseResult struct {
|
||||
ErrorMessage *string `json:"errorMessage,omitempty"`
|
||||
RequestId *string `json:"requestId,omitempty"`
|
||||
}
|
||||
|
||||
type DnsRecord struct {
|
||||
RecordId int32 `json:"recordId"`
|
||||
Host string `json:"host"`
|
||||
Type string `json:"type"`
|
||||
LineCode string `json:"lineCode"`
|
||||
Value string `json:"value"`
|
||||
TTL int32 `json:"ttl"`
|
||||
State int32 `json:"state"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
@@ -22,16 +22,16 @@ type Client struct {
|
||||
|
||||
func NewClient(endpoint, accessKeyId, secretAccessKey string) (*Client, error) {
|
||||
if endpoint == "" {
|
||||
return nil, fmt.Errorf("sdk error: unset endpoint")
|
||||
return nil, fmt.Errorf("sdkerr: unset endpoint")
|
||||
}
|
||||
if _, err := url.Parse(endpoint); err != nil {
|
||||
return nil, fmt.Errorf("sdk error: invalid endpoint: %w", err)
|
||||
return nil, fmt.Errorf("sdkerr: invalid endpoint: %w", err)
|
||||
}
|
||||
if accessKeyId == "" {
|
||||
return nil, fmt.Errorf("sdk error: unset accessKey")
|
||||
return nil, fmt.Errorf("sdkerr: unset accessKey")
|
||||
}
|
||||
if secretAccessKey == "" {
|
||||
return nil, fmt.Errorf("sdk error: unset secretKey")
|
||||
return nil, fmt.Errorf("sdkerr: unset secretKey")
|
||||
}
|
||||
|
||||
client := resty.New().
|
||||
@@ -83,7 +83,7 @@ func NewClient(endpoint, accessKeyId, secretAccessKey string) (*Client, error) {
|
||||
hasher.Write([]byte(accessKeyId))
|
||||
kak := hasher.Sum(nil)
|
||||
|
||||
// 生成 kdata
|
||||
// 生成 kdate
|
||||
hasher = hmac.New(sha256.New, kak)
|
||||
hasher.Write([]byte(now.Format("20060102")))
|
||||
kdate := hasher.Sum(nil)
|
||||
@@ -114,10 +114,10 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
|
||||
|
||||
func (c *Client) NewRequest(method string, path string) (*resty.Request, error) {
|
||||
if method == "" {
|
||||
return nil, fmt.Errorf("sdk error: unset method")
|
||||
return nil, fmt.Errorf("sdkerr: unset method")
|
||||
}
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("sdk error: unset path")
|
||||
return nil, fmt.Errorf("sdkerr: unset path")
|
||||
}
|
||||
|
||||
req := c.client.R()
|
||||
@@ -128,7 +128,7 @@ func (c *Client) NewRequest(method string, path string) (*resty.Request, error)
|
||||
|
||||
func (c *Client) DoRequest(request *resty.Request) (*resty.Response, error) {
|
||||
if request == nil {
|
||||
return nil, fmt.Errorf("sdk error: nil request")
|
||||
return nil, fmt.Errorf("sdkerr: nil request")
|
||||
}
|
||||
|
||||
// WARN:
|
||||
@@ -136,9 +136,9 @@ func (c *Client) DoRequest(request *resty.Request) (*resty.Response, error) {
|
||||
|
||||
resp, err := request.Send()
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("sdk error: failed to send request: %w", err)
|
||||
return resp, fmt.Errorf("sdkerr: failed to send request: %w", err)
|
||||
} else if resp.IsError() {
|
||||
return resp, fmt.Errorf("sdk error: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
||||
return resp, fmt.Errorf("sdkerr: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
@@ -146,7 +146,7 @@ func (c *Client) DoRequest(request *resty.Request) (*resty.Response, error) {
|
||||
|
||||
func (c *Client) DoRequestWithResult(request *resty.Request, result any) (*resty.Response, error) {
|
||||
if request == nil {
|
||||
return nil, fmt.Errorf("sdk error: nil request")
|
||||
return nil, fmt.Errorf("sdkerr: nil request")
|
||||
}
|
||||
|
||||
response, err := c.DoRequest(request)
|
||||
@@ -159,7 +159,7 @@ func (c *Client) DoRequestWithResult(request *resty.Request, result any) (*resty
|
||||
|
||||
if len(response.Body()) != 0 {
|
||||
if err := json.Unmarshal(response.Body(), &result); err != nil {
|
||||
return response, fmt.Errorf("sdk error: failed to unmarshal response: %w", err)
|
||||
return response, fmt.Errorf("sdkerr: failed to unmarshal response: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package typeutil
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// 判断对象是否为 nil。
|
||||
// 与直接使用 `obj == nil` 不同,该函数会正确判断接口类型对象的真实值是否为空。
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package typeutil
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// 将对象转换为指针。
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user