Class: JunosSpace::SD::Service
- Inherits:
-
Object
- Object
- JunosSpace::SD::Service
- Defined in:
- lib/junos-space-api/sd/service.rb
Constant Summary collapse
- @@service_uri =
'/api/juniper/sd/service-management/services'
- @@service_headers =
{ :content_type => "application/vnd.juniper.sd.service-management.service+xml;version=1;charset=UTF-8" }
- @@ucase =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- @@dcase =
'abcdefghijklmnopqrstuvwxyz'
Instance Method Summary collapse
-
#add(data) ⇒ Object
add(data).
-
#delete(name) ⇒ Object
delete(name).
-
#info(name) ⇒ Object
info(name).
-
#list ⇒ Object
list.
Instance Method Details
#add(data) ⇒ Object
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)`
102 103 104 105 106 107 108 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 |
# 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) ⇒ Object
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).
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# 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) ⇒ Object
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).
47 48 49 50 51 52 53 54 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 |
# 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 ⇒ Object
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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 |