Module: Fhcap::Route53Helper

Includes:
ProvidersHelper
Included in:
Tasks::Dns::Aws::CreateRecord, Tasks::Dns::Aws::DeleteRecord, Tasks::Dns::Aws::ListRecords
Defined in:
lib/fhcap/tasks/dns/aws/route53_helper.rb

Instance Method Summary collapse

Methods included from ProvidersHelper

#provider_availability_zones, #provider_config, #provider_credentials, #provider_for, #provider_names, #provider_names_for, #provider_regions, #provider_type, #providers_config

Instance Method Details

#with_hosted_zone(domain) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fhcap/tasks/dns/aws/route53_helper.rb', line 10

def with_hosted_zone(domain)
  host_domain = PublicSuffix.parse(domain).domain
  provider = provider_for(host_domain)

  if provider
    begin
      with_provider_client(provider) do |client|
        resp = client.list_hosted_zones
        zone = resp.hosted_zones.reject { |z| !(z.name =~ /#{host_domain}/) }.first

        if zone
          thor.say "Found zone id: #{zone.id}, name: #{zone.name}"
          yield client, zone
        else
          thor.say_status 'error', "Can't get hosted zone for '#{host_domain}'", :red
        end
      end
    rescue Aws::Route53::Errors::ServiceError => e
      thor.say_status 'error', e.message, :red
    end
  else
    thor.say_status 'error', "No DNS provider configured for '#{domain}'", :red
  end
end

#with_provider_client(provider) {|client| ... } ⇒ Object

Yields:

  • (client)


35
36
37
38
39
40
41
42
43
44
# File 'lib/fhcap/tasks/dns/aws/route53_helper.rb', line 35

def with_provider_client(provider)
  cfg = provider_config(provider)
  region = provider_regions(provider).keys.first
  creds = cfg[:credentials]
  client = Aws::Route53::Client.new(
      region: region,
      credentials: Aws::Credentials.new(creds[:'aws-access-key'], creds[:'aws-secret-key'])
  )
  yield client
end