Class: Esgob::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/esgob/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEsgob::Client #initialize(account, key) ⇒ Esgob::Client #initialize(args) ⇒ Esgob::Client #initialize(config) ⇒ Esgob::Client

Create a new Esgob Client instance.

Examples:

client = Esgob::Client.new('account', 'key')

Overloads:

  • #initializeEsgob::Client

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

  • #initialize(account, key) ⇒ Esgob::Client

    Parameters:

    • account (String)
    • key (String)
  • #initialize(args) ⇒ Esgob::Client

    Parameters:

    • options (Hash)
  • #initialize(config) ⇒ Esgob::Client

    Parameters:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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..nil? or config..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

Instance Attribute Details

#configEsgob::Config (readonly)

Esgob configuration, used to store account name and key

Returns:



8
9
10
# File 'lib/esgob/client.rb', line 8

def config
  @config
end

Instance Method Details

#accounts_getHash

Return account status; credit balance, etc.

Returns:

  • (Hash)

    Key, value pairs, containing account information.



90
91
92
93
94
# File 'lib/esgob/client.rb', line 90

def accounts_get
   = call('accounts.get')
  [:added] = Time.at([:added]) if [:added].is_a?(Fixnum)
  
end

#call(function_name, args = {}) ⇒ Hash

Call a named Esgob API function.

Examples:

client.call(‘domains.slaves.add’, :domain => ‘example.com’, :masterip => ‘192.168.0.1’)

Parameters:

  • function_name (String)

    The name of API function.

  • args (Hash) (defaults to: {})

    Pairs of argument keys and values.

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_listArray<Hash>

Returns all hosted domains

Returns:

  • (Array<Hash>)

    Array of hashes, one per domain.



98
99
100
# File 'lib/esgob/client.rb', line 98

def domains_list
  call('domains.list')[:domains]
end

#domains_slaves_add(domain, masterip) ⇒ Hash

Adds a new slave domain.

Parameters:

  • domain (String)

    The name of the domain to add

  • masterip (String)

    The IP of the master to transfer the zone from.

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



116
117
118
119
120
# 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) ⇒ Hash

Add a host allowed to AXFR out

Parameters:

  • domain (String)

    The name of the domain to update

  • axfrip (String)

    The new IP of the host to allow transfers to.

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



154
155
156
157
158
# 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) ⇒ Hash

Account Delete a host allowed to AXFR out

Parameters:

  • domain (String)

    The name of the domain to update

  • axfrip (String)

    The IP of the host to stop allowing transfers to.

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



164
165
166
167
168
# 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) ⇒ Hash

Deletes a slave domain.

Parameters:

  • domain (String)

    The name of the domain to delete.

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



125
126
127
128
129
# 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) ⇒ Hash

Force AXFR / transfer from master of a slave domain

Parameters:

  • domain (String)

    The name of the domain to transfer.

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



134
135
136
137
138
# 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_listHash

Returns all hosted slave domains as a hash

Returns:

  • (Hash)

    Domain name as key, master ip as value



104
105
106
107
108
109
110
# 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) ⇒ Array<Hash>

Given a list of domains and a master IP, add and delete domains so that the Esgob account matches the local list

Parameters:

  • domains (Array<String>)

    The an array of domains to add to Esgob

  • masterip (String)

    The master IP address to use for all the domains

Returns:

  • (Array<Hash>)

    A list of responses from the Esgob service



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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) ⇒ Hash

Updates the master IP of a slave domain

Parameters:

  • domain (String)

    The name of the domain to update

  • masterip (String)

    The new IP of the master to transfer the zone from.

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



144
145
146
147
148
# 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) ⇒ Hash

Retrieve the domain SOA serial number from the master and each anycast node

Parameters:

  • domain (String)

    The name of the domain to look up

Returns:

  • (Hash)

    The response from the Esgob service, with symbols as keys.



173
174
175
# File 'lib/esgob/client.rb', line 173

def domains_tools_soacheck(domain)
  call('domains.tools.soacheck', :domain => domain)
end

#inspectString

Returns:

  • (String)


216
217
218
# File 'lib/esgob/client.rb', line 216

def inspect
  "\#<#{self.class} account=#{config.}>"
end