Class: Samanage::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/samanage/api.rb,
lib/samanage/api/sites.rb,
lib/samanage/api/users.rb,
lib/samanage/api/utils.rb,
lib/samanage/api/groups.rb,
lib/samanage/api/mobiles.rb,
lib/samanage/api/category.rb,
lib/samanage/api/comments.rb,
lib/samanage/api/contracts.rb,
lib/samanage/api/hardwares.rb,
lib/samanage/api/incidents.rb,
lib/samanage/api/requester.rb,
lib/samanage/api/departments.rb,
lib/samanage/api/custom_forms.rb,
lib/samanage/api/other_assets.rb,
lib/samanage/api/custom_fields.rb

Constant Summary collapse

MAX_RETRIES =
3
PATHS =
{
	category: 'categories.json',
	contract: 'contracts.json',
	custom_fields: 'custom_fields.json',
	custom_forms: 'custom_forms.json',
	department: 'departments.json',
	group: 'groups.json',
	hardware: 'hardwares.json',
	incident: 'incidents.json',
	mobile: 'mobiles.json',
	other_asset: 'other_assets.json',
	site: 'sites.json',
	user: 'users.json',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, datacenter: nil, development_mode: false, max_retries: MAX_RETRIES) ⇒ Api

Development mode forzes authorization & prepopulates custom forms/fields and admins datacenter should equal ‘eu’ or blank



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/samanage/api.rb', line 24

def initialize(token: , datacenter: nil, development_mode: false, max_retries: MAX_RETRIES)
	self.token = token
	if !datacenter.nil? && datacenter.to_s.downcase != 'eu'
		datacenter = nil
	end
	self.datacenter ||= datacenter.to_s.downcase
	self.base_url =  "https://api#{self.datacenter.to_s.downcase}.samanage.com/"
	self.content_type = 'json'
	self.admins = []
	self.max_retries = max_retries
	if development_mode
		if self.authorized? != true
			self.authorize
		end
		self.custom_forms = self.organize_forms
		self.admins = self.list_admins
	end
end

Instance Attribute Details

#adminsObject

Returns the value of attribute admins.



20
21
22
# File 'lib/samanage/api.rb', line 20

def admins
  @admins
end

#authorizedObject

Returns the value of attribute authorized.



20
21
22
# File 'lib/samanage/api.rb', line 20

def authorized
  @authorized
end

#base_urlObject

Returns the value of attribute base_url.



20
21
22
# File 'lib/samanage/api.rb', line 20

def base_url
  @base_url
end

#content_typeObject

Returns the value of attribute content_type.



20
21
22
# File 'lib/samanage/api.rb', line 20

def content_type
  @content_type
end

#custom_formsObject

Returns the value of attribute custom_forms.



20
21
22
# File 'lib/samanage/api.rb', line 20

def custom_forms
  @custom_forms
end

#datacenterObject

Returns the value of attribute datacenter.



20
21
22
# File 'lib/samanage/api.rb', line 20

def datacenter
  @datacenter
end

#max_retriesObject

Returns the value of attribute max_retries.



20
21
22
# File 'lib/samanage/api.rb', line 20

def max_retries
  @max_retries
end

#tokenObject

Returns the value of attribute token.



20
21
22
# File 'lib/samanage/api.rb', line 20

def token
  @token
end

Instance Method Details

#add_item_to_contract(id: nil, payload: nil) ⇒ Object



45
46
47
48
# File 'lib/samanage/api/contracts.rb', line 45

def add_item_to_contract(id: nil, payload: nil)
  path = "contracts/#{id}/items.json"
  self.execute(path: path, http_method: 'post', payload: payload)
end

#add_member_to_group(email: nil, group_id: nil, group_name: nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/samanage/api/groups.rb', line 35

def add_member_to_group(email: nil, group_id: nil, group_name: nil)
	group_id = group_id ||= self.find_group_id_by_name(group: group_name)
	user_id = self.find_user_id_by_email(email: email)
	member_path = "memberships.json?group_id=#{group_id}.json&user_ids=#{user_id}"
	self.execute(path: member_path, http_method: 'post')
end

#authorizeObject

Check token against api.json



48
49
50
51
52
53
54
55
# File 'lib/samanage/api.rb', line 48

def authorize
	self.execute(path: 'api.json')
	rescue OpenSSL::SSL::SSLError => e
		puts "Raised: #{e} #{e.class}"
		puts 'Disabling Local SSL Verification'
		self.execute(path: 'api.json', ssl_fix: true)
	self.authorized = true
end

#authorized?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/samanage/api.rb', line 43

def authorized?
	self.authorized
end

#check_contract(options: {}) ⇒ Object

Check for contract using URL builder



34
35
36
37
# File 'lib/samanage/api/contracts.rb', line 34

def check_contract(options: {})
	url = Samanage::UrlBuilder.new(path: PATHS[:contract], options: options).url
	self.execute(path: url)
end

#check_hardware(options: {}) ⇒ Object

Check for hardware using URL builder



41
42
43
44
# File 'lib/samanage/api/hardwares.rb', line 41

def check_hardware(options: {})
	url = Samanage::UrlBuilder.new(path: PATHS[:hardware], options: options).url
	self.execute(path: url)
end

#check_mobile(options: {}) ⇒ Object

Check for mobile using URL builder



34
35
36
37
# File 'lib/samanage/api/mobiles.rb', line 34

def check_mobile(options: {})
	url = Samanage::UrlBuilder.new(path: PATHS[:mobile], options: options).url
	self.execute(path: url)
end

#check_user(field: 'email', value: nil) ⇒ Object

Check for user by field (ex: users.json?field=value)



53
54
55
56
57
58
59
# File 'lib/samanage/api/users.rb', line 53

def check_user(field: 'email', value: nil)
	if field.to_s.downcase == 'email'
		value = value.gsub("+",'%2B')
	end
	url = "users.json?#{field}=#{value}"
	self.execute(path: url)
end

#collect_categoriesObject Also known as: categories

Samanage categories are not paginated

  • to break into subcategories, add



11
12
13
14
# File 'lib/samanage/api/category.rb', line 11

def collect_categories
	categories = Array.new
	categories += self.execute(http_method: 'get', path: "categories.json")[:data]
end

#collect_comments(incident_id: nil) ⇒ Object Also known as: comments

Return all comments from the incident_id



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/samanage/api/comments.rb', line 18

def collect_comments(incident_id: nil)

	page = 1
	max_pages = 5
	comments = Array.new
	while page <= max_pages
		path = "incidents/#{incident_id}/comments.json?page=#{page}"
		comments += self.execute(path: path)[:data]
		page += 1
	end
	comments
end

#collect_contracts(options: {}) ⇒ Object Also known as: contracts

Get all contracts



11
12
13
14
15
16
17
18
19
20
# File 'lib/samanage/api/contracts.rb', line 11

def collect_contracts(options: {})
	page = 1
	contracts = Array.new
	total_pages = self.get_contracts[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting contracts page: #{page}/#{total_pages}" if options[:verbose]
		contracts += self.execute(http_method: 'get', path: "contracts.json?page=#{page}")[:data]
	end
	contracts
end

#collect_custom_fields(options: {}) ⇒ Object Also known as: custom_fields

Gets all custom fields



11
12
13
14
15
16
17
18
19
20
# File 'lib/samanage/api/custom_fields.rb', line 11

def collect_custom_fields(options: {})
	page = 1
	custom_fields = Array.new
	total_pages = self.get_custom_fields[:total_pages] ||= 2
	1.upto(total_pages) do |page|
		puts "Collecting Custom Fields page: #{page}/#{total_pages}" if options[:verbose]
		custom_fields += self.execute(path: "custom_fields.json")[:data]
	end
	custom_fields
end

#collect_custom_forms(options: {}) ⇒ Object

Get all custom forms



10
11
12
13
14
15
16
17
18
19
# File 'lib/samanage/api/custom_forms.rb', line 10

def collect_custom_forms(options: {})
	page = 1
	custom_forms = Array.new
	total_pages = self.get_custom_forms[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting Custom Forms page: #{page}/#{total_pages}" if options[:verbose]
		custom_forms += self.execute(http_method: 'get', path: "custom_forms.json?page=#{page}")[:data]
	end
	custom_forms
end

#collect_departments(options: {}) ⇒ Object Also known as: departments



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/samanage/api/departments.rb', line 8

def collect_departments(options: {})
	page = 1
	departments = Array.new
	total_pages = self.get_departments[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting Groups page: #{page}/#{total_pages}" if options[:verbose]
		departments += self.execute(http_method: 'get', path: "departments.json?page=#{page}")[:data]
		page += 1
	end
	departments
end

#collect_groups(options: {}) ⇒ Object Also known as: groups



8
9
10
11
12
13
14
15
16
17
# File 'lib/samanage/api/groups.rb', line 8

def collect_groups(options: {})
	page = 1
	groups = Array.new
	total_pages = self.get_groups[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting Groups page: #{page}/#{total_pages}" if options[:verbose]
		groups += self.execute(http_method: 'get', path: "groups.json?page=#{page}")[:data]
	end
	groups
end

#collect_hardwares(options: {}) ⇒ Object Also known as: hardwares

Get all hardwares



11
12
13
14
15
16
17
18
19
20
# File 'lib/samanage/api/hardwares.rb', line 11

def collect_hardwares(options: {})
	page = 1
	hardwares = Array.new
	total_pages = self.get_hardwares[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting Hardwares page: #{page}/#{total_pages}" if options[:verbose]
		hardwares += self.execute(http_method: 'get', path: "hardwares.json?page=#{page}")[:data]
	end
	hardwares
end

#collect_incidents(options: {}) ⇒ Object Also known as: incidents

Returns all incidents. Options:

- audit_archives: true
- layout: 'long'


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/samanage/api/incidents.rb', line 15

def collect_incidents(options: {})
	incidents = Array.new
	total_pages = self.get_incidents[:total_pages]
	puts "Pulling Incidents with Audit Archives (this may take a while)" if options[:audit_archives] && options[:verbose]
	1.upto(total_pages) do |page|
		puts "Collecting Incidents page: #{page}/#{total_pages}" if options[:verbose]
		if options[:audit_archives]
			archives = 'layout=long&audit_archive=true'
			paginated_incidents = self.execute(path: "incidents.json?page=#{page}")[:data]
			paginated_incidents.map do |incident|
				archive_path = "incidents/#{incident['id']}.json?#{archives}"
				incidents << self.execute(path: archive_path)[:data]
			end
		else
			layout = options[:layout] == 'long' ? '&layout=long' : nil
			incidents += self.execute(http_method: 'get', path: "incidents.json?page=#{page}#{layout}")[:data]
		end
	end
	incidents
end

#collect_mobiles(options: {}) ⇒ Object Also known as: mobiles

Get all mobiles



11
12
13
14
15
16
17
18
19
20
# File 'lib/samanage/api/mobiles.rb', line 11

def collect_mobiles(options: {})
	page = 1
	mobiles = Array.new
	total_pages = self.get_mobiles[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting Mobiles page: #{page}/#{total_pages}" if options[:verbose]
		mobiles += self.execute(http_method: 'get', path: "mobiles.json?page=#{page}")[:data]
	end
	mobiles
end

#collect_other_assets(options: {}) ⇒ Object Also known as: other_assets

Returns all other assets



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/samanage/api/other_assets.rb', line 11

def collect_other_assets(options: {})
	page = 1
	other_assets = Array.new
	total_pages = self.get_other_assets[:total_pages]
	other_assets = []
	1.upto(total_pages) do |page|
		puts "Collecting Other Assets page: #{page}/#{total_pages}" if options[:verbose]
		other_assets += self.execute(http_method: 'get', path: "other_assets.json?page=#{page}")[:data]
	end
	other_assets.uniq
end

#collect_sites(options: {}) ⇒ Object Also known as: sites



8
9
10
11
12
13
14
15
16
17
# File 'lib/samanage/api/sites.rb', line 8

def collect_sites(options: {})
	page = 1
	sites = Array.new
	total_pages = self.get_sites[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting Sites page: #{page}/#{total_pages}" if options[:verbose]
		sites += self.execute(http_method: 'get', path: "sites.json?page=#{page}")[:data]
	end
	sites
end

#collect_users(options: {}) ⇒ Object Also known as: users

Returns all users in the account



11
12
13
14
15
16
17
18
19
20
# File 'lib/samanage/api/users.rb', line 11

def collect_users(options: {})
	page = 1
	users = Array.new
	total_pages = self.get_users[:total_pages]
	1.upto(total_pages) do |page|
		puts "Collecting Users page: #{page}/#{total_pages}" if options[:verbose]
		users += self.execute(http_method: 'get', path: "users.json?page=#{page}")[:data]
	end
	users
end

#create_category(payload: nil, options: {}) ⇒ Object



16
17
18
# File 'lib/samanage/api/category.rb', line 16

def create_category(payload: nil, options: {})
	self.execute(path: PATHS[:category], http_method: 'post', payload: payload)
end

#create_comment(incident_id: nil, comment: nil, options: {}) ⇒ Object

Add a new comment



12
13
14
15
# File 'lib/samanage/api/comments.rb', line 12

def create_comment(incident_id: nil, comment: nil, options: {})
	path = "incidents/#{incident_id}/comments.json"
	self.execute(http_method: 'post', path: path, payload: comment)
end

#create_contract(payload: nil, options: {}) ⇒ Object

Create contract given json payload



23
24
25
# File 'lib/samanage/api/contracts.rb', line 23

def create_contract(payload: nil, options: {})
	self.execute(path: PATHS[:contract], http_method: 'post', payload: payload)
end

#create_department(payload: nil, options: {}) ⇒ Object



20
21
22
# File 'lib/samanage/api/departments.rb', line 20

def create_department(payload: nil, options: {})
	self.execute(path: PATHS[:department], http_method: 'post', payload: payload)
end

#create_group(payload: nil, options: {}) ⇒ Object



19
20
21
# File 'lib/samanage/api/groups.rb', line 19

def create_group(payload: nil, options: {})
	self.execute(path: PATHS[:group], http_method: 'post', payload: payload)
end

#create_hardware(payload: nil, options: {}) ⇒ Object

Create hardware given json payload



23
24
25
# File 'lib/samanage/api/hardwares.rb', line 23

def create_hardware(payload: nil, options: {})
	self.execute(path: PATHS[:hardware], http_method: 'post', payload: payload)
end

#create_incident(payload: nil, options: {}) ⇒ Object

Create an incident given json



38
39
40
# File 'lib/samanage/api/incidents.rb', line 38

def create_incident(payload: nil, options: {})
	self.execute(path: PATHS[:incident], http_method: 'post', payload: payload)
end

#create_mobile(payload: nil, options: {}) ⇒ Object

Create mobile given json payload



23
24
25
# File 'lib/samanage/api/mobiles.rb', line 23

def create_mobile(payload: nil, options: {})
	self.execute(path: PATHS[:mobile], http_method: 'post', payload: payload)
end

#create_other_asset(payload: nil, options: {}) ⇒ Object

Create an other_asset given json



25
26
27
# File 'lib/samanage/api/other_assets.rb', line 25

def create_other_asset(payload: nil, options: {})
	self.execute(path: PATHS[:other_asset], http_method: 'post', payload: payload)
end

#create_site(payload: nil, options: {}) ⇒ Object



19
20
21
# File 'lib/samanage/api/sites.rb', line 19

def create_site(payload: nil, options: {})
	self.execute(path: PATHS[:site], http_method: 'post', payload: payload)
end

#create_user(payload: nil, options: {}) ⇒ Object

Create user given JSON



23
24
25
# File 'lib/samanage/api/users.rb', line 23

def create_user(payload: nil, options: {})
	self.execute(path: PATHS[:user], http_method: 'post', payload: payload)
end

#execute(http_method: 'get', path: nil, payload: nil, verbose: nil, ssl_fix: false) ⇒ Object

Calling execute without a method defaults to GET



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/samanage/api.rb', line 58

def execute(http_method: 'get', path: nil, payload: nil, verbose: nil, ssl_fix: false)
	if payload.class == String
		begin
		payload = JSON.parse(payload)
		rescue => e
			puts "Invalid JSON: #{payload.inspect}"
			raise Samanage::Error(error: e, response: nil)
		end
	end
	token = token ||= self.token
	unless verbose.nil?
		verbose = '?layout=long'
	end

	headers = {
		'Accept' => "application/vnd.samanage.v2.0+#{self.content_type}#{verbose}",
		'Content-type'  => "application/#{self.content_type}",
		'X-Samanage-Authorization' => 'Bearer ' + self.token
	}
	@options = {
		headers: headers,
		payload: payload
	}
	full_path = self.base_url + path
	retries = 0
	begin
		case http_method.to_s.downcase
		when 'get'
			api_call = self.class.get(full_path, headers: headers)
		when 'post'
			api_call = self.class.post(full_path, query: payload, headers: headers)
		when 'put'
			api_call = self.class.put(full_path, query: payload, headers: headers)
		when 'delete'
			api_call = self.class.delete(full_path, query: payload, headers: headers)
		else
			raise Samanage::Error.new(response: {response: 'Unknown HTTP method'})
		end
	rescue Errno::ECONNREFUSED, Net::OpenTimeout, Errno::ETIMEDOUT, OpenSSL::SSL::SSLError => e
		puts "Error: #{e} - #{e.class}"
		puts "Retry: #{retries}/#{self.max_retries}"
		sleep 3
		retries += 1
		retry if retries < self.max_retries
		error = e
		response = e.class
		raise Samanage::InvalidRequest.new(error: error, response: response)
	end

	response = Hash.new
	response[:code] = api_call.code.to_i
	response[:json] = api_call.body
	response[:response] = api_call
	response[:headers] = api_call.headers
	response[:total_pages] = api_call.headers['X-Total-Pages'].to_i
	response[:total_pages] = 1 if response[:total_pages] == 0
	response[:total_count] = api_call.headers['X-Total-Count'].to_i

	# Error cases
	case response[:code]
	when 200..201
		begin
			response[:data] = JSON.parse(api_call.body)
		rescue JSON::ParserError => e
			response[:data] = api_call.body
			puts "** Warning **#{e.class}"
			puts e
		end
		response
	when 401
		response[:data] = api_call.body
		error = response[:response]
		self.authorized = false
		raise Samanage::AuthorizationError.new(error: error,response: response)
	when 404
		response[:data] = api_call.body
		error = response[:response]
		raise Samanage::NotFound.new(error: error, response: response)
	when 422
		response[:data] = api_call.body
		error = response[:response]
		raise Samanage::InvalidRequest.new(error: error, response: response)
	else
		response[:data] = api_call.body
		error = response[:response]
		raise Samanage::InvalidRequest.new(error: error, response: response)
	end
end

#find_contract(id: nil) ⇒ Object

Find contract given id



28
29
30
31
# File 'lib/samanage/api/contracts.rb', line 28

def find_contract(id: nil)
	path = "contracts/#{id}.json"
	self.execute(path: path)
end

#find_group(id: nil) ⇒ Object



30
31
32
33
# File 'lib/samanage/api/groups.rb', line 30

def find_group(id: nil)
	path = "groups/#{id}.json"
	self.execute(path: path)
end

#find_group_id_by_name(group: nil) ⇒ Object



23
24
25
26
27
28
# File 'lib/samanage/api/groups.rb', line 23

def find_group_id_by_name(group: nil)
	group_api = self.execute(path: "groups.json?name=#{group}")
	if !group_api[:data].empty? && group == group_api[:data].first['name']
		return group_api[:data].first['id']
	end
end

#find_hardware(id: nil) ⇒ Object

Find hardware given id



28
29
30
31
# File 'lib/samanage/api/hardwares.rb', line 28

def find_hardware(id: nil)
	path = "hardwares/#{id}.json"
	self.execute(path: path)
end

#find_hardwares_by_serial(serial_number: nil) ⇒ Object

Find hardware given a serial number



34
35
36
37
# File 'lib/samanage/api/hardwares.rb', line 34

def find_hardwares_by_serial(serial_number: nil)
	path = "hardwares.json?serial_number[]=#{serial_number}"
	self.execute(path: path)
end

#find_incident(id: nil) ⇒ Object

Find incident by ID



43
44
45
46
# File 'lib/samanage/api/incidents.rb', line 43

def find_incident(id: nil)
	path = "incidents/#{id}.json"
	self.execute(path: path)
end

#find_mobile(id: nil) ⇒ Object

Find mobile given id



28
29
30
31
# File 'lib/samanage/api/mobiles.rb', line 28

def find_mobile(id: nil)
	path = "mobiles/#{id}.json"
	self.execute(path: path)
end

#find_other_asset(id: nil) ⇒ Object

Find other_asset by id



31
32
33
34
# File 'lib/samanage/api/other_assets.rb', line 31

def find_other_asset(id: nil)
	path = "other_assets/#{id}.json"
	self.execute(path: path)
end

#find_user(id: nil) ⇒ Object

Return user by ID



28
29
30
31
# File 'lib/samanage/api/users.rb', line 28

def find_user(id: nil)
	path = "users/#{id}.json"
	self.execute(path: path)
end

#find_user_group_id_by_email(email:) ⇒ Object

Returns nil if no matching group_id



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/samanage/api/users.rb', line 40

def find_user_group_id_by_email(email: )
	user = self.check_user(value: email)
	group_ids = user[:data].select{|u| u['email'].to_s.downcase == email.to_s.downcase}.first.to_h['group_ids'].to_a
	group_ids.each do |group_id|
		group = self.find_group(id: group_id)
		if group[:data]['is_user'] && email == group[:data]['email']
			return group_id
		end
	end
	return nil
end

#find_user_id_by_email(email:) ⇒ Object

Email is unique so compare first for exact match only. Returns nil or the id



34
35
36
37
# File 'lib/samanage/api/users.rb', line 34

def find_user_id_by_email(email: )
	api_call = self.check_user(value: email)
	api_call[:data].select{|u| u['email'].to_s.downcase == email.to_s.downcase}.first.to_h['id']
end

#form_for(object_type: nil) ⇒ Object

Get form for a specific object type



36
37
38
39
40
41
# File 'lib/samanage/api/custom_forms.rb', line 36

def form_for(object_type: nil)
	if self.custom_forms == nil
		self.custom_forms = self.organize_forms
	end
	self.custom_forms[object_type]
end

#get_categories(path: PATHS[:category], options: {}) ⇒ Object



3
4
5
6
# File 'lib/samanage/api/category.rb', line 3

def get_categories(path: PATHS[:category], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_comments(incident_id:) ⇒ Object

Find comments given incident_id



6
7
8
9
# File 'lib/samanage/api/comments.rb', line 6

def get_comments(incident_id: )
	path = "incidents/#{incident_id}/comments.json"
	self.execute(path: path)
end

#get_contracts(path: PATHS[:contract], options: {}) ⇒ Object

Get contract default path



5
6
7
8
# File 'lib/samanage/api/contracts.rb', line 5

def get_contracts(path: PATHS[:contract], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_custom_fields(path: PATHS[:custom_fields], options: {}) ⇒ Object

Get custom fields default url



5
6
7
8
# File 'lib/samanage/api/custom_fields.rb', line 5

def get_custom_fields(path: PATHS[:custom_fields], options:{})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_custom_forms(path: PATHS[:custom_forms], options: {}) ⇒ Object

Get custom forms path



4
5
6
7
# File 'lib/samanage/api/custom_forms.rb', line 4

def get_custom_forms(path: PATHS[:custom_forms], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_departments(path: PATHS[:department], options: {}) ⇒ Object



3
4
5
6
# File 'lib/samanage/api/departments.rb', line 3

def get_departments(path: PATHS[:department], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_groups(path: PATHS[:group], options: {}) ⇒ Object



3
4
5
6
# File 'lib/samanage/api/groups.rb', line 3

def get_groups(path: PATHS[:group], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_hardwares(path: PATHS[:hardware], options: {}) ⇒ Object

Get hardware default path



5
6
7
8
# File 'lib/samanage/api/hardwares.rb', line 5

def get_hardwares(path: PATHS[:hardware], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_incidents(path: PATHS[:incident], options: {}) ⇒ Object

Default get incident path



5
6
7
8
# File 'lib/samanage/api/incidents.rb', line 5

def get_incidents(path: PATHS[:incident], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_mobiles(path: PATHS[:mobile], options: {}) ⇒ Object

Get mobile default path



5
6
7
8
# File 'lib/samanage/api/mobiles.rb', line 5

def get_mobiles(path: PATHS[:mobile], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_other_assets(path: PATHS[:other_asset], options: {}) ⇒ Object

Default get other_assets path



5
6
7
8
# File 'lib/samanage/api/other_assets.rb', line 5

def get_other_assets(path: PATHS[:other_asset], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_requester_id(value: nil) ⇒ Object

Get requester from value (email)



4
5
6
7
# File 'lib/samanage/api/requester.rb', line 4

def get_requester_id(value: nil)
	api_call = self.execute(path: "requesters.json?name=#{value}")
	api_call[:data].size == 1 ? api_call[:data][0] : nil
end

#get_sites(path: PATHS[:site], options: {}) ⇒ Object



3
4
5
6
# File 'lib/samanage/api/sites.rb', line 3

def get_sites(path: PATHS[:site], options: {})
	url = Samanage::UrlBuilder.new(path: path, options: options).url
	self.execute(path: url)
end

#get_users(path: PATHS[:user], options: {}) ⇒ Object

Get users, using URL builder



5
6
7
8
# File 'lib/samanage/api/users.rb', line 5

def get_users(path: PATHS[:user], options: {})
   url = Samanage::UrlBuilder.new(path: path, options: options).url
   self.execute(path: url)
end

#list_adminsObject

Return all admins in the account



149
150
151
152
153
154
# File 'lib/samanage/api.rb', line 149

def list_admins
	admin_role_id = self.execute(path: 'roles.json')[:data].select{|role| role['name'] == 'Administrator'}.first['id']
	self.admins.push(
		self.execute(path: "users.json?role=#{admin_role_id}")[:data].map{|u| u['email']}
	).flatten
end

#organize_formsObject

Set forms by type and map fields



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/samanage/api/custom_forms.rb', line 22

def organize_forms
	custom_forms = self.collect_custom_forms
	custom_forms.map{|form| form.delete_if{|k, v| v.nil?}}
	custom_forms.map{|form| form['custom_form_fields'].map{|fields| fields.delete_if{|k, v| v == false}}}
	custom_forms.map{|form| form['custom_form_fields'].map{|fields| fields['custom_field'].delete_if{|k, v| !v}}}
	custom_forms.group_by{|k|
		k['module']}.each_pair{|forms_name, forms|
			forms.each{|form|
				form['custom_form_fields'].group_by{|f| f['name'] }
			}
		}
end

#send_activation_email(email:) ⇒ Object

Raises:



3
4
5
6
7
# File 'lib/samanage/api/utils.rb', line 3

def send_activation_email(email: )
  user_id = self.find_user_id_by_email(email: email)
  raise Samanage::Error.new(error: 'Invalid Email', response: {}) unless user_id
  self.execute(http_method: 'put', path: "users/#{user_id}.json?send_activation_email=1&add_callbacks=1")
end

#update_contract(payload: nil, id: nil, options: {}) ⇒ Object

Update contract given id



40
41
42
43
# File 'lib/samanage/api/contracts.rb', line 40

def update_contract(payload: nil, id: nil, options: {})
	path = "contracts/#{id}.json"
	self.execute(path: path, http_method: 'put', payload: payload)
end

#update_hardware(payload: nil, id: nil, options: {}) ⇒ Object

Update hardware given id



47
48
49
50
# File 'lib/samanage/api/hardwares.rb', line 47

def update_hardware(payload: nil, id: nil, options: {})
	path = "hardwares/#{id}.json"
	self.execute(path: path, http_method: 'put', payload: payload)
end

#update_incident(payload: nil, id: nil, options: {}) ⇒ Object

Update an incident given id and json



49
50
51
52
# File 'lib/samanage/api/incidents.rb', line 49

def update_incident(payload: nil, id: nil, options: {})
	path = "incidents/#{id}.json"
	self.execute(path: path, http_method: 'put', payload: payload)
end

#update_mobile(payload: nil, id: nil, options: {}) ⇒ Object

Update mobile given id



40
41
42
43
# File 'lib/samanage/api/mobiles.rb', line 40

def update_mobile(payload: nil, id: nil, options: {})
	path = "mobiles/#{id}.json"
	self.execute(path: path, http_method: 'put', payload: payload)
end

#update_other_asset(payload: nil, id: nil, options: {}) ⇒ Object

Update other_asset given json and id



37
38
39
40
# File 'lib/samanage/api/other_assets.rb', line 37

def update_other_asset(payload: nil, id: nil, options: {})
	path = "other_assets/#{id}.json"
	self.execute(path: path, http_method: 'put', payload: payload)
end

#update_user(payload: nil, id: nil) ⇒ Object

Update user by id



62
63
64
65
# File 'lib/samanage/api/users.rb', line 62

def update_user(payload: nil, id: nil)
	path = "users/#{id}.json"
	self.execute(path: path, http_method: 'put', payload: payload)
end