Skip to content

Commit

Permalink
Feature - Valuemap (#77)
Browse files Browse the repository at this point in the history
* add valuemaps support
  • Loading branch information
julienlevasseur authored and aladmit committed Aug 29, 2017
1 parent 4f6c9f9 commit b4cf6ff
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bundler_args: --without development --retry=3 --jobs=3
before_install:
- gem update --system
- gem update bundler
- gem install rspec

before_script:
- sudo sed -i "s,;date.timezone =,date.timezone = 'US/Eastern'," /etc/php5/apache2/php.ini
Expand Down
24 changes: 24 additions & 0 deletions examples/Valuemaps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Valuemaps

This example assumes you have already initialized and connected the ZabbixApi.

For more information and available properties please refer to the Zabbix API documentation for Users:
[https://www.zabbix.com/documentation/3.2/manual/api/reference/valuemap](https://www.zabbix.com/documentation/3.2/manual/api/reference/valuemap)

## Create Valuemap
```ruby
zbx.valuemaps.create_or_update(
:name => "Test valuemap",
"mappings" => [
"newvalue" => "newvalue",
"value" => "value"
]
)
```

## Delete Valuemap
```ruby
zbx_client.valuemaps.delete(
zbx.valuemaps.get_id(:name => "Test valuemap")
)
```
6 changes: 6 additions & 0 deletions lib/zabbixapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require 'zabbixapi/classes/usergroups'
require 'zabbixapi/classes/usermacros'
require 'zabbixapi/classes/users'
require 'zabbixapi/classes/valuemaps'

class ZabbixApi
# @return [ZabbixApi::Client]
Expand Down Expand Up @@ -155,4 +156,9 @@ def usermacros
def users
@users ||= Users.new(@client)
end

# @return [ZabbixApi::ValueMaps]
def valuemaps
@valuemaps ||= ValueMaps.new(@client)
end
end
50 changes: 50 additions & 0 deletions lib/zabbixapi/classes/valuemaps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class ZabbixApi
class ValueMaps < Basic
# The method name used for interacting with ValueMaps via Zabbix API
#
# @return [String]
def method_name
'valuemap'
end

# The key field name used for ValueMap objects via Zabbix API
#
# @return [String]
def key
'valuemapid'
end

# The id field name used for identifying specific ValueMap objects via Zabbix API
#
# @return [String]
def indentify
'name'
end

# Get or Create ValueMap object using Zabbix API
#
# @param data [Hash] Needs to include valuemapids [List] to properly identify ValueMaps via Zabbix API
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
# @return [Integer] Zabbix object id
def get_or_create(data)
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"

unless (id = get_id(:valuemapids => data[:valuemapids]))
id = create(data)
end
id
end

# Create or update Item object using Zabbix API
#
# @param data [Hash] Needs to include valuemapids to properly identify ValueMaps via Zabbix API
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
# @return [Integer] Zabbix object id
def create_or_update(data)
valuemapid = get_id(:name => data[:name])
valuemapid ? update(data.merge(:valuemapids => [:valuemapid])) : create(data)
end
end
end
54 changes: 54 additions & 0 deletions spec/valuemap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# encoding: utf-8

require 'spec_helper'

describe 'valuemap' do
before :all do
@valuemap = gen_name 'valuemap'
end

context 'when not exists' do
describe 'create' do
it 'should return an integer id' do
valuemapid = zbx.valuemaps.create_or_update(
:name => @valuemap,
:mappings => [
'newvalue' => 'test',
'value' => 'test'
]
)
expect(valuemapid).to be_kind_of(Integer)
end
end
end

context 'when exists' do
before do
@valuemapid = zbx.valuemaps.create_or_update(
:name => @valuemap,
:mappings => [
'newvalue' => 'test',
'value' => 'test'
]
)
end

describe 'create_or_update' do
it 'should return id' do
expect(
zbx.valuemaps.create_or_update(
:name => @valuemap,
:mappings => [
'newvalue' => 'test',
'value' => 'test'
]
)
).to eq @valuemapid
end

it 'should return id' do
expect(@valuemapid).to eq @valuemapid
end
end
end
end

0 comments on commit b4cf6ff

Please sign in to comment.