Class: Floe::ServiceNow::Cmdb

Inherits:
Methods
  • Object
show all
Defined in:
lib/floe/servicenow/cmdb.rb

Class Method Summary collapse

Class Method Details

.create_ci(params, secrets, _context) ⇒ Object

Create a new Configuration Item



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/floe/servicenow/cmdb.rb', line 69

def self.create_ci(params, secrets, _context)
  error   = verify_credentials(secrets)
  error ||= verify_create_ci_params(params)
  return ServiceNow.error!({}, :cause => error) if error

  table = params["table"] || "cmdb_ci"

  http_params = build_http_params(
    "POST",
    "/api/now/table/#{table}",
    params,
    secrets,
    :body => params.except("instance_id", "table")
  )

  http_request(http_params, {}, {})
end

.create_ci_relationship(params, secrets, _context) ⇒ Object

Create a CI relationship



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/floe/servicenow/cmdb.rb', line 179

def self.create_ci_relationship(params, secrets, _context)
  error   = verify_credentials(secrets)
  error ||= verify_create_ci_relationship_params(params)
  return ServiceNow.error!({}, :cause => error) if error

  body = {
    "parent"              => params["parent_sys_id"],
    "child"               => params["child_sys_id"],
    "type"                => params["relationship_type"],
    "connection_strength" => params["connection_strength"] || "1"
  }

  http_params = build_http_params(
    "POST",
    "/api/now/table/cmdb_rel_ci",
    params,
    secrets,
    :body => body
  )

  http_request(http_params, {}, {})
end

.delete_ci(params, secrets, _context) ⇒ Object

Delete a Configuration Item



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/floe/servicenow/cmdb.rb', line 110

def self.delete_ci(params, secrets, _context)
  error   = verify_credentials(secrets)
  error ||= verify_delete_ci_params(params)
  return ServiceNow.error!({}, :cause => error) if error

  table = params["table"] || "cmdb_ci"
  sys_id = params["sys_id"]

  http_params = build_http_params(
    "DELETE",
    "/api/now/table/#{table}/#{sys_id}",
    params,
    secrets
  )

  http_request(http_params, {}, {})
end

.get_ci(params, secrets, _context) ⇒ Object

Get a Configuration Item (CI) by sys_id



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/floe/servicenow/cmdb.rb', line 9

def self.get_ci(params, secrets, _context)
  error   = verify_credentials(secrets)
  error ||= verify_get_ci_params(params)
  return ServiceNow.error!({}, :cause => error) if error

  table = params["table"] || "cmdb_ci"
  sys_id = params["sys_id"]

  http_params = build_http_params(
    "GET",
    "/api/now/table/#{table}/#{sys_id}",
    params,
    secrets
  )

  http_request(http_params, {}, {})
end

.get_ci_classes(params, secrets, _context) ⇒ Object

Get CI classes (types)



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/floe/servicenow/cmdb.rb', line 203

def self.get_ci_classes(params, secrets, _context)
  error   = verify_credentials(secrets)
  error ||= verify_instance_id(params)
  return ServiceNow.error!({}, :cause => error) if error

  query_params = {
    "sysparm_query"  => "nameSTARTSWITHcmdb_ci",
    "sysparm_fields" => "name,label,super_class"
  }
  query_params["sysparm_limit"] = params["limit"] if params["limit"]

  http_params = build_http_params(
    "GET",
    "/api/now/table/sys_db_object",
    params,
    secrets,
    :query => query_params
  )

  http_request(http_params, {}, {})
end

.get_ci_relationships(params, secrets, _context) ⇒ Object

Get CI relationships



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/floe/servicenow/cmdb.rb', line 129

def self.get_ci_relationships(params, secrets, _context)
  error   = verify_credentials(secrets)
  error ||= verify_get_ci_relationships_params(params)
  return ServiceNow.error!({}, :cause => error) if error

  sys_id = params["sys_id"]

  # Get CI instance
  query_params = {
    "sysparm_display_value"          => "true",
    "sysparm_exclude_reference_link" => "true"
  }

  http_params = build_http_params(
    "GET",
    "/api/now/cmdb/instance/cmdb_ci/#{sys_id}",
    params,
    secrets,
    :query => query_params
  )

  ci_response = http_request(http_params, {}, {})
  return ci_response unless ci_response["success"]

  # Get relationships
  rel_query_params = {
    "sysparm_query"         => "parent=#{sys_id}^ORchild=#{sys_id}",
    "sysparm_display_value" => "true"
  }

  rel_http_params = build_http_params(
    "GET",
    "/api/now/table/cmdb_rel_ci",
    params,
    secrets,
    :query => rel_query_params
  )

  rel_response = http_request(rel_http_params, {}, {})
  return rel_response unless rel_response["success"]

  # Merge results
  ci_data = ci_response["output"]["result"]
  rel_data = rel_response["output"]["result"]
  output = ci_data.merge("relationships" => rel_data)

  ServiceNow.success!({}, :output => output)
end

.query_cis(params, secrets, _context) ⇒ Object

Query Configuration Items with optional filters



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/floe/servicenow/cmdb.rb', line 28

def self.query_cis(params, secrets, _context)
  error   = verify_credentials(secrets)
  error ||= verify_query_cis_params(params)
  return ServiceNow.error!({}, :cause => error) if error

  instance_id = params["instance_id"]
  table       = params["table"] || "cmdb_ci"
  username    = secrets["username"]
  password    = secrets["password"]

  require "base64"
  authorization = "Basic #{::Base64.urlsafe_encode64("#{username}:#{password}")}"

  headers = {
    "Authorization" => authorization,
    "Content-Type"  => "application/json",
    "Accept"        => "application/json"
  }

  query_params = {}
  query_params["sysparm_query"]  = params["query"]  if params["query"]
  query_params["sysparm_limit"]  = params["limit"]  if params["limit"]
  query_params["sysparm_offset"] = params["offset"] if params["offset"]
  query_params["sysparm_fields"] = params["fields"] if params["fields"]

  http_params = {
    "Method"          => params["method"],
    "Url"             => "https://#{instance_id}.service-now.com/api/now/table/#{table}",
    "Options"         => {"Encoding" => "JSON"},
    "Headers"         => headers,
    "QueryParameters" => query_params
  }

  begin
    http_request(http_params, {}, {})
  rescue => err
    ServiceNow.error!({}, :cause => err.to_s)
  end
end

.update_ci(params, secrets, _context) ⇒ Object

Update an existing Configuration Item



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/floe/servicenow/cmdb.rb', line 88

def self.update_ci(params, secrets, _context)
  error = verify_credentials(secrets)
  return ServiceNow.error!({}, :cause => error) if error

  error = verify_update_ci_params(params)
  return ServiceNow.error!({}, :cause => error) if error

  table = params["table"] || "cmdb_ci"
  sys_id = params["sys_id"]

  http_params = build_http_params(
    "PATCH",
    "/api/now/table/#{table}/#{sys_id}",
    params,
    secrets,
    :body => params.except("sys_id", "instance_id", "table")
  )

  http_request(http_params, {}, {})
end