Class: ZabbixManager::Roles

Inherits:
Basic
  • Object
show all
Defined in:
lib/zabbix_manager/classes/roles.rb

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #create_or_update, #default_options, #delete, #destroy, #get, #get_full_data, #get_id, #get_ids, #get_ids_by_identify, #get_key_ids, #get_key_ids_by_identify, #get_or_create, #get_or_create_keys, #get_raw, #hash_equals?, #initialize, #key, #keys, #log, #merge_hashes, #normalize_array, #normalize_hash, #parse_keys, #request_raw, #symbolize_keys, #update

Constructor Details

This class inherits a constructor from ZabbixManager::Basic

Instance Method Details

#add_user(data) ⇒ Object

使用 Zabbix API 将用户添加到用户组



29
30
31
# File 'lib/zabbix_manager/classes/roles.rb', line 29

def add_user(data)
  update_users(data)
end

#dump_by_id(data) ⇒ Object

从 Zabbix API 中根据指定键和值获取角色对象的数据



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/zabbix_manager/classes/roles.rb', line 34

def dump_by_id(data)
  log "[DEBUG] 调用 dump_by_id 方法,参数: #{data.inspect}"

  get_raw(
    {
      output:      "extend",
      selectRules: "extend",
      roleids:     data[:id]
    }
  )
end

#get_ids_by_name(data) ⇒ Object

通过角色名称从 Zabbix API 获取角色 id



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zabbix_manager/classes/roles.rb', line 47

def get_ids_by_name(data)
  result = get_raw(
    {
      filter: {
        name: data[:name]
      },
      output: "extend"
    }
  )

  result.filter_map do |rule|
    rule["roleid"]
  end
end

#identifyObject

识别特定角色对象的 id 字段名称



11
12
13
# File 'lib/zabbix_manager/classes/roles.rb', line 11

def identify
  "name"
end

#method_nameObject

与 Zabbix API 交互的角色方法名称



6
7
8
# File 'lib/zabbix_manager/classes/roles.rb', line 6

def method_name
  "role"
end

#rules(data) ⇒ Object

使用 Zabbix API 为用户组设置权限



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zabbix_manager/classes/roles.rb', line 16

def rules(data)
  rules  = data[:rules] || 2
  result = @client.api_request(
    method: "role.update",
    params: {
      roleid: data[:roleid],
      rules:  data[:hostgroupids].map { |t| { permission: permission, id: t } }
    }
  )
  result ? result["usrgrpids"][0].to_i : nil
end

#update_users(data) ⇒ Object

使用 Zabbix API 更新用户角色中的用户



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zabbix_manager/classes/roles.rb', line 63

def update_users(data)
  user_groups = data[:usrgrpids].map do |t|
    {
      usrgrpid: t,
      userids:  data[:userids]
    }
  end
  result      = @client.api_request(
    method: "usergroup.update",
    params: user_groups
  )
  result ? result["usrgrpids"][0].to_i : nil
end