-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |