lorj 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -0
  3. data/example/students_1/process/students.rb +1 -1
  4. data/example/students_1/student_v1.md +2 -0
  5. data/example/students_1/students.rb +2 -1
  6. data/example/students_2/process/students.rb +1 -1
  7. data/example/students_2/students.rb +4 -2
  8. data/example/students_3/controller/yaml_students_def.rb +1 -1
  9. data/example/students_3/process/students.rb +1 -1
  10. data/example/students_3/student_v3.md +5 -3
  11. data/example/students_3/students.rb +5 -3
  12. data/example/students_4/controller/yaml_students_def.rb +1 -1
  13. data/example/students_4/process/students/definition/students.rb +1 -1
  14. data/example/students_4/students.rb +5 -3
  15. data/example/students_5/controller/yaml_students.rb +1 -1
  16. data/example/students_5/process/students.rb +1 -1
  17. data/example/students_5/student_v5.md +4 -4
  18. data/example/students_5/students.rb +8 -7
  19. data/example/yaml_students/students.rb +4 -4
  20. data/lib/core/core.rb +110 -64
  21. data/lib/core/core_internal.rb +110 -15
  22. data/lib/core/core_object_data.rb +4 -4
  23. data/lib/core/core_process_setup.rb +3 -30
  24. data/lib/core/lorj_data.rb +1 -1
  25. data/lib/core/lorj_keypath.rb +3 -1
  26. data/lib/core/process.rb +67 -0
  27. data/lib/lorj/version.rb +2 -1
  28. data/lib/lorj.rb +3 -4
  29. data/lib/lorj_account.rb +6 -5
  30. data/lib/lorj_config.rb +3 -2
  31. data/lib/providers/templates/compute.rb +1 -1
  32. data/lorj-spec/controllers/mock/mock.rb +144 -0
  33. data/lorj-spec/data.yaml +0 -0
  34. data/lorj-spec/process/mock2_process.rb +48 -0
  35. data/lorj-spec/process/mock_process.rb +48 -0
  36. data/lorj-spec/providers/mock2/mock2.rb +0 -0
  37. data/lorj.gemspec +2 -4
  38. data/spec/05_lorj_keypath_spec.rb +78 -0
  39. data/spec/20_lorj_meta_spec.rb +1 -1
  40. data/spec/21_lorj_processes_spec.rb +115 -0
  41. metadata +27 -42
  42. data/lib/core_process/cloud/process/common.rb +0 -60
  43. data/lib/core_process/cloud/process/connection.rb +0 -92
  44. data/lib/core_process/cloud/process/external_network.rb +0 -90
  45. data/lib/core_process/cloud/process/flavor.rb +0 -97
  46. data/lib/core_process/cloud/process/images.rb +0 -99
  47. data/lib/core_process/cloud/process/internet_network.rb +0 -33
  48. data/lib/core_process/cloud/process/internet_server.rb +0 -29
  49. data/lib/core_process/cloud/process/keypairs.rb +0 -332
  50. data/lib/core_process/cloud/process/network.rb +0 -107
  51. data/lib/core_process/cloud/process/public_ip.rb +0 -102
  52. data/lib/core_process/cloud/process/router.rb +0 -267
  53. data/lib/core_process/cloud/process/rules.rb +0 -120
  54. data/lib/core_process/cloud/process/security_groups.rb +0 -120
  55. data/lib/core_process/cloud/process/server.rb +0 -126
  56. data/lib/core_process/cloud/process/server_log.rb +0 -34
  57. data/lib/core_process/cloud/process/subnetwork.rb +0 -96
  58. data/lib/core_process/cloud_process.rb +0 -30
  59. data/lib/prc_base_config.rb +0 -300
  60. data/lib/prc_core_config.rb +0 -1258
  61. data/lib/prc_section_config.rb +0 -90
  62. data/lib/providers/hpcloud/compute.rb +0 -105
  63. data/lib/providers/hpcloud/hpcloud.rb +0 -462
  64. data/lib/providers/hpcloud/network.rb +0 -115
  65. data/lib/providers/hpcloud/security_groups.rb +0 -68
  66. data/lib/providers/openstack/openstack.rb +0 -410
  67. data/lib/providers/openstack/openstack_create.rb +0 -205
  68. data/lib/providers/openstack/openstack_delete.rb +0 -28
  69. data/lib/providers/openstack/openstack_get.rb +0 -39
  70. data/lib/providers/openstack/openstack_process.rb +0 -26
  71. data/lib/providers/openstack/openstack_query.rb +0 -96
  72. data/lib/providers/openstack/openstack_update.rb +0 -35
  73. data/lib/rh.rb +0 -426
  74. data/spec/01_hash_rh_spec.rb +0 -282
  75. data/spec/02_prc_base_config_spec.rb +0 -227
  76. data/spec/03_prc_section_config_spec.rb +0 -196
  77. data/spec/04_prc_core_config_spec.rb +0 -325
@@ -57,6 +57,7 @@ module Lorj
57
57
  # - model : Basic model.
58
58
  def initialize_model
59
59
  { :def_class => BaseDefinition,
60
+ :processes => [],
60
61
  :process_class => nil, :process_class_name => nil,
61
62
  :controller_class => nil, :controller_class_name => nil }
62
63
  end
@@ -115,7 +116,7 @@ module Lorj
115
116
  if the_controller.include?('/')
116
117
  file = File.expand_path(the_controller)
117
118
  else
118
- file = controllerfile_from_default(the_controller)
119
+ file = controllerfile_from_default(model, the_controller)
119
120
  end
120
121
 
121
122
  return PrcLib.warning('Controller not loaded: Controller file '\
@@ -146,7 +147,7 @@ module Lorj
146
147
  the_controller_process['.rb'] = '_process.rb'
147
148
  file = File.expand_path(the_controller_process)
148
149
  else
149
- file = controllerfile_from_default(the_controller, '_process')
150
+ file = controllerfile_from_default(model, the_controller, '_process')
150
151
  end
151
152
 
152
153
  return Lorj.debug(2, 'Process not loaded: Process controller file '\
@@ -197,7 +198,7 @@ module Lorj
197
198
  #
198
199
  # * *Returns* :
199
200
  # - file : absolute file path.
200
- def controllerfile_from_default(the_controller, suffix = '')
201
+ def controllerfile_from_default(_model, the_controller, suffix = '')
201
202
  File.join(PrcLib.controller_path, the_controller,
202
203
  the_controller + suffix + '.rb')
203
204
  end
@@ -211,26 +212,120 @@ module Lorj
211
212
  #
212
213
  # * *Args* :
213
214
  # - +model+ : Application model loaded.
214
- # - +processes+ : Processes to load.
215
- # supports:
216
- # - nil => return []
217
- # - String/Symbol => return [String/Symbol]
218
- # - Array => return Array
215
+ # - +processes+ : Array of processes and controller to load
216
+ # - each element contains a Hash with:
217
+ # If you are using a process module, set the following:
218
+ # - :process_module : Name of the process module to load
219
+ #
220
+ # If you are not using a Process module, you need to set the following
221
+ # - :process_path : Path to a local process code.
222
+ # This path must contains at least 'process' subdir. And if needed
223
+ # a 'controllers' path
224
+ # - :process_name : Name of the local process
225
+ #
226
+ # Optionnally, you can set a controller name to use with the process.
227
+ # - :controller_name: Name of the controller to use.
228
+ # - :controller_path: Path to the controller file.
219
229
  #
220
230
  # * *Returns* :
221
231
  # - model : Application model loaded.
222
232
  def init_processes(model, processes)
223
- process_array = processes_as_array(processes)
233
+ processes.each do |a_process|
234
+ my_process = {}
224
235
 
225
- process_array.each do |a_process|
226
- a_process = a_process.to_s if a_process.is_a?(Symbol)
227
- unless load_process(model, a_process)
228
- PrcLib.warning("Process '%s' not properly loaded.", a_process)
236
+ if a_process.key?(:process_module)
237
+ my_process = _process_module_to_load(my_process, a_process)
238
+ else
239
+ my_process = _process_local_to_load(my_process, a_process)
229
240
  end
241
+
242
+ next if my_process.nil?
243
+
244
+ model[:processes] << my_process
245
+
246
+ _process_load(model, my_process)
230
247
  end
248
+
231
249
  model
232
250
  end
233
251
 
252
+ # high level function to load process
253
+ def _process_load(model, my_process)
254
+ if load_process(model, my_process[:process_path])
255
+ controller_class = my_process[:controller_path]
256
+ controller_class = my_process[:controller_name] if controller_class.nil?
257
+
258
+ init_controller(model, controller_class) if controller_class
259
+ else
260
+ PrcLib.warning("Process '%s' not properly loaded.",
261
+ my_process[:process_path])
262
+ end
263
+ end
264
+
265
+ # function prepare of loading a local process
266
+ def _process_local_to_load(my_process, a_process)
267
+ my_process[:process_path] = a_process[:process_path]
268
+
269
+ if a_process[:process_name].nil?
270
+ my_process[:process_name] = File.basename(a_process[:process_path])
271
+ else
272
+ my_process[:process_name] = a_process[:process_name]
273
+ end
274
+
275
+ if a_process[:controller_path]
276
+ my_process[:controller_path] = a_process[:controller_path]
277
+ else
278
+ my_process[:controller_name] = a_process[:controller_name]
279
+ end
280
+ my_process
281
+ end
282
+
283
+ # Function prepare of loading a module process.
284
+ def _process_module_to_load(my_process, a_process)
285
+ name = a_process[:process_module]
286
+
287
+ if name.nil?
288
+ PrcLib.warning(':process_module is empty. Process not properly loaded.')
289
+ return
290
+ end
291
+
292
+ name = name.to_s if name.is_a?(Symbol)
293
+
294
+ unless Lorj.processes.key?(name)
295
+ PrcLib.warning("Unable to find Process module '%s'. Process not "\
296
+ 'properly loaded.', name)
297
+ return
298
+ end
299
+
300
+ module_process = Lorj.processes[name]
301
+ my_process[:process_name] = name
302
+ my_process[:process_path] = module_process.process
303
+
304
+ if a_process[:controller_path]
305
+ my_process[:controller_path] = a_process[:controller_path]
306
+ return my_process
307
+ end
308
+
309
+ _process_module_set_ctr(my_process, module_process.controllers,
310
+ a_process[:controller_name])
311
+
312
+ my_process
313
+ end
314
+
315
+ def _process_module_set_ctr(my_process, controllers, controller_name)
316
+ return if controller_name.nil?
317
+
318
+ controller_path = controllers[controller_name]
319
+
320
+ if controller_path.nil?
321
+ PrcLib.warning("Controller '%s' was not found. Please check. The "\
322
+ 'process may not work.', controller_name)
323
+ return
324
+ end
325
+
326
+ my_process[:controller_path] = controller_path
327
+ end
328
+
234
329
  # Function analyzing the process class parameter
235
330
  # and return the list of processes in an
236
331
  # array of processes.
@@ -266,7 +361,7 @@ module Lorj
266
361
  if the_process.include?('/')
267
362
  file = File.expand_path(the_process)
268
363
  else
269
- file = processfile_from_default(the_process)
364
+ file = processfile_from_default(model, the_process)
270
365
  end
271
366
 
272
367
  return PrcLib.warning("Process file definition '%s' is missing. ",
@@ -311,7 +406,7 @@ module Lorj
311
406
  # * *Returns* :
312
407
  # - file : absolute file path composed by:
313
408
  # PrcLib.process_path/the_process_class + '.rb'
314
- def processfile_from_default(the_process_class)
409
+ def processfile_from_default(_model, the_process_class)
315
410
  File.join(PrcLib.process_path, the_process_class + '.rb')
316
411
  end
317
412
 
@@ -172,7 +172,7 @@ module Lorj
172
172
  if obj.is_a?(Symbol)
173
173
  object_type = obj
174
174
  obj = @params[object_type]
175
- @params[object_type] = nil
175
+ @params.delete(object_type)
176
176
  else
177
177
  object_data_delete(obj)
178
178
  end
@@ -236,8 +236,8 @@ module Lorj
236
236
 
237
237
  def type?(key)
238
238
  return nil unless @params.rh_exist?(key)
239
+ return :DataObject if @params[key].type == :object
239
240
  :data
240
- :DataObject if @params[key].type == :object
241
241
  end
242
242
 
243
243
  def to_s
@@ -327,10 +327,10 @@ module Lorj
327
327
  " framework data Object. Is a '%s'",
328
328
  obj.class unless obj.is_a?(Lorj::Data)
329
329
  if obj.type == :list
330
- @params.rh_set(nil, :query, obj.object_type?)
330
+ @params.rh_del(:query, obj.object_type?)
331
331
  else
332
332
  object_type = obj.object_type?
333
- @params[object_type] = nil
333
+ @params.delete(object_type)
334
334
  end
335
335
  end
336
336
 
@@ -118,23 +118,19 @@ module Lorj
118
118
  #
119
119
  # * *Args* :
120
120
  # - +ObjectType+ : Top object type to ask.
121
- # - +sAccountName+ : Optional. Account Name to load if you are using a
122
- # Lorj::Account Object.
123
121
  #
124
122
  # * *Returns* :
125
123
  # - nothing.
126
124
  #
127
125
  # * *Raises* :
128
126
  #
129
- def process_setup(sObjectType, sAccountName = nil)
127
+ def process_setup(sObjectType)
130
128
  unless PrcLib.model.meta_obj.rh_exist?(sObjectType)
131
129
  PrcLib.runtime_fail "Setup: '%s' not a valid object type."
132
130
  end
133
131
 
134
132
  setup_steps = _setup_load
135
133
 
136
- return nil unless _process_setup_init(sAccountName)
137
-
138
134
  Lorj.debug(2, "Setup is identifying account data to ask for '%s'",
139
135
  sObjectType)
140
136
  # Loop in dependencies to get list of data object to setup
@@ -147,35 +143,12 @@ module Lorj
147
143
 
148
144
  _setup_ask(setup_steps)
149
145
 
150
- PrcLib.info("Configuring account : '#{sAccountName}',"\
151
- " provider '#{config[:provider_name]}'")
146
+ PrcLib.info("Configuring account : '#{config[:name]}',"\
147
+ " provider '#{config[:provider]}'")
152
148
  end
153
149
 
154
150
  private
155
151
 
156
- # Internal function to initialize the account.
157
- #
158
- # return true if initialized, false otherwise.
159
- def _process_setup_init(sAccountName)
160
- return false unless sAccountName
161
-
162
- if @config.ac_load(sAccountName)
163
- if @config[:provider] != @config[:provider_name]
164
- s_ask = format("Account '%s' was configured with a different "\
165
- "provider '%s'.\nAre you sure to re-initialize this "\
166
- "account with '%s' provider instead? "\
167
- 'All data will be lost',
168
- sAccountName, @config[:provider],
169
- @config[:provider_name])
170
- PrcLib.fatal(0, 'Exited by user request.') unless agree(s_ask)
171
- @config.ac_new(sAccountName, config[:provider_name])
172
- end
173
- else
174
- @config.ac_new(sAccountName, config[:provider_name])
175
- end
176
- true
177
- end
178
-
179
152
  # Internal function to insert the data after several data to ask.
180
153
  #
181
154
  # * *Args* :
@@ -547,7 +547,7 @@ module Lorj
547
547
 
548
548
  def elem_exist?(*key)
549
549
  return true if key[0] == :object && @data.key?(key[0])
550
- return true if key[0] == :attrs && @data.rh_exist?(key)
550
+ return true if key[0] == :attrs && @data.rh_exist?(key)
551
551
  (@data.rh_lexist?(:attrs, key) == key.length + 1)
552
552
  end
553
553
 
@@ -113,7 +113,9 @@ module Lorj
113
113
  private
114
114
 
115
115
  def string_to_sarray(sKeyPath)
116
- if %r{[^\\/]?/[^/]} =~ sKeyPath || /:[^:\/]/ =~ sKeyPath
116
+ # rubocop: disable Style/RegexpLiteral
117
+ if %r{[^\\/]?/[^/]} =~ sKeyPath || %r{:[^:/]} =~ sKeyPath
118
+ # rubocop: enable Style/RegexpLiteral
117
119
  # keypath to interpret
118
120
  res = sKeyPath.split('/')
119
121
  res.each_index do |iIndex|
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ # Define process module management in Lorj
19
+ #
20
+ module Lorj
21
+ # This class defines a Process module
22
+ #
23
+ #
24
+ class ProcessResource
25
+ attr_reader :defaults_file, :data_file, :process, :name, :controllers
26
+
27
+ def initialize(name, path, props = {})
28
+ # Determine resources
29
+ name = name.to_s if name.is_a?(Symbol)
30
+
31
+ process_path = File.expand_path(File.join(path, 'process',
32
+ name + '_process.rb'))
33
+
34
+ return nil unless File.exist?(process_path)
35
+ @process = process_path
36
+ @name = name
37
+
38
+ controller_dir = 'controllers'
39
+ controller_dir = props[:controllers_dir] if props.key?(:controllers_dir)
40
+ controller_path = File.expand_path(File.join(path, controller_dir))
41
+
42
+ _identify_controllers(controller_path)
43
+
44
+ defaults_file = File.expand_path(File.join(path, 'defaults.yaml'))
45
+ @defaults_file = defaults_file if File.exist?(defaults_file)
46
+
47
+ data_file = File.expand_path(File.join(path, 'data.yaml'))
48
+ @data_file = data_file if File.exist?(data_file)
49
+
50
+ self
51
+ end
52
+
53
+ private
54
+
55
+ def _identify_controllers(controller_path)
56
+ @controllers = {}
57
+
58
+ Dir.foreach(controller_path) do |dir|
59
+ next if dir.match(/^\.\.?$/)
60
+
61
+ next unless File.exist?(File.join(controller_path, dir, dir + '.rb'))
62
+
63
+ @controllers[dir] = File.join(controller_path, dir, dir + '.rb')
64
+ end
65
+ end
66
+ end
67
+ end
data/lib/lorj/version.rb CHANGED
@@ -16,5 +16,6 @@
16
16
 
17
17
  # Lorj version
18
18
  module Lorj
19
- VERSION = '1.0.9'
19
+ VERSION = '1.0.10'
20
+ DATE = '2015-04-17'
20
21
  end
data/lib/lorj.rb CHANGED
@@ -23,12 +23,10 @@ require 'lorj/version'
23
23
 
24
24
  # To use it, add require 'lorj'
25
25
 
26
+ require 'config_layers' # ConfigLayers
27
+
26
28
  require 'prc.rb' # PrcLib Base module
27
- require 'rh.rb' # recursive Hash
28
29
  require 'logging.rb' # class PrcLib::Logging
29
- require 'prc_base_config.rb' # PRC::BaseConfig class
30
- require 'prc_section_config.rb' # PRC::SectionConfig class
31
- require 'prc_core_config.rb' # PRC::CoreConfig class
32
30
  require 'lorj_meta.rb' # PRC::Meta class - Application defaults
33
31
  require 'lorj_defaults.rb' # PRC::Defaults class - Application defaults
34
32
  require 'lorj_config.rb' # Lorj::Config class -
@@ -54,6 +52,7 @@ require 'core/lorj_basecontroller' # Lorj Lorj::BaseController object
54
52
  require 'core/lorj_keypath' # Lorj Lorj::BaseDefinition object
55
53
  require 'core/definition' # Lorj Process definition
56
54
  require 'core/definition_internal' # Lorj internal functions
55
+ require 'core/process' # Lorj Process Module feature
57
56
 
58
57
  # lorj module
59
58
  module Lorj
data/lib/lorj_account.rb CHANGED
@@ -139,7 +139,7 @@ module Lorj
139
139
  # - local : Represents the config.yaml located in ~/.forj
140
140
  # - account : Represents an Account data located in ~/.forj/accounts
141
141
  # - runtime : Represents the runtime in memory data settings.
142
- def initialize(config_name = nil)
142
+ def initialize(config_name = nil, latest_version = nil)
143
143
  config_layers = []
144
144
 
145
145
  # Application layer
@@ -149,11 +149,11 @@ module Lorj
149
149
  config_layers << define_controller_data_layer
150
150
 
151
151
  # Local Config layer
152
- local = define_local_layer
152
+ local = define_local_layer(latest_version)
153
153
  config_layers << local
154
154
 
155
155
  # Account config layer
156
- config_layers << define_account_layer
156
+ config_layers << define_account_layer(latest_version)
157
157
 
158
158
  # runtime Config layer
159
159
  config_layers << define_runtime_layer
@@ -650,9 +650,10 @@ module Lorj
650
650
  index
651
651
  end
652
652
 
653
- def define_account_layer
653
+ def define_account_layer(latest_version = nil)
654
654
  PRC::CoreConfig.define_layer(:name => 'account',
655
- :config => Lorj::AccountConfig.new,
655
+ :config => \
656
+ Lorj::AccountConfig.new(nil, latest_version),
656
657
  :file_set => true,
657
658
  :load => true, :save => true)
658
659
  end
data/lib/lorj_config.rb CHANGED
@@ -138,9 +138,10 @@ module Lorj
138
138
  :set => false, :load => true)
139
139
  end
140
140
 
141
- def define_local_layer
141
+ def define_local_layer(latest_version = nil)
142
142
  PRC::CoreConfig.define_layer(:name => 'local',
143
- :config => PRC::SectionConfig.new,
143
+ :config => \
144
+ PRC::SectionConfig.new(nil, latest_version),
144
145
  :load => true, :save => true)
145
146
  end
146
147
 
@@ -35,6 +35,6 @@ class Mycloud
35
35
  :auth_uri => @oForjAccount.get(:auth_uri),
36
36
  :project => @oForjAccount.get(:tenant),
37
37
  :compute_service => @oForjAccount.get(:compute)
38
- )
38
+ )
39
39
  end
40
40
  end
@@ -0,0 +1,144 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # This class describes how to process some actions, and will do everything prior
18
+ # this task to make it to work.
19
+
20
+ # This Mock controller keep the data in memory in hash/Array data.
21
+ class Mock
22
+ # mock do not need to use any mapping. It adapts itself to what the process
23
+ # has defined.
24
+ end
25
+
26
+ # This Mock controller keep the data in memory in hash/Array data.
27
+ class MockController
28
+ @@data = {} # rubocop: disable ClassVars
29
+
30
+ def create(sObjectType, hParams)
31
+ PrcLib.debug("Mock: create object '%s' with parameters (hdata) '%s'",
32
+ sObjectType, hParams[:hdata])
33
+
34
+ result = {}
35
+ hParams[].keys.each do |key|
36
+ next if key == :hdata
37
+ result[key] = hParams[key]
38
+ end
39
+ result.merge!(hParams[:hdata])
40
+
41
+ @@data[sObjectType] = [] unless @@data.key?(sObjectType)
42
+
43
+ array = @@data[sObjectType]
44
+
45
+ array.each do |value|
46
+ fail if value.key?(result[:name])
47
+ end
48
+ array << result
49
+
50
+ result[:id] = array.length - 1
51
+
52
+ # Typical code:
53
+ # ~ case sObjectType
54
+ # ~ when :public_ip
55
+ # Following function can be executed to ensure the object :connection exists
56
+ # ~ required?(hParams, :connection)
57
+ # ~ required?(hParams, :server)
58
+ # ~ ... CODE to create
59
+ # ~ else
60
+ # ~ controller_error "'%s' is not a valid object for 'create'", sObjectType
61
+ # ~ end
62
+ # The code should return some data
63
+ # This data will be encapsulated in Lorj::Data object.
64
+ # data will be extracted by the framework with the controller get_attr
65
+ # function and mapped.
66
+ PrcLib.debug("Mock: object '%s' = '%s' is created.", sObjectType, result)
67
+ result
68
+ end
69
+
70
+ # This function return a collection which have to provide:
71
+ # functions: [], length, each
72
+ # Used by network process.
73
+ def query(sObjectType, sQuery, hParams)
74
+ PrcLib.debug("Mock: query object '%s' with hdata '%s' using query '%s'",
75
+ sObjectType, hParams[:hdata], sQuery)
76
+
77
+ return [] unless @@data.key?(sObjectType)
78
+
79
+ result = []
80
+
81
+ @@data[sObjectType].each do |value|
82
+ elem = value
83
+ sQuery.each do |query_key, query_value|
84
+ elem = nil if !value.key?(query_key) || value[query_key] != query_value
85
+ end
86
+ result << elem if elem
87
+ end
88
+ result
89
+ end
90
+
91
+ def delete(sObjectType, hParams)
92
+ PrcLib.debug("Mock: delete object '%s' with hdata '%s'",
93
+ sObjectType, hParams[:hdata])
94
+ return nil unless @@data.key?(sObjectType)
95
+
96
+ return false if !hParams.exist?(sObjectType) || hParams[sObjectType].nil?
97
+ @@data[sObjectType].delete(hParams[sObjectType])
98
+ PrcLib.debug("Mock: object '%s' = '%s' is deleted.",
99
+ sObjectType, hParams[sObjectType])
100
+ true
101
+ end
102
+
103
+ def get(sObjectType, sUniqId, hParams)
104
+ PrcLib.debug("Mock: Get object '%s' = '%s' with hdata '%s'",
105
+ sObjectType, sUniqId, hParams[:hdata])
106
+ return nil unless @@data.key?(sObjectType)
107
+ @@data[sObjectType][sUniqId]
108
+ end
109
+
110
+ def get_attr(oControlerObject, key)
111
+ # This controller function read the data and
112
+ # extract the information requested by the framework.
113
+ # Those data will be mapped to the process data model.
114
+ # The key is an array, to get data from a level tree.
115
+ # [data_l1, data_l2, data_l3] => should retrieve data from structure like
116
+ # data[ data_l2[ data_l3 ] ]
117
+ attributes = oControlerObject
118
+
119
+ attributes.rh_get(key)
120
+ rescue => e
121
+ controller_error "Unable to map '%s'.\n%s", key, e.message
122
+ end
123
+
124
+ def set_attr(oControlerObject, key, value)
125
+ attributes = oControlerObject
126
+
127
+ attributes.rh_set(value, key)
128
+ rescue => e
129
+ controller_error "Unable to map '%s' on '%s'.\n%s",
130
+ key, sObjectType, e.message
131
+ end
132
+
133
+ def update(sObjectType, oObject, hParams)
134
+ PrcLib.debug("Mock: Update object '%s' = '%s' with hdata '%s'",
135
+ sObjectType, sUniqId, hParams[:hdata])
136
+ return false unless @@data.key?(sObjectType)
137
+
138
+ return false unless @@data[sObjectType][oObject[:id]].nil?
139
+ # Considered hash object is already updated.
140
+ # This action emule the object save which doesn't make sense in this empty
141
+ # Mock controller.
142
+ true
143
+ end
144
+ end
File without changes
@@ -0,0 +1,48 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # Students process
18
+ class StudentsProcess
19
+ def create_student(sObjectType, hParams)
20
+ PrcLib.state(format("Running creation process for object '%s' = '%s'",
21
+ sObjectType, hParams[:student_name]))
22
+ # byebug if ENV['BYEBUG'] # rubocop: disable Debugger
23
+ object = controller_create(sObjectType)
24
+ PrcLib.runtime_fail "Student '%s' not created.",
25
+ hParams[:student_name] if object.nil?
26
+ PrcLib.info("'%s': '%s' created with id %s",
27
+ sObjectType, hParams[:student_name], object[:id])
28
+ object
29
+ end
30
+ end
31
+
32
+ # Declaring your data model and handlers.
33
+ class Lorj::BaseDefinition # rubocop: disable Style/ClassAndModuleChildren
34
+ # We need to define the student object and the handler to use while we need to
35
+ # create it.
36
+ define_obj(:student,
37
+ # The function to call in the class Students
38
+ :create_e => :create_student,
39
+ # We use predefined call to the controller query
40
+ :query_e => :controller_query,
41
+ # We use predefined call to the controller get
42
+ :get_e => :controller_get,
43
+ # We use predefined call to the controller delete
44
+ :delete_e => :controller_delete
45
+ )
46
+
47
+ obj_needs :data, :student_name, :for => [:create_e], :mapping => :name
48
+ end