Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TTL and TXT #4

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OSX
.DS_Store

14 changes: 11 additions & 3 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package main
import (
"context"
"fmt"

"github.com/libdns/huaweicloud"
"time"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls run gofmt to fix lint.

"github.com/jicjoy/huaweicloud"
"github.com/libdns/libdns"
)

func main() {
Expand All @@ -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,
},
})
Comment on lines +17 to +24
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this.


fmt.Println("Result:", ret)
fmt.Println("Error:", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/libdns/huaweicloud
module github.com/jicjoy/huaweicloud
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not change module name.


go 1.23

Expand Down
18 changes: 13 additions & 5 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"slices"
"time"

"github.com/devhaozi/huaweicloud-sdk-go-v3/services/dns/v2/model"
"github.com/libdns/libdns"
)
Expand Down Expand Up @@ -65,16 +64,20 @@ 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)
Comment on lines +70 to +71
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix lint.


ttl := int32(record.TTL.Seconds())
request := &model.CreateRecordSetRequest{
ZoneId: zoneId,
Body: &model.CreateRecordSetRequestBody{
Name: libdns.AbsoluteName(record.Name, zone),
Type: record.Type,
Ttl: &ttl,
Records: []string{record.Value},
Records: SolveRecordValue(record.Type,record.Value),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix lint.

},
}
response, err := client.CreateRecordSet(request)
Expand All @@ -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
Comment on lines +107 to +109
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix lint.


// 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)
Expand All @@ -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,
Expand Down Expand Up @@ -169,4 +177,4 @@ var (
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)
)
24 changes: 24 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package huaweicloud
import (
"fmt"
"github.com/libdns/libdns"
"time"
Comment on lines +4 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix lint.

)

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}
}