Created
April 20, 2016 14:38
-
-
Save elight/1e53b81ce94cc2c8afa1358609ede99c 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
module API::Clients | |
class DnsManager < Base | |
def initialize(url) | |
headers = { | |
"Accept" => "application/json", | |
"Content-Type" => "application/json", | |
"Request-Id" => API::RequestStore.request_ids, | |
"X-Request-Id" => API::RequestStore.request_id, | |
} | |
super(url, "heroku-dns-manager", headers: headers) | |
end | |
def create_cname(domain, target) | |
send_request( | |
params( | |
action: :CREATE, | |
domain: domain, | |
target: target | |
) | |
) | |
end | |
def delete_cname(domain, target) | |
send_request( | |
params( | |
action: :DELETE, | |
domain: domain, | |
target: target | |
) | |
) | |
end | |
private | |
def params(action:, domain:, target:, type:) | |
case type | |
when "CNAME" | |
params_cname(action: action, domain: domain, target: target) | |
when "ALIAS" | |
params_alias(action: action, domain: domain, target: target) | |
end | |
end | |
def params_cname(action:, domain:, target:) | |
{ | |
change: { | |
:action => action, | |
"resource-record-set" => { | |
:name => domain, | |
:type => "CNAME", | |
:ttl => "600", | |
"resource-records" => [ | |
{ | |
value: target | |
} | |
] | |
} | |
} | |
} | |
end | |
def params_alias(action:, domain:, target:) | |
{ | |
change: { | |
:action => "UPSERT", | |
"resource-record-set" => { | |
:name => domain, | |
:type => "A", | |
"alias-target" => { | |
"dns-name" => target, | |
"evaluate-target-health" => false | |
} | |
} | |
} | |
} | |
end | |
def send_request(p) | |
request( | |
method: :post, | |
body: MultiJson.encode(p), | |
expects: [201], | |
path: "/requests/#{API::Config.heroku_stable_dns_domain}" | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment