Class: JunosSpace::SD::Address
- Inherits:
-
Object
- Object
- JunosSpace::SD::Address
- Defined in:
- lib/junos-space-api/sd/address.rb
Constant Summary collapse
- @@address_uri =
'/api/juniper/sd/address-management/addresses'
- @@address_headers =
{ :content_type => "application/vnd.juniper.sd.address-management.address+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 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’‘
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 |
# 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) ⇒ Object
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).
143 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 175 176 |
# 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) ⇒ Object
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).
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 |
# 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 ⇒ Object
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.
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/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 |