Created
January 14, 2014 03:43
-
-
Save vincenting/8412656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| require "faraday" | |
| require "json" | |
| LOGIN_EMAIL = '[email protected]' | |
| LOGIN_PASSWORD = 'password here' | |
| def send_req(url, params = {}) | |
| req_params = { | |
| :login_email => LOGIN_EMAIL, | |
| :login_password => LOGIN_PASSWORD, | |
| :format => 'json' | |
| } | |
| res = Faraday.post(url, req_params.merge(params)) | |
| JSON.parse res.body | |
| end | |
| def get_domain_id(domain) | |
| domain_list_res = send_req 'https://dnsapi.cn/Domain.List' | |
| domain_index = domain_list_res['domains'].index do |domain_details| | |
| domain_details['name'] == domain | |
| end | |
| domain_list_res['domains'][domain_index]['id'] | |
| end | |
| def update_dns(domain_id, record_id, sub_domain, type, value, mx) | |
| update_res = send_req('https://dnsapi.cn/Record.Modify', { | |
| :domain_id => domain_id, | |
| :record_id => record_id, | |
| :sub_domain => sub_domain, | |
| :record_type => type.upcase, | |
| :record_line => '默认', | |
| :value => value, | |
| :mx => mx | |
| }) | |
| if update_res['status']['code'] == 1 | |
| end | |
| end | |
| def create_dns(domain_id, sub_domain, type, value, mx) | |
| create_res = send_req('https://dnsapi.cn/Record.Create', { | |
| :domain_id => domain_id, | |
| :sub_domain => sub_domain, | |
| :record_type => type.upcase, | |
| :record_line => '默认', | |
| :value => value, | |
| :mx => mx | |
| }) | |
| if create_res['status']['code'] == 1 | |
| end | |
| end | |
| def upsert_dns(domain, sub_domain, type, value, mx=10) | |
| domain_id = get_domain_id domain | |
| dns_records = send_req('https://dnsapi.cn/Record.List' ,{:domain_id => domain_id})['records'] | |
| record_index = dns_records.index do |record| | |
| record['type'] == type && record['name'] == sub_domain | |
| end | |
| if record_index | |
| record_id = dns_records[record_index]['id'] | |
| update_dns(domain_id, record_id, sub_domain, type, value, mx) | |
| else | |
| create_dns(domain_id, sub_domain, type, value, mx) | |
| end | |
| end | |
| # Usage | |
| # upsert_dns 'example.com', 'sub_domain', 'A', '10.10.10.10' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment