class JunosSpace::SD::Address
Public Instance Methods
add(data)
This method will add a new address object to Space give the information in 'data'. This information is a Hash with the following structure:
`name => Name of the object you want to create`, `type => Either 'address' or 'network'`, `ip => IP address or subnet/mask`, `desc => Description (optional)`, `version => Either 'v4' or 'v6'`
# File lib/junos-space-api/sd/address.rb, line 102 def add(data) result = {} type = data['type'] == 'address' ? 'IPADDRESS' : 'NETWORK' version = data['version'] == 'v4' ? 'IPV4' : 'IPV6' if data['desc'] desc = data['desc'] else desc = '' end xml = "<address><name>#{data['name']}</name><address-type>#{type}</address-type>" + "<host-name/><edit-version/><members/><address-version>#{version}</address-version>" + "<definition-type>CUSTOM</definition-type><ip-address>#{data['ip']}</ip-address>" + "<description>#{desc}</description></address>" begin res = RestClient.post("#{JunosSpace.base_uri}#{@@address_uri}", xml, @@address_headers) if res.code == 200 result['status'] = '200 OK - Success' end return result rescue RestClient::Unauthorized result['status'] = '401 Error - Auth failure (bad username/password).' return result rescue RestClient::InternalServerError result['status'] = '500 Error - Check and see if the device already exists.' return result end end
delete(name)
Deletes the object matching the name 'name'. If more than one address object is found during the search, then send a warning to refine the search (i.e. use the exact name if possible).
# File lib/junos-space-api/sd/address.rb, line 143 def delete(name) result = {} begin res = RestClient.get("#{JunosSpace.base_uri}#{@@address_uri}", :params => {:filter => "(global eq '#{name}')"}) doc = Nokogiri::XML::Document.parse(res) headers = { :content_type => 'application/vnd.juniper.sd.address-management.delete-address-response+xml;version=1;q=0.01' } num_results = doc.xpath('//addresses/@total').text if num_results == "0" result['status'] = 'Address object does not exist.' elsif num_results.to_i > 1 result['status'] = 'More than one object was found. Please refine your search (use the exact name if possible).' else doc.xpath("//address[contains(translate(name, '#{@@ucase}', '#{@@dcase}'), translate('#{name}', '#{@@ucase}', '#{@@dcase}'))]").each do |address| id = address.xpath('id').text res = RestClient.delete("#{JunosSpace.base_uri}#{@@address_uri}/#{id}", headers) if res == '' result['status'] = '200 OK - Success.' end end end return result rescue RestClient::Unauthorized result['status'] = '401 Error - Auth failure (bad username/password).' return result end end
info(name)
Searches for address object 'name' and returns a Hash with the address object(s) name as the key, and the IP address as the value. If the address object(s) are a group, then the value within the Hash is an array of the address object names within the group(s).
# File lib/junos-space-api/sd/address.rb, line 47 def info(name) result = {} begin res = RestClient.get("#{JunosSpace.base_uri}#{@@address_uri}", :params => {:filter => "(global eq '#{name}')"}) doc = Nokogiri::XML::Document.parse(res) count = doc.xpath('//addresses/@total').text if count == "0" result['status'] = 'No address object(s) found.' else doc.xpath("//address[contains(translate(name, '#{@@ucase}', '#{@@dcase}'), translate('#{name}', '#{@@ucase}', '#{@@dcase}'))]").each do |address| name = address.xpath('name').text type = address.xpath('address-type').text id = address.xpath('id').text if type != 'GROUP' address.xpath('//address').each do |info| ip = info.xpath('ip-address').text result[name] = ip end elsif type == 'GROUP' res = RestClient.get("#{JunosSpace.base_uri}#{@@address_uri}/#{id}") doc = Nokogiri::XML::Document.parse(res) group_members = [] doc.xpath('//member').each do |member| member_name = member.xpath('name').text member_ip = member.xpath('ip-address').text group_members << member_name end result[name] = group_members end end end return result rescue RestClient::Unauthorized result['status'] = '401 Error - Auth failure (bad username/password).' return result end end
list
Returns a Hash of all of the address objects in Space. The name of the object is the key, and the ID is the value.
# File lib/junos-space-api/sd/address.rb, line 18 def list result = {} begin res = RestClient.get("#{JunosSpace.base_uri}#{@@address_uri}") doc = Nokogiri::XML::Document.parse(res) doc.xpath('//address').each do |address| name = address.xpath('name').text id = address.xpath('id').text result[name] = id end return result rescue RestClient::Unauthorized result['status'] = '401 Error - Auth failure (bad username/password).' return result end end