lorj 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +46 -0
  4. data/Gemfile +15 -15
  5. data/README.md +22 -17
  6. data/Rakefile +12 -2
  7. data/bin/cloud_test.rb +13 -65
  8. data/example/students_1/process/students.rb +39 -0
  9. data/example/students_1/students.rb +22 -5
  10. data/example/students_2/process/students.rb +48 -0
  11. data/example/students_2/students.rb +39 -16
  12. data/example/students_3/controller/yaml_students.rb +50 -43
  13. data/example/students_3/controller/yaml_students_controller.rb +100 -87
  14. data/example/students_3/process/students.rb +161 -97
  15. data/example/students_3/students.rb +85 -56
  16. data/example/yaml_students/students.rb +40 -40
  17. data/example/yaml_students/yaml_students.rb +103 -90
  18. data/lib/core/core.rb +356 -696
  19. data/lib/core/core_controller.rb +227 -0
  20. data/lib/core/core_internal.rb +339 -0
  21. data/lib/core/core_model.rb +328 -0
  22. data/lib/core/core_object_data.rb +330 -0
  23. data/lib/core/core_object_params.rb +230 -0
  24. data/lib/core/core_process.rb +391 -0
  25. data/lib/core/core_process_setup.rb +353 -0
  26. data/lib/core/core_setup_ask.rb +241 -0
  27. data/lib/core/core_setup_encrypt.rb +146 -0
  28. data/lib/core/core_setup_init.rb +229 -0
  29. data/lib/core/core_setup_list.rb +160 -0
  30. data/lib/core/definition.rb +647 -469
  31. data/lib/core/definition_internal.rb +264 -308
  32. data/lib/core/lorj_basecontroller.rb +95 -0
  33. data/lib/core/lorj_basedefinition.rb +307 -0
  34. data/lib/core/lorj_baseprocess.rb +265 -0
  35. data/lib/core/lorj_data.rb +583 -0
  36. data/lib/core/lorj_keypath.rb +119 -0
  37. data/lib/core_process/cloud/process/common.rb +63 -0
  38. data/lib/core_process/cloud/process/connection.rb +93 -0
  39. data/lib/core_process/cloud/process/external_network.rb +94 -0
  40. data/lib/core_process/cloud/process/flavor.rb +99 -0
  41. data/lib/core_process/cloud/process/images.rb +87 -0
  42. data/lib/core_process/cloud/process/internet_network.rb +34 -0
  43. data/lib/core_process/cloud/process/internet_server.rb +30 -0
  44. data/lib/core_process/cloud/process/keypairs.rb +276 -0
  45. data/lib/core_process/cloud/process/network.rb +108 -0
  46. data/lib/core_process/cloud/process/public_ip.rb +100 -0
  47. data/lib/core_process/cloud/process/router.rb +260 -0
  48. data/lib/core_process/cloud/process/rules.rb +120 -0
  49. data/lib/core_process/cloud/process/security_groups.rb +121 -0
  50. data/lib/core_process/cloud/process/server.rb +127 -0
  51. data/lib/core_process/cloud/process/server_log.rb +35 -0
  52. data/lib/core_process/cloud/process/subnetwork.rb +108 -0
  53. data/lib/core_process/cloud_process.rb +30 -0
  54. data/lib/logging.rb +298 -0
  55. data/lib/lorj/version.rb +18 -1
  56. data/lib/lorj.rb +58 -18
  57. data/lib/lorj_account.rb +556 -0
  58. data/lib/lorj_config.rb +468 -0
  59. data/lib/lorj_defaults.rb +278 -0
  60. data/lib/prc.rb +136 -104
  61. data/lib/prc_base_config.rb +285 -0
  62. data/lib/prc_core_config.rb +878 -0
  63. data/lib/prc_section_config.rb +57 -0
  64. data/lib/providers/hpcloud/compute.rb +81 -93
  65. data/lib/providers/hpcloud/hpcloud.rb +462 -0
  66. data/lib/providers/hpcloud/network.rb +96 -98
  67. data/lib/providers/hpcloud/security_groups.rb +41 -40
  68. data/lib/providers/mock/mock.rb +144 -0
  69. data/lib/providers/openstack/openstack.rb +45 -0
  70. data/lib/providers/templates/compute.rb +21 -23
  71. data/lib/providers/templates/mycloud.rb +72 -0
  72. data/lib/providers/templates/network.rb +11 -12
  73. data/lib/rh.rb +339 -0
  74. data/lorj-spec/defaults.yaml +4 -0
  75. data/lorj.gemspec +6 -0
  76. data/spec/00_lorj_log_spec.rb +53 -0
  77. data/spec/01_hash_rh_spec.rb +243 -0
  78. data/spec/02_prc_base_config_spec.rb +216 -0
  79. data/spec/04_prc_core_config_spec.rb +83 -0
  80. data/spec/11_lorj_config_spec.rb +263 -0
  81. data/spec/12_lorj_account_spec.rb +181 -0
  82. metadata +76 -28
  83. data/Gemfile.lock +0 -37
  84. data/example/students_1/process/Students.rb +0 -20
  85. data/example/students_2/process/Students.rb +0 -27
  86. data/example/students_4/controller/yaml_students.rb +0 -82
  87. data/example/students_4/controller/yaml_students_controller.rb +0 -141
  88. data/example/students_4/process/students.rb +0 -112
  89. data/example/students_4/students.rb +0 -103
  90. data/lib/core/lorj-basecontroller.rb +0 -90
  91. data/lib/core/lorj-basedefinition.rb +0 -1139
  92. data/lib/core/lorj-baseprocess.rb +0 -236
  93. data/lib/core/lorj-data.rb +0 -567
  94. data/lib/core/lorj-keypath.rb +0 -115
  95. data/lib/core_process/CloudProcess.rb +0 -337
  96. data/lib/core_process/global_process.rb +0 -502
  97. data/lib/core_process/network_process.rb +0 -605
  98. data/lib/prc-account.rb +0 -339
  99. data/lib/prc-config.rb +0 -1030
  100. data/lib/prc-logging.rb +0 -261
  101. data/lib/providers/hpcloud/Hpcloud.rb +0 -426
  102. data/lib/providers/mock/Mock.rb +0 -141
  103. data/lib/providers/openstack/Openstack.rb +0 -47
  104. data/lib/providers/templates/core.rb +0 -61
  105. data/spec/forj-account_spec.rb +0 -75
  106. data/spec/forj-config_spec.rb +0 -196
@@ -0,0 +1,95 @@
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
+ # Module Lorj which contains several classes.
19
+ #
20
+ # Those classes describes :
21
+ # - processes (BaseProcess) : How to create/delete/edit/query object.
22
+ # - controler (BaseControler) : If a provider is defined, define how will do
23
+ # object creation/etc...
24
+ # - definition(BaseDefinition): Functions to declare objects, query/data mapping
25
+ # and setup
26
+ # this task to make it to work.
27
+
28
+ module Lorj
29
+ # Defining basic Controller functions
30
+ class BaseController
31
+ # Default handlers which needs to be defined by the controller,
32
+ # called by BaseDefinition Create functions.
33
+ def connect(_sObjectType, _hParams)
34
+ process_error 'connect has not been redefined by the controller.'
35
+ end
36
+
37
+ # Default handlers which needs to be defined by the controller,
38
+ # called by BaseDefinition Create functions.
39
+ def create(_sObjectType, _hParams)
40
+ process_error 'create_object has not been redefined by the controller.'
41
+ end
42
+
43
+ # Default handlers which needs to be defined by the controller,
44
+ # called by BaseDefinition Delete functions.
45
+ def delete(_sObjectType, _hParams)
46
+ process_error 'delete_object has not been redefined by the controller.'
47
+ end
48
+
49
+ # Default handlers which needs to be defined by the controller,
50
+ # called by BaseDefinition Get functions.
51
+ def get(_sObjectType, _sUniqId, _hParams)
52
+ process_error 'get_object has not been redefined by the controller.'
53
+ end
54
+
55
+ # Default handlers which needs to be defined by the controller,
56
+ # called by BaseDefinition Query functions.
57
+ def query(_sObjectType, _sQuery, _hParams)
58
+ process_error 'query_object has not been redefined by the controller.'
59
+ end
60
+
61
+ # Default handlers which needs to be defined by the controller,
62
+ # called by BaseDefinition Update functions.
63
+ def update(_sObjectType, _oObject, _hParams)
64
+ process_error 'update_object has not been redefined by the controller.'
65
+ end
66
+
67
+ # Simply raise an error
68
+ #
69
+ # * *Args* :
70
+ # - +Msg+ : Error message to print out.
71
+ # * *Returns* :
72
+ # - nil
73
+ # * *Raises* :
74
+ # - Lorj::PrcError
75
+ def controller_error(msg, *p)
76
+ msg = format(msg, *p)
77
+ runtime_fail '%s: %s', self.class, msg
78
+ end
79
+
80
+ # check if required data is loaded. raise an error if not
81
+ #
82
+ # * *Args* :
83
+ # - +Params+ : Lorj::ObjectData object for controller.
84
+ # - +key+ : Key to check.
85
+ # * *Returns* :
86
+ # - nil
87
+ # * *Raises* :
88
+ # - +Error+ if the key do not exist.
89
+ def required?(oParams, *key)
90
+ return if oParams.exist?(key)
91
+
92
+ runtime_fail '%s: %s is not set.', self.class, key
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,307 @@
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
+ # Module Lorj which contains several classes.
18
+ #
19
+ # Those classes describes :
20
+ # - processes (BaseProcess) : How to create/delete/edit/query object.
21
+ # - controler (BaseControler) : If a provider is defined, define how will do
22
+ # object creation/etc...
23
+ # - definition(BaseDefinition): Functions to declare objects, query/data mapping
24
+ # and setup
25
+ # this task to make it to work.
26
+
27
+ module Lorj
28
+ # This class limits ERC template to access only to config object data.
29
+ class ERBConfig
30
+ attr_reader :config
31
+
32
+ def initialize(config)
33
+ @config = config
34
+ end
35
+
36
+ # Bind this limited class with ERB templates
37
+ def get_binding # rubocop: disable AccessorMethodName
38
+ binding
39
+ end
40
+ end
41
+
42
+ # Following class defines class levels function to
43
+ # declare framework objects.
44
+ # As each process needs to define new object to deal with
45
+ # require that process to define it with definition functions
46
+ # See definition.rb for functions to use.
47
+ class BaseDefinition
48
+ # Initialize Lorj BaseDefinition object
49
+ def initialize(oForjConfig, process, controller = nil)
50
+ # Object Data object. Contains all loaded object data.
51
+ # This object is used to build hParams as well.
52
+ @object_data = ObjectData.new(true)
53
+ #
54
+ @runtime_context = {
55
+ :oCurrentObj => nil
56
+ }
57
+
58
+ @config = oForjConfig
59
+ @erb_config = ERBConfig.new(oForjConfig)
60
+ fail Lorj::PrcError.new, "'%s' is not a valid ForjAccount or ForjConfig"\
61
+ ' Object.',
62
+ [oForjConfig.class] if
63
+ !oForjConfig.is_a?(Lorj::Account) &&
64
+
65
+ !oForjConfig.is_a?(Lorj::Config)
66
+
67
+ @controller = controller
68
+ if controller
69
+ runtime_fail "'%s' is not a valid ForjProvider Object type.",
70
+ controller.class unless controller.is_a?(BaseController)
71
+ end
72
+
73
+ @process = process
74
+ fail Lorj::PrcError.new, "'%s' is not a valid BaseProcess Object type.",
75
+ [process.class] unless process.is_a?(BaseProcess)
76
+
77
+ @process.base_object(self)
78
+ end
79
+
80
+ # ------------------------------------------------------
81
+ # Functions used by processes functions
82
+ # ------------------------------------------------------
83
+ # Ex: object.set_data(...)
84
+ # config
85
+
86
+ # Function to manipulate the config object.
87
+ # 2 kind of functions:
88
+ # - set (key, value) and []=(key, value)
89
+ # From processes, you can set a runtime data with:
90
+ # config.set(key, value)
91
+ # OR
92
+ # config[key] = value
93
+ #
94
+ # - get (key, default) and [](key, default)
95
+ # default is an optional value.
96
+ # From processes, you can get a data (runtime/account/config.yaml or
97
+ # defaults.yaml) with:
98
+ # config.get(key)
99
+ # OR
100
+ # config[key]
101
+
102
+ def config
103
+ fail Lorj::PrcError.new, 'No config object loaded.' unless @config
104
+ @config
105
+ end
106
+
107
+ def format_query(sObjectType, oControlerObject, hQuery)
108
+ {
109
+ :object => oControlerObject,
110
+ :object_type => :object_list,
111
+ :list_type => sObjectType,
112
+ :list => [],
113
+ :query => hQuery
114
+ }
115
+ end
116
+
117
+ def format_object(object_type, oMiscObject)
118
+ return nil unless [String, Symbol].include?(object_type.class)
119
+
120
+ object_type = object_type.to_sym if object_type.class == String
121
+
122
+ { :object_type => object_type,
123
+ :attrs => {},
124
+ :object => oMiscObject
125
+ }
126
+ end
127
+
128
+ def get_data_metadata(sKey)
129
+ _get_meta_data(sKey)
130
+ end
131
+
132
+ # Before doing a query, mapping fields
133
+ # Transform Object query field to Provider query Fields
134
+ def _query_map(object_type, hParams)
135
+ return {} unless hParams
136
+
137
+ object_type = object_type.to_sym if object_type.class == String
138
+
139
+ result = {}
140
+ maps = PrcLib.model.meta_obj.rh_get(object_type, :query_mapping)
141
+ hParams.each do |key, value|
142
+ key_path_obj = KeyPath.new(key)
143
+ key_path = key_path_obj.fpath
144
+ PrcLib.runtime_fail "Forj query field '%s.%s' not defined by class"\
145
+ " '%s'.\n#{ANSI.bold}ACTION REQUIRED#{ANSI.clear}"\
146
+ ":\nMissing data model 'def_attribute' or "\
147
+ "'def_query_attribute' for '%s'??? "\
148
+ "Check the object '%s' data model.",
149
+ object_type, key_path_obj.key,
150
+ self.class, key_path_obj.key,
151
+ object_type unless maps.key?(key_path_obj.fpath)
152
+ map_path = KeyPath.new(maps[key_path_obj.fpath])
153
+ value_mapping = PrcLib.model.meta_obj.rh_get(object_type,
154
+ :value_mapping, key_path)
155
+ if value_mapping
156
+ PrcLib.runtime_fail "'%s.%s': No value mapping for '%s'",
157
+ object_type, key_path_obj.key,
158
+ value unless value_mapping.rh_exist?(value)
159
+
160
+ result.rh_set(value_mapping[value], map_path.tree)
161
+ else
162
+ result.rh_set(value, map_path.tree)
163
+ end
164
+ end
165
+ result
166
+ end
167
+
168
+ # Obsolete. Used by the Process.
169
+ # Ask controller get_attr to get a data
170
+ # The result is the data of a defined data attribute.
171
+ # If the value is normally mapped (value mapped), the value is
172
+ # returned as a recognized data attribute value.
173
+ # def get_attr(oObject, key)
174
+ # fail Lorj::PrcError.new, "'%s' is not a valid Object type. " % [oObject.
175
+ # class] if !oObject.is_a?(Hash) &&
176
+ # ! oObject.rh_exist?(:object_type)
177
+ # sCloudObj = oObject[:object_type]
178
+ # oKeyPath = KeyPath.new(key)
179
+ # fail Lorj::PrcError.new, "'%s' key is not declared as data of '%s' "\
180
+ # 'CloudObject. You may need to add obj_needs...'
181
+ # % [oKeyPath.key, sCloudObj] if PrcLib.model.meta_obj.rh_exist?(sClou
182
+ # dObj, :returns, oKeyPath.fpath) != 3
183
+ # begin
184
+ # oMapPath = KeyPath.new PrcLib.model.meta_obj.rh_get(sCloudObj, :
185
+ # returns, oKeyPath.fpath)
186
+ # hMap = oMapPath.fpath
187
+ # value = @controller.get_attr(get_cObject(oObject), hMap)
188
+
189
+ # hValueMapping = PrcLib.model.meta_obj.rh_get(sCloudObj, :value_m
190
+ # apping, oKeyPath.fpath)
191
+
192
+ # if hValueMapping
193
+ # hValueMapping.each do | found_key, found_value |
194
+ # if found_value == value
195
+ # value = found_key
196
+ # break
197
+ # end
198
+ # end
199
+ # end
200
+ # rescue => e
201
+ # raise Lorj::PrcError.new, "'%s.get_attr' fails to provide value of '%s'
202
+ # " % [oProvider.class, key]
203
+ # end
204
+ # end
205
+
206
+ # Register the object to the internal @object_data instance
207
+ def register(oObject, sObjectType = nil, sDataType = :object)
208
+ if oObject.is_a?(Lorj::Data)
209
+ data_objects = oObject
210
+ else
211
+ runtime_fail "Unable to register an object '%s' "\
212
+ 'as Lorj::Data object if ObjectType is not given.',
213
+ oObject.class unless sObjectType
214
+ data_objects = Lorj::Data.new(sDataType)
215
+ data_objects.set(oObject, sObjectType) do | sObjType, oControlerObject |
216
+ _return_map(sObjType, oControlerObject)
217
+ end
218
+ end
219
+ @object_data.add data_objects
220
+ end
221
+
222
+ def data_objects(sObjectType, *key)
223
+ @object_data[sObjectType, key]
224
+ end
225
+
226
+ # get an attribute/object/... from an object.
227
+ def get_data(oObj, *key)
228
+ if oObj.is_a?(Hash) && oObj.key?(:object_type)
229
+ object_data = ObjectData.new
230
+ object_data << oObj
231
+ else
232
+ object_data = @object_data
233
+ end
234
+ object_data[oObj, *key]
235
+ end
236
+
237
+ # ~ def hParams(sCloudObj, hParams)
238
+ # ~ aParams = _get_object_params(sCloudObj, ":ObjectData.hParams")
239
+ # ~ end
240
+
241
+ # def get_cObject(oObject)
242
+ # return nil unless oObject.rh_exist?(:object)
243
+ # oObject.rh_get(:object)
244
+ # end
245
+
246
+ private
247
+
248
+ # -------------------------------------------------------------------------
249
+ # Functions available for Process to communicate with the controler Object
250
+ # -------------------------------------------------------------------------
251
+ # def cloud_obj_requires(sCloudObj, res = {})
252
+ # aCaller = caller
253
+ # aCaller.pop
254
+
255
+ # return res if @object_data.exist?(sCloudObj)
256
+ # # ~ return res if Lorj::rh_exist?(@CloudData, sCloudObj)
257
+
258
+ # PrcLib.model.meta_obj.rh_get(sCloudObj, :params).each do |
259
+ # key, hParams|
260
+ # case hParams[:type]
261
+ # when :data
262
+ # if hParams.key?(:array)
263
+ # hParams[:array].each do | aElem |
264
+ # aElem = aElem.clone
265
+ # aElem.pop # Do not go until last level, as used to loop next.
266
+ # hParams.rh_get(aElem).each do | subkey, _hSubParam |
267
+ # next if aElem.length == 0 && [:array, :type].include?(subkey)
268
+ # if hSubParams[:required] && @config.get(subkey).nil?
269
+ # res[subkey] = hSubParams
270
+ # end
271
+ # end
272
+ # end
273
+ # else
274
+ # if hParams[:required] && @config.get(key).nil?
275
+ # res[key] = hParams
276
+ # end
277
+ # end
278
+ # when :CloudObject
279
+ # if hParams[:required] && !@object_data.exist?(sCloudObj)
280
+ # res[key] = hParams
281
+ # cloud_obj_requires(key, res)
282
+ # end
283
+ # end
284
+ # end
285
+ # res
286
+ # end
287
+
288
+ def get_object(sCloudObj)
289
+ # ~ return nil unless Lorj::rh_exist?(@CloudData, sCloudObj)
290
+ return nil unless @object_data.exist?(sCloudObj)
291
+ @object_data[sCloudObj, :ObjectData]
292
+ # ~ Lorj::rh_get(@CloudData, sCloudObj)
293
+ end
294
+
295
+ # def objectExist?(sCloudObj)
296
+ # @object_data.exist?(sCloudObj)
297
+ # # ~ !(Lorj::rh_exist?(@CloudData, sCloudObj))
298
+ # end
299
+
300
+ # def get_forjKey(sCloudObj, key)
301
+ # return nil unless @object_data.exist?(sCloudObj)
302
+ # @object_data[sCloudObj, :attrs, key]
303
+ # # ~ return nil unless Lorj::rh_exist?(oCloudData, sCloudObj)
304
+ # # ~ Lorj::rh_get(oCloudData, sCloudObj, :attrs, key)
305
+ # end
306
+ end
307
+ end
@@ -0,0 +1,265 @@
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
+ # Module Lorj which contains several classes.
19
+ #
20
+ # Those classes describes :
21
+ # - processes (BaseProcess) : How to create/delete/edit/query object.
22
+ # - controler (BaseControler) : If a provider is defined, define how will do
23
+ # object creation/etc...
24
+ # - definition(BaseDefinition): Functions to declare objects, query/data mapping
25
+ # and setup
26
+ # this task to make it to work.
27
+
28
+ module Lorj
29
+ # class describing generic Object Process
30
+ # with controller calls
31
+ class BaseProcess
32
+ def initialize
33
+ @definition = nil
34
+ end
35
+
36
+ # Simply raise an error
37
+ #
38
+ # * *Args* :
39
+ # - +Msg+ : Error message to print out.
40
+ # * *Returns* :
41
+ # - nil
42
+ # * *Raises* :
43
+ # - Lorj::PrcError
44
+ def process_error(msg, *p)
45
+ msg = format(msg, *p)
46
+ runtime_fail '%s: %s', self.class, msg
47
+ end
48
+
49
+ attr_writer :base_object
50
+
51
+ def controller_connect(sObjectType, hParams = {})
52
+ fail Lorj::PrcError.new, 'No Controler object loaded.' unless @definition
53
+ @definition.controller_connect(sObjectType, hParams)
54
+ end
55
+
56
+ def controller_create(sObjectType, hParams = {})
57
+ fail Lorj::PrcError.new, 'No Controler object loaded.' unless @definition
58
+ @definition.controller_create(sObjectType, hParams)
59
+ end
60
+
61
+ def controller_query(sObjectType, sQuery, hParams = {})
62
+ fail Lorj::PrcError.new, 'No Controler object loaded.' unless @definition
63
+ @definition.controller_query(sObjectType, sQuery, hParams)
64
+ end
65
+
66
+ def controller_update(sObjectType, hParams = {})
67
+ fail Lorj::PrcError.new, 'No Controler object loaded.' unless @definition
68
+ @definition.controller_update(sObjectType, hParams)
69
+ end
70
+
71
+ def controller_delete(sObjectType, hParams = {})
72
+ fail Lorj::PrcError.new, 'No Controler object loaded.' unless @definition
73
+ @definition.controller_delete(sObjectType, hParams)
74
+ end
75
+
76
+ def controller_get(sObjectType, sId, hParams = {})
77
+ fail Lorj::PrcError.new, 'No Controler object loaded.' unless @definition
78
+ @definition.controller_get(sObjectType, sId, hParams)
79
+ end
80
+ end
81
+
82
+ # class describing generic Object Process
83
+ # with process calls
84
+ class BaseProcess
85
+ def process_create(sObjectType)
86
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
87
+ @definition.process_create(sObjectType)
88
+ end
89
+
90
+ def process_query(sObjectType, sQuery)
91
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
92
+ @definition.process_query(sObjectType, sQuery)
93
+ end
94
+
95
+ def process_update(sObjectType)
96
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
97
+ @definition.process_update(sObjectType)
98
+ end
99
+
100
+ def process_get(sObjectType, sId)
101
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
102
+ @definition.process_get(sObjectType, sId)
103
+ end
104
+
105
+ def process_delete(sObjectType)
106
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
107
+ @definition.process_delete(sObjectType)
108
+ end
109
+ end
110
+
111
+ # class describing generic Object Process
112
+ # with additionnal functions
113
+ class BaseProcess
114
+ private
115
+
116
+ def query_cache_cleanup(sObjectType)
117
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
118
+ @definition.query_cleanup(sObjectType)
119
+ end
120
+
121
+ def object_cache_cleanup(sObjectType)
122
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
123
+ @definition.object_cleanup(sObjectType)
124
+ end
125
+
126
+ def controler
127
+ PrcLib.warning('controler object call is obsolete. Please update your'\
128
+ " code. Use controller_<action> instead.\n%s", caller)
129
+ fail Lorj::PrcError.new, 'No Controler object loaded.' unless @definition
130
+ @definition
131
+ end
132
+
133
+ def object
134
+ PrcLib.warning('object call is obsolete. Please update your code.'\
135
+ "Use <Action> instead.\n%s", caller)
136
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
137
+ @definition
138
+ end
139
+
140
+ def format_object(sObjectType, oMiscObj)
141
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
142
+ @definition.format_object(sObjectType, oMiscObj)
143
+ end
144
+
145
+ def format_query(sObjectType, oMiscObj, hQuery)
146
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
147
+ @definition.format_list(sObjectType, oMiscObj, hQuery)
148
+ end
149
+
150
+ def data_objects(sObjectType, *key)
151
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
152
+ @definition.data_objects(sObjectType, *key)
153
+ end
154
+
155
+ def get_data(oObj, *key)
156
+ PrcLib.warning('get_data call is obsolete. Please update your code. '\
157
+ "Use [] instead.\n%s", caller)
158
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
159
+ @definition.get_data(oObj, :attrs, key)
160
+ end
161
+
162
+ def register(oObject, sObjectType = nil)
163
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
164
+ @definition.register(oObject, sObjectType)
165
+ end
166
+
167
+ def config
168
+ fail Lorj::PrcError.new, 'No Base object loaded.' unless @definition
169
+ @definition.config
170
+ end
171
+
172
+ def query_single(sCloudObj, list, sQuery, name, sInfoMsg = {})
173
+ list = controller_query(sCloudObj, sQuery)
174
+
175
+ info = _qs_info_init(sInfoMsg)
176
+
177
+ case list.length
178
+ when 0
179
+ PrcLib.info(info[:notfound], sCloudObj, name)
180
+ list
181
+ when 1
182
+ Lorj.debug(2, info[:checkmatch], sCloudObj, name)
183
+
184
+ _qs_check_match(list, sQuery)
185
+
186
+ if list.length == 0
187
+ PrcLib.info(info[:nomatch], sCloudObj, name)
188
+ return list
189
+ end
190
+
191
+ item = _qs_info(list, info[:items], info[:items_form])
192
+
193
+ PrcLib.info(info[:found], sCloudObj, item)
194
+
195
+ list
196
+ else
197
+ Lorj.debug(2, info[:more], sCloudObj, name)
198
+ # Looping to find the one corresponding
199
+ _qs_check_match(list, sQuery)
200
+
201
+ if list.length == 0
202
+ PrcLib.info(info[:notfound], sCloudObj, name)
203
+ return list
204
+ end
205
+ item = _qs_info(list, info[:items], info[:items_form])
206
+ PrcLib.info(info[:found], sCloudObj, item)
207
+
208
+ list
209
+ end
210
+ end
211
+ end
212
+
213
+ # internal functions
214
+ class BaseProcess
215
+ private
216
+
217
+ def _qs_info(list, items, items_form)
218
+ items_built = []
219
+ if items.is_a?(Array)
220
+ items.each do | key |
221
+ items_built << list[0, key]
222
+ end
223
+ else
224
+ items_built << list[0, items]
225
+ end
226
+ format(items_form, items_built)
227
+ end
228
+
229
+ def _qs_check_match(list, sQuery)
230
+ list.each do | oElem |
231
+ is_found = true
232
+ sQuery.each do | key, value |
233
+ if oElem[key] != value
234
+ is_found = false
235
+ break
236
+ end
237
+ end
238
+ :remove unless is_found
239
+ end
240
+ end
241
+
242
+ # qs_info_init is not omplex as well described. Disabling rubocop
243
+ # rubocop: disable PerceivedComplexity, CyclomaticComplexity
244
+
245
+ def _qs_info_init(sInfoMsg)
246
+ info = {
247
+ :notfound => "No %s '%s' found",
248
+ :checkmatch => "Found 1 %s. checking exact match for '%s'.",
249
+ :nomatch => "No %s '%s' match",
250
+ :found => "Found %s '%s'.",
251
+ :more => "Found several %s. Searching for '%s'.",
252
+ :items_form => '%s',
253
+ :items => [:name]
254
+ }
255
+ info[:notfound] = sInfoMsg[:notfound] if sInfoMsg.key?(:notfound)
256
+ info[:checkmatch] = sInfoMsg[:checkmatch] if sInfoMsg.key?(:checkmatch)
257
+ info[:nomatch] = sInfoMsg[:nomatch] if sInfoMsg.key?(:nomatch)
258
+ info[:found] = sInfoMsg[:found] if sInfoMsg.key?(:found)
259
+ info[:more] = sInfoMsg[:more] if sInfoMsg.key?(:more)
260
+ info[:items] = sInfoMsg[:items] if sInfoMsg.key?(:items)
261
+ info[:items_form] = sInfoMsg[:items_form] if sInfoMsg.key?(:items_form)
262
+ info
263
+ end
264
+ end
265
+ end