Class: Ec2UltraDNSUpdater::UltraDNS

Inherits:
Object
  • Object
show all
Includes:
Preconditions
Defined in:
lib/ec2_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 Method Summary collapse

Methods included from Preconditions

#precondition, #str_not_empty

Constructor Details

#initialize(opts = {}) ⇒ UltraDNS

Returns a new instance of UltraDNS.



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

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.str_not_empty(@username)
  precondition.str_not_empty(@password)
  precondition.str_not_empty(@zone)
end

Instance Method Details

#clientObject



193
194
195
196
197
198
199
200
201
202
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 193

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



61
62
63
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 61

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



53
54
55
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 53

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



109
110
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
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 109

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

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

  if guid != ''
    @logger.info "Updating Record Mapping: #{fixed_label} => #{to_value}"
    change_method = 'updateResourceRecord' # 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

  resp = client.request(:v01, 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?
end

#delete_record_by_guid(guid) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 72

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

#delete_record_by_label(label) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 88

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



96
97
98
99
100
101
102
103
104
105
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 96

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 '.'



209
210
211
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 209

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

#get_a_guid(node_label) ⇒ Object



57
58
59
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 57

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

#get_cname_guid(cname) ⇒ Object



49
50
51
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 49

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

#get_guid_by_label_and_type(label, type) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 149

def get_guid_by_label_and_type(label, type)
  zone = @zone
  fixed_label = fix_zone_name(label)
  
  guids_resp = client.request(:v01, 'getResourceRecordGUIDList') {
    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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 172

def get_record_by_label_and_type(label, type)
  zone = @zone
  fixed_label = fix_zone_name(label)
  
  records_resp = client.request(:v01, 'getResourceRecordsOfDNameByType') {
    soap.body do |xml|
      xml.zoneName(zone)
      xml.rrType(type)
      xml.hostName(fixed_label)
    end
  }
  log_error(records_resp)

  begin
    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



84
85
86
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 84

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

#log_error(response) ⇒ Object



204
205
206
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 204

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

#network_status?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/ec2_ultradns_updater/ultradns.rb', line 65

def network_status?    
  resp = client.request(:v01, 'getNeustarNetworkStatus') #:get_neustar_network_status) 
  @logger.debug(resp)
  log_error(resp)
  resp.success?
end