diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cff6299 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# OSX +.DS_Store + diff --git a/_example/main.go b/_example/main.go index 5e0eb18..110a466 100644 --- a/_example/main.go +++ b/_example/main.go @@ -3,8 +3,9 @@ package main import ( "context" "fmt" - - "github.com/libdns/huaweicloud" + "time" + "github.com/jicjoy/huaweicloud" + "github.com/libdns/libdns" ) func main() { @@ -13,7 +14,14 @@ func main() { SecretAccessKey: "YOUR_Secret_Key", } - ret, err := p.GetRecords(context.TODO(), "your-domain") + ret, err := p.SetRecords(context.TODO(), "iitmall.com",[]libdns.Record{ + { + Type: "TXT", + Name: "es", + Value: "ssssfsdfsdf", + TTL: time.Duration(10)* time.Second, + }, + }) fmt.Println("Result:", ret) fmt.Println("Error:", err) diff --git a/go.mod b/go.mod index c463d38..870c84c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/libdns/huaweicloud +module github.com/jicjoy/huaweicloud go 1.23 diff --git a/provider.go b/provider.go index aa3cd13..7870721 100644 --- a/provider.go +++ b/provider.go @@ -4,7 +4,6 @@ import ( "context" "slices" "time" - "github.com/devhaozi/huaweicloud-sdk-go-v3/services/dns/v2/model" "github.com/libdns/libdns" ) @@ -65,8 +64,12 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib if err != nil { return nil, err } - + + for i, record := range records { + //ttl + parseTTL(&record) + ttl := int32(record.TTL.Seconds()) request := &model.CreateRecordSetRequest{ ZoneId: zoneId, @@ -74,7 +77,7 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib Name: libdns.AbsoluteName(record.Name, zone), Type: record.Type, Ttl: &ttl, - Records: []string{record.Value}, + Records: SolveRecordValue(record.Type,record.Value), }, } response, err := client.CreateRecordSet(request) @@ -100,6 +103,11 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns } for i, record := range records { + + //parse ttl + //parseTTL(&record) + record.TTL = time.Duration(300)* time.Second + // If the record id is empty try to get it by name and type if record.ID == "" { id, err := p.getRecordIdByNameAndType(ctx, zone, record.Name, record.Type) @@ -117,7 +125,7 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns } else { name := libdns.AbsoluteName(record.Name, zone) ttl := int32(record.TTL.Seconds()) - value := []string{record.Value} + value := SolveRecordValue(record.Type,record.Value) request := &model.UpdateRecordSetRequest{ ZoneId: zoneId, RecordsetId: record.ID, @@ -169,4 +177,4 @@ var ( _ libdns.RecordAppender = (*Provider)(nil) _ libdns.RecordSetter = (*Provider)(nil) _ libdns.RecordDeleter = (*Provider)(nil) -) +) \ No newline at end of file diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..d82179a --- /dev/null +++ b/utils.go @@ -0,0 +1,24 @@ +package huaweicloud +import ( + "fmt" + "github.com/libdns/libdns" + "time" +) + +func parseTTL(record *libdns.Record) { + ttl := int32(record.TTL.Seconds()) + if ttl == 0 { + record.TTL = time.Duration(300)* time.Second + } + +} + +func SolveRecordValue(rType string,value string) []string { + switch rType { + case "TXT": + value = fmt.Sprintf("\"%s\"", value) + + } + + return []string{value} +} \ No newline at end of file