samanage 1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +3 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +83 -0
- data/Guardfile +23 -0
- data/LICENSE +674 -0
- data/README.md +1 -0
- data/lib/.DS_Store +0 -0
- data/lib/samanage.rb +15 -0
- data/lib/samanage/.DS_Store +0 -0
- data/lib/samanage/api.rb +83 -0
- data/lib/samanage/api/custom_fields.rb +20 -0
- data/lib/samanage/api/custom_forms.rb +41 -0
- data/lib/samanage/api/hardwares.rb +47 -0
- data/lib/samanage/api/incidents.rb +39 -0
- data/lib/samanage/api/other_assets.rb +40 -0
- data/lib/samanage/api/requester.rb +9 -0
- data/lib/samanage/api/users.rb +48 -0
- data/lib/samanage/error.rb +21 -0
- data/lib/samanage/url_builder.rb +49 -0
- data/lib/test.rb +4 -0
- data/samanage.gemspec +13 -0
- data/spec/api/samanage_custom_field_spec.rb +16 -0
- data/spec/api/samanage_custom_form_spec.rb +28 -0
- data/spec/api/samanage_hardware_spec.rb +76 -0
- data/spec/api/samanage_incident_spec.rb +79 -0
- data/spec/api/samanage_other_asset_spec.rb +84 -0
- data/spec/api/samanage_user_spec.rb +74 -0
- data/spec/samanage_api_spec.rb +30 -0
- data/spec/samanage_url_builder_spec.rb +18 -0
- data/spec/spec_helper.rb +9 -0
- data/test.rb +4 -0
- metadata +75 -0
data/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# samanage-ruby
|
data/lib/.DS_Store
ADDED
|
Binary file
|
data/lib/samanage.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'http'
|
|
2
|
+
|
|
3
|
+
require_relative 'samanage/api'
|
|
4
|
+
require_relative 'samanage/api/users'
|
|
5
|
+
require_relative 'samanage/api/requester'
|
|
6
|
+
require_relative 'samanage/api/hardwares'
|
|
7
|
+
require_relative 'samanage/api/other_assets'
|
|
8
|
+
require_relative 'samanage/api/incidents'
|
|
9
|
+
require_relative 'samanage/api/custom_fields'
|
|
10
|
+
require_relative 'samanage/api/custom_forms'
|
|
11
|
+
require_relative 'samanage/error'
|
|
12
|
+
require_relative 'samanage/url_builder'
|
|
13
|
+
module Samanage
|
|
14
|
+
|
|
15
|
+
end
|
|
Binary file
|
data/lib/samanage/api.rb
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Api
|
|
3
|
+
PATHS = {
|
|
4
|
+
hardware: 'hardwares.json',
|
|
5
|
+
user: 'users.json',
|
|
6
|
+
incident: 'incidents.json',
|
|
7
|
+
other_asset: 'other_assets.json',
|
|
8
|
+
custom_fields: 'custom_fields.json',
|
|
9
|
+
custom_forms: 'custom_forms.json',
|
|
10
|
+
}
|
|
11
|
+
attr_accessor :datacenter, :content_type, :base_url, :token, :custom_forms
|
|
12
|
+
def initialize(token: , dacenter: '', development_mode: false)
|
|
13
|
+
self.token = token
|
|
14
|
+
self.datacenter = nil || datacenter
|
|
15
|
+
self.base_url = "https://api#{datacenter}.samanage.com/"
|
|
16
|
+
# self.content_type = content_type || 'json'
|
|
17
|
+
self.content_type = 'json'
|
|
18
|
+
self.authorize
|
|
19
|
+
if development_mode
|
|
20
|
+
self.custom_forms = self.organize_forms
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def authorize
|
|
25
|
+
self.execute(path: 'api.json')
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Defaults to GET
|
|
29
|
+
def execute(http_method: 'get', path: , payload: nil, verbose: nil)
|
|
30
|
+
unless verbose.nil?
|
|
31
|
+
verbose = '?layout=long'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
api_call = HTTP.headers(
|
|
35
|
+
'Accept' => "application/vnd.samanage.v2.0+#{self.content_type}#{verbose}",
|
|
36
|
+
'Content-type' => "application/#{self.content_type}",
|
|
37
|
+
'X-Samanage-Authorization' => 'Bearer ' + self.token
|
|
38
|
+
)
|
|
39
|
+
api_call = api_call.public_send(http_method.to_sym, self.base_url + Addressable::URI.encode_component(path, Addressable::URI::CharacterClasses::QUERY).gsub('+',"%2B"), :body => payload)
|
|
40
|
+
response = Hash.new
|
|
41
|
+
response[:code] = api_call.code.to_i
|
|
42
|
+
response[:json] = api_call.body
|
|
43
|
+
response[:response] = api_call
|
|
44
|
+
response[:headers] = api_call.headers
|
|
45
|
+
response[:total_pages] = api_call.headers['X-Total-Pages'].to_i
|
|
46
|
+
response[:total_pages] = 1 if response[:total_pages] == 0
|
|
47
|
+
response[:total_count] = api_call.headers['X-Total-Count'].to_i
|
|
48
|
+
response[:total_count] = 1 if response[:total_count] == 0
|
|
49
|
+
|
|
50
|
+
# puts "Body Class: #{api_call.body.class}"
|
|
51
|
+
# puts "#{api_call.body}"
|
|
52
|
+
# Raise error if not Authentication or 200,201 == Success,Okay
|
|
53
|
+
# Error cases
|
|
54
|
+
case response[:code]
|
|
55
|
+
when 200..201
|
|
56
|
+
response[:data] = JSON.parse(api_call.body)
|
|
57
|
+
response
|
|
58
|
+
when 401
|
|
59
|
+
response[:data] = api_call.body
|
|
60
|
+
error = response[:response]
|
|
61
|
+
puts "Returned 401: #{error}"
|
|
62
|
+
raise Samanage::AuthorizationError.new(error: error,response: response)
|
|
63
|
+
when 404
|
|
64
|
+
response[:data] = api_call.body
|
|
65
|
+
error = response[:response]
|
|
66
|
+
puts "Returned 404: #{error}"
|
|
67
|
+
raise Samanage::NotFound.new(error: error, response: response)
|
|
68
|
+
when 422
|
|
69
|
+
response[:data] = api_call.body
|
|
70
|
+
error = response[:response]
|
|
71
|
+
puts "Returned 422: #{error}"
|
|
72
|
+
raise Samanage::InvalidRequest.new(error: error, response: response)
|
|
73
|
+
else
|
|
74
|
+
response[:data] = api_call.body
|
|
75
|
+
error = response[:response]
|
|
76
|
+
puts "Returned #{response[:code]}: #{error}"
|
|
77
|
+
raise Samanage::InvalidRequest.new(error: error, response: response)
|
|
78
|
+
end
|
|
79
|
+
# Always return response hash
|
|
80
|
+
response
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Api
|
|
3
|
+
def get_custom_fields(path: PATHS[:custom_fields], options:{})
|
|
4
|
+
url = Samanage::UrlBuilder.new(path: path, options: options).url
|
|
5
|
+
api_call = self.execute(path: url)
|
|
6
|
+
api_call
|
|
7
|
+
end
|
|
8
|
+
def collect_custom_fields
|
|
9
|
+
page = 1
|
|
10
|
+
custom_fields = Array.new
|
|
11
|
+
total_pages = self.get_custom_fields[:total_pages] ||= 2
|
|
12
|
+
while page <= total_pages
|
|
13
|
+
api_call = self.execute(path: "custom_fields.json")
|
|
14
|
+
custom_fields += api_call[:data]
|
|
15
|
+
page += 1
|
|
16
|
+
end
|
|
17
|
+
custom_fields
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Api
|
|
3
|
+
def get_custom_forms(path: PATHS[:custom_forms], options: {})
|
|
4
|
+
url = Samanage::UrlBuilder.new(path: path, options: options).url
|
|
5
|
+
api_call = self.execute(path: url)
|
|
6
|
+
api_call
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def collect_custom_forms
|
|
10
|
+
page = 1
|
|
11
|
+
custom_forms = Array.new
|
|
12
|
+
total_pages = self.get_custom_forms[:total_pages]
|
|
13
|
+
# puts api_call
|
|
14
|
+
while page <= total_pages
|
|
15
|
+
api_call = self.execute(http_method: 'get', path: "custom_forms.json?page=#{page}")
|
|
16
|
+
custom_forms += api_call[:data]
|
|
17
|
+
page += 1
|
|
18
|
+
end
|
|
19
|
+
custom_forms
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def organize_forms
|
|
23
|
+
custom_forms = self.collect_custom_forms
|
|
24
|
+
custom_forms.map{|form| form.delete_if{|k, v| v.nil?}}
|
|
25
|
+
custom_forms.map{|form| form['custom_form_fields'].map{|fields| fields.delete_if{|k, v| v == false}}}
|
|
26
|
+
custom_forms.map{|form| form['custom_form_fields'].map{|fields| fields['custom_field'].delete_if{|k, v| !v}}}
|
|
27
|
+
custom_forms.group_by{|k|
|
|
28
|
+
k['module']}.each_pair{|forms_name, forms|
|
|
29
|
+
forms.each{|form|
|
|
30
|
+
form['custom_form_fields'].group_by{|f| f['name'] }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
def form_for(object_type: )
|
|
35
|
+
if self.custom_forms == nil
|
|
36
|
+
self.custom_forms = self.organize_forms
|
|
37
|
+
end
|
|
38
|
+
self.custom_forms[object_type]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Api
|
|
3
|
+
def get_hardwares(path: PATHS[:hardware], options: {})
|
|
4
|
+
url = Samanage::UrlBuilder.new(path: path, options: options).url
|
|
5
|
+
api_call = self.execute(path: url)
|
|
6
|
+
api_call
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def collect_hardwares
|
|
10
|
+
page = 1
|
|
11
|
+
hardwares = Array.new
|
|
12
|
+
total_pages = self.get_hardwares[:total_pages]
|
|
13
|
+
# puts api_call
|
|
14
|
+
while page <= total_pages
|
|
15
|
+
api_call = self.execute(http_method: 'get', path: "hardwares.json?page=#{page}")
|
|
16
|
+
hardwares += api_call[:data]
|
|
17
|
+
page += 1
|
|
18
|
+
end
|
|
19
|
+
hardwares
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def create_hardware(payload: , options: {})
|
|
23
|
+
api_call = self.execute(path: PATHS[:hardware], http_method: 'post', payload: payload)
|
|
24
|
+
api_call
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def find_hardware(id: )
|
|
28
|
+
path = "hardwares/#{id}.json"
|
|
29
|
+
api_call = self.execute(path: path)
|
|
30
|
+
api_call
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def check_hardware(options: {})
|
|
34
|
+
url = Samanage::UrlBuilder.new(path: PATHS[:hardware], options: options).url
|
|
35
|
+
puts "Url: #{url}"
|
|
36
|
+
api_call = self.execute(path: url)
|
|
37
|
+
api_call
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def update_hardware(payload:, id:, options: {})
|
|
42
|
+
path = "hardwares/#{id}.json"
|
|
43
|
+
api_call = self.execute(path: path, http_method: 'put', payload: payload)
|
|
44
|
+
api_call
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Api
|
|
3
|
+
def get_incidents(path: PATHS[:incident], options: {})
|
|
4
|
+
url = Samanage::UrlBuilder.new(path: path, options: options).url
|
|
5
|
+
api_call = self.execute(path: url)
|
|
6
|
+
api_call
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def collect_incidents
|
|
10
|
+
page = 1
|
|
11
|
+
incidents = Array.new
|
|
12
|
+
total_pages = self.get_incidents[:total_pages]
|
|
13
|
+
# puts api_call
|
|
14
|
+
while page <= total_pages
|
|
15
|
+
api_call = self.execute(http_method: 'get', path: "incidents.json?page=#{page}")
|
|
16
|
+
incidents += api_call[:data]
|
|
17
|
+
page += 1
|
|
18
|
+
end
|
|
19
|
+
incidents
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def create_incident(payload: , options: {})
|
|
23
|
+
api_call = self.execute(path: PATHS[:incident], http_method: 'post', payload: payload)
|
|
24
|
+
api_call
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def find_incident(id: )
|
|
28
|
+
path = "incidents/#{id}.json"
|
|
29
|
+
api_call = self.execute(path: path)
|
|
30
|
+
api_call
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def update_incident(payload:, id:, options: {})
|
|
34
|
+
path = "incidents/#{id}.json"
|
|
35
|
+
api_call = self.execute(path: path, http_method: 'put', payload: payload)
|
|
36
|
+
api_call
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Api
|
|
3
|
+
def get_other_assets(path: PATHS[:other_asset], options: {})
|
|
4
|
+
url = Samanage::UrlBuilder.new(path: path, options: options).url
|
|
5
|
+
api_call = self.execute(path: url)
|
|
6
|
+
api_call
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def collect_other_assets
|
|
10
|
+
page = 1
|
|
11
|
+
other_assets = Array.new
|
|
12
|
+
total_pages = self.get_other_assets[:total_pages]
|
|
13
|
+
# puts api_call
|
|
14
|
+
other_assets = []
|
|
15
|
+
while page <= total_pages
|
|
16
|
+
api_call = self.execute(http_method: 'get', path: "other_assets.json?page=#{page}")
|
|
17
|
+
other_assets += api_call[:data]
|
|
18
|
+
page += 1
|
|
19
|
+
end
|
|
20
|
+
other_assets.uniq
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def create_other_asset(payload: , options: {})
|
|
24
|
+
api_call = self.execute(path: PATHS[:other_asset], http_method: 'post', payload: payload)
|
|
25
|
+
api_call
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def find_other_asset(id: )
|
|
29
|
+
path = "other_assets/#{id}.json"
|
|
30
|
+
api_call = self.execute(path: path)
|
|
31
|
+
api_call
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def update_other_asset(payload:, id:, options: {})
|
|
35
|
+
path = "other_assets/#{id}.json"
|
|
36
|
+
api_call = self.execute(path: path, http_method: 'put', payload: payload)
|
|
37
|
+
api_call
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Api
|
|
3
|
+
# Returns all users in the account
|
|
4
|
+
|
|
5
|
+
def get_users(path: PATHS[:user], options: {})
|
|
6
|
+
url = Samanage::UrlBuilder.new(path: path, options: options).url
|
|
7
|
+
api_call = self.execute(path: url)
|
|
8
|
+
api_call
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def collect_users
|
|
12
|
+
page = 1
|
|
13
|
+
users = Array.new
|
|
14
|
+
total_pages = self.get_users[:total_pages]
|
|
15
|
+
# puts api_call
|
|
16
|
+
while page <= total_pages
|
|
17
|
+
api_call = self.execute(http_method: 'get', path: "users.json?page=#{page}")
|
|
18
|
+
users += api_call[:data]
|
|
19
|
+
page += 1
|
|
20
|
+
end
|
|
21
|
+
users
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def create_user(payload: , options: {})
|
|
25
|
+
api_call = self.execute(path: PATHS[:user], http_method: 'post', payload: payload)
|
|
26
|
+
api_call
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def find_user(id: )
|
|
30
|
+
path = "users/#{id}.json"
|
|
31
|
+
api_call = self.execute(path: path)
|
|
32
|
+
api_call
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def check_user(field: 'email', value: )
|
|
36
|
+
url = "users.json?#{field}=#{value}"
|
|
37
|
+
api_call = self.execute(path: url)
|
|
38
|
+
api_call
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def update_user(payload:, id:)
|
|
42
|
+
path = "users/#{id}.json"
|
|
43
|
+
puts "Path: #{path}"
|
|
44
|
+
api_call = self.execute(path: path, http_method: 'put', payload: payload)
|
|
45
|
+
api_call
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class Error < StandardError
|
|
3
|
+
attr_accessor :status_code, :response, :error
|
|
4
|
+
def initialize(error: , response: {})
|
|
5
|
+
self.status_code = response[:code]
|
|
6
|
+
self.response = response[:data] ||= response[:response]
|
|
7
|
+
self.error = error
|
|
8
|
+
puts "Response: #{self.status_code}: #{self.response}"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class AuthorizationError < Error
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class InvalidRequest < Error
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class NotFound < Error
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module Samanage
|
|
2
|
+
class UrlBuilder
|
|
3
|
+
attr_accessor :url
|
|
4
|
+
@url = ''
|
|
5
|
+
def initialize(path: ,options: nil)
|
|
6
|
+
self.url = map_path(path, options)
|
|
7
|
+
return url
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def map_path(path, options)
|
|
12
|
+
url = String.new
|
|
13
|
+
parameters = String.new
|
|
14
|
+
case path.to_s
|
|
15
|
+
when /user/
|
|
16
|
+
url += 'users'
|
|
17
|
+
when /hardware/
|
|
18
|
+
url += 'hardwares'
|
|
19
|
+
when /other_asset/
|
|
20
|
+
url += 'other_assets'
|
|
21
|
+
when /incident/
|
|
22
|
+
url += 'incidents'
|
|
23
|
+
when /change/
|
|
24
|
+
url += 'changes'
|
|
25
|
+
when /custom_field/
|
|
26
|
+
url += 'custom_fields'
|
|
27
|
+
when /custom_form/
|
|
28
|
+
url += 'custom_forms'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
if path.match(/(\d)+/)
|
|
32
|
+
url += "/" + path.match(/(\d)+/)[0] + ".json"
|
|
33
|
+
return url
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
options.each_pair do |field, value|
|
|
37
|
+
if field.to_s == 'id' && value.to_s.match(/(\d)+/)
|
|
38
|
+
url += "/#{value}.json"
|
|
39
|
+
# Return. Filters not valid on an id
|
|
40
|
+
return url
|
|
41
|
+
end
|
|
42
|
+
sub_param = "?#{field}=#{value}"
|
|
43
|
+
parameters += sub_param + '&'
|
|
44
|
+
end
|
|
45
|
+
url += ".json" + parameters
|
|
46
|
+
return url
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/test.rb
ADDED
data/samanage.gemspec
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = 'samanage'
|
|
3
|
+
s.version = '1.4'
|
|
4
|
+
s.date = '2017-01-01'
|
|
5
|
+
s.summary = "Samanage Ruby Gem"
|
|
6
|
+
s.description = "Connect to Samanage using Ruby!"
|
|
7
|
+
s.authors = ["Chris Walls"]
|
|
8
|
+
s.email = 'chris.walls@samanage.com'
|
|
9
|
+
s.files = `git ls-files`.split("\n")
|
|
10
|
+
s.require_paths = ["lib"]
|
|
11
|
+
s.homepage = 'http://rubygems.org/gems/samanage'
|
|
12
|
+
s.license = 'MIT'
|
|
13
|
+
end
|