@@ -143,20 +143,27 @@ def request(method, path, param=None, body=None, **params):
143
143
144
144
def get_zone_id (domain ):
145
145
"""
146
- 切割域名获取主域名和对应ID https://support.huaweicloud.com/api-dns/zh-cn_topic_0037134402.html
146
+ 切割域名获取主域名和对应ID https://support.huaweicloud.com/api-dns/dns_api_62003.html
147
+ 优先匹配级数最长的主域名
147
148
"""
148
- zones = request ('GET' , '/v2/zones' , limit = 500 )['zones' ]
149
- domain += '.'
150
- zone = next ((z for z in zones if domain .endswith (z .get ('name' ))), None )
151
- zoneid = zone and zone ['id' ]
149
+ zoneid = None
150
+ domain_slice = domain .split ('.' )
151
+ index = len (domain_slice )
152
+ root_domain = '.' .join (domain_slice [- 2 :])
153
+ zones = request ('GET' , '/v2/zones' , limit = 500 , name = root_domain )['zones' ]
154
+ while (not zoneid ) and (index >= 2 ):
155
+ domain = '.' .join (domain_slice [- index :]) + '.'
156
+ zone = next ((z for z in zones if domain == (z .get ('name' ))), None )
157
+ zoneid = zone and zone ['id' ]
158
+ index -= 1
152
159
return zoneid
153
160
154
161
155
162
def get_records (zoneid , ** conditions ):
156
163
"""
157
164
获取记录ID
158
165
返回满足条件的所有记录[]
159
- https://support.huaweicloud.com/api-dns/zh-cn_topic_0037129970 .html
166
+ https://support.huaweicloud.com/api-dns/dns_api_64004 .html
160
167
TODO 大于500翻页
161
168
"""
162
169
cache_key = zoneid + "_" + \
@@ -187,13 +194,13 @@ def get_records(zoneid, **conditions):
187
194
return records
188
195
189
196
190
- def update_record (domain , value , record_type = 'A' , name = None ):
197
+ def update_record (domain , value , record_type = 'A' ):
191
198
"""
192
199
更新记录
193
200
update
194
- https://support.huaweicloud.com/api-dns/dns_api_64006 .html
201
+ https://support.huaweicloud.com/api-dns/UpdateRecordSet .html
195
202
add
196
- https://support.huaweicloud.com/api-dns/zh-cn_topic_0037134404 .html
203
+ https://support.huaweicloud.com/api-dns/dns_api_64001 .html
197
204
"""
198
205
info (">>>>>%s(%s)" , domain , record_type )
199
206
zoneid = get_zone_id (domain )
@@ -207,20 +214,27 @@ def update_record(domain, value, record_type='A', name=None):
207
214
for (rid , record ) in records .items ():
208
215
if record ['records' ] != value :
209
216
"""
217
+ PUT https://{endpoint}/v2/zones/{zone_id}/recordsets/{recordset_id}
218
+
210
219
{
211
- "description": "This is an example record set.",
212
- "ttl": 3600,
213
- "records": [
214
- "192.168.10.1",
215
- "192.168.10.2"
216
- ]
220
+ "name" : "www.example.com.",
221
+ "description" : "This is an example record set.",
222
+ "type" : "A",
223
+ "ttl" : 3600,
224
+ "records" : [ "192.168.10.1", "192.168.10.2" ]
217
225
}
218
226
"""
219
227
body = {
228
+ "name" : domain ,
229
+ "description" : "Managed by DDNS." ,
230
+ "type" : record_type ,
220
231
"records" : [
221
232
value
222
233
]
223
234
}
235
+ # 如果TTL不为空,则添加到字典中
236
+ if Config .TTL is not None :
237
+ body ['ttl' ] = Config .TTL
224
238
res = request ('PUT' , '/v2/zones/' + zoneid + '/recordsets/' + record ['id' ],
225
239
body = str (jsonencode (body )))
226
240
if res :
@@ -231,14 +245,17 @@ def update_record(domain, value, record_type='A', name=None):
231
245
else :
232
246
result [rid ] = domain
233
247
else : # create
234
- print (domain )
235
248
body = {
236
249
"name" : domain ,
250
+ "description" : "Managed by DDNS." ,
237
251
"type" : record_type ,
238
252
"records" : [
239
253
value
240
254
]
241
255
}
256
+ # 如果TTL不为空,则添加到字典中
257
+ if Config .TTL is not None :
258
+ body ['ttl' ] = Config .TTL
242
259
res = request ('POST' , '/v2/zones/' + zoneid + '/recordsets' ,
243
260
body = str (jsonencode (body )))
244
261
if res :
0 commit comments