class Esgob::Client

Attributes

config[R]

Esgob configuration, used to store account name and key @return [Esgob::Config]

Public Class Methods

new(*args) click to toggle source

Create a new Esgob Client instance.

@overload initialize

Create a new client, using one of the default configuration files.
Or the ESGOB_ACCOUNT and ESGOB_KEY environment variables.

@overload initialize(account, key)

@param [String] account
@param [String] key

@overload initialize(args)

@param [Hash] options
@option options [String] :endpoint The URI of the API endpoint
@option options [String] :account The account name
@option options [String] :key The API key

@overload initialize(config)

@param [Esgob::Config] config

@return [Esgob::Client] A new client instance. @example

client = Esgob::Client.new('account', 'key')
# File lib/esgob/client.rb, line 28
def initialize(*args)
  if args.empty?
    # Load configuration from file if no arguments were given
    @config = Esgob::Config.load
  elsif args.first.is_a?(Esgob::Config)
    @config = args.first
  elsif args.first.is_a?(Hash)
    @config = Esgob::Config.new(args.first)
  elsif args.length == 2
    @config = Esgob::Config.new(:account => args[0], :key => args[1])
  else
    raise(ArgumentError, "Unsupported arguments for creating Esgob::Client")
  end

  if config.nil?
    raise(Esgob::UnconfiguredError, "Unable to load Esgob configuration")
  end

  if config.account.nil? or config.account.empty?
    raise(ArgumentError, "No account name configured for Esgob")
  end

  if config.key.nil? or config.key.empty?
    raise(ArgumentError, "No API key configured for Esgob")
  end
end

Public Instance Methods

accounts_get() click to toggle source

Return account status; credit balance, etc. @return [Hash] Key, value pairs, containing account information.

# File lib/esgob/client.rb, line 90
def accounts_get
  account = call('accounts.get')
  account[:added] = Time.at(account[:added]) if account[:added].is_a?(Fixnum)
  account
end
call(function_name, args = {}) click to toggle source

Call a named Esgob API function.

@param [String] function_name The name of API function. @param [Hash] args Pairs of argument keys and values. @return [Hash] The response from the Esgob service, with symbols as keys. @example client.call(‘domains.slaves.add’, :domain => ‘example.com’, :masterip => ‘192.168.0.1’)

# File lib/esgob/client.rb, line 61
def call(function_name, args = {})
  uri = URI(config.endpoint + function_name)
  uri.query = build_query(default_arguments.merge(args))

  res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    req = Net::HTTP::Get.new(uri.request_uri)
    req['Accept'] = 'application/json'
    http.request(req)
  end

  if res.content_type == 'application/json'
    data = symbolize_keys! JSON.parse(res.body)
    if data.key?(:error)
      raise Esgob::ServerError.new(
        data[:error][:message],
        data[:error][:code].to_s
      )
    elsif res.code !~ /^2/
      raise Esgob::ServerError.new(res.message, res.code)
    else
      return data
    end
  else
    raise "HTTP response from ESGOB is not of type JSON"
  end
end
domains_list() click to toggle source

Returns all hosted domains @return [Array<Hash>] Array of hashes, one per domain.

# File lib/esgob/client.rb, line 98
def domains_list
  call('domains.list')[:domains]
end
domains_slaves_add(domain, masterip) click to toggle source

Adds a new slave domain. @param [String] domain The name of the domain to add @param [String] masterip The IP of the master to transfer the zone from. @return [Hash] The response from the Esgob service, with symbols as keys.

# File lib/esgob/client.rb, line 116
def domains_slaves_add(domain, masterip)
  result = call('domains.slaves.add', :domain => domain, :masterip => masterip)
  result[:domain] ||= domain
  result
end
domains_slaves_axfrout_add(domain, axfrip) click to toggle source

Add a host allowed to AXFR out @param [String] domain The name of the domain to update @param [String] axfrip The new IP of the host to allow transfers to. @return [Hash] The response from the Esgob service, with symbols as keys.

# File lib/esgob/client.rb, line 154
def domains_slaves_axfrout_add(domain, axfrip)
  result = call('domains.slaves.axfrout.add', :domain => domain, :axfrip => axfrip)
  result[:domain] ||= domain
  result
end
domains_slaves_axfrout_delete(domain, axfrip) click to toggle source

Account Delete a host allowed to AXFR out @param [String] domain The name of the domain to update @param [String] axfrip The IP of the host to stop allowing transfers to. @return [Hash] The response from the Esgob service, with symbols as keys.

# File lib/esgob/client.rb, line 164
def domains_slaves_axfrout_delete(domain, axfrip)
  result = call('domains.slaves.axfrout.delete', :domain => domain, :axfrip => axfrip)
  result[:domain] ||= domain
  result
end
domains_slaves_delete(domain) click to toggle source

Deletes a slave domain. @param [String] domain The name of the domain to delete. @return [Hash] The response from the Esgob service, with symbols as keys.

# File lib/esgob/client.rb, line 125
def domains_slaves_delete(domain)
  result = call('domains.slaves.delete', :domain => domain)
  result[:domain] ||= domain
  result
end
domains_slaves_forcetransfer(domain) click to toggle source

Force AXFR / transfer from master of a slave domain @param [String] domain The name of the domain to transfer. @return [Hash] The response from the Esgob service, with symbols as keys.

# File lib/esgob/client.rb, line 134
def domains_slaves_forcetransfer(domain)
  result = call('domains.slaves.forcetransfer', :domain => domain)
  result[:domain] ||= domain
  result
end
domains_slaves_list() click to toggle source

Returns all hosted slave domains as a hash @return [Hash] Domain name as key, master ip as value

# File lib/esgob/client.rb, line 104
def domains_slaves_list
  Hash[
    call('domains.slaves.list')[:domains].map do |item|
      [item[:domain], item[:masterip]]
    end
  ]
end
domains_slaves_sync(domains, masterip) click to toggle source

Given a list of domains and a master IP, add and delete domains so that the Esgob account matches the local list @param [Array<String>] domains The an array of domains to add to Esgob @param [String] masterip The master IP address to use for all the domains @return [Array<Hash>] A list of responses from the Esgob service

# File lib/esgob/client.rb, line 182
def domains_slaves_sync(domains, masterip)
  existing_domains = domains_slaves_list

  # Add any missing domains
  responses = []
  domains.each do |domain|
    unless existing_domains.include?(domain)
      response = domains_slaves_add(domain, masterip)
      response[:domain] ||= domain
      responses << response
    end
  end

  # Now check the existing domains
  existing_domains.keys.sort.each do |domain|
    if domains.include?(domain)
      # Update the masterip if it isn't correct
      if existing_domains[domain] != masterip
        response = domains_slaves_updatemasterip(domain, masterip)
        response[:domain] ||= domain
        responses << response
      end
    else
      # Delete domain; not on list
      response = domains_slaves_delete(domain)
      response[:domain] ||= domain
      responses << response
    end
  end

  responses
end
domains_slaves_updatemasterip(domain, masterip) click to toggle source

Updates the master IP of a slave domain @param [String] domain The name of the domain to update @param [String] masterip The new IP of the master to transfer the zone from. @return [Hash] The response from the Esgob service, with symbols as keys.

# File lib/esgob/client.rb, line 144
def domains_slaves_updatemasterip(domain, masterip)
  result = call('domains.slaves.updatemasterip', :domain => domain, :masterip => masterip)
  result[:domain] ||= domain
  result
end
domains_tools_soacheck(domain) click to toggle source

Retrieve the domain SOA serial number from the master and each anycast node @param [String] domain The name of the domain to look up @return [Hash] The response from the Esgob service, with symbols as keys.

# File lib/esgob/client.rb, line 173
def domains_tools_soacheck(domain)
  call('domains.tools.soacheck', :domain => domain)
end
inspect() click to toggle source

@return [String]

# File lib/esgob/client.rb, line 216
def inspect
  "\#<#{self.class} account=#{config.account}>"
end

Protected Instance Methods

build_query(hash) click to toggle source
# File lib/esgob/client.rb, line 244
def build_query(hash)
  hash.keys.sort { |a, b| a.to_s <=> b.to_s }.map do |key|
    URI.escape(key.to_s) + '=' + URI.escape(hash[key].to_s)
  end.join('&')
end
default_arguments() click to toggle source
# File lib/esgob/client.rb, line 236
def default_arguments
  {
    :account => config.account,
    :key => config.key,
    :f => 'json'
  }
end
symbolize_keys!(hash) click to toggle source
# File lib/esgob/client.rb, line 222
def symbolize_keys!(hash)
  hash.keys.each do |key|
    ks = key.to_sym
    hash[ks] = hash.delete(key)
    case hash[ks]
      when Hash
        symbolize_keys!(hash[ks])
      when Array
        hash[ks].each { |item| symbolize_keys!(item) if item.is_a?(Hash) }
    end
  end
  hash
end