add support for multiple records.

This commit is contained in:
Gitea 2021-04-23 23:08:06 +08:00
parent ec7743a9a2
commit 0167a96f0a
2 changed files with 57 additions and 53 deletions

View File

@ -11,10 +11,8 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns" "github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
) )
// TODO: support array of (Type, RR)
type Record struct { type Record struct {
Type string `json:"Type"` Type string `json:"type"`
RR string `json:"RR"` RR string `json:"RR"`
} }
@ -23,7 +21,7 @@ type Config struct {
AccessKeyId string `json:"accessKeyId"` AccessKeyId string `json:"accessKeyId"`
AccessSecret string `json:"accessSecret"` AccessSecret string `json:"accessSecret"`
DomainName string `json:"domainName"` DomainName string `json:"domainName"`
Records []Record Records []Record `json:"records"`
} }
type IpJson struct { type IpJson struct {
@ -85,23 +83,26 @@ func main() {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
var i int64 = 0
var found bool = false var found bool = false
// find subdomain // find subdomain
for i = 0; i < domainRecords.TotalCount; i++ { for _, record := range config.Records {
if config.RR == domainRecords.DomainRecords.Record[i].RR && config.Type == domainRecords.DomainRecords.Record[i].Type { for _, record_ := range domainRecords.DomainRecords.Record {
if record.Type == record_.Type &&
record.RR == record_.RR {
found = true //found subdomain record found = true //found subdomain record
if ip == domainRecords.DomainRecords.Record[i].Value { // ip not changed if ip == record_.Value { // ip not changed
fmt.Println("ip", ip, "not changed!") fmt.Println(record.RR+"."+config.DomainName, "unchanged:", ip)
} else { } else {
fmt.Printf("ip changed from %s to %s\n", domainRecords.DomainRecords.Record[i].Value, ip) fmt.Println(record.RR+"."+config.DomainName, "changed:", record_.Value, "->", ip)
fmt.Printf("---> update %s record to %s ...\n", config.RR+"."+config.DomainName, ip) fmt.Println("updating record...")
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest() updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
updateDomainRecordRequest.Scheme = "https" updateDomainRecordRequest.Scheme = "https"
updateDomainRecordRequest.RecordId = domainRecords.DomainRecords.Record[i].RecordId updateDomainRecordRequest.RecordId = record_.RecordId
updateDomainRecordRequest.RR = config.RR updateDomainRecordRequest.RR = record.RR
updateDomainRecordRequest.Type = config.Type updateDomainRecordRequest.Type = record.Type
updateDomainRecordRequest.Value = ip updateDomainRecordRequest.Value = ip
response, err := client.UpdateDomainRecord(updateDomainRecordRequest) response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
if err != nil { if err != nil {
@ -110,23 +111,22 @@ func main() {
if response.IsSuccess() { if response.IsSuccess() {
fmt.Println("success") fmt.Println("success")
} else { } else {
fmt.Printf("failed.\n%s\n", response.GetHttpContentString()) fmt.Println("failed.", response.GetHttpContentString())
} }
} }
} }
} }
if !found { if !found {
fmt.Printf("ip is %s, but no domain record found.\n", ip) fmt.Println(record.RR+"."+config.DomainName, "not found in records.")
fmt.Printf("---> add %s record to %s ...\n", config.RR+"."+config.DomainName, ip) fmt.Println("adding record:", record.RR+"."+config.DomainName, "->", ip)
addDomainRequest := alidns.CreateAddDomainRecordRequest() addDomainRequest := alidns.CreateAddDomainRecordRequest()
addDomainRequest.Scheme = "https" addDomainRequest.Scheme = "https"
addDomainRequest.DomainName = config.DomainName addDomainRequest.DomainName = config.DomainName
addDomainRequest.RR = config.RR addDomainRequest.RR = record.RR
addDomainRequest.Type = config.Type addDomainRequest.Type = record.Type
addDomainRequest.Value = ip addDomainRequest.Value = ip
response, err := client.AddDomainRecord(addDomainRequest) response, err := client.AddDomainRecord(addDomainRequest)
@ -136,8 +136,10 @@ func main() {
if response.IsSuccess() { if response.IsSuccess() {
fmt.Println("success") fmt.Println("success")
} else { } else {
fmt.Printf("failed.\n%s\n", response.GetHttpContentString()) fmt.Println("failed.", response.GetHttpContentString())
} }
} }
}
fmt.Println() fmt.Println()
} }

View File

@ -3,6 +3,8 @@
"accessKeyId": "YOUR_ACCESS_KEY_ID", "accessKeyId": "YOUR_ACCESS_KEY_ID",
"accessSecret": "YOUR_ACCESS_SECRET", "accessSecret": "YOUR_ACCESS_SECRET",
"domainName": "YOUR_DOMAIN_NAME", "domainName": "YOUR_DOMAIN_NAME",
"Type": "A", "records": [
"RR": "@" { "type": "A", "RR": "www1"},
{ "type": "A", "RR": "www2"}
]
} }