class Samanage::Api

Constants

PATHS

Attributes

admins[RW]
authorized[RW]
base_url[RW]
content_type[RW]
custom_forms[RW]
datacenter[RW]
max_retries[RW]
sleep_time[RW]
token[RW]

Public Class Methods

new(token:, datacenter: nil, development_mode: false, max_retries: 3, content_type: "json", sleep_time: 5) click to toggle source

Development mode forces authorization & pre-populates admins and custom forms / fields datacenter should equal 'eu' or blank

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

Public Instance Methods

add_item_to_contract(id:, payload:) click to toggle source
# File lib/samanage/api/contracts.rb, line 53
def add_item_to_contract(id:, payload:)
  path = "contracts/#{id}/items.json"
  self.execute(path: path, http_method: "post", payload: payload)
end
add_member_to_group(email:, group_id: nil, group_name: nil) click to toggle source
# File lib/samanage/api/groups.rb, line 48
def add_member_to_group(email:, group_id: nil, group_name: nil)
  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
authorize() click to toggle source

Check “oken against api.json”

# File lib/samanage/api.rb, line 60
def authorize
  execute(path: "api.#{content_type}")
  self.authorized = true
end
authorized?() click to toggle source
# File lib/samanage/api.rb, line 55
def authorized?
  authorized
end
categories(options: {})
Alias for: collect_categories
changes(options: {})
Alias for: collect_changes
check_contract(options: {}) click to toggle source

Check for contract using URL builder

# File lib/samanage/api/contracts.rb, line 42
def check_contract(options: {})
  url = Samanage::UrlBuilder.new(path: PATHS[:contract], options: options).url
  self.execute(path: url)
end
check_hardware(options: {}) click to toggle source

Check for hardware using URL builder

# File lib/samanage/api/hardwares.rb, line 49
def check_hardware(options: {})
  url = Samanage::UrlBuilder.new(path: PATHS[:hardware], options: options).url
  self.execute(path: url)
end
check_mobile(options: {}) click to toggle source

Check for mobile using URL builder

# File lib/samanage/api/mobiles.rb, line 42
def check_mobile(options: {})
  url = Samanage::UrlBuilder.new(path: PATHS[:mobile], options: options).url
  self.execute(path: url)
end
check_user(field: "email", value:, options: {}) click to toggle source

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

# File lib/samanage/api/users.rb, line 66
def check_user(field: "email", value:, options: {})
  if field.to_s.downcase == "email"
    value = value.to_s.gsub("+", "%2B")
  end
  url = "users.json?#{field}=#{value}"
  self.execute(path: url)
end
collect_categories(options: {}) click to toggle source

Samanage categories are not paginated

  • to break into subcategories, add

# File lib/samanage/api/category.rb, line 13
def collect_categories(options: {})
  request = self.execute(http_method: "get", path: "categories.json", options: options)
  request[:data]
end
Also aliased as: categories
collect_changes(options: {}) { |change| ... } click to toggle source

Returns all changes. Options:

- audit_archives: true
- layout: 'long'
# File lib/samanage/api/changes.rb, line 16
def collect_changes(options: {})
  changes = Array.new
  total_pages = self.get_changes(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Changes page: #{page}/#{total_pages}" if options[:verbose]
    path = "changes.json?"
    request = self.execute(http_method: "get", path: path, options: options)
    request[:data].each do |change|
      if block_given?
        yield change
      end
      changes << change
    end
  end
  changes
end
Also aliased as: changes
collect_configuration_items(options: {}) { |configuration_item| ... } click to toggle source

Returns all configuration_items. Options:

- audit_archives: true
- layout: 'long'
# File lib/samanage/api/configuration_items.rb, line 16
def collect_configuration_items(options: {})
  configuration_items = Array.new
  total_pages = self.get_configuration_items(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Configuration Items page: #{page}/#{total_pages}" if options[:verbose]
    path = "configuration_items.json?"
    request = self.execute(http_method: "get", path: path, options: options)
    request[:data].each do |configuration_item|
      if block_given?
        yield configuration_item
      end
      configuration_items << configuration_item
    end
  end
  configuration_items
end
Also aliased as: configuration_items
collect_contracts(options: {}) { |contract| ... } click to toggle source

Get all contracts

# File lib/samanage/api/contracts.rb, line 12
def collect_contracts(options: {})
  contracts = Array.new
  total_pages = self.get_contracts(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Contracts page: #{page}/#{total_pages}" if options[:verbose]
    path = "contracts.json?"
    self.execute(path: path, options: options)[:data].each do |contract|
      if block_given?
        yield contract
      end
      contracts << contract
    end
  end
  contracts
end
Also aliased as: contracts
collect_custom_fields(options: {}) { |custom_field| ... } click to toggle source

Gets all custom fields

# File lib/samanage/api/custom_fields.rb, line 12
def collect_custom_fields(options: {})
  custom_fields = Array.new
  total_pages = self.get_custom_fields(options: options)[:total_pages] ||= 2
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Custom Fields page: #{page}/#{total_pages}" if options[:verbose]
    path = "custom_fields.json?"
    self.execute(path: path, options: options)[:data].each do |custom_field|
      if block_given?
        yield custom_field
      end
      custom_fields << custom_field
    end
  end
  custom_fields
end
Also aliased as: custom_fields
collect_custom_forms(options: {}) { |custom_form| ... } click to toggle source

Get all custom forms

# File lib/samanage/api/custom_forms.rb, line 11
def collect_custom_forms(options: {})
  custom_forms = Array.new
  total_pages = self.get_custom_forms(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Custom Forms page: #{page}/#{total_pages}" if options[:verbose]
    path = "custom_forms.json?"
    self.execute(path: path, options: options)[:data].each do |custom_form|
      if block_given?
        yield custom_form
      end
      custom_forms << custom_form
    end
  end
  custom_forms
end
collect_departments(options: {}) { |department| ... } click to toggle source
# File lib/samanage/api/departments.rb, line 10
def collect_departments(options: {})
  departments = Array.new
  total_pages = self.get_departments(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Departments page: #{page}/#{total_pages}" if options[:verbose]
    path = "departments.json?"
    self.execute(path: path, options: options)[:data].each do |department|
      if block_given?
        yield department
      end
      departments << department
    end
  end
  departments
end
Also aliased as: departments
collect_groups(options: {}) { |group| ... } click to toggle source
# File lib/samanage/api/groups.rb, line 10
def collect_groups(options: {})
  groups = Array.new
  total_pages = self.get_groups(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Groups page: #{page}/#{total_pages}" if options[:verbose]
    path = "groups.json?"
    self.execute(path: path, options: options)[:data].each do |group|
      if block_given?
        yield group
      end
      groups << group
    end
  end
  groups
end
Also aliased as: groups
collect_hardwares(options: {}) { |hardware| ... } click to toggle source

Get all hardwares

# File lib/samanage/api/hardwares.rb, line 12
def collect_hardwares(options: {})
  hardwares = Array.new
  total_pages = self.get_hardwares(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Hardwares page: #{page}/#{total_pages}" if options[:verbose]
    path = "hardwares.json?"
    self.execute(path: path, options: options)[:data].each do |hardware|
      if block_given?
        yield hardware
      end
      hardwares << hardware
    end
  end
  hardwares
end
Also aliased as: hardwares
collect_incidents(options: {}) { |incident_with_archive| ... } click to toggle source

Returns all incidents. Options:

- audit_archives: true
- layout: 'long'
# File lib/samanage/api/incidents.rb, line 14
def collect_incidents(options: {})
  incidents = []
  total_pages = get_incidents(options: options.except(:audit_archives, :audit_archive, :layout))[:total_pages]
  message = "Requesting Incidents with Audit Archives (this may take a while)"
  puts message if options[:audit_archives] && options[:verbose]
  1.upto(total_pages) do |page|
    puts "Collecting Incidents page: #{page}/#{total_pages}" if options[:verbose]
    options[:page] = page
    if options[:audit_archives]
      paginated_path = "incidents.json?"
      paginated_incidents = execute(path: paginated_path, options: options)[:data]
      paginated_incidents.map do |incident|
        archive_uri = "incidents/#{incident['id']}.json?layout=long&audit_archive=true"
        incident_with_archive = execute(path: archive_uri)[:data]
        yield incident_with_archive if block_given?
        incidents.push(incident_with_archive)
      end
    else
      path = "incidents.json?"
      execute(path: path, options: options)[:data].each do |incident|
        yield incident if block_given?
        incidents.push(incident)
      end
    end
  end
  incidents
end
Also aliased as: incidents
collect_mobiles(options: {}) { |mobiles| ... } click to toggle source

Get all mobiles

# File lib/samanage/api/mobiles.rb, line 12
def collect_mobiles(options: {})
  mobiles = Array.new
  total_pages = self.get_mobiles(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Mobiles page: #{page}/#{total_pages}" if options[:verbose]
    path = "mobiles.json?"
    self.execute(path: path, options: options)[:data].each do |mobile|
      if block_given?
        yield mobiles
      end
      mobiles << mobile
    end
  end
  mobiles
end
Also aliased as: mobiles
collect_other_assets(options: {}) { |other_asset| ... } click to toggle source

Returns all other assets

# File lib/samanage/api/other_assets.rb, line 12
def collect_other_assets(options: {})
  other_assets = Array.new
  total_pages = self.get_other_assets(options: options)[:total_pages]
  other_assets = []
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Other Assets page: #{page}/#{total_pages}" if options[:verbose]
    path = "other_assets.json?"
    self.execute(path: path, options: options)[:data].each do |other_asset|
      if block_given?
        yield other_asset
      end
      other_assets << other_asset
    end
  end
  other_assets
end
Also aliased as: other_assets
collect_problems(options: {}) { |problem| ... } click to toggle source

Returns all problems. Options:

- audit_archives: true
- layout: 'long'
# File lib/samanage/api/problems.rb, line 16
def collect_problems(options: {})
  problems = Array.new
  total_pages = self.get_problems(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Problems page: #{page}/#{total_pages}" if options[:verbose]
    path = "problems.json?"
    request = self.execute(http_method: "get", path: path, options: options)
    request[:data].each do |problem|
      if block_given?
        yield problem
      end
      problems << problem
    end
  end
  problems
end
Also aliased as: problems
collect_purchase_orders(options: {}) { |purchase_order| ... } click to toggle source

Returns all purchase_orders. Options:

- audit_archives: true
- layout: 'long'
# File lib/samanage/api/purchase_orders.rb, line 16
def collect_purchase_orders(options: {})
  purchase_orders = Array.new
  total_pages = self.get_purchase_orders(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Purchase Orders page: #{page}/#{total_pages}" if options[:verbose]
    path = "purchase_orders.json?"
    request = self.execute(http_method: "get", path: path, options: options)
    request[:data].each do |purchase_order|
      if block_given?
        yield purchase_order
      end
      purchase_orders << purchase_order
    end
  end
  purchase_orders
end
Also aliased as: purchase_orders
collect_releases(options: {}) { |release| ... } click to toggle source

Returns all other assets

# File lib/samanage/api/releases.rb, line 12
def collect_releases(options: {})
  releases = Array.new
  total_pages = self.get_releases(options: options)[:total_pages]
  releases = []
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Other Assets page: #{page}/#{total_pages}" if options[:verbose]
    path = "releases.json?"
    self.execute(path: path, options: options)[:data].each do |release|
      if block_given?
        yield release
      end
      releases << release
    end
  end
  releases
end
Also aliased as: releases
collect_requesters(options: {page: 1}, requesters: [], path: PATHS[:requester]) { |requester| ... } click to toggle source

X-Total headers are not returned on this resource

# File lib/samanage/api/requester.rb, line 9
def collect_requesters(options: {page: 1}, requesters: [], path: PATHS[:requester])
  puts "Collecting requesters page: #{options[:page]}" if options[:verbose]
  paginated_requesters = self.execute(path: path, options: options)[:data]
  paginated_requesters.each do |requester|
    if block_given?
      yield requester
    end
    requesters << requester
  end
  if paginated_requesters.empty?
    return requesters.uniq{|r| r['id']}
  else
    collect_requesters(options: {page: options[:page] + 1}, requesters: requesters)
  end
end
Also aliased as: requesters
collect_sites(options: {}) { |site| ... } click to toggle source
# File lib/samanage/api/sites.rb, line 10
def collect_sites(options: {})
  sites = Array.new
  total_pages = self.get_sites(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Sites page: #{page}/#{total_pages}" if options[:verbose]
    path = "sites.json?"
    self.execute(path: path, options: options)[:data].each do |site|
      if block_given?
        yield site
      end
      sites << site
    end
  end
  sites
end
Also aliased as: sites
collect_solutions(options: {}) { |solution| ... } click to toggle source
# File lib/samanage/api/solutions.rb, line 10
def collect_solutions(options: {})
  solutions = Array.new
  total_pages = self.get_solutions(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Solutions page: #{page}/#{total_pages}" if options[:verbose]
    path = "solutions.json?"
    self.execute(http_method: "get", path: path, options: options)[:data].each do |solution|
      if block_given?
        yield solution
      end
      solutions << solution
    end
  end
  solutions
end
Also aliased as: solutions
collect_tasks(options: {}) { |task| ... } click to toggle source
# File lib/samanage/api/tasks.rb, line 10
def collect_tasks(options: {})
  tasks = Array.new
  total_pages = self.get_tasks(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting tasks page: #{page}/#{total_pages}" if options[:verbose]
    path = "tasks.json?"
    self.execute(path: path, options: options)[:data].each do |task|
      if block_given?
        yield task
      end
      tasks << task
    end
  end
  tasks
end
Also aliased as: tasks
collect_users(options: {}) { |user| ... } click to toggle source

Returns all users in the account

# File lib/samanage/api/users.rb, line 12
def collect_users(options: {})
  users = Array.new
  total_pages = self.get_users(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Users page: #{page}/#{total_pages}" if options[:verbose]
    path = "users.json?"
    self.execute(path: path, options: options)[:data].each do |user|
      if block_given?
        yield user
      end
      users << user
    end
  end
  users
end
Also aliased as: users
collect_vendors(options: {}) { |vendor| ... } click to toggle source
# File lib/samanage/api/vendors.rb, line 10
def collect_vendors(options: {})
  vendors = Array.new
  total_pages = self.get_vendors(options: options)[:total_pages]
  1.upto(total_pages) do |page|
    options[:page] = page

    puts "Collecting Vendors page: #{page}/#{total_pages}" if options[:verbose]
    path = "vendors.json?"
    self.execute(http_method: "get", path: path, options: options)[:data].each do |vendor|
      if block_given?
        yield vendor
      end
      vendors << vendor
    end
  end
  vendors
end
Also aliased as: vendors
comments(incident_id:)
Alias for: get_comments
configuration_items(options: {})
contracts(options: {})
Alias for: collect_contracts
create_attachment(filepath:, attachable_type:, attachable_id:) click to toggle source
# File lib/samanage/api/attachments.rb, line 21
def create_attachment(filepath:, attachable_type:, attachable_id:)
  unless File.exist?(filepath)
    puts "Cannot find filepath: '#{filepath.inspect}'"
    return
  end
  req = execute(
    path: "attachments.json",
    http_method: "post",
    multipart: true,
    payload: {
      "file[attachable_type]" => attachable_type,
      "file[attachable_id]" => attachable_id,
      "file[attachment]" => File.open(filepath, "rb")
    },
    headers: {
      "Content-Type" => "multipart/form-data",
      "X-Samanage-Authorization" => "Bearer " + token
    }
  )
  req
end
create_category(payload: nil, options: {}) click to toggle source
# File lib/samanage/api/category.rb, line 18
def create_category(payload: nil, options: {})
  self.execute(path: PATHS[:category], http_method: "post", payload: payload, options: options)
end
create_change(payload: nil, options: {}) click to toggle source

Create an change given json

# File lib/samanage/api/changes.rb, line 37
def create_change(payload: nil, options: {})
  self.execute(path: PATHS[:change], http_method: "post", payload: payload)
end
create_comment(incident_id:, comment:, options: {}) click to toggle source

Add a new comment

# File lib/samanage/api/comments.rb, line 12
def create_comment(incident_id:, comment:, options: {})
  path = "incidents/#{incident_id}/comments.json?"
  self.execute(http_method: "post", path: path, payload: comment, options: options)
end
create_configuration_item(payload: nil, options: {}) click to toggle source

Create an configuration_item given json

# File lib/samanage/api/configuration_items.rb, line 37
def create_configuration_item(payload: nil, options: {})
  self.execute(path: PATHS[:configuration_item], http_method: "post", payload: payload)
end
create_contract(payload:, options: {}) click to toggle source

Create contract given json payload

# File lib/samanage/api/contracts.rb, line 31
def create_contract(payload:, options: {})
  self.execute(path: PATHS[:contract], http_method: "post", payload: payload)
end
create_department(payload:, options: {}) click to toggle source
# File lib/samanage/api/departments.rb, line 28
def create_department(payload:, options: {})
  self.execute(path: PATHS[:department], http_method: "post", payload: payload)
end
create_group(payload:, options: {}) click to toggle source
# File lib/samanage/api/groups.rb, line 28
def create_group(payload:, options: {})
  self.execute(path: PATHS[:group], http_method: "post", payload: payload)
end
create_hardware(payload:, options: {}) click to toggle source

Create hardware given json payload

# File lib/samanage/api/hardwares.rb, line 31
def create_hardware(payload:, options: {})
  self.execute(path: PATHS[:hardware], http_method: "post", payload: payload)
end
create_incident(payload: nil, options: {}) click to toggle source

Create an incident given json

# File lib/samanage/api/incidents.rb, line 43
def create_incident(payload: nil, options: {})
  execute(path: PATHS[:incident], http_method: "post", payload: payload, options: options)
end
create_mobile(payload: nil, options: {}) click to toggle source

Create mobile given json payload

# File lib/samanage/api/mobiles.rb, line 31
def create_mobile(payload: nil, options: {})
  self.execute(path: PATHS[:mobile], http_method: "post", payload: payload)
end
create_other_asset(payload:, options: {}) click to toggle source

Create an other_asset given json

# File lib/samanage/api/other_assets.rb, line 33
def create_other_asset(payload:, options: {})
  self.execute(path: PATHS[:other_asset], http_method: "post", payload: payload)
end
create_problem(payload: nil, options: {}) click to toggle source

Create an problem given json

# File lib/samanage/api/problems.rb, line 37
def create_problem(payload: nil, options: {})
  self.execute(path: PATHS[:problem], http_method: "post", payload: payload)
end
create_purchase_order(payload: nil, options: {}) click to toggle source

Create an purchase_order given json

# File lib/samanage/api/purchase_orders.rb, line 37
def create_purchase_order(payload: nil, options: {})
  self.execute(path: PATHS[:purchase_order], http_method: "post", payload: payload)
end
create_release(payload:, options: {}) click to toggle source

Create an release given json

# File lib/samanage/api/releases.rb, line 33
def create_release(payload:, options: {})
  self.execute(path: PATHS[:release], http_method: "post", payload: payload)
end
create_site(payload:, options: {}) click to toggle source
# File lib/samanage/api/sites.rb, line 28
def create_site(payload:, options: {})
  self.execute(path: PATHS[:site], http_method: "post", payload: payload)
end
create_solution(payload:, options: {}) click to toggle source
# File lib/samanage/api/solutions.rb, line 34
def create_solution(payload:, options: {})
  self.execute(path: PATHS[:solution], http_method: "post", payload: payload)
end
create_task(incident_id:, payload:, options: {}) click to toggle source
# File lib/samanage/api/tasks.rb, line 28
def create_task(incident_id:, payload:, options: {})
  path = "incidents/#{incident_id}/tasks.json"
  self.execute(path: path, http_method: "post", payload: payload)
end
create_time_track(incident_id:, payload:) click to toggle source
# File lib/samanage/api/time_tracks.rb, line 9
def create_time_track(incident_id:, payload:)
  self.execute(path: "incidents/#{incident_id}/time_tracks.json", http_method: "post", payload: payload)
end
create_user(payload:, options: {}) click to toggle source

Create user given JSON

# File lib/samanage/api/users.rb, line 31
def create_user(payload:, options: {})
  self.execute(path: PATHS[:user], http_method: "post", payload: payload, options: options)
end
create_vendor(payload:, options: {}) click to toggle source
# File lib/samanage/api/vendors.rb, line 28
def create_vendor(payload:, options: {})
  self.execute(path: PATHS[:vendor], http_method: "post", payload: payload)
end
custom_fields(options: {})
delete_category(id:, options: {}) click to toggle source
# File lib/samanage/api/category.rb, line 22
def delete_category(id:, options: {})
  self.execute(path: "categories/#{id}", http_method: "delete", options: options)
end
delete_change(id:) click to toggle source
# File lib/samanage/api/changes.rb, line 56
def delete_change(id:)
  self.execute(path: "changes/#{id}.json", http_method: "delete")
end
delete_configuration_item(id:) click to toggle source
# File lib/samanage/api/configuration_items.rb, line 56
def delete_configuration_item(id:)
  self.execute(path: "configuration_items/#{id}.json", http_method: "delete")
end
delete_contract(id:) click to toggle source
# File lib/samanage/api/contracts.rb, line 58
def delete_contract(id:)
  self.execute(path: "contracts/#{id}.json", http_method: "delete")
end
delete_department(id:) click to toggle source
# File lib/samanage/api/departments.rb, line 31
def delete_department(id:)
  self.execute(path: "departments/#{id}.json", http_method: "delete")
end
delete_group(id:) click to toggle source
# File lib/samanage/api/groups.rb, line 55
def delete_group(id:)
  self.execute(path: "groups/#{id}.json", http_method: "delete")
end
delete_hardware(id:) click to toggle source
# File lib/samanage/api/hardwares.rb, line 60
def delete_hardware(id:)
  self.execute(path: "hardwares/#{id}.json", http_method: "delete")
end
delete_incident(id:, options: {}) click to toggle source
# File lib/samanage/api/incidents.rb, line 59
def delete_incident(id:, options: {})
  execute(path: "incidents/#{id}.json", http_method: "delete", options: options)
end
delete_mobile(id:) click to toggle source
# File lib/samanage/api/mobiles.rb, line 53
def delete_mobile(id:)
  self.execute(path: "mobiles/#{id}.json", http_method: "delete")
end
delete_other_asset(id:) click to toggle source
# File lib/samanage/api/other_assets.rb, line 50
def delete_other_asset(id:)
  self.execute(path: "other_assets/#{id}.json", http_method: "delete")
end
delete_problem(id:) click to toggle source
# File lib/samanage/api/problems.rb, line 56
def delete_problem(id:)
  self.execute(path: "problems/#{id}.json", http_method: "delete")
end
delete_purchase_order(id:) click to toggle source
# File lib/samanage/api/purchase_orders.rb, line 56
def delete_purchase_order(id:)
  self.execute(path: "purchase_orders/#{id}.json", http_method: "delete")
end
delete_release(id:) click to toggle source
# File lib/samanage/api/releases.rb, line 50
def delete_release(id:)
  self.execute(path: "releases/#{id}.json", http_method: "delete")
end
delete_site(id:) click to toggle source
# File lib/samanage/api/sites.rb, line 32
def delete_site(id:)
  self.execute(path: "sites/#{id}.json", http_method: "delete")
end
delete_solution(id:) click to toggle source
# File lib/samanage/api/solutions.rb, line 42
def delete_solution(id:)
  self.execute(path: "solutions/#{id}.json", http_method: "delete")
end
delete_task(incident_id:, task_id:) click to toggle source
# File lib/samanage/api/tasks.rb, line 33
def delete_task(incident_id:, task_id:)
  self.execute(path: "incidents/#{incident_id}/tasks/#{task_id}.json", http_method: "delete")
end
delete_user(id:) click to toggle source
# File lib/samanage/api/users.rb, line 80
def delete_user(id:)
  self.execute(path: "users/#{id}.json", http_method: "delete")
end
delete_vendor(id:) click to toggle source
# File lib/samanage/api/vendors.rb, line 36
def delete_vendor(id:)
  self.execute(path: "vendors/#{id}.json", http_method: "delete")
end
departments(options: {})
Alias for: collect_departments
download_attachment(attachment: {}, filename: nil, path: nil) click to toggle source
# File lib/samanage/api/attachments.rb, line 5
def download_attachment(attachment: {}, filename: nil, path: nil)
  attachable_type = attachment["attachable_type"]
  attachable_id = attachment["attachable_id"].to_s
  filename ||= attachment["filename"]
  url = attachment["url"]

  file_path = path || File.join(Dir.pwd, attachable_type, attachable_id)
  FileUtils.mkpath(file_path) unless File.directory?(file_path)

  exact_path = File.join(file_path, filename)
  downloaded_attachment = File.open(exact_path, "wb+") do |file|
    file << URI.parse(url).read
  end
  downloaded_attachment
end
execute(http_method: "get", path: nil, payload: nil, verbose: nil, headers: {}, options: {}, multipart: false) click to toggle source

Calling execute without a method defaults to GET

# File lib/samanage/api.rb, line 66
def execute(http_method: "get", path: nil, payload: nil, verbose: nil, headers: {}, options: {}, multipart: false)
  if payload.class == Hash && content_type == "json"
    begin
      payload = payload.to_json if path != "attachments.json"
    rescue StandardError => e
      puts "Invalid JSON: #{payload.inspect}"
      raise Samanage::Error.new(error: e, response: nil, options: options)
    end
  end
  verbose = "?layout=long" unless verbose.nil?

  headers = {
    "Accept" => "application/vnd.samanage.v2.1+#{content_type}#{verbose}",
    "Content-Type" => "application/#{content_type}",
    "X-Samanage-Authorization" => "Bearer " + self.token
  }.merge(headers)
  options = options.except(:verbose)
  full_path = base_url + path
  retries = 0
  begin
    case http_method.to_s.downcase
    when "get"
      api_call = self.class.get(full_path, headers: headers, query: options)
    when "post"
      api_call = self.class.post(full_path, multipart: multipart,  body: payload, headers: headers, query: options)
    when "put"
      api_call = self.class.put(full_path, body: payload, headers: headers, query: options)
    when "delete"
      api_call = self.class.delete(full_path, body: payload, headers: headers, query: options)
    else
      raise Samanage::Error.new(response: { response: "Unknown HTTP method" }, options: options)
    end
  rescue Errno::ECONNREFUSED, Net::OpenTimeout, Errno::ETIMEDOUT, Net::ReadTimeout, OpenSSL::SSL::SSLError,
        Errno::ENETDOWN, Errno::ECONNRESET, Errno::ENOENT, EOFError, Net::HTTPTooManyRequests, SocketError => e
    retries += 1
    if retries < max_retries
      puts "[Warning] #{e.class}: #{e} -  Retry: #{retries}/#{max_retries}"
      sleep sleep_time
      retry
    else
      error = e
      response = e.class
      raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
    end
  rescue StandardError => e
    retries += 1
    if retries < max_retries
      puts "[Warning] #{e.class}: #{e} -  Retry: #{retries}/#{max_retries}"
      sleep sleep_time
      retry
    else
      error = e
      response = e.class
      raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
    end

  end

  response = {}
  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}: #{e}" unless path.match?("send_activation_email")
    end
    response
  when 401
    response[:data] = api_call.body
    error = response[:response]
    self.authorized = false
    raise Samanage::AuthorizationError.new(error: error, response: response, options: options)
  when 404
    response[:data] = api_call.body
    error = response[:response]
    raise Samanage::NotFound.new(error: error, response: response, options: options)
  when 422
    response[:data] = api_call.body
    error = response[:response]
    raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
  when 429
    response[:data] = api_call.body
    error = response[:response]
    raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
  else
    response[:data] = api_call.body
    error = response[:response]
    raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
  end
end
find_change(id:, options: {}) click to toggle source

Find change by ID

# File lib/samanage/api/changes.rb, line 42
def find_change(id:, options: {})
  path = "changes/#{id}.json"
  if options[:layout] == "long"
    path += "?layout=long"
  end
  self.execute(path: path)
end
find_configuration_item(id:, options: {}) click to toggle source

Find configuration_item by ID

# File lib/samanage/api/configuration_items.rb, line 42
def find_configuration_item(id:, options: {})
  path = "configuration_items/#{id}.json"
  if options[:layout] == "long"
    path += "?layout=long"
  end
  self.execute(path: path)
end
find_contract(id:, options: {}) click to toggle source

Find contract given id

# File lib/samanage/api/contracts.rb, line 36
def find_contract(id:, options: {})
  path = "contracts/#{id}.json"
  self.execute(path: path, options: {})
end
find_group(id:, options: {}) click to toggle source
# File lib/samanage/api/groups.rb, line 43
def find_group(id:, options: {})
  path = "groups/#{id}.json"
  self.execute(path: path, options: {})
end
find_group_id_by_name(group: "", options: {}) click to toggle source
# File lib/samanage/api/groups.rb, line 32
def find_group_id_by_name(group: "", options: {})
  options[:name] = group if group && !options.keys.include?(:name)

  path = "groups.json?"
  group_api = self.execute(path: path, options: options)
  # Group names are case sensitive
  if !group_api[:data].empty? && group == group_api[:data].first["name"]
    return group_api[:data].first["id"]
  end
end
find_hardware(id:) click to toggle source

Find hardware given id

# File lib/samanage/api/hardwares.rb, line 36
def find_hardware(id:)
  path = "hardwares/#{id}.json"
  self.execute(path: path)
end
find_hardwares_by_serial(serial_number:) click to toggle source

Find hardware given a serial number

# File lib/samanage/api/hardwares.rb, line 42
def find_hardwares_by_serial(serial_number:)
  path = "hardwares.json?serial_number[]=#{serial_number}"
  self.execute(path: path)
end
find_incident(id:, options: {}) click to toggle source

Find incident by ID

# File lib/samanage/api/incidents.rb, line 48
def find_incident(id:, options: {})
  path = "incidents/#{id}.json?"
  execute(path: path, options: options)
end
find_mobile(id:, options: {}) click to toggle source

Find mobile given id

# File lib/samanage/api/mobiles.rb, line 36
def find_mobile(id:, options: {})
  path = "mobiles/#{id}.json"
  self.execute(path: path)
end
find_name_from_group_id(group_id:) click to toggle source
# File lib/samanage/api/utils.rb, line 19
def find_name_from_group_id(group_id:)
  return if [-1, "-1", nil, ""].include?(group_id)
  begin
    self.find_group(id: group_id).to_h.dig(:data, "name")
  rescue => e
    return "[#{e.class}]: #{e.inspect} Unable to find user for group id #{group_id}"
  end
end
find_other_asset(id:) click to toggle source

Find other_asset by id

# File lib/samanage/api/other_assets.rb, line 39
def find_other_asset(id:)
  path = "other_assets/#{id}.json"
  self.execute(path: path)
end
find_problem(id:, options: {}) click to toggle source

Find problem by ID

# File lib/samanage/api/problems.rb, line 42
def find_problem(id:, options: {})
  path = "problems/#{id}.json"
  if options[:layout] == "long"
    path += "?layout=long"
  end
  self.execute(path: path)
end
find_purchase_order(id:, options: {}) click to toggle source

Find purchase_order by ID

# File lib/samanage/api/purchase_orders.rb, line 42
def find_purchase_order(id:, options: {})
  path = "purchase_orders/#{id}.json"
  if options[:layout] == "long"
    path += "?layout=long"
  end
  self.execute(path: path)
end
find_release(id:, options: {}) click to toggle source

Find release by id

# File lib/samanage/api/releases.rb, line 39
def find_release(id:, options: {})
  path = "releases/#{id}.json"
  self.execute(path: path, options: {})
end
find_solution(id:, options: {}) click to toggle source
# File lib/samanage/api/solutions.rb, line 28
def find_solution(id:, options: {})
  path = "solutions/#{id}.json"
  self.execute(path: path)
end
find_user(id:, options: {}) click to toggle source

Return user by ID

# File lib/samanage/api/users.rb, line 36
def find_user(id:, options: {})
  path = "users/#{id}.json"
  self.execute(path: path, options: {})
end
find_user_group_id_by_email(email:) click to toggle source

Returns nil if no matching group_id

# File lib/samanage/api/users.rb, line 50
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)
    group_email = group[:data]["email"].to_s.downcase
    if group[:data]["is_user"] && email.to_s.downcase == group_email
      return group_id
    end
  end
  nil
end
find_user_id_by_email(email:) click to toggle source

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

# File lib/samanage/api/users.rb, line 42
def find_user_id_by_email(email:)
  api_call = self.get_users(options: { email: 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) click to toggle source

Get form for a specific object type

# File lib/samanage/api/custom_forms.rb, line 46
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: {}) click to toggle source
# File lib/samanage/api/category.rb, line 5
def get_categories(path: PATHS[:category], options: {})
  path = "categories.json"
  self.execute(path: path, options: options)
end
get_changes(path: PATHS[:change], options: {}) click to toggle source

Default get change path

# File lib/samanage/api/changes.rb, line 6
def get_changes(path: PATHS[:change], options: {})
  path = "changes.json?"
  self.execute(path: path, options: options)
end
get_comments(incident_id:) click to toggle source

Find comments given incident_id

# File lib/samanage/api/comments.rb, line 6
def get_comments(incident_id:)
  path = "incidents/#{incident_id}/comments.json"
  self.execute(path: path)
end
Also aliased as: comments
get_configuration_items(path: PATHS[:configuration_item], options: {}) click to toggle source

Default get configuration_item path

# File lib/samanage/api/configuration_items.rb, line 6
def get_configuration_items(path: PATHS[:configuration_item], options: {})
  path = "configuration_items.json?"
  self.execute(path: path, options: options)
end
get_contracts(path: PATHS[:contract], options: {}) click to toggle source

Get contract default path

# File lib/samanage/api/contracts.rb, line 6
def get_contracts(path: PATHS[:contract], options: {})
  path = "contracts.json?"
  self.execute(path: path, options: options)
end
get_custom_fields(path: PATHS[:custom_fields], options: {}) click to toggle source

Get custom fields default url

# File lib/samanage/api/custom_fields.rb, line 6
def get_custom_fields(path: PATHS[:custom_fields], options: {})
  path = "custom_fields.json?"
  self.execute(path: path, options: options)
end
get_custom_forms(path: PATHS[:custom_forms], options: {}) click to toggle source

Get custom forms path

# File lib/samanage/api/custom_forms.rb, line 6
def get_custom_forms(path: PATHS[:custom_forms], options: {})
  self.execute(path: path)
end
get_departments(path: PATHS[:department], options: {}) click to toggle source
# File lib/samanage/api/departments.rb, line 5
def get_departments(path: PATHS[:department], options: {})
  path = "departments.json?"
  self.execute(path: path, options: options)
end
get_groups(path: PATHS[:group], options: {}) click to toggle source
# File lib/samanage/api/groups.rb, line 5
def get_groups(path: PATHS[:group], options: {})
  path = "groups.json?"
  self.execute(path: path, options: options)
end
get_hardwares(path: PATHS[:hardware], options: {}) click to toggle source

Get hardware default path

# File lib/samanage/api/hardwares.rb, line 6
def get_hardwares(path: PATHS[:hardware], options: {})
  path = "hardwares.json?"
  self.execute(path: path, options: options)
end
get_incidents(path: PATHS[:incident], options: {}) click to toggle source

Default get incident path

# File lib/samanage/api/incidents.rb, line 6
def get_incidents(path: PATHS[:incident], options: {})
  execute(path: path, options: options)
end
get_mobiles(path: PATHS[:mobile], options: {}) click to toggle source

Get mobile default path

# File lib/samanage/api/mobiles.rb, line 6
def get_mobiles(path: PATHS[:mobile], options: {})
  path = "mobiles.json?"
  self.execute(path: path, options: options)
end
get_other_assets(path: PATHS[:other_asset], options: {}) click to toggle source

Default get other_assets path

# File lib/samanage/api/other_assets.rb, line 6
def get_other_assets(path: PATHS[:other_asset], options: {})
  path = "other_assets.json?"
  self.execute(path: path, options: options)
end
get_problems(path: PATHS[:problem], options: {}) click to toggle source

Default get problem path

# File lib/samanage/api/problems.rb, line 6
def get_problems(path: PATHS[:problem], options: {})
  path = "problems.json?"
  self.execute(path: path, options: options)
end
get_purchase_orders(path: PATHS[:purchase_order], options: {}) click to toggle source

Default get purchase_order path

# File lib/samanage/api/purchase_orders.rb, line 6
def get_purchase_orders(path: PATHS[:purchase_order], options: {})
  path = "purchase_orders.json?"
  self.execute(path: path, options: options)
end
get_releases(path: PATHS[:release], options: {}) click to toggle source

Default get releases path

# File lib/samanage/api/releases.rb, line 6
def get_releases(path: PATHS[:release], options: {})
  path = "releases.json?"
  self.execute(path: path, options: options)
end
get_requester_id(value:) click to toggle source

Get requester from value (email)

# File lib/samanage/api/requester.rb, line 25
def get_requester_id(value:)
  api_call = self.execute(path: "requesters.json?name=#{value}")
  api_call[:data].size == 1 ? api_call[:data][0] : nil
end
get_requesters(path: PATHS[:requester], options: {}) click to toggle source
# File lib/samanage/api/requester.rb, line 5
def get_requesters(path: PATHS[:requester], options: {})
  self.execute(path: path, options: options)
end
get_sites(path: PATHS[:site], options: {}) click to toggle source
# File lib/samanage/api/sites.rb, line 5
def get_sites(path: PATHS[:site], options: {})
  path = "sites.json?"
  self.execute(path: path, options: options)
end
get_solutions(path: PATHS[:solution], options: {}) click to toggle source
# File lib/samanage/api/solutions.rb, line 5
def get_solutions(path: PATHS[:solution], options: {})
  path = "solutions.json?"
  self.execute(path: path, options: options)
end
get_tasks(path: PATHS[:task], options: {}) click to toggle source
# File lib/samanage/api/tasks.rb, line 5
def get_tasks(path: PATHS[:task], options: {})
  path = "tasks.json?"
  self.execute(path: path, options: options)
end
get_users(path: PATHS[:user], options: {}) click to toggle source

Get users, using URL builder

# File lib/samanage/api/users.rb, line 6
def get_users(path: PATHS[:user], options: {})
  path = "users.json?"
  self.execute(path: path, options: options)
end
get_vendors(path: PATHS[:vendor], options: {}) click to toggle source
# File lib/samanage/api/vendors.rb, line 5
def get_vendors(path: PATHS[:vendor], options: {})
  path = "vendors.json?"
  self.execute(path: path, options: options)
end
groups(options: {})
Alias for: collect_groups
hardwares(options: {})
Alias for: collect_hardwares
incidents(options: {})
Alias for: collect_incidents
list_admins() click to toggle source

Return all admins in the account

# File lib/samanage/api.rb, line 173
def list_admins
  admin_role_id = execute(path: "roles.json")[:data]
    .find { |role| role["name"] == "Administrator" }
    .dig("id")
  admin_path = "users.json?role=#{admin_role_id}"
  admins.push(
    execute(path: admin_path)[:data]
      .map { |u| u["email"] }
  ).flatten
end
mobiles(options: {})
Alias for: collect_mobiles
organize_forms() click to toggle source

Set forms by type and map fields

# File lib/samanage/api/custom_forms.rb, line 30
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
other_assets(options: {})
problems(options: {})
Alias for: collect_problems
purchase_orders(options: {})
releases(options: {})
Alias for: collect_releases
requesters(options: {page: 1}, requesters: [], path: PATHS[:requester])
Alias for: collect_requesters
send_activation_email(email:) click to toggle source
# File lib/samanage/api/utils.rb, line 9
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
  options = {
    send_activation_email: 1,
    add_callbacks: 1
  }
  self.execute(http_method: "put", path: "users/#{user_id}.json", options: options)
end
set_params(options:) click to toggle source
# File lib/samanage/api.rb, line 167
def set_params(options:)
  options[:audit_archive] = options[:audit_archive] || options[:audit_archives] if options[:audit_archives]
  URI.encode_www_form(options.except(:verbose))
end
sites(options: {})
Alias for: collect_sites
solutions(options: {})
Alias for: collect_solutions
tasks(options: {})
Alias for: collect_tasks
time_tracks(incident_id:) click to toggle source
# File lib/samanage/api/time_tracks.rb, line 5
def time_tracks(incident_id:)
  self.execute(path: "incidents/#{incident_id}/time_tracks.json")[:data]
end
update_change(payload:, id:, options: {}) click to toggle source

Update an change given id and json

# File lib/samanage/api/changes.rb, line 51
def update_change(payload:, id:, options: {})
  path = "changes/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_configuration_item(payload:, id:, options: {}) click to toggle source

Update an configuration_item given id and json

# File lib/samanage/api/configuration_items.rb, line 51
def update_configuration_item(payload:, id:, options: {})
  path = "configuration_items/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_contract(payload:, id:, options: {}) click to toggle source

Update contract given id

# File lib/samanage/api/contracts.rb, line 48
def update_contract(payload:, id:, options: {})
  path = "contracts/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_hardware(payload:, id:, options: {}) click to toggle source

Update hardware given id

# File lib/samanage/api/hardwares.rb, line 55
def update_hardware(payload:, id:, options: {})
  path = "hardwares/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_incident(payload:, id:, options: {}) click to toggle source

Update an incident given id and json

# File lib/samanage/api/incidents.rb, line 54
def update_incident(payload:, id:, options: {})
  path = "incidents/#{id}.json?"
  execute(path: path, http_method: "put", payload: payload, options: options)
end
update_mobile(payload:, id:, options: {}) click to toggle source

Update mobile given id

# File lib/samanage/api/mobiles.rb, line 48
def update_mobile(payload:, id:, options: {})
  path = "mobiles/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_other_asset(payload:, id:, options: {}) click to toggle source

Update other_asset given json and id

# File lib/samanage/api/other_assets.rb, line 45
def update_other_asset(payload:, id:, options: {})
  path = "other_assets/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_problem(payload:, id:, options: {}) click to toggle source

Update an problem given id and json

# File lib/samanage/api/problems.rb, line 51
def update_problem(payload:, id:, options: {})
  path = "problems/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_purchase_order(payload:, id:, options: {}) click to toggle source

Update an purchase_order given id and json

# File lib/samanage/api/purchase_orders.rb, line 51
def update_purchase_order(payload:, id:, options: {})
  path = "purchase_orders/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_release(payload:, id:, options: {}) click to toggle source

Update release given json and id

# File lib/samanage/api/releases.rb, line 45
def update_release(payload:, id:, options: {})
  path = "releases/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_solution(id:, payload:, options: {}) click to toggle source
# File lib/samanage/api/solutions.rb, line 38
def update_solution(id:, payload:, options: {})
  self.execute(path: "solutions/#{id}.json", http_method: "put", payload: payload)
end
update_time_track(incident_id:, time_track_id:, payload:) click to toggle source
# File lib/samanage/api/time_tracks.rb, line 13
def update_time_track(incident_id:, time_track_id:, payload:)
  self.execute(path: "incidents/#{incident_id}/time_tracks.json")
end
update_user(payload:, id:) click to toggle source

Update user by id

# File lib/samanage/api/users.rb, line 75
def update_user(payload:, id:)
  path = "users/#{id}.json"
  self.execute(path: path, http_method: "put", payload: payload)
end
update_vendor(id:, payload:, options: {}) click to toggle source
# File lib/samanage/api/vendors.rb, line 32
def update_vendor(id:, payload:, options: {})
  self.execute(path: "vendors/#{id}.json", http_method: "put", payload: payload)
end
users(options: {})
Alias for: collect_users
vendors(options: {})
Alias for: collect_vendors