From 6d9fef3d2afd635ab26fa7f91ade2392c64dbb98 Mon Sep 17 00:00:00 2001
From: yoan <536464346@qq.com>
Date: Wed, 21 Aug 2024 22:30:33 +0800
Subject: [PATCH] Support deployment to Alibaba Cloud CDN.
---
go.mod | 1 +
go.sum | 2 +
internal/deployer/aliyun_cdn.go | 68 +++++++++++++++++++++++++++++++++
internal/deployer/deployer.go | 31 +++++++++------
ui/src/pages/domains/Edit.tsx | 3 ++
ui/src/pages/domains/Home.tsx | 7 ++++
6 files changed, 101 insertions(+), 11 deletions(-)
create mode 100644 internal/deployer/aliyun_cdn.go
diff --git a/go.mod b/go.mod
index 0ba644c3..dbca20bb 100644
--- a/go.mod
+++ b/go.mod
@@ -6,6 +6,7 @@ toolchain go1.22.5
require (
github.com/alibabacloud-go/cas-20200407/v2 v2.3.0
+ github.com/alibabacloud-go/cdn-20180510/v5 v5.0.0
github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.8
github.com/alibabacloud-go/tea v1.2.1
github.com/alibabacloud-go/tea-utils/v2 v2.0.5
diff --git a/go.sum b/go.sum
index d9d3182c..3eb1af7c 100644
--- a/go.sum
+++ b/go.sum
@@ -23,6 +23,8 @@ github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4 h1:iC9YFYKDGEy3n/FtqJ
github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4/go.mod h1:sCavSAvdzOjul4cEqeVtvlSaSScfNsTQ+46HwlTL1hc=
github.com/alibabacloud-go/cas-20200407/v2 v2.3.0 h1:nOrp0n2nFZiYN0wIG7S26YVVaMMzOBkX9GJqUvYnGeE=
github.com/alibabacloud-go/cas-20200407/v2 v2.3.0/go.mod h1:yzkgdLANANu/v56k0ptslGl++JJL4Op1V09HTavfoCo=
+github.com/alibabacloud-go/cdn-20180510/v5 v5.0.0 h1:yTKngw4rBR3hdpoo/uCyBffYXfPfjNjlaDL8nTYUIds=
+github.com/alibabacloud-go/cdn-20180510/v5 v5.0.0/go.mod h1:HxQrwVKBx3/6bIwmdDcpqBpSQt2tpi/j4LfEhl+QFPk=
github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.6/go.mod h1:CzQnh+94WDnJOnKZH5YRyouL+OOcdBnXY5VWAf0McgI=
github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.8 h1:benoD0QHDrylMzEQVpX/6uKtrN8LohT66ZlKXVJh7pM=
github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.8/go.mod h1:CzQnh+94WDnJOnKZH5YRyouL+OOcdBnXY5VWAf0McgI=
diff --git a/internal/deployer/aliyun_cdn.go b/internal/deployer/aliyun_cdn.go
new file mode 100644
index 00000000..827a008f
--- /dev/null
+++ b/internal/deployer/aliyun_cdn.go
@@ -0,0 +1,68 @@
+package deployer
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+
+ cdn20180510 "github.com/alibabacloud-go/cdn-20180510/v5/client"
+ openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
+ util "github.com/alibabacloud-go/tea-utils/v2/service"
+ "github.com/alibabacloud-go/tea/tea"
+)
+
+type AliyunCdn struct {
+ client *cdn20180510.Client
+ option *DeployerOption
+}
+
+func NewAliyunCdn(option *DeployerOption) (*AliyunCdn, error) {
+ access := &aliyunAccess{}
+ json.Unmarshal([]byte(option.Access), access)
+ a := &AliyunCdn{
+ option: option,
+ }
+ client, err := a.createClient(access.AccessKeyId, access.AccessKeySecret)
+ if err != nil {
+ return nil, err
+ }
+
+ return &AliyunCdn{
+ client: client,
+ option: option,
+ }, nil
+}
+
+func (a *AliyunCdn) Deploy(ctx context.Context) error {
+
+ certName := fmt.Sprintf("%s-%s", a.option.Domain, a.option.DomainId)
+ setCdnDomainSSLCertificateRequest := &cdn20180510.SetCdnDomainSSLCertificateRequest{
+ DomainName: tea.String(a.option.Domain),
+ CertName: tea.String(certName),
+ CertType: tea.String("upload"),
+ SSLProtocol: tea.String("on"),
+ SSLPub: tea.String(a.option.Certificate.Certificate),
+ SSLPri: tea.String(a.option.Certificate.PrivateKey),
+ CertRegion: tea.String("cn-hangzhou"),
+ }
+
+ runtime := &util.RuntimeOptions{}
+
+ _, err := a.client.SetCdnDomainSSLCertificateWithOptions(setCdnDomainSSLCertificateRequest, runtime)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (a *AliyunCdn) createClient(accessKeyId, accessKeySecret string) (_result *cdn20180510.Client, _err error) {
+ config := &openapi.Config{
+ AccessKeyId: tea.String(accessKeyId),
+ AccessKeySecret: tea.String(accessKeySecret),
+ }
+ config.Endpoint = tea.String("cdn.aliyuncs.com")
+ _result = &cdn20180510.Client{}
+ _result, _err = cdn20180510.NewClient(config)
+ return _result, _err
+}
diff --git a/internal/deployer/deployer.go b/internal/deployer/deployer.go
index a4724a7a..c12fbc75 100644
--- a/internal/deployer/deployer.go
+++ b/internal/deployer/deployer.go
@@ -13,7 +13,13 @@ const (
configTypeAliyun = "aliyun"
)
+const (
+ targetAliyunOss = "aliyun-oss"
+ targetAliyunCdn = "aliyun-cdn"
+)
+
type DeployerOption struct {
+ DomainId string `json:"domainId"`
Domain string `json:"domain"`
Product string `json:"product"`
Access string `json:"access"`
@@ -26,18 +32,21 @@ type Deployer interface {
func Get(record *models.Record) (Deployer, error) {
access := record.ExpandedOne("targetAccess")
- switch access.GetString("configType") {
- case configTypeAliyun:
- option := &DeployerOption{
- Domain: record.GetString("domain"),
- Product: getProduct(record),
- Access: access.GetString("config"),
- Certificate: applicant.Certificate{
- Certificate: record.GetString("certificate"),
- PrivateKey: record.GetString("privateKey"),
- },
- }
+ option := &DeployerOption{
+ DomainId: record.Id,
+ Domain: record.GetString("domain"),
+ Product: getProduct(record),
+ Access: access.GetString("config"),
+ Certificate: applicant.Certificate{
+ Certificate: record.GetString("certificate"),
+ PrivateKey: record.GetString("privateKey"),
+ },
+ }
+ switch record.GetString("targetType") {
+ case targetAliyunOss:
return NewAliyun(option)
+ case targetAliyunCdn:
+ return NewAliyunCdn(option)
}
return nil, errors.New("not implemented")
}
diff --git a/ui/src/pages/domains/Edit.tsx b/ui/src/pages/domains/Edit.tsx
index d9c982e4..69916c20 100644
--- a/ui/src/pages/domains/Edit.tsx
+++ b/ui/src/pages/domains/Edit.tsx
@@ -243,6 +243,9 @@ const Edit = () => {
阿里云-OSS
+
+ 阿里云-CDN
+
diff --git a/ui/src/pages/domains/Home.tsx b/ui/src/pages/domains/Home.tsx
index 35760d69..0769efd5 100644
--- a/ui/src/pages/domains/Home.tsx
+++ b/ui/src/pages/domains/Home.tsx
@@ -91,6 +91,13 @@ const Home = () => {
return domain;
});
setDomains(updatedDomains);
+ toast.toast({
+ title: "执行成功",
+ description: "执行成功",
+ });
+ setTimeout(() => {
+ navigate(0);
+ }, 1000);
} catch (e) {
toast.toast({
title: "执行失败",