-
Notifications
You must be signed in to change notification settings - Fork 898
/
miq_ae_service.rb
471 lines (376 loc) · 11.3 KB
/
miq_ae_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
require_relative 'miq_ae_service_model_legacy'
module MiqAeMethodService
class Deprecation < Vmdb::Deprecation
def self.default_log
$miq_ae_logger
end
end
class MiqAeServiceFront
include DRbUndumped
def find(id)
MiqAeService.find(id)
end
end
class MiqAeService
include Vmdb::Logging
include DRbUndumped
include MiqAeServiceModelLegacy
@@id_hash = {}
@@current = []
def self.current
@@current.last
end
def self.find(id)
@@id_hash[id.to_i]
end
def self.add(obj)
@@id_hash[obj.object_id] = obj
@@current << obj
end
def self.destroy(obj)
@@id_hash.delete(obj.object_id)
@@current.delete(obj)
end
def initialize(ws)
@drb_server_references = []
@inputs = {}
@workspace = ws
@preamble_lines = 0
@body = []
@persist_state_hash = ws.persist_state_hash
self.class.add(self)
end
def destroy
self.class.destroy(self)
end
def body=(data)
@body_raw = data
@body = begin
lines = []
@body_raw.each_line { |l| lines << l.rstrip }
lines
end
end
def body
@body_raw
end
def preamble
@preamble_raw
end
def preamble=(data)
@preamble_raw = data
@preamble = begin
lines = []
@preamble_raw.each_line { |l| lines << l.rstrip }
lines
end
@preamble_lines = @preamble.length
end
def method_body(options = {})
if options[:line_numbers]
line_number = 0
@body.collect do |line|
line_number += 1
"#{format "%03d" % line_number}: #{line}"
end
else
@body
end
end
def backtrace(callers)
return callers unless callers.respond_to?(:collect)
callers.collect do |c|
file, line, context = c.split(':')
if file == "-"
line_adjusted_for_preamble = line.to_i - @preamble_lines
file = @body[line_adjusted_for_preamble - 1].to_s.strip
"<code: #{file}>:#{line_adjusted_for_preamble}:#{context}"
else
c
end
end
end
def disconnect_sql
ActiveRecord::Base.connection_pool.release_connection
end
attr_writer :inputs
attr_reader :inputs
####################################################
def log(level, msg)
$miq_ae_logger.send(level, "<AEMethod #{current_method}> #{msg}")
end
def set_state_var(name, value)
@persist_state_hash[name] = value
end
def state_var_exist?(name)
@persist_state_hash.key?(name)
end
def get_state_var(name)
@persist_state_hash[name]
end
def instantiate(uri)
obj = @workspace.instantiate(uri, @workspace.ae_user, @workspace.current_object)
return nil if obj.nil?
drb_return(MiqAeServiceObject.new(obj, self))
rescue => e
return nil
end
def drb_return(obj)
# Save a reference to the object, so that we control when it gets deleted. Otherwise, Ruby Garbage Collection may remove it prematurely.
# If it is removed prematurely and then referenced by the method, we get a DRb recycled object error
@drb_server_references << obj
obj
end
def object(path = nil)
obj = @workspace.get_obj_from_path(path)
return nil if obj.nil?
drb_return MiqAeServiceObject.new(obj, self)
end
def hash_to_query(hash)
MiqAeEngine::MiqAeUri.hash2query(hash)
end
def query_to_hash(query)
MiqAeEngine::MiqAeUri.query2hash(query)
end
def current_namespace
@workspace.current_namespace
end
def current_class
@workspace.current_class
end
def current_instance
@workspace.current_instance
end
def current_message
@workspace.current_message
end
def current_object
@current_object ||= drb_return(MiqAeServiceObject.new(@workspace.current_object, self))
end
def current_method
@workspace.current_method
end
def current
current_object
end
def root
@root_object ||= object("/")
end
def parent
@parent_object ||= object("..")
end
def objects(aobj)
aobj.collect do |obj|
obj = drb_return(MiqAeServiceObject.new(obj, self)) unless obj.kind_of?(MiqAeServiceObject)
obj
end
end
def vmdb(model_name, *args)
service = service_model(model_name)
args.empty? ? service : service.find(*args)
end
def service_model(model_name)
"MiqAeMethodService::MiqAeService#{model_name}".constantize
rescue NameError
service_model_lookup(model_name)
end
def datastore
end
def ldap
end
CUSTOMER_ROOT = File.expand_path(File.join(Rails.root, "..", "customer"))
$:.push CUSTOMER_ROOT
def new_object(what, *args)
begin
require what.underscore
rescue LoadError => err
_log.warn("Error requiring <#{what}> from #{CUSTOMER_ROOT} because <#{err.message}>")
return nil
end
begin
klass = what.constantize
rescue NameError => err
_log.warn("Error converting <#{what}> to a constant because <#{err.message}>")
ruby_file = File.join(CUSTOMER_ROOT, "#{what.underscore}.rb")
contents = File.read(ruby_file) rescue nil
_log.warn("Contents of Customer Library <#{ruby_file}> are:\n#{contents}")
return nil
end
klass.send(:include, DRbUndumped) unless klass.ancestors.include?(DRbUndumped)
drb_return klass.new(*args)
end
def execute(m, *args)
drb_return MiqAeServiceMethods.send(m, *args)
rescue NoMethodError => err
raise MiqAeException::MethodNotFound, err.message
end
def instance_exists?(path)
_log.info "<< path=#{path.inspect}"
__find_instance_from_path(path) ? true : false
end
def instance_create(path, values_hash = {})
_log.info "<< path=#{path.inspect}, values_hash=#{values_hash.inspect}"
return false unless editable_instance?(path)
ns, klass, instance = MiqAeEngine::MiqAePath.split(path)
$log.info("Instance Create for ns: #{ns} class #{klass} instance: #{instance}")
aec = MiqAeClass.find_by_namespace_and_name(ns, klass)
return false if aec.nil?
aei = aec.ae_instances.detect { |i| instance.casecmp(i.name) == 0 }
return false unless aei.nil?
aei = MiqAeInstance.create(:name => instance, :class_id => aec.id)
values_hash.each { |key, value| aei.set_field_value(key, value) }
true
end
def instance_get_display_name(path)
_log.info "<< path=#{path.inspect}"
aei = __find_instance_from_path(path)
aei ? aei.display_name : nil
end
def instance_set_display_name(path, display_name)
_log.info "<< path=#{path.inspect}, display_name=#{display_name.inspect}"
aei = __find_instance_from_path(path)
return false if aei.nil?
aei.update_attributes(:display_name => display_name)
true
end
def instance_update(path, values_hash)
_log.info "<< path=#{path.inspect}, values_hash=#{values_hash.inspect}"
return false unless editable_instance?(path)
aei = __find_instance_from_path(path)
return false if aei.nil?
values_hash.each { |key, value| aei.set_field_value(key, value) }
true
end
def instance_find(path, options = {})
_log.info "<< path=#{path.inspect}"
result = {}
ns, klass, instance = MiqAeEngine::MiqAePath.split(path)
aec = MiqAeClass.find_by_namespace_and_name(ns, klass)
unless aec.nil?
instance.gsub!(".", '\.')
instance.gsub!("*", ".*")
instance.gsub!("?", ".{1}")
instance_re = Regexp.new("^#{instance}$", Regexp::IGNORECASE)
aec.ae_instances.select { |i| instance_re =~ i.name }.each do |aei|
iname = if options[:path]
aei.fqname
else
aei.name
end
result[iname] = aei.field_attributes
end
end
result
end
def instance_get(path)
_log.info "<< path=#{path.inspect}"
aei = __find_instance_from_path(path)
return nil if aei.nil?
aei.field_attributes
end
def instance_delete(path)
_log.info "<< path=#{path.inspect}"
return false unless editable_instance?(path)
aei = __find_instance_from_path(path)
return false if aei.nil?
aei.destroy
true
end
def __find_instance_from_path(path)
dom, ns, klass, instance = MiqAeEngine::MiqAePath.get_domain_ns_klass_inst(path)
return false unless visible_domain?(dom)
aec = MiqAeClass.find_by_namespace_and_name("#{dom}/#{ns}", klass)
return nil if aec.nil?
aec.ae_instances.detect { |i| instance.casecmp(i.name) == 0 }
end
private
def editable_instance?(path)
dom, = MiqAeEngine::MiqAePath.get_domain_ns_klass_inst(path)
return false unless owned_domain?(dom)
domain = MiqAeDomain.find_by_fqname(dom, false)
return false unless domain
$log.warn "path=#{path.inspect} : is not editable" unless domain.editable?
domain.editable?
end
def owned_domain?(dom)
domains = @workspace.ae_user.current_tenant.ae_domains.collect(&:name).map(&:upcase)
return true if domains.include?(dom.upcase)
$log.warn "domain=#{dom} : is not editable"
false
end
def visible_domain?(dom)
domains = @workspace.ae_user.current_tenant.visible_domains.collect(&:name).map(&:upcase)
return true if domains.include?(dom.upcase)
$log.warn "domain=#{dom} : is not viewable"
false
end
end
module MiqAeServiceObjectCommon
def attributes
@object.attributes.each_with_object({}) do |(key, value), hash|
hash[key] = value.kind_of?(MiqAePassword) ? value.to_s : value
end
end
def attributes=(hash)
@object.attributes = hash
end
def [](attr)
value = @object[attr.downcase]
value = value.to_s if value.kind_of?(MiqAePassword)
value
end
def []=(attr, value)
@object[attr.downcase] = value
end
# To explicitly override Object#id method, which is spewing deprecation warnings to use Object#object_id
def id
@object ? @object.id : nil
end
def decrypt(attr)
MiqAePassword.decrypt_if_password(@object[attr.downcase])
end
def current_field_name
@object.current_field_name
end
def current_field_type
@object.current_field_type
end
def current_message
@object.current_message
end
def namespace
@object.namespace
end
def class_name
@object.klass
end
def instance_name
@object.instance
end
def name
@object.object_name
end
end
class MiqAeServiceObject
include MiqAeServiceObjectCommon
include DRbUndumped
def initialize(obj, svc)
raise "object cannot be nil" if obj.nil?
@object = obj
@service = svc
end
def children(name = nil)
objs = @object.children(name)
return nil if objs.nil?
objs = @service.objects([objs].flatten)
objs.length == 1 ? objs.first : objs
end
def to_s
name
end
def inspect
hex_id = (object_id << 1).to_s(16).rjust(14, '0')
"#<#{self.class.name}:0x#{hex_id} name: #{name.inspect}>"
end
end
end