Class: Iblox

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

Class Method Summary collapse

Class Method Details

.batch_dhcp_load(batch_file) ⇒ Object



55
56
57
58
59
60
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
87
88
89
90
91
92
93
# File 'lib/iblox.rb', line 55

def self.batch_dhcp_load(batch_file)
  ext=batch_file.split('.')[-1]
  if ext.match(/(csv|yaml|yml|json)/)
    #make sure it exists
    if (File.exist? File.expand_path batch_file)
      batch_data=[]
      case ext
      when "csv"
        batch_data=[]
        f=File.open(batch_file)
        f.each_line do|line|
          entry = {}
          entry[:fqdn] = line.split(',')[0].chomp
          #figure out if this entry is an ip or CIDR
          if line.split(',')[1].chomp.match('/')
            puts "CIDR"
            entry[:network] = line.split(',')[1].chomp
          else
            puts "IP"
            entry[:ip] = line.split(',')[1].chomp
          end
          entry[:mac] = line.split(',')[2].chomp
          batch_data.push(entry)
        end
      when "yaml","yml"
        batch_data=YAML::load(File.open(batch_file))
      when "json"
        batch_data=JSON.parse(File.read(batch_file),{:symbolize_names => true})
      else
        raise ("Batch filetype #{ext} not supported")
      end
      return batch_data
    else
      raise "Batch file does not exist"
    end
  else
    raise ("Batch filetype #{ext} not supported")
  end
end

.batch_dns_load(batch_file) ⇒ Object



23
24
25
26
27
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
54
# File 'lib/iblox.rb', line 23

def self.batch_dns_load(batch_file)
  #First, see if we're using .csv, .yaml/.yml, or .json
  ext=batch_file.split('.')[-1]
  if ext.match(/(csv|yaml|yml|json)/)
    #make sure it exists
    if (File.exist? File.expand_path batch_file)
      batch_data=[]
      case ext
      when "csv"
        batch_data=[]
        f=File.open(batch_file)
        f.each_line do|line|
          entry = {}
          entry[:fqdn] = line.split(',')[0].chomp
          entry[:ip] = line.split(',')[1].chomp
          batch_data.push(entry)
        end
      when "yaml","yml"
        batch_data=YAML::load(File.open(batch_file))
      when "json"
        batch_data=JSON.parse(File.read(batch_file),{:symbolize_names => true})
      else
        raise ("Batch filetype #{ext} not supported")
      end
      return batch_data
    else
      raise "Batch file does not exist"
    end
  else
    raise ("Batch filetype #{ext} not supported")
  end
end

.config_findObject

config find



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/iblox.rb', line 4

def self.config_find()
  config_search_paths=['~/.iblox.yaml','/etc/iblox.yaml']
  config_search_paths.each do |path|
  #If #{path} is a file and re can read it, return it.
    if File.exists?(File.expand_path(path))
      return (path)
    end
  end
  #no config found
  raise ("No config found!")
end

.connect(username, password, host, wapi_version) ⇒ Object

Open infoblox connection object



17
18
19
20
21
# File 'lib/iblox.rb', line 17

def self.connect (username, password, host, wapi_version)
  ::Infoblox.wapi_version = "#{wapi_version}"
  connection = Infoblox::Connection.new(username: "#{username}",password: "#{password}", host: "#{host}")
  return connection
end

.dhcp_add(fqdn, ip, mac, verbose, connection) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/iblox.rb', line 196

def self.dhcp_add(fqdn,ip,mac,verbose,connection)
  if verbose == true
    puts "Adding DHCP fixed address for #{fqdn}(#{ip}  #{mac})"
  end
  dhcp_res = Infoblox::Fixedaddress.new(connection: connection,
  name: fqdn,
  mac: mac,
  ipv4addr: ip,
  )
  dhcp_res.post
end

.dhcp_delete(fqdn, ip, mac, verbose, connection) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/iblox.rb', line 224

def self.dhcp_delete(fqdn,ip,mac,verbose,connection)
  if verbose == true
    puts "Deleting DHCP fixed address for #{fqdn}(#{ip}  #{mac})"
  end
  dhcp_res = Infoblox::Fixedaddress.find( connection, { ipv4addr: ip, mac: mac}).first
  dhcp_res.delete

end

.dhcp_exists(mac, verbose, connection) ⇒ Object

check for DHCP reservation by mac



175
176
177
178
179
180
181
182
# File 'lib/iblox.rb', line 175

def self.dhcp_exists(mac,verbose,connection)
  dhcp_res = Infoblox::Fixedaddress.find( connection, { mac: mac}).first
  if dhcp_res == nil
    return false
  else
    return true
  end
end

.dhcp_next(network, verbose, connection, range) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/iblox.rb', line 183

def self.dhcp_next(network,verbose,connection,range)
  if verbose == true
    puts "Getting next available IP address for network #{network}"
  end
  if range == true
    range = Infoblox::Range.find(connection, network: network).first
    ip = range.next_available_ip[0]
  else
    net = Infoblox::Network.find(connection, network: network).first
    ip = net.next_available_ip[0]
  end
  return ip
end

.dhcp_update(fqdn, ip, mac, new_fqdn, new_ip, new_mac, verbose, connection) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/iblox.rb', line 207

def self.dhcp_update(fqdn,ip,mac,new_fqdn,new_ip,new_mac,verbose,connection)
  dhcp_res = Infoblox::Fixedaddress.find( connection, { ipv4addr: ip, mac: mac}).first
  if dhcp_res == nil
    raise 'no record to update found'
  else
    if new_fqdn != nil
      dhcp_res.name = new_fqdn
    end
    if new_ip != nil
      dhcp_res.ipv4addr = new_ip
    end
    if new_mac != nil
      dhcp_res.mac = new_mac
    end
    dhcp_res.post
  end
end

.dns_add(fqdn, ip, verbose, connection) ⇒ Object

Add dns A record + PTR record



129
130
131
132
133
134
135
136
137
# File 'lib/iblox.rb', line 129

def self.dns_add(fqdn,ip,verbose,connection)
  if verbose == true
    puts "Adding DNS A Record for #{fqdn}(#{ip})"
  end
  a_record = Infoblox::Arecord.new(connection: connection, name: fqdn, ipv4addr: ip)
  ptr_record = Infoblox::Ptr.new(connection: connection, ptrdname: fqdn, ipv4addr: ip)
  a_record.post
  ptr_record.post
end

.dns_delete(fqdn, ip, verbose, connection) ⇒ Object

Delete DNS record



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/iblox.rb', line 161

def self.dns_delete(fqdn,ip,verbose,connection)
  if verbose == true
    puts "Deleting DNS A Record for #{fqdn}(#{ip})"
  end
  a_record = Infoblox::Arecord.find( connection, { name: fqdn, ipv4addr: ip }).first
  ptr_record = Infoblox::Ptr.find( connection, { ptrdname: fqdn, ipv4addr: ip}).first
  #pp a_record
  #pp ptr_record
  a_record.delete
  ptr_record.delete
end

.dns_exists(fqdn, ip, verbose, connection) ⇒ Object

Check if the record we want exists



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/iblox.rb', line 117

def self.dns_exists(fqdn,ip,verbose,connection)
  #try to find A
  a_record = Infoblox::Arecord.find( connection, { name: fqdn, ipv4addr: ip }).first
  #try to find PTR
  ptr_record = Infoblox::Ptr.find( connection, { ptrdname: fqdn, ipv4addr: ip}).first
  if a_record == nil or ptr_record == nil
    return false
  else
    return true
  end
end

.dns_update(fqdn, ip, new_fqdn, new_ip, verbose, connection) ⇒ Object

Update dns record



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/iblox.rb', line 139

def self.dns_update(fqdn,ip,new_fqdn,new_ip,verbose,connection)
  a_record = Infoblox::Arecord.find( connection, { name: fqdn, ipv4addr: ip }).first
  ptr_record = Infoblox::Ptr.find( connection, { ptrdname: fqdn, ipv4addr: ip}).first
  if a_record == nil or ptr_record == nil
    raise 'no record to update found'
  else
    if new_fqdn != nil
      a_record.name = new_fqdn
      ptr_record.ptrdname = new_fqdn
    end
    if new_ip != nil
      a_record.ipv4addr = new_ip
      ptr_record.ipv4addr = new_ip
    end
    a_record.view=nil
    ptr_record.view=nil
    ptr_record.ipv6addr=nil
    a_record.put
    ptr_record.put
  end
end

.ipv4check(ip) ⇒ Object

Check we got an IPv4 address



95
96
97
98
99
100
101
102
103
# File 'lib/iblox.rb', line 95

def self.ipv4check (ip)
  ipaddr1 = IPAddr.new "#{ip}"
  if ipaddr1.ipv4?
    return true
  else
    raise "#{ip} is not a valid IP address!"
    return false
  end
end

.ipv4netcheck(ip, network) ⇒ Object

check that the ip we got is in the network we got



106
107
108
109
110
111
112
113
114
# File 'lib/iblox.rb', line 106

def self.ipv4netcheck(ip, network)
  net=IPAddr.new "#{network}"
  if net.include?(IPAddr.new "#{ip}")
    return true
  else
    raise "#{ip} is not within #{network}"
    return false
  end
end