class JunosSpace::SD::Service
Public Instance Methods
add(data)
This method will add a new service object to Space give the information in 'data'. This information is a Hash table, with the following structure:
`name => `Name of the object you want to create` `port => Port number` `desc => Description (optional)`
# File lib/junos-space-api/sd/service.rb, line 102 def add(data) result = {} if data['desc'] desc = data['desc'] else desc = '' end xml = "<service><name>#{data['name']}</name><is-group>false</is-group><edit-version/>" + "<id/><created-by-user-name/><members/><protocols><protocol><description/>" + "<sunrpc-protocol-type>6</sunrpc-protocol-type><msrpc-protocol-type>6</msrpc-protocol-type>" + "<protocol-number>6</protocol-number><name>#{data['name']}</name><dst-port>#{data['port']}</dst-port>" + "<disable-timeout>false</disable-timeout><protocol-type>0</protocol-type><icmp-code>0</icmp-code>" + "<icmp-type>0</icmp-type><rpc-program-number>0</rpc-program-number>" + "</protocol></protocols><description>#{desc}</description></service>" begin res = RestClient.post("#{JunosSpace.base_uri}#{@@service_uri}", xml, @@service_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 service 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/service.rb, line 144 def delete(name) result = {} begin res = RestClient.get("#{JunosSpace.base_uri}#{@@service_uri}", :params => {:filter => "(global eq '#{name}')"}) doc = Nokogiri::XML::Document.parse(res) num_results = doc.xpath('//services/@total').text if num_results == "0" result['status'] = 'Service 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("//service[contains(translate(name, '#{@@ucase}', '#{@@dcase}'), translate('#{name}', '#{@@ucase}', '#{@@dcase}'))]").each do |service| id = service.xpath('id').text res = RestClient.delete("#{JunosSpace.base_uri}#{@@service_uri}/#{id}", @@service_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 the service 'name' and returns a Hash with the service object(s) name as the key, and the ID as the value. If the service object(s) are a group, then the value within the Hash is an array of the service object names within the group(s).
# File lib/junos-space-api/sd/service.rb, line 47 def info(name) result = {} begin res = RestClient.get("#{JunosSpace.base_uri}#{@@service_uri}", :params => {:filter => "(global eq '#{name}')"}) doc = Nokogiri::XML::Document.parse(res) count = doc.xpath('//services/@total').text if count == "0" result['status'] = 'No service object(s) found.' else doc.xpath("//service[contains(translate(name, '#{@@ucase}', '#{@@dcase}'), translate('#{name}', '#{@@ucase}', '#{@@dcase}'))]").each do |service| group = service.xpath('is-group').text name = service.xpath('name').text id = service.xpath('id').text if group == 'false' res = RestClient.get("#{JunosSpace.base_uri}#{@@service_uri}/#{id}") doc = Nokogiri::XML::Document.parse(res) doc.xpath('//protocol').each do |info| dst_port = info.xpath('dst-port').text result[name] = dst_port end elsif group == 'true' res = RestClient.get("#{JunosSpace.base_uri}#{@@service_uri}/#{id}") doc = Nokogiri::XML::Document.parse(res) group_members = [] doc.xpath('//member').each do |member| member_name = member.xpath('name').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 individual service objects in Space. The name of the service is the key, and the ID is the value.
# File lib/junos-space-api/sd/service.rb, line 18 def list result = {} begin res = RestClient.get("#{JunosSpace.base_uri}#{@@service_uri}") doc = Nokogiri::XML::Document.parse(res) doc.xpath('//service').each do |service| name = service.xpath('name').text id = service.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