Class: UltraDNSUpdater::UltraDNS

Inherits:
Object
  • Object
show all
Includes:
Preconditions
Defined in:
lib/ultradns_updater/ultradns.rb

Defined Under Namespace

Modules: TYPE

Constant Summary collapse

ULTRADNS_WSDL_URL =
"http://ultra-api.ultradns.com:8008/UltraDNS_WS/v01?wsdl"
DEFAULT_TTL =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Preconditions

#not_empty, #precondition

Constructor Details

#initialize(opts = {}) ⇒ UltraDNS

Returns a new instance of UltraDNS.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ultradns_updater/ultradns.rb', line 31

def initialize(opts = {})
  @logger = opts[:logger] || Logger.new(STDOUT)
  
  Savon.configure do |config|
    config.logger = @logger
    config.raise_errors = false
  end
  # also configure HTTPI
  HTTPI.logger = @logger

  @username = opts[:username]
  @password = opts[:password]
  @zone = fix_zone_name(opts[:zone] || '')
  
  precondition.not_empty(@username)
  precondition.not_empty(@password)
  precondition.not_empty(@zone)
end

Instance Attribute Details

#zoneObject (readonly)

Returns the value of attribute zone.



19
20
21
# File 'lib/ultradns_updater/ultradns.rb', line 19

def zone
  @zone
end

Instance Method Details

#clientObject



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ultradns_updater/ultradns.rb', line 200

def client
  soap_username = @username
  soap_password = @password
  
  @client ||= Savon::Client.new do
    wsdl.document = ULTRADNS_WSDL_URL
    wsse.credentials soap_username, soap_password
  end

  @client
end

#create_or_update_a(node_label, to_ip) ⇒ Object



63
64
65
# File 'lib/ultradns_updater/ultradns.rb', line 63

def create_or_update_a(node_label, to_ip)
  create_or_update_info1(TYPE::A, node_label, to_ip)
end

#create_or_update_cname(cname, to_hostname) ⇒ Object



55
56
57
# File 'lib/ultradns_updater/ultradns.rb', line 55

def create_or_update_cname(cname, to_hostname)
  create_or_update_info1(TYPE::CNAME, cname, fix_zone_name(to_hostname))
end

#create_or_update_info1(type, label, to_value) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ultradns_updater/ultradns.rb', line 111

def create_or_update_info1(type, label, to_value)
  precondition.not_empty(label)
  precondition.not_empty(to_value)
  
  zone = @zone
  fixed_label = fix_zone_name(label)
  
  guid = get_guid_by_label_and_type(label, type) || ''

  change_method = :create_resource_record
  record_opts = {'Type' => type, 'DName' => fixed_label, 'TTL' => DEFAULT_TTL}

  if guid != ''
    @logger.info "Updating Record Mapping: #{fixed_label} => #{to_value}"
    change_method = :update_resource_record # update it
    record_opts['Guid'] = guid;
  else
    # make sure there isn't another record at this label of a different type
    delete_record_by_label_not_matching_type(label, type)
    @logger.info "Creating Record Mapping: #{fixed_label} => #{to_value}"
    record_opts['ZoneName'] = zone;
  end

  begin
    resp = client.request(change_method) {
      ns = ''
      soap.namespaces.each do |k, v|
        ns = k.gsub(/xmlns:/,'') if v =~ /schema\.ultraservice\.neustar\.com/
      end
      soap.body do |xml|
        xml.transactionID()
        xml.resourceRecord(record_opts) {
          xml.tag!("#{ns}:InfoValues", {"Info1Value" => to_value})
        }
      end
    }
  
    log_error(resp)
    resp.success?
  rescue
    false
  end
end

#delete_record_by_guid(guid) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/ultradns_updater/ultradns.rb', line 74

def delete_record_by_guid(guid)
  resp = client.request(:delete_resource_record) {
    soap.body do |xml|
      xml.transactionID()
      xml.guid(guid)
    end
  }
  log_error(resp)
  resp.success?
end

#delete_record_by_label(label) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/ultradns_updater/ultradns.rb', line 90

def delete_record_by_label(label)
  recs = get_records_by_label(label)
  unless recs.nil?
    guid = (recs[:resource_record][:@guid] rescue '') || ''
    delete_record_by_guid(guid) unless guid == ''
  end
end

#delete_record_by_label_not_matching_type(label, type) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/ultradns_updater/ultradns.rb', line 98

def delete_record_by_label_not_matching_type(label, type)
  recs = get_records_by_label(label)
  unless recs.nil?
    guid = (recs[:resource_record][:@guid] rescue '') || ''
    rec_type = (recs[:resource_record][:@type] rescue '') || ''
    if guid != '' && rec_type != type
      delete_record_by_guid(guid)
    end
  end
end

#fix_zone_name(name) ⇒ Object

..Must end with a '.'



217
218
219
220
# File 'lib/ultradns_updater/ultradns.rb', line 217

def fix_zone_name(name)
  name.strip!
  (name != nil && name != '' && name[-1, 1] != '.') ? "#{name}." : name
end

#get_a_guid(node_label) ⇒ Object



59
60
61
# File 'lib/ultradns_updater/ultradns.rb', line 59

def get_a_guid(node_label)
  get_guid_by_label_and_type(node_label, TYPE::A)
end

#get_cname_guid(cname) ⇒ Object



51
52
53
# File 'lib/ultradns_updater/ultradns.rb', line 51

def get_cname_guid(cname)
  get_guid_by_label_and_type(cname, TYPE::CNAME)
end

#get_guid_by_label_and_type(label, type) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ultradns_updater/ultradns.rb', line 155

def get_guid_by_label_and_type(label, type)
  zone = @zone
  fixed_label = fix_zone_name(label)

  guids_resp = client.request(:get_resource_record_guid_list) {
    soap.body do |xml|
      xml.resourceRecord({
        "ZoneName" => zone, 'Type' => type, 'DName' => fixed_label
      }) 
    end
  }

  log_error(guids_resp)
  
  begin
    guid = guids_resp[:get_resource_record_guid_list_response][:resource_record_guid_list][:guid]
    @logger.debug("guid found: #{guid}")
  rescue
    @logger.debug("guid not found")
  end
  guid
end

#get_record_by_label_and_type(label, type) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ultradns_updater/ultradns.rb', line 179

def get_record_by_label_and_type(label, type)
  zone = @zone
  fixed_label = fix_zone_name(label)
  
  begin
    records_resp = client.request('getResourceRecordsOfDNameByType'.snakecase.to_sym) {
      soap.body do |xml|
        xml.zoneName(zone)
        xml.rrType(type)
        xml.hostName(fixed_label)
      end
    }
    log_error(records_resp)  
  
    records_resp.to_hash[:get_resource_records_of_d_name_by_type_response][:resource_record_list]
  rescue
    @logger.debug("Resource Record list is empty")
  end
end

#get_records_by_label(label) ⇒ Object



86
87
88
# File 'lib/ultradns_updater/ultradns.rb', line 86

def get_records_by_label(label)
  get_record_by_label_and_type(label, TYPE::ALL)
end

#log_error(response) ⇒ Object



212
213
214
# File 'lib/ultradns_updater/ultradns.rb', line 212

def log_error(response)
  @logger.error {response.to_xml} unless response.success?
end

#network_status?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/ultradns_updater/ultradns.rb', line 67

def network_status?    
  resp = client.request(:get_neustar_network_status) 
  @logger.debug(resp)
  log_error(resp)
  resp.success?
end