Files
goodBaby/drivers/alidns/tool.go
T

52 lines
1.5 KiB
Go
Raw Normal View History

2026-08-28 13:51:51 +08:00
package alidns
import (
2026-08-30 09:13:15 +08:00
"encoding/json"
2026-09-12 15:32:27 +08:00
"fmt"
2026-08-30 09:13:15 +08:00
2026-08-28 13:51:51 +08:00
alidns "github.com/alibabacloud-go/alidns-20150109/v5/client"
2026-08-30 09:13:15 +08:00
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
2026-08-28 13:51:51 +08:00
"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/credentials-go/credentials"
)
2026-09-12 15:32:27 +08:00
// ParseAccountConfig 解析阿里云账号配置
func ParseAccountConfig(config string) (*AliDNSAccount, error) {
if config == "" {
return nil, fmt.Errorf("解析阿里云账号配置失败: 规则没有关联账号")
2026-08-28 13:51:51 +08:00
}
2026-09-12 15:32:27 +08:00
var account AliDNSAccount
if err := json.Unmarshal([]byte(config), &account); err != nil {
return nil, fmt.Errorf("解析阿里云账号配置失败: %v", err)
}
return &account, nil
2026-08-30 16:03:32 +08:00
}
2026-09-12 15:32:27 +08:00
// ParseDeleteRecordConfig 解析删除解析记录规则配置
func ParseDeleteRecordConfig(configJSON string) (*DeleteRecordConfig, error) {
var config DeleteRecordConfig
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
return nil, fmt.Errorf("解析阿里云删除记录规则配置失败: %v", err)
}
return &config, nil
}
// newClient 用 AccessKey 创建阿里云 DNS 客户端
func newClient(ak string, sk string) (*alidns.Client, error) {
2026-08-28 13:51:51 +08:00
credentialsConfig := new(credentials.Config).
SetType("access_key").
2026-08-30 16:03:32 +08:00
SetAccessKeyId(ak).
SetAccessKeySecret(sk)
2026-09-12 15:32:27 +08:00
2026-08-28 13:51:51 +08:00
akCredential, err := credentials.NewCredential(credentialsConfig)
if err != nil {
2026-09-12 15:32:27 +08:00
return nil, fmt.Errorf("创建阿里云凭据失败: %w", err)
2026-08-30 09:13:15 +08:00
}
2026-09-12 15:32:27 +08:00
return alidns.NewClient(&openapi.Config{
Credential: akCredential,
Endpoint: tea.String("alidns.aliyuncs.com"),
})
2026-08-30 09:13:15 +08:00
}