idoit.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Idoit
  3. =begin
  4. get list ob types
  5. result = Idoit.verify(api_token, endpoint, client_id)
  6. returns
  7. array with cmdb.object_types or an exeption if no data was able to retrive
  8. =end
  9. def self.verify(api_token, endpoint, _client_id = nil, verify_ssl: false)
  10. raise __('Invalid i-doit configuration (missing endpoint or api_token).') if api_token.blank? || endpoint.blank?
  11. params = {
  12. apikey: api_token,
  13. }
  14. _query('cmdb.object_types', params, _url_cleanup(endpoint), verify_ssl: verify_ssl)
  15. end
  16. =begin
  17. get list ob types
  18. result = Idoit.query(method, filter)
  19. result = Idoit.query(method, { type: '59' })
  20. returns
  21. result = [
  22. {
  23. "id": "1",
  24. "title": "System service",
  25. "container": "0",
  26. "const": "C__OBJTYPE__SERVICE",
  27. "color": "987384",
  28. "image": "https://demo.example.com/i-doit/images/objecttypes/service.jpg",
  29. "icon": "images/icons/silk/application_osx_terminal.png",
  30. "cats": "4",
  31. "tree_group": "1",
  32. "status": "2",
  33. "type_group": "1",
  34. "type_group_title": "Software"
  35. },
  36. {
  37. "id": "2",
  38. "title": "Application",
  39. "container": "0",
  40. "const": "C__OBJTYPE__APPLICATION",
  41. "color": "E4B9D7",
  42. "image": "https://demo.example.com/i-doit/images/objecttypes/application.jpg",
  43. "icon": "images/icons/silk/application_xp.png",
  44. "cats": "20",
  45. "tree_group": "1",
  46. "status": "2",
  47. "type_group": "1",
  48. "type_group_title": "Software"
  49. },
  50. ]
  51. or with filter:
  52. "result": [
  53. {
  54. "id": "26",
  55. "title": "demo.example.com",
  56. "sysid": "SYSID_1485512390",
  57. "type": "59",
  58. "created": "2017-01-27 11:19:24",
  59. "updated": "2017-01-27 11:19:49",
  60. "type_title": "Virtual server",
  61. "type_group_title": "Infrastructure",
  62. "status": "2",
  63. "cmdb_status": "6",
  64. "cmdb_status_title": "in operation",
  65. "image": "https://demo.example.com/i-doit/images/objecttypes/empty.png"
  66. },
  67. ],
  68. =end
  69. def self.query(method, filter = {})
  70. setting = Setting.get('idoit_config')
  71. raise __("The required field 'api_token' is missing from the config.") if setting[:api_token].blank?
  72. raise __("The required field 'endpoint' is missing from the config.") if setting[:endpoint].blank?
  73. params = {
  74. apikey: setting[:api_token],
  75. }
  76. if filter.present?
  77. params[:filter] = filter
  78. end
  79. _query(method, params, _url_cleanup(setting[:endpoint]), verify_ssl: setting[:verify_ssl])
  80. end
  81. def self._query(method, params, url, verify_ssl: false)
  82. result = UserAgent.post(
  83. url,
  84. {
  85. method: method,
  86. params: params,
  87. version: '2.0',
  88. # the id attribute is required by the JSON-RPC standard
  89. # but i-doit doesn't actually use it so we send a hard coded id
  90. # see issue #2412 and community topic for further information
  91. id: 42,
  92. },
  93. {
  94. verify_ssl: verify_ssl,
  95. json: true,
  96. open_timeout: 6,
  97. read_timeout: 16,
  98. log: {
  99. facility: 'idoit',
  100. },
  101. },
  102. )
  103. raise "Can't fetch objects from #{url}: Unable to parse response from server. Invalid JSON response." if !result.success? && result.error =~ %r{JSON::ParserError:.+?\s+unexpected\s+token\s+at\s+'<!DOCTYPE\s+html}i
  104. raise "Can't fetch objects from #{url}: #{result.error}" if !result.success?
  105. # add link to idoit
  106. if result.data['result'].instance_of?(Array)
  107. result.data['result'].each do |item|
  108. next if !item['id']
  109. item['link'] = "#{_url_cleanup_baseurl(url)}/?objID=#{item['id']}"
  110. item['link'].gsub!(%r{([^:])//+}, '\\1/')
  111. end
  112. end
  113. result.data
  114. end
  115. def self._url_cleanup(url)
  116. url.strip!
  117. raise "Invalid endpoint '#{url}', need to start with http:// or https://" if !url.match?(%r{^http(s|)://}i)
  118. url = _url_cleanup_baseurl(url)
  119. url = "#{url}/src/jsonrpc.php"
  120. url.gsub(%r{([^:])//+}, '\\1/')
  121. end
  122. def self._url_cleanup_baseurl(url)
  123. url.strip!
  124. raise "Invalid endpoint '#{url}', need to start with http:// or https://" if !url.match?(%r{^http(s|)://}i)
  125. url.gsub!(%r{src/jsonrpc.php}, '')
  126. url.gsub(%r{([^:])//+}, '\\1/')
  127. end
  128. end