Class: HP::Cloud::DnsHelper

Inherits:
BaseHelper show all
Defined in:
lib/hpcloud/dns_helper.rb

Instance Attribute Summary collapse

Attributes inherited from BaseHelper

#connection, #cstatus, #fog

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseHelper

#is_valid?, #set_error, #to_hash

Constructor Details

#initialize(connection, foggy = nil) ⇒ DnsHelper

Returns a new instance of DnsHelper.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hpcloud/dns_helper.rb', line 32

def initialize(connection, foggy = nil)
  super(connection, foggy)
  return if foggy.nil?
  foggy = Hash[foggy.map{ |k, v| [k.to_sym, v] }]
  @id = foggy[:id]
  @name = foggy[:name]
  @ttl = foggy[:ttl]
  @serial = foggy[:serial]
  @email = foggy[:email]
  @created_at = foggy[:created_at]
  @updated_at = foggy[:updated_at]
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



25
26
27
# File 'lib/hpcloud/dns_helper.rb', line 25

def created_at
  @created_at
end

#emailObject

Returns the value of attribute email.



25
26
27
# File 'lib/hpcloud/dns_helper.rb', line 25

def email
  @email
end

#idObject

Returns the value of attribute id.



25
26
27
# File 'lib/hpcloud/dns_helper.rb', line 25

def id
  @id
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/hpcloud/dns_helper.rb', line 25

def name
  @name
end

#serialObject

Returns the value of attribute serial.



25
26
27
# File 'lib/hpcloud/dns_helper.rb', line 25

def serial
  @serial
end

#ttlObject

Returns the value of attribute ttl.



25
26
27
# File 'lib/hpcloud/dns_helper.rb', line 25

def ttl
  @ttl
end

#updated_atObject

Returns the value of attribute updated_at.



26
27
28
# File 'lib/hpcloud/dns_helper.rb', line 26

def updated_at
  @updated_at
end

Class Method Details

.get_keysObject



28
29
30
# File 'lib/hpcloud/dns_helper.rb', line 28

def self.get_keys()
  return [ "id", "name", "ttl", "serial", "email", "created_at" ]
end

Instance Method Details

#create_record(name, type, data, priority) ⇒ Object



92
93
94
95
96
97
# File 'lib/hpcloud/dns_helper.rb', line 92

def create_record(name, type, data, priority)
  rsp = @connection.dns.create_record(@id, name, type, data, priority)
  hash = rsp.body
  hash = Hash[hash.map{ |k, v| [k.to_sym, v] }]
  return hash
end

#delete_record(target) ⇒ Object



117
118
119
120
121
122
# File 'lib/hpcloud/dns_helper.rb', line 117

def delete_record(target)
  record = get_record(target)
  return false if record.nil?
  @connection.dns.delete_record(@id, record[:id])
  return true
end

#destroyObject



66
67
68
# File 'lib/hpcloud/dns_helper.rb', line 66

def destroy
  @connection.dns.delete_domain(@id)
end

#get_record(target) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/hpcloud/dns_helper.rb', line 99

def get_record(target)
  rsp = @connection.dns.list_records_in_a_domain(@id)
  ray = rsp.body['records']
  hash = ray.select { |x| x['id'] == target }.first
  return nil if hash.nil?
  Hash[hash.map{ |k, v| [k.to_sym, v] }]
end

#record_keysObject



81
82
83
# File 'lib/hpcloud/dns_helper.rb', line 81

def record_keys
  ["id", "name", "type", "data", "priority", "created_at"]
end

#recordsObject



85
86
87
88
89
90
# File 'lib/hpcloud/dns_helper.rb', line 85

def records
  rsp = @connection.dns.list_records_in_a_domain(@id)
  hash = rsp.body
  hash = Hash[hash.map{ |k, v| [k.to_sym, v] }]
  hash[:records]
end

#saveObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hpcloud/dns_helper.rb', line 45

def save
  return false if is_valid? == false
  if @fog.nil?
    hsh = {:ttl => @ttl.to_i}
    dns = @connection.dns.create_domain(name, email, hsh)
    if dns.nil?
      set_error("Error creating dns '#{@name}'")
      return false
    end
    hash = dns.body
    hash = Hash[hash.map{ |k, v| [k.to_sym, v] }]
    @id = hash[:id]
    @fog = dns
    return true
  end
  hsh = {:ttl => @ttl.to_i,
         :email => @email}
  @connection.dns.update_domain(@id, hsh)
  return true
end

#server_keysObject



70
71
72
# File 'lib/hpcloud/dns_helper.rb', line 70

def server_keys
  ["id", "name", "created_at"]
end

#serversObject



74
75
76
77
78
79
# File 'lib/hpcloud/dns_helper.rb', line 74

def servers
  rsp = @connection.dns.get_servers_hosting_domain(@id)
  hash = rsp.body
  hash = Hash[hash.map{ |k, v| [k.to_sym, v] }]
  hash[:servers]
end

#update_record(target, type, data) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/hpcloud/dns_helper.rb', line 107

def update_record(target, type, data)
  record = get_record(target)
  return nil if record.nil?
  options = { :type => type, :data => data}
  rsp = @connection.dns.update_record(@id, record[:id], options)
  hash = rsp.body
  hash = Hash[hash.map{ |k, v| [k.to_sym, v] }]
  return hash
end