Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Account for the latest SN API changes. #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
*.gem
*.rbc
.bundle
Expand Down
9 changes: 4 additions & 5 deletions lib/classes/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ def self.configure(auth_hash = {})
end

def self.get_resource(query_hash = {}, displayvalue = false, table)
# to be filled in
RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_action=getRecords&sysparm_query=#{hash_to_query(query_hash)}&displayvalue=#{displayvalue}"), $username, $password)
RestClient::Resource.new(URI.escape($root_url + "/api/now/v1/table/#{table}?sysparm_display_value=true&sysparm_exclude_reference_link=true&sysparm_query=#{hash_to_query(query_hash)}&displayvalue=#{displayvalue}"), {user: $username, password: $password, headers: {accept: 'application/json'}})
end

def self.post_resource(table)
RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_action=insert"), $username, $password)
RestClient::Resource.new(URI.escape($root_url + "/api/now/v1/table/#{table}?sysparm_input_display_value=true&sysparm_display_value=true&sysparm_exclude_reference_link=true"), {user: $username, password: $password, headers: {accept: 'application/json', content_type: 'application/json'}})
end

def self.update_resource(incident_number, table)
RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_query=number=#{incident_number}&sysparm_action=update"), $username, $password)
def self.update_resource(incident_sys_id, table)
RestClient::Resource.new(URI.escape($root_url + "/api/now/v1/table/#{table}/#{incident_sys_id}?sysparm_input_display_value=true&sysparm_display_value=true&sysparm_exclude_reference_link=true"), {user: $username, password: $password, headers: {accept: 'application/json', content_type: 'application/json'}})
end

private
Expand Down
187 changes: 94 additions & 93 deletions lib/classes/incident.rb
Original file line number Diff line number Diff line change
@@ -1,105 +1,106 @@
module ServiceNow
class Incident
class Incident

def inspect
@attributes.each do |k, v|
puts "#{k} => #{v}"
end
end
def inspect
@attributes.each do |k, v|
puts "#{k} => #{v}"
end
end

def initialize(attributes = {}, saved_on_sn = false, internal_call = false)
Incident.check_configuration
symbolized_attributes = Hash[attributes.map{|k, v| [k.to_sym, v]}]
if !symbolized_attributes[:number].nil? && !internal_call # allow setting INC number if it's called internally
raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
end
@attributes = symbolized_attributes
@saved_on_sn = saved_on_sn
end

def attributes
@attributes
end
def initialize(attributes = {}, saved_on_sn = false, internal_call = false)
Incident.check_configuration
symbolized_attributes = Hash[attributes.map { |k, v| [k.to_sym, v] }]
if !symbolized_attributes[:number].nil? && !internal_call # allow setting INC number if it's called internally
raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
end
@attributes = symbolized_attributes
@saved_on_sn = saved_on_sn
end

def client # must be used only when displayvable is false
return User.find_by_sys_id(self.caller_id)
end
def attributes
@attributes
end

def method_missing(method, args = nil)
method_name = method.to_s
if match = method_name.match(/(.*)=/) # writer method
attribute = match[1]
if attribute == "number" && @saved_on_sn
raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
end
@attributes[attribute.to_sym] = args
else # reader method
@attributes[method_name.to_sym]
end
end
def client # must be used only when displayvable is false
return User.find_by_sys_id(self.caller_id)
end

def save!
# if this is a new incident (still in memory and not on SN), and the user set the Incident number
# we raise an exception
if !@attributes[:number].nil? && !@saved_on_sn
raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
end
# we only create new incidents if it's not saved already
if !@saved_on_sn
response = Configuration.post_resource(table = "incident").post(self.attributes.to_json)
else
response = Configuration.update_resource(self.number, table = "incident").post(self.attributes.to_json)
end
hash = JSON.parse(response, { :symbolize_names => true })
# this is the object
# and there is always only one
# since we're creating or updating
inc_object = hash[:records][0]
inc_object.each do |key, value|
key_name = key.to_s
eval("self.#{key_name} = value")
end
@saved_on_sn = true
self
def method_missing(method, args = nil)
method_name = method.to_s
if match = method_name.match(/(.*)=/) # writer method
attribute = match[1]
if attribute == "number" && @saved_on_sn
raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
end
@attributes[attribute.to_sym] = args
else # reader method
@attributes[method_name.to_sym]
end
end

def self.find(inc_number)
Incident.check_configuration
inc_string = inc_number.to_s.match(/[123456789]+\d*$/).to_s
if inc_string.length > 7
raise "SN::Error: invalid Incident number"
end
query_hash = {}
query_hash[:number] = "INC" + "0"*(7-inc_string.length) + inc_string
response = Configuration.get_resource(query_hash, table = "incident").get();
# returned hash
hash = JSON.parse(response, { :symbolize_names => true })
# return the Incident object
inc_obj = Incident.new(attributes = hash[:records][0], saved_on_sn = true, internal_call = true)
if inc_obj.attributes.nil?
"SN::Alert: No incident with incident number #{query_hash[:number]} found"
else
inc_obj
end
end
def save!
# if this is a new incident (still in memory and not on SN), and the user set the Incident number
# we raise an exception
if !@attributes[:number].nil? && !@saved_on_sn
raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
end
# we only create new incidents if it's not saved already
if !@saved_on_sn
response = Configuration.post_resource(table = "incident").post(self.attributes.compact.to_json)
else
@saved_on_sn = false # Allow the number to be set again from the data pulled back
response = Configuration.update_resource(self.sys_id, table = "incident").patch(self.attributes.compact.to_json)
end
hash = JSON.parse(response, {:symbolize_names => true})
# this is the object
# and there is always only one
# since we're creating or updating
inc_object = hash[:result]
inc_object.each do |key, value|
key_name = key.to_s
eval("self.#{key_name} = value")
end
@saved_on_sn = true
self
end

def self.where(query_hash = {})
Incident.check_configuration
response = Configuration.get_resource(query_hash, table = "incident").get();
hash = JSON.parse(response, { :symbolize_names => true })
array_of_records = hash[:records]
array_of_inc = []
array_of_records.each do |record|
array_of_inc << Incident.new(attributes = record, saved_on_sn = true, internal_call = true)
end
array_of_inc
end
def self.find(inc_number)
Incident.check_configuration
inc_string = inc_number.to_s.match(/[123456789]+\d*$/).to_s
if inc_string.length > 7
raise "SN::Error: invalid Incident number"
end
query_hash = {}
query_hash[:number] = "INC" + "0"*(7-inc_string.length) + inc_string
response = Configuration.get_resource(query_hash, table = "incident").get();
# returned hash
hash = JSON.parse(response, {:symbolize_names => true})
# return the Incident object
inc_obj = Incident.new(attributes = hash[:result][0], saved_on_sn = true, internal_call = true)
if inc_obj.attributes.nil?
"SN::Alert: No incident with incident number #{query_hash[:number]} found"
else
inc_obj
end
end

def self.where(query_hash = {})
Incident.check_configuration
response = Configuration.get_resource(query_hash, table = "incident").get();
hash = JSON.parse(response, {:symbolize_names => true})
array_of_records = hash[:result]
array_of_inc = []
array_of_records.each do |record|
array_of_inc << Incident.new(attributes = record, saved_on_sn = true, internal_call = true)
end
array_of_inc
end

private
def self.check_configuration
if $root_url.nil? || $username.nil? || $password.nil?
raise "SN::Error: You have not configured yet, please run ServiceNow::Configuration.configure() first"
end
end
private
def self.check_configuration
if $root_url.nil? || $username.nil? || $password.nil?
raise "SN::Error: You have not configured yet, please run ServiceNow::Configuration.configure() first"
end
end
end
end
6 changes: 3 additions & 3 deletions lib/classes/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def self.find(netid)
response = Configuration.get_resource(query_hash = query_hash, table = "sys_user").get()
hash = JSON.parse(response, { :symbolize_names => true })
# there should be only one
user = User.new(hash[:records][0])
user = User.new(hash[:result][0])
if user.attributes.nil?
puts "SN::Alert: No user with netID: #{netid} found"
return User.new
Expand All @@ -31,7 +31,7 @@ def self.find_by_sys_id(sys_id)
query_hash[:sys_id] = sys_id
response = Configuration.get_resource(query_hash = query_hash, table = "sys_user").get()
hash = JSON.parse(response, { :symbolize_names => true })
user = User.new(hash[:records][0])
user = User.new(hash[:result][0])
if user.attributes.nil?
puts "SN::Alert: No user with sys_id: #{sys_id} found"
return User.new
Expand All @@ -46,7 +46,7 @@ def self.find_by_name(name)
query_hash[:name] = name
response = Configuration.get_resource(query_hash = query_hash, table = "sys_user").get()
hash = JSON.parse(response, { :symbolize_names => true })
user = User.new(hash[:records][0])
user = User.new(hash[:result][0])
if user.attributes.nil?
puts "SN::Alert: No user with user_name: #{name} found"
return User.new
Expand Down