Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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