zenoss_client 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.5.2
@@ -31,11 +31,12 @@ module Zenoss
31
31
  include Zenoss::JSONAPI::ReportRouter
32
32
  include Zenoss::RESTAPI
33
33
 
34
- def initialize(url, user, pass)
34
+ def initialize(url, user, pass, &block)
35
35
  @zenoss_uri = (url.is_a?(URI) ? url : URI.parse(url))
36
36
  @request_number = 1
37
37
  @httpcli = HTTPClient.new
38
38
  @httpcli.receive_timeout = 360 # six minutes should be more that sufficient
39
+ yield(@httpcli) if block_given?
39
40
  sign_in(user,pass)
40
41
  end
41
42
 
@@ -24,6 +24,7 @@ module Zenoss
24
24
  include Zenoss::Model::EventView
25
25
  include Zenoss::Model::RRDView
26
26
  include Zenoss::Model::DeviceResultInt
27
+ include Zenoss::Model::ZenPropertyManager
27
28
 
28
29
  # Initialize this object from a Hash returned via getDevices from the JSON api
29
30
  # @param[Zenoss] zenoss the current instance we are connecting with
@@ -0,0 +1,85 @@
1
+ #############################################################################
2
+ # Copyright © 2010 Dan Wanek <dwanek@nd.gov>
3
+ #
4
+ #
5
+ # This file is part of zenoss_client.
6
+ #
7
+ # zenoss_client is free software: you can redistribute it and/or
8
+ # modify it under the terms of the GNU General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or (at
10
+ # your option) any later version.
11
+ #
12
+ # zenoss_client is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15
+ # Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License along
18
+ # with zenoss_client. If not, see <http://www.gnu.org/licenses/>.
19
+ #############################################################################
20
+ module Zenoss
21
+ module Model
22
+ module ZenPropertyManager
23
+
24
+ # ------------------ REST Calls ------------------ #
25
+
26
+ # @param [TrueClass,FalseClass] all true to return all Ids or false
27
+ # to get just the overridden Ids.
28
+ # @return [Array] Return an Array of zProperty Ids for this device
29
+ def zen_property_ids(all=true)
30
+ method = "zenPropertyIds?all=#{all.to_s.capitalize}"
31
+
32
+ plist_to_array( custom_rest(method) )
33
+ end
34
+
35
+ # @return [Hash] Return a Hash of zProperies and values
36
+ def zen_property_items
37
+ method = 'zenPropertyItems'
38
+
39
+ outstr = rest(method)
40
+ ptuples_to_hash outstr.slice(1..-2).split(/,(?=\s\()/)
41
+ end
42
+
43
+ # Set a zProperty
44
+ # @param [String] propname the property to set
45
+ # @param [String, Array, Boolean] propvalue the value to set the property to
46
+ # @return [Boolean] true on success false otherwise
47
+ def set_zen_property(propname, propvalue)
48
+ method = 'setZenProperty'
49
+ if(propvalue.is_a? Array)
50
+ custom_rest("#{method}?propname=#{propname}&propvalue=[#{propvalue.join(',')}]") == "None\n"
51
+ else
52
+ if(propvalue.is_a?(TrueClass) || propvalue.is_a?(FalseClass))
53
+ propvalue = propvalue.to_s.capitalize
54
+ end
55
+ custom_rest("#{method}?propname=#{propname}&propvalue=#{propvalue}") == "None\n"
56
+ end
57
+ end
58
+
59
+ # Delete a zProperty from the device
60
+ # @param [String] propname the overridden zProperty to delete from the device
61
+ # @return [Boolean] true on success false otherwise
62
+ def delete_zen_property(propname)
63
+ method = "deleteZenProperty?propname=#{propname}"
64
+ custom_rest(method) == "None\n"
65
+ end
66
+
67
+ def cust_property_ids
68
+ plist_to_array( rest('custPropertyIds') )
69
+ end
70
+
71
+ def get_cust_property(id)
72
+ custom_rest("getProperty?id=#{id}").chomp
73
+ end
74
+
75
+ def set_cust_property(id, value)
76
+ set_zen_property(id, value)
77
+ end
78
+
79
+ def delete_cust_property(id)
80
+ delete_zen_property(id)
81
+ end
82
+
83
+ end # ZenPropertyManager
84
+ end # Model
85
+ end # Zenoss
data/lib/zenoss/model.rb CHANGED
@@ -48,6 +48,9 @@ end # Zenoss
48
48
  require 'zenoss/model/event_view'
49
49
  require 'zenoss/model/rrd_view'
50
50
 
51
+ # Methods related to zProperties
52
+ require 'zenoss/model/zen_property_manager'
53
+
51
54
  # Device Loader interface. You can use it directly or use the
52
55
  # utility methods in DeviceClass to create devices beneath
53
56
  # that class
@@ -57,7 +57,7 @@ module Zenoss
57
57
  end
58
58
  meth << "&filterFunc=#{callback_func}" unless callback_func.nil?
59
59
  meth << "&filterAttr=#{callback_attr}" unless callback_attr.nil?
60
- meth = "#{URI.encode(dev_path)}/#{meth}"
60
+ meth = "#{URI.encode(dev_path)}/#{URI.encode(meth)}"
61
61
  puts "METHOD: #{meth}" if $DEBUG
62
62
  rest(meth)
63
63
  end
data/lib/zenoss.rb CHANGED
@@ -32,8 +32,8 @@ module Zenoss
32
32
 
33
33
  # initialize a connection to a Zenoss server. This is the same as doing
34
34
  # Zenoss::Connection.new(server,user,pass)
35
- def Zenoss.connect(server, user, pass)
36
- Connection.new(server,user,pass)
35
+ def Zenoss.connect(server, user, pass, &block)
36
+ Connection.new(server,user,pass,&block)
37
37
  end
38
38
 
39
39
  # Some of the REST methods return Strings that are formated like a Python list.
@@ -84,7 +84,7 @@ module Zenoss
84
84
  while( list[0] =~ /\d/ )
85
85
  token << list.shift
86
86
  end
87
- narray << token.to_i
87
+ narray << token.to_i
88
88
  end
89
89
  end
90
90
 
@@ -122,8 +122,20 @@ module Zenoss
122
122
  # @param [Array] tuple_array an Array of Strings formatted like two-element Python tuples
123
123
  # @return [Hash] a Ruby hash of key-value pairs taken from the tuple argument
124
124
  def ptuples_to_hash(tuple_array)
125
+ return nil if tuple_array.empty?
126
+ thash = {}
125
127
  tuple_array.each do |tuple|
128
+ str = sanitize_str(tuple.strip)
129
+ k, *v = str.slice!(1..-2).split(/\s*,\s*/)
130
+ if(v.length <= 1)
131
+ thash[k] = v.first
132
+ else
133
+ v[0] = v[0].slice(1..-1)
134
+ v[-1] = v[-1].slice(0..-2)
135
+ thash[k] = v
136
+ end
126
137
  end
138
+ thash
127
139
  end
128
140
 
129
141
  # Do some clean-up on the string returned from REST calls. Removes some
@@ -132,7 +144,9 @@ module Zenoss
132
144
  # @param [String] str string returned from REST call
133
145
  # @return [String] sanitized string
134
146
  def sanitize_str(str)
135
- str.gsub(/['"]/,'')
147
+ str.gsub!(/['"]/,'')
148
+ str.chomp!
149
+ str
136
150
  end
137
151
 
138
152
  end # Zenoss
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: zenoss_client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.1
5
+ version: 0.5.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dan Wanek
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-31 00:00:00 -05:00
13
+ date: 2011-07-08 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -57,6 +57,7 @@ extra_rdoc_files:
57
57
  - COPYING.txt
58
58
  files:
59
59
  - COPYING.txt
60
+ - Gemfile
60
61
  - README.textile
61
62
  - Rakefile
62
63
  - VERSION
@@ -91,6 +92,7 @@ files:
91
92
  - lib/zenoss/model/systems.rb
92
93
  - lib/zenoss/model/systems/system.rb
93
94
  - lib/zenoss/model/z_device_loader.rb
95
+ - lib/zenoss/model/zen_property_manager.rb
94
96
  - lib/zenoss/model/zenpack/zenpack_manager.rb
95
97
  - lib/zenoss/restapi.rb
96
98
  - lib/zenoss_client.rb