module CustomResource::Route53::MixIns::Cli

Public Instance Methods

connect() click to toggle source
# File lib/customresource/route53/mixins/cli.rb, line 41
def connect()
  begin
    credentials = ::Aws::InstanceProfileCredentials.new( :ip_address => @options[:ip_address] || '169.254.169.254')
    credentials.refresh!
    raise 'Cannot obtain InstanceProfileCredentials' unless credentials.access_key_id
    @logger.info credentials.inspect
    # noinspection RubyArgCount
    @awssdk = ::Aws::Route53::Client.new(credentials: credentials, region: @region, :retry_limit => 10)
  rescue Exception => e
    if e.class.to_s.match %r'^Errno'
      abort! "#{e.message}: (InstanceProfileCredentials)?"
    else
      case e.class.to_s
        when /URI::InvalidURIError/
          abort! "#{e.message}: HINT: Is the AWS SDK credentials configured correctly?"
        else
          abort! "#{e.message}"
      end
    end
  end
end
get_item_data(item, section, params) click to toggle source
# File lib/customresource/route53/mixins/cli.rb, line 123
def get_item_data(item, section, params)
  # noinspection RubyStringKeysInHashInspection
  hash = case section
    when /PrivateHostedZones/
      {
          'HostedZoneId' => item[:id],
          'HostedZone'   => params[:hostedzone],
          'Name'         => params[:name],
          'VpcId'        => params[:vpcid],
          'Type'         => params[:type],
          'TTL'          => params[:ttl],
          'CIDRBlock'    => params[:cidrblock],
          'Comment'      => params[:comment],
      }
    when /ReverseDNSEntries/
      {
          'Name'         => item[:name],
          'Value'        => item[:value],
          'IpAddress'    => item[:ipaddress],
          'TTL'          => item[:ttl],
          'Type'         => params[:type],
          'Comment'      => params[:comment],
          'HostedZoneId' => item[:hostedzoneid],
      }
    else
      abort! "Unsupported resource type: #{section}"
  end
  data = {}
  hash.each{ |k,v| data[k] = v unless v.nil? }
  data
end
infer_params(section, params, strict = false) click to toggle source
# File lib/customresource/route53/mixins/cli.rb, line 63
def infer_params(section, params, strict = false)
  case section
  when /PrivateHostedZones/
    unless params['type']
      params['type'] = 'reverse'
    end
    unless params['hostedzone']
      cidr = IPAddress(params['cidrblock'])
      unless cidr.prefix.to_i % 8 == 0
        bits   =
        prefix = (cidr.prefix.to_i - (cidr.prefix.to_i % 8))
        i = 0
        ip = []
        while prefix > 0
          ip << cidr[i]
          prefix -= 8
          i += 1
        end

        (bits/8..3).each { |i|
            ip[i] = 0
        }
        params['cidrblock'] = ip.join('.') + '/' + bits.to_s
        # params['cidrblock'].gsub!(%r'#{cidr.prefix}$', (cidr.prefix.to_i - (cidr.prefix.to_i % 8)).to_s)
        cidr = IPAddress(params['cidrblock'])
      end
      params['hostedzone'] = "#{cidr.arpa}."
      prefix = cidr.prefix.to_i
      # Strip of leading octets from the ARPA
      while prefix > 0 #and params['hostedzone'].match(%r/^0\./)
        params['hostedzone'].gsub!(%r/^\d+\./, '')
        prefix -= 8
      end
    end
  when /ReverseDNSEntries/
    unless params[:hostedzoneid]
      unless params[:hostedzone]
        if params[:hostedzonename]
          params[:hostedzone] = params[:hostedzonename]
        end
      end
    end
    unless params[:name]
      if params[:ipaddress]
        ip = IPAddress(params[:ipaddress])
        params[:name] = "#{ip.arpa}."
      end
    end
    unless params[:resourcerecords] or (params[:value].nil? or params[:value].match(%r/\.$/))
      params[:value] = "#{params[:value]}."
    end
      if params[:resourcerecords]
        params[:resourcerecords].each{ |rr| rr[:value] = "#{rr[:value]}." unless rr[:value].match(%r/\.$/) }
      end
  else
  end

  params
end
init_attributes() click to toggle source
# File lib/customresource/route53/mixins/cli.rb, line 10
def init_attributes
  @valid_keys = {
      :PrivateHostedZones => {
          # required
          :name        => nil,
          :hostedzone  => nil,
          :comment     => nil,
          :type        => nil,
          :ttl         => nil,
          # required
          :cidrblock   => nil,
          # required
          :vpcid       => nil,
      },
      :ReverseDNSEntries => {
          :name           => nil,
          :ipaddress      => nil,
          # required
          :comment        => nil,
          :hostedzone     => nil,
          :hostedzoneid   => nil,
          :hostedzonename => nil,
          :type           => nil,
          :ttl            => nil,
          :value          => nil,
          :resourcerecords => {
              :value      => nil,
          },
      }, }
end