Module: Icinga2::Servicegroups

Included in:
Client
Defined in:
lib/icinga2/servicegroups.rb

Overview

namespace for servicegroup handling

Instance Method Summary collapse

Instance Method Details

#add_servicegroup(params) ⇒ Hash

add a servicegroup

Examples:

add_servicegroup(service_group: 'foo', display_name: 'FOO')

Parameters:

Options Hash (params):

  • service_group (String)

    servicegroup to create

  • display_name (String)

    the displayed name

  • notes (String)
  • notes_url (String)
  • action_url (String)

Returns:

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/icinga2/servicegroups.rb', line 23

def add_servicegroup( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing \'params\'') if( params.size.zero? )

  service_group = validate( params, required: true, var: 'service_group', type: String )
  display_name = validate( params, required: true, var: 'display_name', type: String )
  notes = validate( params, required: false, var: 'notes', type: String )
  notes_url = validate( params, required: false, var: 'notes_url', type: String )
  action_url = validate( params, required: false, var: 'action_url', type: String )

  payload = {
    attrs: {
      display_name: display_name,
      notes: notes,
      notes_url: notes_url,
      action_url: action_url
    }
  }

  # remove all empty attrs
  payload.reject!{ |_k, v| v.nil? }
  payload[:attrs].reject!{ |_k, v| v.nil? }

  put(
    url: format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#delete_servicegroup(params) ⇒ Array

delete a servicegroup

Examples:

delete_servicegroup('foo')

Parameters:

Options Hash (params):

  • name (String)

    servicegroup to delete

  • cascade (Bool) — default: false

    delete servicegroup also when other objects depend on it

Returns:

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/icinga2/servicegroups.rb', line 66

def delete_servicegroup( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing \'params\'') if( params.size.zero? )

  name = validate( params, required: true, var: 'name', type: String )
  cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false

  return { 'code' => 404, 'status' => 'Object not Found' } if( exists_servicegroup?( name ) == false )

  url = format( '%s/objects/servicegroups/%s%s', @icinga_api_url_base, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )

  delete(
    url: url,
    headers: @headers,
    options: @options
  )
end

#exists_servicegroup?(service_group) ⇒ Bool

checks if the servicegroup exists

Examples:

exists_servicegroup?('disk')

Parameters:

  • service_group (String)

    the name of the servicegroup

Returns:

  • (Bool)

    returns true if the servicegroup exists

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/icinga2/servicegroups.rb', line 120

def exists_servicegroup?( service_group )

  raise ArgumentError.new(format('wrong type. \'service_group\' must be an String, given \'%s\'', service_group.class.to_s)) unless( service_group.is_a?(String) )
  raise ArgumentError.new('Missing \'service_group\'') if( service_group.size.zero? )

  result = servicegroups(service_group)
  result = JSON.parse( result ) if  result.is_a?( String )
  result = result.first if( result.is_a?(Array) )

  return false if( result.is_a?(Hash) && result.dig('code') == 404 )

  true
end

#servicegroups(service_group = nil) ⇒ Array

returns all servicegroups

Examples:

to get all users

servicegroups

to get one user

servicegroups(service_group: 'disk')

Parameters:

  • service_group (String) (defaults to: nil)

    (nil) optional for a single servicegroup

Returns:

  • (Array)

    returns a hash with all servicegroups

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/icinga2/servicegroups.rb', line 97

def servicegroups( service_group = nil )

  raise ArgumentError.new(format('wrong type. \'service_group\' must be an String, given \'%s\'', service_group.class.to_s)) unless( service_group.nil? || service_group.is_a?(String) )

  url = format( '%s/objects/servicegroups'   , @icinga_api_url_base )
  url = format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group ) unless( service_group.nil? )

  api_data(
    url: url,
    headers: @headers,
    options: @options
  )
end