refactor: clean code

This commit is contained in:
Fu Diwei
2026-09-23 22:18:43 +08:00
parent e68d1ca04d
commit 6968e67232
6 changed files with 44 additions and 98 deletions
+6 -6
View File
@@ -117,8 +117,8 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
Name: certName,
Description: certDesc,
Type: "upload",
SSLPEM: certPEM,
SSLKey: privkeyPEM,
SSLPEM: base64.StdEncoding.EncodeToString([]byte(certPEM)),
SSLKey: base64.StdEncoding.EncodeToString([]byte(privkeyPEM)),
AutoRenewal: false,
}
updateSSLCertResp, err := sdkClient.UpdateCertificateWithContext(ctx, d.config.CertificateId, updateSSLCertReq)
@@ -135,8 +135,8 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
Name: certName,
Description: certDesc,
Type: "upload",
SSLPEM: certPEM,
SSLKey: privkeyPEM,
SSLPEM: base64.StdEncoding.EncodeToString([]byte(certPEM)),
SSLKey: base64.StdEncoding.EncodeToString([]byte(privkeyPEM)),
AutoRenewal: false,
}
updateSSLCertResp, err := sdkClient.UpdateCertificateWithContext(ctx, d.config.CertificateId, updateSSLCertReq)
@@ -208,7 +208,7 @@ func createSDKClient(serverUrl, apiVersion, apiRole, authMethod, username, passw
case "", AUTH_METHOD_PASSWORD:
client, err = lecdnclientsdkv3.NewClient(serverUrl, lecdnclientsdkv3.WithLogins(username, password))
case AUTH_METHOD_APIKEY:
client, err = lecdnclientsdkv3.NewClient(serverUrl, lecdnclientsdkv3.WithLogins(username, password))
client, err = lecdnclientsdkv3.NewClient(serverUrl, lecdnclientsdkv3.WithApiKey(apiKey))
default:
err = fmt.Errorf("unsupported auth method '%s'", authMethod)
}
@@ -234,7 +234,7 @@ func createSDKClient(serverUrl, apiVersion, apiRole, authMethod, username, passw
case "", AUTH_METHOD_PASSWORD:
client, err = lecdnmastersdkv3.NewClient(serverUrl, lecdnmastersdkv3.WithLogins(username, password))
case AUTH_METHOD_APIKEY:
client, err = lecdnmastersdkv3.NewClient(serverUrl, lecdnmastersdkv3.WithLogins(username, password))
client, err = lecdnmastersdkv3.NewClient(serverUrl, lecdnmastersdkv3.WithApiKey(apiKey))
default:
err = fmt.Errorf("unsupported auth method '%s'", authMethod)
}
@@ -133,16 +133,39 @@ func (d *Deployer) deployWithCNAME(ctx context.Context, cloudCertId string) erro
// REF: https://www.volcengine.com/docs/6511/1214835
domainInfo := listDomainResp.Data[0]
updateDomainReq := &vewaf.UpdateDomainInput{
ProjectName: lo.EmptyableToPtr(d.config.ProjectName),
Region: ve.String(d.config.Region),
Domain: ve.String(d.config.Domain),
AccessMode: ve.Int32(10),
LBAlgorithm: domainInfo.LBAlgorithm,
PublicRealServer: ve.Int32(1),
VpcID: domainInfo.VpcID,
BackendGroups: toUpdateBackendGroups(domainInfo.BackendGroups),
CloudAccessConfig: toUpdateCloudAccessConfigs(domainInfo.CloudAccessConfig),
Protocols: ve.StringSlice([]string{"HTTP", "HTTPS"}),
ProjectName: lo.EmptyableToPtr(d.config.ProjectName),
Region: ve.String(d.config.Region),
Domain: ve.String(d.config.Domain),
AccessMode: ve.Int32(10),
LBAlgorithm: domainInfo.LBAlgorithm,
PublicRealServer: ve.Int32(1),
VpcID: domainInfo.VpcID,
BackendGroups: lo.Map(domainInfo.BackendGroups, func(g *vewaf.BackendGroupForListDomainOutput, _ int) *vewaf.BackendGroupForUpdateDomainInput {
return &vewaf.BackendGroupForUpdateDomainInput{
Name: g.Name,
AccessPort: lo.Clone(g.AccessPort),
Backends: lo.Map(g.Backends, func(b *vewaf.BackendForListDomainOutput, _ int) *vewaf.BackendForUpdateDomainInput {
return &vewaf.BackendForUpdateDomainInput{
IP: b.IP,
Port: b.Port,
Protocol: b.Protocol,
Weight: b.Weight,
}
}),
}
}),
CloudAccessConfig: lo.Map(domainInfo.CloudAccessConfig, func(c *vewaf.CloudAccessConfigForListDomainOutput, _ int) *vewaf.CloudAccessConfigForUpdateDomainInput {
return &vewaf.CloudAccessConfigForUpdateDomainInput{
InstanceID: c.InstanceID,
InstanceName: c.InstanceName,
ListenerID: c.ListenerID,
AccessProtocol: c.AccessProtocol,
Protocol: c.Protocol,
Port: c.Port,
DefenceMode: c.DefenceMode,
}
}),
Protocols: ve.StringSlice([]string{"HTTP", "HTTPS"}),
ProtocolPorts: &vewaf.ProtocolPortsForUpdateDomainInput{
HTTP: ve.Int32Slice([]int32{80}),
HTTPS: ve.Int32Slice([]int32{443}),
@@ -172,63 +195,6 @@ func (d *Deployer) deployWithCNAME(ctx context.Context, cloudCertId string) erro
return nil
}
// 将 ListDomain 返回的回源组转换为 UpdateDomain 所需的结构。
func toUpdateBackendGroups(groups []*vewaf.BackendGroupForListDomainOutput) []*vewaf.BackendGroupForUpdateDomainInput {
if groups == nil {
return nil
}
out := make([]*vewaf.BackendGroupForUpdateDomainInput, 0, len(groups))
for _, g := range groups {
backend := lo.Map(g.Backends, func(b *vewaf.BackendForListDomainOutput, _ int) *vewaf.BackendForUpdateDomainInput {
if b == nil {
return nil
}
return &vewaf.BackendForUpdateDomainInput{
IP: b.IP,
Port: b.Port,
Protocol: b.Protocol,
Weight: b.Weight,
}
})
out = append(out, &vewaf.BackendGroupForUpdateDomainInput{
Name: g.Name,
AccessPort: lo.Clone(g.AccessPort),
Backends: backend,
})
}
return out
}
// 将 ListDomain 返回的云产品接入配置转换为 UpdateDomain 所需的结构。
func toUpdateCloudAccessConfigs(configs []*vewaf.CloudAccessConfigForListDomainOutput) []*vewaf.CloudAccessConfigForUpdateDomainInput {
if configs == nil {
return nil
}
out := make([]*vewaf.CloudAccessConfigForUpdateDomainInput, 0, len(configs))
for _, c := range configs {
if c == nil {
continue
}
out = append(out, &vewaf.CloudAccessConfigForUpdateDomainInput{
InstanceID: c.InstanceID,
InstanceName: c.InstanceName,
ListenerID: c.ListenerID,
AccessProtocol: c.AccessProtocol,
Protocol: c.Protocol,
Port: c.Port,
DefenceMode: c.DefenceMode,
})
}
return out
}
func createSDKClient(accessKeyId, secretAccessKey, region string) (*vewaf.WAF, error) {
config := ve.NewConfig().
WithAkSk(accessKeyId, secretAccessKey).
+2 -9
View File
@@ -47,20 +47,13 @@ func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
return nil, fmt.Errorf("sdkerr: cannot set both apiKey and username/password")
}
baseUrl := strings.TrimSuffix(serverUrl, "/")
if opts.ApiKey != "" {
baseUrl += "/api/client"
} else {
baseUrl += "/prod-api"
}
client := &Client{
username: opts.Username,
password: opts.Password,
apiKey: opts.ApiKey,
}
client.rc = resty.New().
SetBaseURL(baseUrl).
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/prod-api").
SetHeader("User-Agent", app.AppUserAgent).
SetPreRequestHook(func(_ *resty.Client, req *http.Request) error {
if client.apiKey != "" {
@@ -150,7 +143,7 @@ func (c *Client) ensureToken(ctx context.Context) error {
return nil
}
httpreq, err := c.newRequest(http.MethodPost, "/auth/login")
httpreq, err := c.newRequest(http.MethodPost, "/login")
if err != nil {
return err
} else {
+1 -4
View File
@@ -47,16 +47,13 @@ func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
return nil, fmt.Errorf("sdkerr: cannot set both apiKey and username/password")
}
baseUrl := strings.TrimSuffix(serverUrl, "/")
baseUrl += "/prod-api"
client := &Client{
username: opts.Username,
password: opts.Password,
apiKey: opts.ApiKey,
}
client.rc = resty.New().
SetBaseURL(baseUrl).
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/prod-api").
SetHeader("User-Agent", app.AppUserAgent).
SetPreRequestHook(func(_ *resty.Client, req *http.Request) error {
if client.apiKey != "" {
+1 -8
View File
@@ -47,20 +47,13 @@ func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
return nil, fmt.Errorf("sdkerr: cannot set both apiKey and username/password")
}
baseUrl := strings.TrimSuffix(serverUrl, "/")
if opts.ApiKey != "" {
baseUrl += "/api/admin"
} else {
baseUrl += "/prod-api"
}
client := &Client{
username: opts.Username,
password: opts.Password,
apiKey: opts.ApiKey,
}
client.rc = resty.New().
SetBaseURL(baseUrl).
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/prod-api").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
+1 -4
View File
@@ -47,16 +47,13 @@ func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
return nil, fmt.Errorf("sdkerr: cannot set both apiKey and username/password")
}
baseUrl := strings.TrimSuffix(serverUrl, "/")
baseUrl += "/prod-api"
client := &Client{
username: opts.Username,
password: opts.Password,
apiKey: opts.ApiKey,
}
client.rc = resty.New().
SetBaseURL(baseUrl).
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/prod-api").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).