lorj 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +46 -0
- data/Gemfile +15 -15
- data/README.md +22 -17
- data/Rakefile +12 -2
- data/bin/cloud_test.rb +13 -65
- data/example/students_1/process/students.rb +39 -0
- data/example/students_1/students.rb +22 -5
- data/example/students_2/process/students.rb +48 -0
- data/example/students_2/students.rb +39 -16
- data/example/students_3/controller/yaml_students.rb +50 -43
- data/example/students_3/controller/yaml_students_controller.rb +100 -87
- data/example/students_3/process/students.rb +161 -97
- data/example/students_3/students.rb +85 -56
- data/example/yaml_students/students.rb +40 -40
- data/example/yaml_students/yaml_students.rb +103 -90
- data/lib/core/core.rb +356 -696
- data/lib/core/core_controller.rb +227 -0
- data/lib/core/core_internal.rb +339 -0
- data/lib/core/core_model.rb +328 -0
- data/lib/core/core_object_data.rb +330 -0
- data/lib/core/core_object_params.rb +230 -0
- data/lib/core/core_process.rb +391 -0
- data/lib/core/core_process_setup.rb +353 -0
- data/lib/core/core_setup_ask.rb +241 -0
- data/lib/core/core_setup_encrypt.rb +146 -0
- data/lib/core/core_setup_init.rb +229 -0
- data/lib/core/core_setup_list.rb +160 -0
- data/lib/core/definition.rb +647 -469
- data/lib/core/definition_internal.rb +264 -308
- data/lib/core/lorj_basecontroller.rb +95 -0
- data/lib/core/lorj_basedefinition.rb +307 -0
- data/lib/core/lorj_baseprocess.rb +265 -0
- data/lib/core/lorj_data.rb +583 -0
- data/lib/core/lorj_keypath.rb +119 -0
- data/lib/core_process/cloud/process/common.rb +63 -0
- data/lib/core_process/cloud/process/connection.rb +93 -0
- data/lib/core_process/cloud/process/external_network.rb +94 -0
- data/lib/core_process/cloud/process/flavor.rb +99 -0
- data/lib/core_process/cloud/process/images.rb +87 -0
- data/lib/core_process/cloud/process/internet_network.rb +34 -0
- data/lib/core_process/cloud/process/internet_server.rb +30 -0
- data/lib/core_process/cloud/process/keypairs.rb +276 -0
- data/lib/core_process/cloud/process/network.rb +108 -0
- data/lib/core_process/cloud/process/public_ip.rb +100 -0
- data/lib/core_process/cloud/process/router.rb +260 -0
- data/lib/core_process/cloud/process/rules.rb +120 -0
- data/lib/core_process/cloud/process/security_groups.rb +121 -0
- data/lib/core_process/cloud/process/server.rb +127 -0
- data/lib/core_process/cloud/process/server_log.rb +35 -0
- data/lib/core_process/cloud/process/subnetwork.rb +108 -0
- data/lib/core_process/cloud_process.rb +30 -0
- data/lib/logging.rb +298 -0
- data/lib/lorj/version.rb +18 -1
- data/lib/lorj.rb +58 -18
- data/lib/lorj_account.rb +556 -0
- data/lib/lorj_config.rb +468 -0
- data/lib/lorj_defaults.rb +278 -0
- data/lib/prc.rb +136 -104
- data/lib/prc_base_config.rb +285 -0
- data/lib/prc_core_config.rb +878 -0
- data/lib/prc_section_config.rb +57 -0
- data/lib/providers/hpcloud/compute.rb +81 -93
- data/lib/providers/hpcloud/hpcloud.rb +462 -0
- data/lib/providers/hpcloud/network.rb +96 -98
- data/lib/providers/hpcloud/security_groups.rb +41 -40
- data/lib/providers/mock/mock.rb +144 -0
- data/lib/providers/openstack/openstack.rb +45 -0
- data/lib/providers/templates/compute.rb +21 -23
- data/lib/providers/templates/mycloud.rb +72 -0
- data/lib/providers/templates/network.rb +11 -12
- data/lib/rh.rb +339 -0
- data/lorj-spec/defaults.yaml +4 -0
- data/lorj.gemspec +6 -0
- data/spec/00_lorj_log_spec.rb +53 -0
- data/spec/01_hash_rh_spec.rb +243 -0
- data/spec/02_prc_base_config_spec.rb +216 -0
- data/spec/04_prc_core_config_spec.rb +83 -0
- data/spec/11_lorj_config_spec.rb +263 -0
- data/spec/12_lorj_account_spec.rb +181 -0
- metadata +76 -28
- data/Gemfile.lock +0 -37
- data/example/students_1/process/Students.rb +0 -20
- data/example/students_2/process/Students.rb +0 -27
- data/example/students_4/controller/yaml_students.rb +0 -82
- data/example/students_4/controller/yaml_students_controller.rb +0 -141
- data/example/students_4/process/students.rb +0 -112
- data/example/students_4/students.rb +0 -103
- data/lib/core/lorj-basecontroller.rb +0 -90
- data/lib/core/lorj-basedefinition.rb +0 -1139
- data/lib/core/lorj-baseprocess.rb +0 -236
- data/lib/core/lorj-data.rb +0 -567
- data/lib/core/lorj-keypath.rb +0 -115
- data/lib/core_process/CloudProcess.rb +0 -337
- data/lib/core_process/global_process.rb +0 -502
- data/lib/core_process/network_process.rb +0 -605
- data/lib/prc-account.rb +0 -339
- data/lib/prc-config.rb +0 -1030
- data/lib/prc-logging.rb +0 -261
- data/lib/providers/hpcloud/Hpcloud.rb +0 -426
- data/lib/providers/mock/Mock.rb +0 -141
- data/lib/providers/openstack/Openstack.rb +0 -47
- data/lib/providers/templates/core.rb +0 -61
- data/spec/forj-account_spec.rb +0 -75
- data/spec/forj-config_spec.rb +0 -196
@@ -0,0 +1,583 @@
|
|
1
|
+
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
# Module Lorj which contains several classes.
|
16
|
+
#
|
17
|
+
# Those classes describes :
|
18
|
+
# - processes (BaseProcess) : How to create/delete/edit/query object.
|
19
|
+
# - controler (BaseControler) : If a provider is defined, define how will do
|
20
|
+
# object creation/etc...
|
21
|
+
# - definition(BaseDefinition): Functions to declare objects, query/data mapping
|
22
|
+
# and setup
|
23
|
+
# this task to make it to work.
|
24
|
+
module Lorj
|
25
|
+
# This class is the Data object used by lorj object!
|
26
|
+
# This is a key component of lorj
|
27
|
+
#
|
28
|
+
# You find this object in different places.
|
29
|
+
#
|
30
|
+
# type:
|
31
|
+
# - object/data : The Data object contains any kind of data.
|
32
|
+
# This data contains 2 elements:
|
33
|
+
# - controler object : This is a ruby object managed by the controller.
|
34
|
+
# Only the controller has the knowledge to manage this kind of data.
|
35
|
+
# - attributes : This is the internal data mapping from the controller
|
36
|
+
# object.
|
37
|
+
# The controller helped to build this data thanks to the BaseController.
|
38
|
+
# get_attr / BaseController.set_attr
|
39
|
+
# Attributes are declared by the Data model in BaseDefinition. At least
|
40
|
+
# usually, each object has 2 attributes: :id and :name
|
41
|
+
#
|
42
|
+
# - list : The Data object contains a list of Lorj::Data
|
43
|
+
#
|
44
|
+
# If the object is of type :list, following functions are usable:
|
45
|
+
#
|
46
|
+
# - length : return numbers of Lorj::Data
|
47
|
+
# - each / each_index : loop on the list, key/value or key_index. yield
|
48
|
+
# can return 'remove' to remove the element from the
|
49
|
+
# list during loop.
|
50
|
+
#
|
51
|
+
# If the Data object is of type :data or :object or even :list, following
|
52
|
+
# functions are usable.
|
53
|
+
#
|
54
|
+
# - set/[]=/get/[]/exist? : Basic get/set/exist? feature.
|
55
|
+
# - type?/object_type? : Determine the type of this object.
|
56
|
+
# ie :data (stands for :object as well) or :list
|
57
|
+
# - to_a : Array of Data attributes
|
58
|
+
# - empty? nil? : Identify if the object is empty. Avoid to use
|
59
|
+
# nil?.
|
60
|
+
# - register/unregister : Used by Lorj::BaseDefinition internal @object_data
|
61
|
+
# registered? : Determine if this object is stored in the global
|
62
|
+
# object cache.
|
63
|
+
class Data
|
64
|
+
# Initialize Lorj::Data object
|
65
|
+
#
|
66
|
+
# * *Args* :
|
67
|
+
# - +type+ : default is :object
|
68
|
+
# Support :data/:object for single object data
|
69
|
+
# :list for a list of object data
|
70
|
+
#
|
71
|
+
# * *Returns* :
|
72
|
+
# hash - internal data object.
|
73
|
+
#
|
74
|
+
# * *Raises* :
|
75
|
+
# No exceptions
|
76
|
+
#
|
77
|
+
def initialize(type = :object)
|
78
|
+
type = :data unless [:list, :object, :data].include?(type)
|
79
|
+
@type = type
|
80
|
+
case type
|
81
|
+
when :data, :object
|
82
|
+
@data = new_object
|
83
|
+
when :list
|
84
|
+
@data = new_object_list
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Return Lorj::Data object type
|
89
|
+
attr_reader :type
|
90
|
+
|
91
|
+
# Return :object type of the Lorj::Data object.
|
92
|
+
#
|
93
|
+
# * *Args* :
|
94
|
+
# Nothing
|
95
|
+
#
|
96
|
+
# * *Returns* :
|
97
|
+
# - +type+ : Symbol or nil
|
98
|
+
# nil if no object.
|
99
|
+
# :object type
|
100
|
+
#
|
101
|
+
# * *Raises* :
|
102
|
+
# No exceptions
|
103
|
+
#
|
104
|
+
def object_type?
|
105
|
+
@data[:object_type]
|
106
|
+
end
|
107
|
+
|
108
|
+
# Set a Lorj::Data object and return itself.
|
109
|
+
#
|
110
|
+
# There 3 usages:
|
111
|
+
# - Set from a Lorj::Data.
|
112
|
+
# ex: if data is already a Lorj::Data,
|
113
|
+
# copy = Lorj::Data.new()
|
114
|
+
# copy.set(data)
|
115
|
+
# - Set from an object, not Lorj::Data and not a list.
|
116
|
+
# ex:
|
117
|
+
# data = { :test => 'toto'}
|
118
|
+
# copy = Lorj::Data.new()
|
119
|
+
# copy.set(data, :object) { |oObject |
|
120
|
+
# oObject
|
121
|
+
# }
|
122
|
+
# - Set from a list of objects, not Lorj::Data and not a :object.
|
123
|
+
# ex:
|
124
|
+
# data = [{ :name => 'toto'}, {:name => 'test'}]
|
125
|
+
# copy = Lorj::Data.new()
|
126
|
+
# copy.set(data, :list, { :name => /^t/ }) { |oObject |
|
127
|
+
# oObject
|
128
|
+
# }
|
129
|
+
#
|
130
|
+
# * *Args* :
|
131
|
+
# - +data+ : Lorj::Data or any other data.
|
132
|
+
# - +ObjType: required only if data is not a Lorj::Data
|
133
|
+
# Use :object to store and extract attributes
|
134
|
+
# Use :list to extract elements, store them and extract attributes for
|
135
|
+
# each of them. data must support each(oObject) loop function
|
136
|
+
# - +Query+ : Optional. To store the query object used to get the list
|
137
|
+
# objects. It assumes ObjectType = :list
|
138
|
+
# - +yield+ : code to extract +data+ attributes from the
|
139
|
+
# object (:object or :list). Should return an hash containing
|
140
|
+
# attributes data.
|
141
|
+
#
|
142
|
+
# * *Returns* :
|
143
|
+
# - +self+ : Lorj::Data
|
144
|
+
#
|
145
|
+
# * *Raises* :
|
146
|
+
# No exceptions
|
147
|
+
#
|
148
|
+
def set(oObj, sObjType = nil, hQuery = {})
|
149
|
+
return obj_data_set(oObj, sObjType) if oObj.is_a?(Lorj::Data)
|
150
|
+
|
151
|
+
# while saving the object, a mapping work is done?
|
152
|
+
case @type
|
153
|
+
when :data, :object
|
154
|
+
@data[:object_type] = sObjType
|
155
|
+
@data[:object] = oObj
|
156
|
+
@data[:attrs] = yield(sObjType, oObj)
|
157
|
+
when :list
|
158
|
+
list_set(oObj, sObjType, hQuery)
|
159
|
+
end
|
160
|
+
self
|
161
|
+
end
|
162
|
+
|
163
|
+
# Set the :object type
|
164
|
+
#
|
165
|
+
# * *Args* :
|
166
|
+
# - +ObjType: required only if data is not a Lorj::Data
|
167
|
+
#
|
168
|
+
# * *Returns* :
|
169
|
+
# - +self+ : Lorj::Data
|
170
|
+
#
|
171
|
+
# * *Raises* :
|
172
|
+
# No exceptions
|
173
|
+
#
|
174
|
+
def type=(sObjType)
|
175
|
+
return self if self.empty?
|
176
|
+
@data[:object_type] = sObjType
|
177
|
+
self
|
178
|
+
end
|
179
|
+
|
180
|
+
# Get value from Lorj::data
|
181
|
+
#
|
182
|
+
# * *Args* :
|
183
|
+
# - +keys+: See get function.
|
184
|
+
#
|
185
|
+
# * *Returns* :
|
186
|
+
# - +self+ : Lorj::Data
|
187
|
+
#
|
188
|
+
# * *Raises* :
|
189
|
+
# No exceptions
|
190
|
+
#
|
191
|
+
def [](*key)
|
192
|
+
get(*key)
|
193
|
+
end
|
194
|
+
|
195
|
+
# Set Lorj::data attribute value for an :object
|
196
|
+
#
|
197
|
+
# * *Args* :
|
198
|
+
# - +keys+ : attribute keys
|
199
|
+
# - +value+: Value to set
|
200
|
+
#
|
201
|
+
# * *Returns* :
|
202
|
+
# true
|
203
|
+
#
|
204
|
+
# * *Raises* :
|
205
|
+
# No exceptions
|
206
|
+
#
|
207
|
+
def []=(*key, value)
|
208
|
+
return false if @type == :list
|
209
|
+
@data.rh_set(value, :attrs, key)
|
210
|
+
true
|
211
|
+
end
|
212
|
+
|
213
|
+
# Get value from Lorj::data
|
214
|
+
# Depending on Lorj::Data type, you can get:
|
215
|
+
# - :object
|
216
|
+
# - get internal object data (:object)
|
217
|
+
# ex: object = data[:object]
|
218
|
+
# - get attribute data
|
219
|
+
# ex:
|
220
|
+
# data = { :name => 'toto'}
|
221
|
+
# copy = Lorj::Data.new()
|
222
|
+
# copy.set(data, :object) { |oObject |
|
223
|
+
# {:real_name => oObject[:name]}
|
224
|
+
# }
|
225
|
+
#
|
226
|
+
# puts copy[:name] # => nil
|
227
|
+
# puts copy[:real_name] # => 'toto'
|
228
|
+
# puts copy[:object] # => { :name => 'toto'}
|
229
|
+
# puts copy[:attrs] # => { :real_name => 'toto'}
|
230
|
+
# - :list
|
231
|
+
# - get internal object data (:object)
|
232
|
+
# ex: object = data[:object]
|
233
|
+
# - get stored query object data (:query)
|
234
|
+
# ex: object = data[:query]
|
235
|
+
# - get one element attribute or object data
|
236
|
+
# ex:
|
237
|
+
# data = [{ :name => 'toto'}, {:name => 'test'}]
|
238
|
+
# copy = Lorj::Data.new()
|
239
|
+
# copy.set(data, :list, { :name => /^t/ }) { |oObject |
|
240
|
+
# {:real_name => oObject[:name]}
|
241
|
+
# }
|
242
|
+
#
|
243
|
+
# puts copy[0] # => { :real_name => 'toto'}
|
244
|
+
# puts copy[0][:object] # => { :name => 'toto'}
|
245
|
+
# puts copy[0][:attrs] # => { :real_name => 'toto'}
|
246
|
+
# puts copy[1] # => { :real_name => 'test'}
|
247
|
+
# puts copy[1, :real_name] # => 'test'
|
248
|
+
# puts copy[1, :test] # => nil
|
249
|
+
#
|
250
|
+
# * *Args* :
|
251
|
+
# - +keys+: See get function.
|
252
|
+
#
|
253
|
+
# * *Returns* :
|
254
|
+
# - +self+ : Lorj::Data
|
255
|
+
#
|
256
|
+
# * *Raises* :
|
257
|
+
# No exceptions
|
258
|
+
#
|
259
|
+
def get(*key)
|
260
|
+
return @data if key.length == 0
|
261
|
+
|
262
|
+
case @type
|
263
|
+
when :data, :object # Return only attrs or the real object.
|
264
|
+
elem_get(*key)
|
265
|
+
when :list
|
266
|
+
list_get(*key)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
# Get the list of elements in an array from Lorj::Data :list type.
|
271
|
+
#
|
272
|
+
# * *Args* :
|
273
|
+
# No parameters
|
274
|
+
#
|
275
|
+
# * *Returns* :
|
276
|
+
# - +Elements+ : Array of elements
|
277
|
+
#
|
278
|
+
# * *Raises* :
|
279
|
+
# No exceptions
|
280
|
+
#
|
281
|
+
def to_a
|
282
|
+
result = []
|
283
|
+
each do |elem|
|
284
|
+
result << elem[:attrs]
|
285
|
+
end
|
286
|
+
result
|
287
|
+
end
|
288
|
+
|
289
|
+
# return true if a data object exist or if an extracted attribute exist.
|
290
|
+
#
|
291
|
+
# * *Args* :
|
292
|
+
# - +keys+ : Keys to verify.
|
293
|
+
#
|
294
|
+
# * *Returns* :
|
295
|
+
# - +exist?+ : true or false.
|
296
|
+
#
|
297
|
+
# * *Raises* :
|
298
|
+
# No exceptions
|
299
|
+
#
|
300
|
+
# Examples:
|
301
|
+
# data = Lorj::Data.new()
|
302
|
+
#
|
303
|
+
# puts data.exist?(:object) # => false
|
304
|
+
#
|
305
|
+
# data.set({ :name => 'toto'}, :object) { |oObject |
|
306
|
+
# {:real_name => oObject[:name]}
|
307
|
+
# }
|
308
|
+
# list = Lorj::Data.new()
|
309
|
+
#
|
310
|
+
# puts data.exist?(:object) # => false
|
311
|
+
#
|
312
|
+
# list.set([{ :name => 'toto'}, {:name => 'test'}], :list) { |oObject |
|
313
|
+
# {:real_name => oObject[:name]}
|
314
|
+
# }
|
315
|
+
#
|
316
|
+
# puts data.exist?(:object) # => true
|
317
|
+
# puts data.exist?(:name) # => true
|
318
|
+
# puts data.exist?(:test) # => false
|
319
|
+
# puts data.exist?(:attrs, :name) # => true
|
320
|
+
# puts list.exist?(0) # => true
|
321
|
+
# puts list.exist?(0, :object) # => true
|
322
|
+
# puts list.exist?(2) # => false
|
323
|
+
# puts list.exist?(2, :object) # => false
|
324
|
+
# puts list.exist?(0, :name) # => true
|
325
|
+
# puts list.exist?(0, :test) # => false
|
326
|
+
def exist?(*key)
|
327
|
+
case @type
|
328
|
+
when :data, :object
|
329
|
+
elem_exist?(*key)
|
330
|
+
when :list
|
331
|
+
list_exist?(*key)
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
# return true if the Lorj::Data object is nil.
|
336
|
+
#
|
337
|
+
# * *Args* :
|
338
|
+
# No parameters
|
339
|
+
#
|
340
|
+
# * *Returns* :
|
341
|
+
# - true/false
|
342
|
+
#
|
343
|
+
# * *Raises* :
|
344
|
+
# No exceptions
|
345
|
+
#
|
346
|
+
def empty?
|
347
|
+
@data[:object].nil?
|
348
|
+
end
|
349
|
+
|
350
|
+
# Redefine nil? Warning! Can be confused. Cannot see if your object is
|
351
|
+
# simply nil or if current object is simply empty.
|
352
|
+
# Use empty? instead.
|
353
|
+
# A warning will be raised soon to ask developer to update it.
|
354
|
+
#
|
355
|
+
# * *Args* :
|
356
|
+
# No parameters
|
357
|
+
#
|
358
|
+
# * *Returns* :
|
359
|
+
# - true/false
|
360
|
+
#
|
361
|
+
# * *Raises* :
|
362
|
+
# No exceptions
|
363
|
+
#
|
364
|
+
def nil?
|
365
|
+
# Obsolete Use empty? instead.
|
366
|
+
@data[:object].nil?
|
367
|
+
end
|
368
|
+
|
369
|
+
# return 0, 1 or N if the Lorj::Data object is nil.
|
370
|
+
# 0 if no objects stored
|
371
|
+
# 1 if an object exist even if type :object or :list
|
372
|
+
# >1 if a list is stored. It will give the number of elements in the list.
|
373
|
+
#
|
374
|
+
# * *Args* :
|
375
|
+
# No parameters
|
376
|
+
#
|
377
|
+
# * *Returns* :
|
378
|
+
# - >=0 : Number of elements
|
379
|
+
#
|
380
|
+
# * *Raises* :
|
381
|
+
# No exceptions
|
382
|
+
#
|
383
|
+
def length
|
384
|
+
case @type
|
385
|
+
when :data
|
386
|
+
return 0 if self.empty?
|
387
|
+
1
|
388
|
+
when :list
|
389
|
+
@data[:list].length
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
# yield loop on a list
|
394
|
+
#
|
395
|
+
# * *Args* :
|
396
|
+
# - +yield+ : sAction = yield (elem), where action can :removed to
|
397
|
+
# remove the element from the list.
|
398
|
+
#
|
399
|
+
# * *Returns* :
|
400
|
+
# no values
|
401
|
+
#
|
402
|
+
# * *Raises* :
|
403
|
+
# No exceptions
|
404
|
+
#
|
405
|
+
def each(sData = :list)
|
406
|
+
to_remove = []
|
407
|
+
return nil if @type != :list || ![:object, :list].include?(sData)
|
408
|
+
|
409
|
+
@data[:list].each do |elem|
|
410
|
+
case yield (elem)
|
411
|
+
when :remove
|
412
|
+
to_remove << elem
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
return if to_remove.length <= 0
|
417
|
+
to_remove.each { | elem | @data[:list].delete(elem) }
|
418
|
+
end
|
419
|
+
|
420
|
+
# yield loop on a list
|
421
|
+
#
|
422
|
+
# * *Args* :
|
423
|
+
# - +yield+ : sAction = yield (index), where action can :removed to
|
424
|
+
# remove the element from the list.
|
425
|
+
#
|
426
|
+
# * *Returns* :
|
427
|
+
# no values
|
428
|
+
#
|
429
|
+
# * *Raises* :
|
430
|
+
# No exceptions
|
431
|
+
#
|
432
|
+
def each_index(sData = :list)
|
433
|
+
to_remove = []
|
434
|
+
return nil if @type != :list || ![:object, :list].include?(sData)
|
435
|
+
|
436
|
+
@data[:list].each_index do |iIndex|
|
437
|
+
case yield (iIndex)
|
438
|
+
when :remove
|
439
|
+
to_remove << @data[:list][iIndex]
|
440
|
+
end
|
441
|
+
end
|
442
|
+
return if to_remove.length <= 0
|
443
|
+
|
444
|
+
to_remove.each { | elem | @data[:list].delete(elem) }
|
445
|
+
end
|
446
|
+
|
447
|
+
# A Lorj::Data can be cached by Lorj::ObjectData.
|
448
|
+
# When adding Lorj::Data to Lorj::ObjectData, Lorj::Data object will be
|
449
|
+
# registered.
|
450
|
+
# This function will determine if this object is registered or not.
|
451
|
+
#
|
452
|
+
attr_accessor :is_registered
|
453
|
+
|
454
|
+
# A Lorj::Data can be cached by Lorj::ObjectData.
|
455
|
+
# When adding Lorj::Data to Lorj::ObjectData, Lorj::Data object will be
|
456
|
+
# registered.
|
457
|
+
# Lorj::ObjectData will call this function to marked it as registered.
|
458
|
+
#
|
459
|
+
# * *Args* :
|
460
|
+
# none
|
461
|
+
#
|
462
|
+
# * *Returns* :
|
463
|
+
# - self
|
464
|
+
#
|
465
|
+
# * *Raises* :
|
466
|
+
# No exceptions
|
467
|
+
#
|
468
|
+
def register
|
469
|
+
@is_registered = true
|
470
|
+
self
|
471
|
+
end
|
472
|
+
|
473
|
+
# A Lorj::Data can be cached by Lorj::ObjectData.
|
474
|
+
# When adding Lorj::Data to Lorj::ObjectData, Lorj::Data object will be
|
475
|
+
# registered.
|
476
|
+
# Lorj::ObjectData will call this function to marked it as unregistered.
|
477
|
+
#
|
478
|
+
# * *Args* :
|
479
|
+
# none
|
480
|
+
#
|
481
|
+
# * *Returns* :
|
482
|
+
# - self
|
483
|
+
#
|
484
|
+
# * *Raises* :
|
485
|
+
# No exceptions
|
486
|
+
#
|
487
|
+
def unregister
|
488
|
+
@is_registered = false
|
489
|
+
self
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
#
|
494
|
+
class Data
|
495
|
+
private
|
496
|
+
|
497
|
+
def list_set(oObj, sObjType = nil, hQuery = {})
|
498
|
+
@data[:object] = oObj
|
499
|
+
@data[:object_type] = sObjType
|
500
|
+
@data[:query] = hQuery
|
501
|
+
|
502
|
+
return self if oObj.nil?
|
503
|
+
|
504
|
+
begin
|
505
|
+
oObj.each do | oElemObject |
|
506
|
+
next if oElemObject.nil?
|
507
|
+
begin
|
508
|
+
data_obj = Lorj::Data.new(:object)
|
509
|
+
data_obj.set(oElemObject, sObjType) do |sObjectType, oObject|
|
510
|
+
yield(sObjectType, oObject)
|
511
|
+
end
|
512
|
+
@data[:list] << data_obj
|
513
|
+
rescue => e
|
514
|
+
runtime_fail "'%s' Mapping attributes issue.\n%s",
|
515
|
+
sObjType, e.message
|
516
|
+
end
|
517
|
+
end
|
518
|
+
rescue => e
|
519
|
+
runtime_fail "each function is not supported by '%s'.\n%s",
|
520
|
+
oObj.class, e.message
|
521
|
+
end
|
522
|
+
self
|
523
|
+
end
|
524
|
+
|
525
|
+
def obj_data_set(oObj, sObjType = nil)
|
526
|
+
type = oObj.type?
|
527
|
+
case type
|
528
|
+
when :data, :object
|
529
|
+
@data[:object_type] = (sObjType.nil?) ? (oObj.object_type?) : sObjType
|
530
|
+
@data[:object] = oObj.get(:object)
|
531
|
+
@data[:attrs] = oObj.get(:attrs)
|
532
|
+
when :list
|
533
|
+
@data[:object_type] = (sObjType.nil?) ? (oObj.object_type?) : sObjType
|
534
|
+
@data[:object] = oObj.get(:object)
|
535
|
+
@data[:list] = oObj.get(:list)
|
536
|
+
@data[:query] = oObj.get(:query)
|
537
|
+
end
|
538
|
+
self
|
539
|
+
end
|
540
|
+
|
541
|
+
def elem_get(*key)
|
542
|
+
return @data[key[0]] if key[0] == :object
|
543
|
+
return @data.rh_get(key) if key[0] == :attrs
|
544
|
+
@data.rh_get(:attrs, key)
|
545
|
+
end
|
546
|
+
|
547
|
+
def list_get(*key)
|
548
|
+
return @data[key[0]] if [:object, :query].include?(key[0])
|
549
|
+
return @data[:list][key[0]] if key.length == 1
|
550
|
+
# can Return only attrs or the real object.
|
551
|
+
@data[:list][key[0]][key[1..-1]]
|
552
|
+
end
|
553
|
+
|
554
|
+
def elem_exist?(*key)
|
555
|
+
return true if key[0] == :object && @data.key?(key[0])
|
556
|
+
return true if key[0] == :attrs && @data.rh_exist?(key)
|
557
|
+
(@data.rh_lexist?(:attrs, key) == key.length + 1)
|
558
|
+
end
|
559
|
+
|
560
|
+
def list_exist?(*key)
|
561
|
+
return true if key[0] == :object && @data.key?(key[0])
|
562
|
+
@data[:list][key[0]].rh_exist?(:attrs, key[1..-1])
|
563
|
+
end
|
564
|
+
|
565
|
+
# Define minimal @data structure for a :list object type.
|
566
|
+
def new_object_list
|
567
|
+
{
|
568
|
+
:object => nil,
|
569
|
+
:object_type => nil,
|
570
|
+
:list => [],
|
571
|
+
:query => nil
|
572
|
+
}
|
573
|
+
end
|
574
|
+
|
575
|
+
# Define minimal @data structure for a :object object type.
|
576
|
+
def new_object
|
577
|
+
{ :object_type => nil,
|
578
|
+
:attrs => {},
|
579
|
+
:object => nil
|
580
|
+
}
|
581
|
+
end
|
582
|
+
end
|
583
|
+
end
|
@@ -0,0 +1,119 @@
|
|
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
|
24
|
+
# mapping and setup
|
25
|
+
# this task to make it to work.
|
26
|
+
|
27
|
+
module Lorj
|
28
|
+
# Class to handle key or keypath on needs
|
29
|
+
# The application configuration can configure a key tree, instead of a key.
|
30
|
+
# KeyPath is used to commonly handle key or key tree.
|
31
|
+
# Thus, a Keypath can be converted in different format:
|
32
|
+
#
|
33
|
+
# Ex:
|
34
|
+
# oKey = KeyPath(:test)
|
35
|
+
# puts oKey.to_s # => 'test'
|
36
|
+
# puts oKey.key # => :test
|
37
|
+
# puts oKey.key[0] # => :test
|
38
|
+
# puts oKey.key[1] # => nil
|
39
|
+
# puts oKey.fpath # => ':test'
|
40
|
+
# puts oKey.tree # => [:test]
|
41
|
+
#
|
42
|
+
# oKey = KeyPath([:test,:test2,:test3])
|
43
|
+
# puts oKey.to_s # => 'test/test2/test3'
|
44
|
+
# puts oKey.key # => :test3
|
45
|
+
# puts oKey.key[0] # => :test
|
46
|
+
# puts oKey.key[1] # => :test2
|
47
|
+
# puts oKey.fpath # => ':test/:test2/:test3'
|
48
|
+
# puts oKey.tree # => [:test,:test2,:test3]
|
49
|
+
#
|
50
|
+
class KeyPath
|
51
|
+
def initialize(sKeyPath = nil)
|
52
|
+
@keypath = []
|
53
|
+
set sKeyPath
|
54
|
+
end
|
55
|
+
|
56
|
+
def key=(sKeyPath)
|
57
|
+
set(sKeyPath)
|
58
|
+
end
|
59
|
+
|
60
|
+
def set(sKeyPath)
|
61
|
+
if sKeyPath.is_a?(Symbol)
|
62
|
+
@keypath = [sKeyPath]
|
63
|
+
elsif sKeyPath.is_a?(Array)
|
64
|
+
@keypath = sKeyPath
|
65
|
+
elsif sKeyPath.is_a?(String)
|
66
|
+
@keypath = string_to_sarray(sKeyPath)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def tree # rubocop: disable TrivialAccessors
|
71
|
+
@keypath
|
72
|
+
end
|
73
|
+
|
74
|
+
def fpath
|
75
|
+
return nil if @keypath.length == 0
|
76
|
+
key_access = @keypath.clone
|
77
|
+
key_access.each_index do |iIndex|
|
78
|
+
next unless key_access[iIndex].is_a?(Symbol)
|
79
|
+
key_access[iIndex] = ':' + key_access[iIndex].to_s
|
80
|
+
end
|
81
|
+
key_access.join('/')
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_s
|
85
|
+
return nil if @keypath.length == 0
|
86
|
+
key_access = @keypath.clone
|
87
|
+
key_access.each_index do |iIndex|
|
88
|
+
next unless key_access[iIndex].is_a?(Symbol)
|
89
|
+
key_access[iIndex] = key_access[iIndex].to_s
|
90
|
+
end
|
91
|
+
key_access.join('/')
|
92
|
+
end
|
93
|
+
|
94
|
+
def key(iIndex = -1)
|
95
|
+
return nil if @keypath.length == 0
|
96
|
+
@keypath[iIndex] if length >= 1
|
97
|
+
end
|
98
|
+
|
99
|
+
def length
|
100
|
+
@keypath.length
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def string_to_sarray(sKeyPath)
|
106
|
+
if %r{[^\\/]?/[^/]} =~ sKeyPath || /:[^:\/]/ =~ sKeyPath
|
107
|
+
# keypath to interpret
|
108
|
+
res = sKeyPath.split('/')
|
109
|
+
res.each_index do | iIndex |
|
110
|
+
next unless res[iIndex].is_a?(String)
|
111
|
+
res[iIndex] = res[iIndex][1..-1].to_sym if res[iIndex][0] == ':'
|
112
|
+
end
|
113
|
+
@keypath = res
|
114
|
+
else
|
115
|
+
@keypath = [sKeyPath]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|