This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package alidns
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/alibabacloud-go/alidns-20150109/v5/client"
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
type AliDNSAccountConfigValidator struct{}
|
||||
|
||||
func (v *AliDNSAccountConfigValidator) GetType() string { return AccountType }
|
||||
|
||||
func (v *AliDNSAccountConfigValidator) Validate(config string) error {
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg.AK == "" || cfg.SK == "" {
|
||||
return fmt.Errorf("阿里云账号配置不完整")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseAccount(config string) (*AliDNSAccount, error) {
|
||||
var cfg AliDNSAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析阿里云账号配置失败: %v", err)
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (v *AliDNSAccountConfigValidator) Test(config string) error {
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
aliDNSClient, err := getAliDNSClient(cfg.AK, cfg.SK)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = aliDNSClient.DescribeDnsProductInstances(&client.DescribeDnsProductInstancesRequest{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *AliDNSAccountConfigValidator) Meta() meta.AccountMeta {
|
||||
return meta.AccountMeta{
|
||||
Type: AccountType,
|
||||
Label: "阿里云",
|
||||
Description: "阿里云国内站",
|
||||
Fields: []meta.Field{
|
||||
{Key: "ak", Label: "Access Key", Type: meta.FieldPassword, Required: true, Secret: true, Placeholder: "xxxxxxxxx"},
|
||||
{Key: "sk", Label: "Secret Key", Type: meta.FieldPassword, Required: true, Secret: true, Placeholder: "xxxxxxxxx"},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,16 @@ package alidns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/alibabacloud-go/alidns-20150109/v5/client"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// deleteAliDNSRecord deletes a DNS record from Alibaba Cloud DNS based on the provided rule.
|
||||
func deleteAliDNSRecord(ctx context.Context, rule *model.Rule) error {
|
||||
aliDNSClient, err := getAliDNSClient(rule)
|
||||
aliDNSClient, err := getAliDNSClientFromRule(rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -17,11 +20,14 @@ func deleteAliDNSRecord(ctx context.Context, rule *model.Rule) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = aliDNSClient.DeleteDomainRecord(&client.DeleteDomainRecordRequest{
|
||||
RecordId: &deleteRecordConfig.RecordID,
|
||||
})
|
||||
if err != nil {
|
||||
if err := retry.Do(ctx, func() error {
|
||||
_, err := aliDNSClient.DeleteDomainRecord(&client.DeleteDomainRecordRequest{
|
||||
RecordId: &deleteRecordConfig.RecordID,
|
||||
})
|
||||
return err
|
||||
}); err != nil {
|
||||
return fmt.Errorf("删除阿里云DNS记录失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package alidns
|
||||
|
||||
const (
|
||||
AccountType = "alidns"
|
||||
RuleTypeDeleteRecord = "alidns-delete-record"
|
||||
)
|
||||
|
||||
type AliDNSAccount struct {
|
||||
AK string `json:"ak"`
|
||||
SK string `json:"sk"`
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package alidns
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
// 阿里云删除记录规则验证器
|
||||
type AliDNSDeleteRecordRuleValidator struct{}
|
||||
|
||||
func (v *AliDNSDeleteRecordRuleValidator) GetType() string {
|
||||
return RuleTypeDeleteRecord
|
||||
}
|
||||
|
||||
func (v *AliDNSDeleteRecordRuleValidator) Validate(configJSON string) error {
|
||||
var config DeleteRecordConfig
|
||||
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
|
||||
return fmt.Errorf("解析阿里云删除记录规则配置失败: %v", err)
|
||||
}
|
||||
|
||||
if config.RecordID == "" {
|
||||
return fmt.Errorf("阿里云删除记录规则配置中 record_id 不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *AliDNSDeleteRecordRuleValidator) Meta() meta.RuleMeta {
|
||||
return meta.RuleMeta{
|
||||
Type: RuleTypeDeleteRecord,
|
||||
Label: "删除阿里云DNS解析记录",
|
||||
Description: "触发时删除阿里云DNS解析记录",
|
||||
AccountType: AccountType,
|
||||
Fields: []meta.Field{
|
||||
{Key: "record_id", Label: "记录ID", Type: meta.FieldString, Required: true},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package alidns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// DeleteAliDNSRecordExecutor 删除阿里云DNS记录执行器
|
||||
type DeleteAliDNSRecordExecutor struct{}
|
||||
|
||||
func (e *DeleteAliDNSRecordExecutor) GetType() string {
|
||||
return RuleTypeDeleteRecord
|
||||
}
|
||||
|
||||
func (e *DeleteAliDNSRecordExecutor) Execute(ctx context.Context, rule *model.Rule) error {
|
||||
log.Printf("执行删除阿里云DNS记录规则: %s (ID: %d)", rule.Name, rule.ID)
|
||||
|
||||
return deleteAliDNSRecord(ctx, rule)
|
||||
}
|
||||
@@ -11,7 +11,8 @@ import (
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
func getAliDNSClient(rule *model.Rule) (client *alidns.Client, err error) {
|
||||
// getAliDNSClient initializes and returns an Alibaba Cloud DNS client based on the provided rule's account configuration. It retrieves the account configuration from the database, sets up the necessary credentials, and creates a new client instance for interacting with Alibaba Cloud DNS services.
|
||||
func getAliDNSClientFromRule(rule *model.Rule) (client *alidns.Client, err error) {
|
||||
var accountConfig AliDNSAccount
|
||||
|
||||
// get config
|
||||
@@ -19,11 +20,15 @@ func getAliDNSClient(rule *model.Rule) (client *alidns.Client, err error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return getAliDNSClient(accountConfig.AK, accountConfig.SK)
|
||||
}
|
||||
|
||||
func getAliDNSClient(ak string, sk string) (client *alidns.Client, err error) {
|
||||
// init aliyun account config
|
||||
credentialsConfig := new(credentials.Config).
|
||||
SetType("access_key").
|
||||
SetAccessKeyId(accountConfig.AK).
|
||||
SetAccessKeySecret(accountConfig.SK)
|
||||
SetAccessKeyId(ak).
|
||||
SetAccessKeySecret(sk)
|
||||
akCredential, err := credentials.NewCredential(credentialsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -38,6 +43,7 @@ func getAliDNSClient(rule *model.Rule) (client *alidns.Client, err error) {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// getDeleteRecordConfig retrieves the configuration for deleting DNS records from the provided rule. It unmarshals the rule's configuration JSON into a DeleteRecordConfig structure and returns it.
|
||||
func getDeleteRecordConfig(rule *model.Rule) (*DeleteRecordConfig, error) {
|
||||
var deleteRecordConfig DeleteRecordConfig
|
||||
|
||||
|
||||
Reference in New Issue
Block a user