idoit.rb 4.2 KB

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