-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar_amazon.go
212 lines (198 loc) · 6.21 KB
/
registrar_amazon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package gd
import (
"encoding/json"
"fmt"
"log"
"reflect"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
)
const (
TableAwsZone = `awsZone`
TableAwsRecord = `awsRecord`
)
type AwsZone struct {
IsTruncated bool `json:"IsTruncated"`
MaxItems string `json:"MaxItems"`
NextMarker string `json:"NextMarker"`
HostedZones []AwsZoneView `json:"HostedZones"`
}
type AwsZoneView struct {
CallerReference string `json:"CallerReference"`
Id string `json:"Id"`
Name string `json:"Name"`
ResourceRecordSetCount int `json:"ResourceRecordSetCount"`
Config map[string]interface{} `json:"Config"`
}
func (a *AwsZoneView) CreateTableQuery() string {
e := reflect.ValueOf(a).Elem()
var tableFields string
for i := 0; i < e.NumField(); i++ {
var sqlType string
varName := e.Type().Field(i).Name
varType := e.Field(i).Type().String()
if varType == "int" {
sqlType = varType
} else {
sqlType = "varchar(256)"
}
tableFields = tableFields + varName + " " + sqlType + ","
}
tableFields = strings.TrimRight(tableFields, ",")
createQuery := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s(%s)`, TableAwsZone, tableFields)
return createQuery
}
func (a *AwsZoneView) InsertDataQuery() (string, []interface{}) {
e := reflect.ValueOf(a).Elem()
insertQuery := fmt.Sprintf(`INSERT %s SET `, TableAwsZone)
var insertValue []interface{}
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varValue := e.Field(i).Interface()
if i >= 4 {
varValue = fmt.Sprintf(`%v`, varValue)
}
insertQuery = insertQuery + varName + "=?,"
insertValue = append(insertValue, varValue)
}
insertQuery = strings.TrimRight(insertQuery, ",")
return insertQuery, insertValue
}
func (a *AwsZoneView) GetZones(keyId, key string) ([]string, []AwsZoneView) {
var temp, zone AwsZone
var id []string
req := AwsRequest(keyId, key)
param := &route53.ListHostedZonesInput{
MaxItems: aws.String("100"),
}
for temp.IsTruncated || temp.NextMarker == "" {
if temp.IsTruncated {
param = &route53.ListHostedZonesInput{
Marker: aws.String(temp.NextMarker),
}
}
resp, err := req.ListHostedZones(param)
if err != nil {
log.Println(err)
return nil, nil
}
content, err := json.Marshal(resp)
if err != nil {
log.Println(err)
return nil, nil
}
err = json.Unmarshal(content, &temp)
if err != nil {
log.Println(err)
return nil, nil
}
for i := range temp.HostedZones {
id = append(id, temp.HostedZones[i].Id)
}
zone.HostedZones = append(zone.HostedZones, temp.HostedZones...)
}
return id, zone.HostedZones
}
type AwsRecord struct {
ResourceRecordSets []AwsRecordView `json:"ResourceRecordSets"`
IsTruncated bool `json:"IsTruncated"`
MaxItems string `json:"MaxItems"`
NextRecordIdentifier string `json:"NextRecordIdentifier"`
NextRecordName string `json:"NextRecordName"`
NextRecordType string `json:"NextRecordType"`
}
type AwsRecordView struct {
Name string `json:"Name"`
TTL int `json:"TTL"`
Type string `json:"Type"`
Weight int `json:"Weight"`
ResourceRecords []map[string]string `json:"ResourceRecords"`
GeoLocation map[string]string `json:"GeoLocation"`
AliasTarget string `json:"AliasTarget"`
Failover string `json:"Failover"`
HealthCheckId string `json:"HealthCheckId"`
MultiValueAnswer string `json:"MultiValueAnswer"`
Region string `json:"Region"`
SetIdentifier string `json:"SetIdentifier"`
TrafficPolicyInstanceId string `json:"TrafficPolicyInstanceId"`
}
func (a *AwsRecordView) CreateTableQuery() string {
e := reflect.ValueOf(a).Elem()
var tableFields string
for i := 0; i < e.NumField(); i++ {
var sqlType string
varName := e.Type().Field(i).Name
varType := e.Field(i).Type().String()
if varType == "int" {
sqlType = varType
} else {
sqlType = "varchar(256)"
}
tableFields = tableFields + varName + " " + sqlType + ","
}
tableFields = strings.TrimRight(tableFields, ",")
createQuery := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s(%s)`, TableAwsRecord, tableFields)
return createQuery
}
func (a *AwsRecordView) InsertDataQuery() (string, []interface{}) {
e := reflect.ValueOf(a).Elem()
insertQuery := fmt.Sprintf(`INSERT %s SET `, TableAwsRecord)
var insertValue []interface{}
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varValue := e.Field(i).Interface()
if i >= 4 && i <= 5 {
varValue = fmt.Sprintf(`%v`, varValue)
}
insertQuery = insertQuery + varName + "=?,"
insertValue = append(insertValue, varValue)
}
insertQuery = strings.TrimRight(insertQuery, ",")
return insertQuery, insertValue
}
func (a *AwsRecordView) GetRecords(keyId, key, zoneId string) []AwsRecordView {
req := AwsRequest(keyId, key)
param := &route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(zoneId),
// MaxItems: aws.String("100"),
// StartRecordIdentifier: aws.String("update"),
}
var record AwsRecord
resp, err := req.ListResourceRecordSets(param)
if err != nil {
log.Println(err)
return nil
}
content, err := json.Marshal(resp)
if err != nil {
log.Println(err)
return nil
}
err = json.Unmarshal(content, &record)
if err != nil {
log.Println(err)
return nil
}
return record.ResourceRecordSets
/* Replace \052 with "*" */
// if strings.Contains(view.Name, `\052`) {
// m := regexp.MustCompile(`\\052`)
// str := "*"
// m.ReplaceAllString(view.Name, str)
// }
}
func AwsRequest(keyId, key string) (client *route53.Route53) {
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(keyId, key, ""),
})
if err != nil {
log.Println("Session error,")
log.Println(err)
return
}
client = route53.New(sess)
return
}