Merge pull request 'add support for multiple records.' (#2) from wangjiacai/alidns:dev01 into master
Reviewed-on: https://gitea.wq520.cloud:4435/jiacai_wang/alidns/pulls/2
This commit is contained in:
commit
28178f54b9
106
alidns.go
106
alidns.go
@ -11,13 +11,17 @@ import (
|
|||||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Record struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
RR string `json:"RR"`
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
RegionId string `json:"regionId"`
|
RegionId string `json:"regionId"`
|
||||||
AccessKeyId string `json:"accessKeyId"`
|
AccessKeyId string `json:"accessKeyId"`
|
||||||
AccessSecret string `json:"accessSecret"`
|
AccessSecret string `json:"accessSecret"`
|
||||||
DomainName string `json:"domainName"`
|
DomainName string `json:"domainName"`
|
||||||
Type string `json:"Type"`
|
Records []Record `json:"records"`
|
||||||
RR string `json:"RR"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type IpJson struct {
|
type IpJson struct {
|
||||||
@ -41,7 +45,7 @@ func main() {
|
|||||||
|
|
||||||
configPath := flag.String("config", "./config.json", "path to config file")
|
configPath := flag.String("config", "./config.json", "path to config file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Println("config file path: ", *configPath)
|
fmt.Println("config file path:", *configPath)
|
||||||
|
|
||||||
configJson, err := ioutil.ReadFile(*configPath)
|
configJson, err := ioutil.ReadFile(*configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -79,59 +83,63 @@ func main() {
|
|||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
var i int64 = 0
|
|
||||||
var found bool = false
|
var found bool = false
|
||||||
// find subdomain
|
|
||||||
for i = 0; i < domainRecords.TotalCount; i++ {
|
|
||||||
if config.RR == domainRecords.DomainRecords.Record[i].RR && config.Type == domainRecords.DomainRecords.Record[i].Type {
|
|
||||||
found = true //found subdomain record
|
|
||||||
if ip == domainRecords.DomainRecords.Record[i].Value { // ip not changed
|
|
||||||
fmt.Println("ip", ip, "not changed!")
|
|
||||||
} else {
|
|
||||||
fmt.Printf("ip changed from %s to %s\n", domainRecords.DomainRecords.Record[i].Value, ip)
|
|
||||||
fmt.Printf("---> update %s record to %s ...\n", config.RR+"."+config.DomainName, ip)
|
|
||||||
|
|
||||||
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
|
// find subdomain
|
||||||
updateDomainRecordRequest.Scheme = "https"
|
for _, record := range config.Records {
|
||||||
updateDomainRecordRequest.RecordId = domainRecords.DomainRecords.Record[i].RecordId
|
for _, record_ := range domainRecords.DomainRecords.Record {
|
||||||
updateDomainRecordRequest.RR = config.RR
|
|
||||||
updateDomainRecordRequest.Type = config.Type
|
if record.Type == record_.Type &&
|
||||||
updateDomainRecordRequest.Value = ip
|
record.RR == record_.RR {
|
||||||
response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
|
found = true //found subdomain record
|
||||||
if err != nil {
|
if ip == record_.Value { // ip not changed
|
||||||
fmt.Println(err.Error())
|
fmt.Println(record.RR+"."+config.DomainName, "unchanged:", ip)
|
||||||
}
|
|
||||||
if response.IsSuccess() {
|
|
||||||
fmt.Println("success")
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
fmt.Println(record.RR+"."+config.DomainName, "changed:", record_.Value, "->", ip)
|
||||||
|
fmt.Println("updating record...")
|
||||||
|
|
||||||
|
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
|
||||||
|
updateDomainRecordRequest.Scheme = "https"
|
||||||
|
updateDomainRecordRequest.RecordId = record_.RecordId
|
||||||
|
updateDomainRecordRequest.RR = record.RR
|
||||||
|
updateDomainRecordRequest.Type = record.Type
|
||||||
|
updateDomainRecordRequest.Value = ip
|
||||||
|
response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
if response.IsSuccess() {
|
||||||
|
fmt.Println("success")
|
||||||
|
} else {
|
||||||
|
fmt.Println("failed.", response.GetHttpContentString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
if !found {
|
||||||
|
fmt.Println(record.RR+"."+config.DomainName, "not found in records.")
|
||||||
|
fmt.Println("adding record:", record.RR+"."+config.DomainName, "->", ip)
|
||||||
|
|
||||||
if !found {
|
addDomainRequest := alidns.CreateAddDomainRecordRequest()
|
||||||
fmt.Printf("ip is %s, but no domain record found.\n", ip)
|
addDomainRequest.Scheme = "https"
|
||||||
fmt.Printf("---> add %s record to %s ...\n", config.RR+"."+config.DomainName, ip)
|
|
||||||
|
|
||||||
addDomainRequest := alidns.CreateAddDomainRecordRequest()
|
addDomainRequest.DomainName = config.DomainName
|
||||||
addDomainRequest.Scheme = "https"
|
addDomainRequest.RR = record.RR
|
||||||
|
addDomainRequest.Type = record.Type
|
||||||
|
addDomainRequest.Value = ip
|
||||||
|
|
||||||
addDomainRequest.DomainName = config.DomainName
|
response, err := client.AddDomainRecord(addDomainRequest)
|
||||||
addDomainRequest.RR = config.RR
|
if err != nil {
|
||||||
addDomainRequest.Type = config.Type
|
fmt.Println(err.Error())
|
||||||
addDomainRequest.Value = ip
|
}
|
||||||
|
if response.IsSuccess() {
|
||||||
response, err := client.AddDomainRecord(addDomainRequest)
|
fmt.Println("success")
|
||||||
if err != nil {
|
} else {
|
||||||
fmt.Println(err.Error())
|
fmt.Println("failed.", response.GetHttpContentString())
|
||||||
}
|
}
|
||||||
if response.IsSuccess() {
|
|
||||||
fmt.Println("success")
|
|
||||||
} else {
|
|
||||||
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
@ -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"}
|
||||||
|
]
|
||||||
}
|
}
|
Reference in New Issue
Block a user