2021-06-01 12:20:20 +00:00
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
2017-09-06 06:39:02 +00:00
class Idoit
= begin
get list ob types
result = Idoit . verify ( api_token , endpoint , client_id )
returns
array with cmdb . object_types or an exeption if no data was able to retrive
= end
def self . verify ( api_token , endpoint , _client_id = nil )
raise 'api_token required' if api_token . blank?
raise 'endpoint required' if endpoint . blank?
params = {
apikey : api_token ,
}
_query ( 'cmdb.object_types' , params , _url_cleanup ( endpoint ) )
end
= begin
get list ob types
result = Idoit . query ( method , filter )
result = Idoit . query ( method , { type : '59' } )
returns
result = [
{
" id " : " 1 " ,
" title " : " System service " ,
" container " : " 0 " ,
" const " : " C__OBJTYPE__SERVICE " ,
" color " : " 987384 " ,
2017-09-25 20:45:21 +00:00
" image " : " https://demo.example.com/i-doit/images/objecttypes/service.jpg " ,
2017-09-06 06:39:02 +00:00
" icon " : " images/icons/silk/application_osx_terminal.png " ,
" cats " : " 4 " ,
" tree_group " : " 1 " ,
" status " : " 2 " ,
" type_group " : " 1 " ,
" type_group_title " : " Software "
} ,
{
" id " : " 2 " ,
" title " : " Application " ,
" container " : " 0 " ,
" const " : " C__OBJTYPE__APPLICATION " ,
" color " : " E4B9D7 " ,
2017-09-25 20:45:21 +00:00
" image " : " https://demo.example.com/i-doit/images/objecttypes/application.jpg " ,
2017-09-06 06:39:02 +00:00
" icon " : " images/icons/silk/application_xp.png " ,
" cats " : " 20 " ,
" tree_group " : " 1 " ,
" status " : " 2 " ,
" type_group " : " 1 " ,
" type_group_title " : " Software "
} ,
]
or with filter :
" result " : [
{
" id " : " 26 " ,
2017-09-25 20:45:21 +00:00
" title " : " demo.example.com " ,
2017-09-06 06:39:02 +00:00
" sysid " : " SYSID_1485512390 " ,
" type " : " 59 " ,
" created " : " 2017-01-27 11:19:24 " ,
" updated " : " 2017-01-27 11:19:49 " ,
" type_title " : " Virtual server " ,
" type_group_title " : " Infrastructure " ,
" status " : " 2 " ,
" cmdb_status " : " 6 " ,
" cmdb_status_title " : " in operation " ,
2017-09-25 20:45:21 +00:00
" image " : " https://demo.example.com/i-doit/images/objecttypes/empty.png "
2017-09-06 06:39:02 +00:00
} ,
] ,
= end
def self . query ( method , filter = { } )
setting = Setting . get ( 'idoit_config' )
raise 'Unable for find api_token in config' if setting [ :api_token ] . blank?
raise 'Unable for find endpoint in config' if setting [ :endpoint ] . blank?
2021-07-16 13:44:10 +00:00
# translator_key = Setting.get('translator_key')
2017-09-06 06:39:02 +00:00
params = {
apikey : setting [ :api_token ] ,
}
if filter . present?
params [ :filter ] = filter
end
_query ( method , params , _url_cleanup ( setting [ :endpoint ] ) )
end
def self . _query ( method , params , url )
result = UserAgent . post (
url ,
{
2018-12-19 17:31:51 +00:00
method : method ,
params : params ,
2017-09-06 06:39:02 +00:00
version : '2.0' ,
2018-12-20 14:19:37 +00:00
# the id attribute is required by the JSON-RPC standard
# but i-doit doesn't actually use it so we send a hard coded id
# see issue #2412 and community topic for further information
id : 42 ,
2017-09-06 06:39:02 +00:00
} ,
{
2018-12-19 17:31:51 +00:00
json : true ,
2017-09-06 06:39:02 +00:00
open_timeout : 6 ,
read_timeout : 16 ,
2018-12-19 17:31:51 +00:00
log : {
2017-09-06 06:39:02 +00:00
facility : 'idoit' ,
} ,
} ,
)
2021-05-12 11:37:44 +00:00
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
2017-09-06 06:39:02 +00:00
raise " Can't fetch objects from #{ url } : #{ result . error } " if ! result . success?
# add link to idoit
2020-10-22 13:57:01 +00:00
if result . data [ 'result' ] . instance_of? ( Array )
2017-10-01 12:25:52 +00:00
result . data [ 'result' ] . each do | item |
2017-09-06 06:39:02 +00:00
next if ! item [ 'id' ]
2018-10-09 06:17:41 +00:00
2017-09-06 06:39:02 +00:00
item [ 'link' ] = " #{ _url_cleanup_baseurl ( url ) } /?objID= #{ item [ 'id' ] } "
item [ 'link' ] . gsub! ( %r{ ([^:])//+ } , '\\1/' )
2017-10-01 12:25:52 +00:00
end
2017-09-06 06:39:02 +00:00
end
result . data
end
def self . _url_cleanup ( url )
2017-10-24 20:34:52 +00:00
url . strip!
2019-09-16 09:21:10 +00:00
raise " Invalid endpoint ' #{ url } ', need to start with http:// or https:// " if ! url . match? ( %r{ ^http(s|):// }i )
2018-10-09 06:17:41 +00:00
2017-09-06 06:39:02 +00:00
url = _url_cleanup_baseurl ( url )
url = " #{ url } /src/jsonrpc.php "
url . gsub ( %r{ ([^:])//+ } , '\\1/' )
end
def self . _url_cleanup_baseurl ( url )
2017-10-24 20:34:52 +00:00
url . strip!
2019-09-16 09:21:10 +00:00
raise " Invalid endpoint ' #{ url } ', need to start with http:// or https:// " if ! url . match? ( %r{ ^http(s|):// }i )
2018-10-09 06:17:41 +00:00
2017-09-06 06:39:02 +00:00
url . gsub! ( %r{ src/jsonrpc.php } , '' )
url . gsub ( %r{ ([^:])//+ } , '\\1/' )
end
end