Class: ZenossAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zenoss_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password, verify_ssl = true) ⇒ Client

Returns a new instance of Client.



71
72
73
74
75
76
77
78
79
80
# File 'lib/zenoss_api.rb', line 71

def initialize(url, username, password, verify_ssl = true)
  @url = url
  @auth = {:username => username, :password => password}
  @rec_count = 1
  @headers = {
    'Content-Type' => 'application/json', 
    'Accept' => 'application/json',
  }
  @verify = verify_ssl
end

Instance Method Details

#_post(url, payload) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zenoss_api.rb', line 104

def _post(url, payload) 
 #### TODO wrap in rescue and handle exceptions
 response = HTTParty.post(url,
   {
     :body => payload,
     :basic_auth => @auth,
     :headers => @headers,
     :timeout => 300,
     :verify => @verify,
   }) 
 if response.code == 200
   response 
 else 
   response
 end
end

#_returnDevicePropertyObject



148
149
# File 'lib/zenoss_api.rb', line 148

def _returnDeviceProperty()
end

#_validateUid(u, type) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/zenoss_api.rb', line 159

def _validateUid(u, type)
  if u.match(/^\/zport\/dmd\/.*$/)
    u
  else 
    response = self.getDeviceProps(u, [ "uid" ])
    if not response.nil?
      response['result'][type][0]['uid']
    else 
      nil
    end
  end
end

#getDeviceProps(device, props = []) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/zenoss_api.rb', line 172

def getDeviceProps(device, props=[])
  response = self.router("DeviceRouter", "getDevices", { 'keys' => props, 'params' => { 'name' => device } })
  if response['result']['success'] == true and response['result']['totalCount'] == 1
    response
  else
    nil
end
end

#paginated_get(url, payload, xoffset = 0) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zenoss_api.rb', line 82

def paginated_get(url, payload, xoffset=0)
  Enumerator.new do |y|
    total = 0
    offset = 0
    limit = payload[0]['data'][0]['limit']
    loop do
      response = self._post(url, payload.to_json)
      totalCount = response['result']['totalCount']
      data = response['result']['events']
      total += data.length
      offset += limit
      payload[0]['data'][0]['start'] = offset
      
      data.each do |element|
        y.yield element
      end

      break if (data.empty? || total >= totalCount)
    end
  end
end

#router(router, method, method_params = {}, options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/zenoss_api.rb', line 122

def router(router, method, method_params={}, options={})
  if method_params.empty? 
    _method_params = []
  else 
    _method_params = [ method_params ]
  end

  raise "Error: #{router} is not a valid route." unless ROUTERS.key?(router.to_sym)

  api_call = "#{@url}/zport/dmd/#{ROUTERS[router.to_sym]}_router"
  payload = [{ 
    'action' => router,
    'method' => method,
    'data' => _method_params,
    'type' => 'rpc',
    'tid' => @rec_count
  }]

   if options[:paginate]  
     self.paginated_get(api_call, payload)
   else 
     self._post(api_call, payload.to_json) 
   end
   #@rec_count += 1
end

#setProductionState(fqdn, state = 1000) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/zenoss_api.rb', line 181

def setProductionState(fqdn, state = 1000)
  # set a single device prod state

  # Production:1000
  # Pre-Production:500
  # Test:400
  # Maintenance:300
  # Decommissioned:-1

  # lookup device uid
  node = self.getDeviceProps(fqdn, ["uid"])
  # set prod state
  if not node.nil?
    uids = [ node['result']['devices'][0]['uid'] ]
    hashcheck = node['result']['hash']  
    method_params = { 
      'uids' => uids, 
      'prodState' => state,
      'hashcheck' => hashcheck, 
    }
    self.router("DeviceRouter", "setProductionState", method_params)
  else 
    "Set production state falied"
  end
end

#validateUids(uids, type = "devices") ⇒ Object



151
152
153
154
155
156
157
# File 'lib/zenoss_api.rb', line 151

def validateUids(uids, type="devices")
  if uids.kind_of?(Array)
    uids.collect { |x| self._validateUid(x, type) }
  else 
    self._validateUid(uids, type)
  end 
end