Skip to content

Commit

Permalink
feat: refactor DNS record functions and improve validation
Browse files Browse the repository at this point in the history
- Add `dnspod` configuration for `main` and `sub`
- Add an instruction for adding DNS records
- Modify the `CreateOrUpdateRecord` and `DeleteRecord` functions in `dnspod.go` to search for records without specifying the IP address
- Add validation for multiple records in `searchRecord` function
- Log a warning when finding more than one record

Signed-off-by: ysicing <i@ysicing.me>
  • Loading branch information
ysicing committed Feb 26, 2024
1 parent 2fa6243 commit b9a0888
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,29 @@ qcloud:
# 安全组
securitygroup:
id: sg-xxxx

dnspod:
main: "ysicing.net"
sub: "*.dev"
```
### 使用
```bash
# 创建1台机器, 默认开启公网访问100M按流量计费, 超过1台则默认不分配公网ip(因为我们环境默认nat出去)
spot new --config /home/ysicing/.spot.yaml
spot new --config /home/ysicing/.spot.yaml
# 列表
spot list --config /home/ysicing/.spot.yaml
spot list --config /home/ysicing/.spot.yaml
INFO[0000] Using config file: /home/ysicing/.spot.yaml
创建时间 Name ID 内网IP 公网IP 规格 类型 状态
2022-08-22T13:17:34Z spot-20220822211647 ins-kysdso6l 10.10.16.39 42.192.202.136 SA2.MEDIUM4 SPOTPAID RUNNING
创建时间 Name ID 内网IP 公网IP 规格 类型 状态
2022-08-22T13:17:34Z spot-20220822211647 ins-kysdso6l 10.10.16.39 42.192.202.136 SA2.MEDIUM4 SPOTPAID RUNNING
# 销毁
spot destroy --config /home/ysicing/.spot.yaml
spot destroy --config /home/ysicing/.spot.yaml
# 销毁全部
spot destroy --config /home/ysicing/.spot.yaml --all
spot destroy --config /home/ysicing/.spot.yaml --all
# 添加解析记录
spot dnspod --config /home/ysicing/.spot.yaml
INFO[0000] Using config file: /home/ysicing/.spot.yaml
🎉 10.10.16.25
INFO[0003] create record success *.dev.ysicing.net ---> 106.54.x.x
```
19 changes: 13 additions & 6 deletions cloud/qcloud/dnspod.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package qcloud

import (
"errors"

"github.com/sirupsen/logrus"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
Expand All @@ -10,7 +12,7 @@ func (d *Client) CreateOrUpdateRecord(ip string) error {
if len(d.domain) == 0 || len(d.sub) == 0 {
return nil
}
recordID, err := d.searchRecord(ip)
recordID, err := d.searchRecord()
if err != nil {
return err
}
Expand All @@ -24,7 +26,7 @@ func (d *Client) DeleteRecord(ip string) error {
if len(d.domain) == 0 || len(d.sub) == 0 {
return nil
}
recordID, err := d.searchRecord(ip)
recordID, err := d.searchRecord()
if err != nil {
return err
}
Expand All @@ -42,7 +44,7 @@ func (d *Client) DeleteRecord(ip string) error {
return nil
}

func (d *Client) searchRecord(ip string) (*uint64, error) {
func (d *Client) searchRecord() (*uint64, error) {
request := dnspod.NewDescribeRecordListRequest()
request.Domain = common.StringPtr(d.domain)
request.Subdomain = common.StringPtr(d.sub)
Expand All @@ -51,10 +53,15 @@ func (d *Client) searchRecord(ip string) (*uint64, error) {
if err != nil {
return nil, err
}
if len(response.Response.RecordList) == 0 {
return nil, nil
}
if len(response.Response.RecordList) > 1 {
logrus.Warnf("find more than one record %s.%s", d.sub, d.domain)
return nil, errors.New("find more than one record")
}
for _, record := range response.Response.RecordList {
if *record.Value == ip {
return record.RecordId, nil
}
return record.RecordId, nil
}
return nil, nil
}
Expand Down

0 comments on commit b9a0888

Please sign in to comment.