opennebula 5.12.7 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ActionManager.rb +1 -1
  3. data/lib/CommandManager.rb +1 -1
  4. data/lib/DriverExecHelper.rb +44 -28
  5. data/lib/OpenNebulaDriver.rb +8 -4
  6. data/lib/VirtualMachineDriver.rb +9 -2
  7. data/lib/cloud/CloudClient.rb +3 -3
  8. data/lib/datacenter.rb +1257 -0
  9. data/lib/datastore.rb +1025 -0
  10. data/lib/distributed_firewall.rb +280 -0
  11. data/lib/file_helper.rb +370 -0
  12. data/lib/host.rb +1517 -0
  13. data/lib/logical_port.rb +50 -0
  14. data/lib/logical_switch.rb +77 -0
  15. data/lib/memoize.rb +74 -0
  16. data/lib/models.rb +32 -0
  17. data/lib/models/role.rb +1126 -0
  18. data/lib/models/service.rb +709 -0
  19. data/lib/network.rb +699 -0
  20. data/lib/nsx_client.rb +144 -0
  21. data/lib/nsx_component.rb +28 -0
  22. data/lib/nsx_constants.rb +149 -0
  23. data/lib/nsx_driver.rb +78 -0
  24. data/lib/nsx_error.rb +77 -0
  25. data/lib/nsx_rule.rb +193 -0
  26. data/lib/nsxt_client.rb +176 -0
  27. data/lib/nsxt_dfw.rb +196 -0
  28. data/lib/nsxt_logical_port.rb +94 -0
  29. data/lib/nsxt_rule.rb +188 -0
  30. data/lib/nsxt_tz.rb +38 -0
  31. data/lib/nsxv_client.rb +176 -0
  32. data/lib/nsxv_dfw.rb +202 -0
  33. data/lib/nsxv_logical_port.rb +107 -0
  34. data/lib/nsxv_rule.rb +172 -0
  35. data/lib/nsxv_tz.rb +41 -0
  36. data/lib/opaque_network.rb +134 -0
  37. data/lib/opennebula.rb +5 -2
  38. data/lib/opennebula/acl.rb +1 -1
  39. data/lib/opennebula/acl_pool.rb +1 -1
  40. data/lib/opennebula/client.rb +10 -2
  41. data/lib/opennebula/cluster.rb +1 -1
  42. data/lib/opennebula/cluster_pool.rb +1 -1
  43. data/lib/opennebula/datastore.rb +1 -1
  44. data/lib/opennebula/datastore_pool.rb +1 -1
  45. data/lib/opennebula/document.rb +8 -29
  46. data/lib/opennebula/document_json.rb +42 -12
  47. data/lib/opennebula/document_pool.rb +1 -1
  48. data/lib/opennebula/document_pool_json.rb +1 -1
  49. data/lib/opennebula/error.rb +4 -1
  50. data/lib/opennebula/flow.rb +23 -0
  51. data/lib/opennebula/flow/grammar.rb +1195 -0
  52. data/lib/opennebula/flow/service_pool.rb +190 -0
  53. data/lib/opennebula/flow/service_template.rb +572 -0
  54. data/lib/opennebula/flow/service_template_ext.rb +84 -0
  55. data/lib/opennebula/flow/service_template_pool.rb +32 -0
  56. data/lib/opennebula/flow/validator.rb +499 -0
  57. data/lib/opennebula/group.rb +1 -1
  58. data/lib/opennebula/group_pool.rb +1 -1
  59. data/lib/opennebula/hook.rb +5 -12
  60. data/lib/opennebula/hook_log.rb +1 -1
  61. data/lib/opennebula/hook_pool.rb +1 -1
  62. data/lib/opennebula/host.rb +1 -1
  63. data/lib/opennebula/host_pool.rb +1 -1
  64. data/lib/opennebula/image.rb +19 -14
  65. data/lib/opennebula/image_pool.rb +1 -1
  66. data/lib/opennebula/ldap_auth.rb +1 -1
  67. data/lib/opennebula/ldap_auth_spec.rb +1 -1
  68. data/lib/opennebula/lockable_ext.rb +163 -0
  69. data/lib/opennebula/marketplace.rb +1 -1
  70. data/lib/opennebula/marketplace_pool.rb +1 -1
  71. data/lib/opennebula/marketplaceapp.rb +9 -119
  72. data/lib/opennebula/marketplaceapp_ext.rb +586 -0
  73. data/lib/opennebula/marketplaceapp_pool.rb +1 -1
  74. data/lib/opennebula/oneflow_client.rb +4 -3
  75. data/lib/opennebula/pool.rb +19 -3
  76. data/lib/opennebula/pool_element.rb +1 -1
  77. data/lib/opennebula/security_group.rb +1 -1
  78. data/lib/opennebula/security_group_pool.rb +1 -1
  79. data/lib/opennebula/server_cipher_auth.rb +1 -1
  80. data/lib/opennebula/server_x509_auth.rb +1 -1
  81. data/lib/opennebula/ssh_auth.rb +1 -1
  82. data/lib/opennebula/system.rb +1 -1
  83. data/lib/opennebula/template.rb +4 -13
  84. data/lib/opennebula/template_ext.rb +342 -0
  85. data/lib/opennebula/template_pool.rb +1 -1
  86. data/lib/opennebula/user.rb +26 -2
  87. data/lib/opennebula/user_pool.rb +1 -1
  88. data/lib/opennebula/utils.rb +1 -1
  89. data/lib/opennebula/vdc.rb +1 -1
  90. data/lib/opennebula/vdc_pool.rb +1 -1
  91. data/lib/opennebula/virtual_machine.rb +26 -206
  92. data/lib/opennebula/virtual_machine_ext.rb +469 -0
  93. data/lib/opennebula/virtual_machine_pool.rb +5 -1
  94. data/lib/opennebula/virtual_network.rb +4 -10
  95. data/lib/opennebula/virtual_network_pool.rb +1 -1
  96. data/lib/opennebula/virtual_router.rb +4 -12
  97. data/lib/opennebula/virtual_router_pool.rb +1 -1
  98. data/lib/opennebula/vm_group.rb +4 -11
  99. data/lib/opennebula/vm_group_pool.rb +1 -1
  100. data/lib/opennebula/vntemplate.rb +4 -13
  101. data/lib/opennebula/vntemplate_pool.rb +1 -1
  102. data/lib/opennebula/wait_ext.rb +257 -0
  103. data/lib/opennebula/x509_auth.rb +1 -1
  104. data/lib/opennebula/xml_element.rb +1 -1
  105. data/lib/opennebula/xml_pool.rb +1 -1
  106. data/lib/opennebula/xml_utils.rb +1 -1
  107. data/lib/opennebula/zone.rb +1 -1
  108. data/lib/opennebula/zone_pool.rb +1 -1
  109. data/lib/rest_client.rb +201 -0
  110. data/lib/scripts_common.rb +183 -0
  111. data/lib/transport_zone.rb +43 -0
  112. data/lib/vcenter_driver.rb +13 -12
  113. data/lib/vcenter_importer.rb +616 -0
  114. data/lib/vi_client.rb +281 -0
  115. data/lib/vi_helper.rb +313 -0
  116. data/lib/virtual_machine.rb +3477 -0
  117. data/lib/virtual_wire.rb +158 -0
  118. data/lib/vm_device.rb +80 -0
  119. data/lib/vm_disk.rb +202 -0
  120. data/lib/vm_folder.rb +69 -0
  121. data/lib/vm_helper.rb +30 -0
  122. data/lib/vm_monitor.rb +303 -0
  123. data/lib/vm_nic.rb +70 -0
  124. data/lib/vm_template.rb +1963 -0
  125. data/lib/vmm_importer.rb +121 -0
  126. metadata +129 -16
@@ -0,0 +1,84 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+ # Module to decorate ServiceTemplate class with additional helpers not directly
18
+ # exposed through the OpenNebula XMLRPC API. The extensions include
19
+ # - mp_import helper that imports a template into a marketplace
20
+ #
21
+ # rubocop:disable Style/ClassAndModuleChildren
22
+ module OpenNebula::ServiceTemplateExt
23
+
24
+ def self.extend_object(obj)
25
+ if !obj.is_a?(OpenNebula::ServiceTemplate)
26
+ raise StandardError, "Cannot extended #{obj.class} " \
27
+ 'with MarketPlaceAppExt'
28
+ end
29
+
30
+ class << obj
31
+
32
+ ####################################################################
33
+ # Public extended interface
34
+ ####################################################################
35
+ # Imports service template into marketplace
36
+ #
37
+ # @param templates [Hash] Service roles templates information
38
+ # @param market_id [Integer] Marketplace ID to import app
39
+ # @param name [String] Service Template App name
40
+ def mp_import(templates, market_id, name)
41
+ template = ''
42
+ name ||= "#{@body['name']}-#{SecureRandom.hex[0..9]}"
43
+
44
+ template = <<-EOT
45
+ NAME = "#{name}"
46
+ ORIGIN_ID = "-1"
47
+ TYPE = "SERVICE_TEMPLATE"
48
+ APPTEMPLATE64 = "#{Base64.strict_encode64(@body.to_json)}"
49
+ EOT
50
+
51
+ # Add VM template name into roles information
52
+ @body['roles'].each do |role|
53
+ # Find role template into templates to get the name to use
54
+ t = templates.find do |_, v|
55
+ v[:template]['ID'].to_i == role['vm_template']
56
+ end
57
+
58
+ next if t.nil? || t[1].nil? || t[1][:name].nil?
59
+
60
+ app_name = t[1][:name]
61
+
62
+ template << <<-EOT
63
+ ROLE = [ NAME="#{role['name']}", APP="#{app_name}"]
64
+ EOT
65
+
66
+ role.delete('vm_template')
67
+ end
68
+
69
+ xml = MarketPlaceApp.build_xml
70
+ app = MarketPlaceApp.new(xml, @client)
71
+ rc = app.allocate(template, market_id)
72
+
73
+ if OpenNebula.is_error?(rc)
74
+ [-1, rc]
75
+ else
76
+ [0, app.id]
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ end
84
+ # rubocop:enable Style/ClassAndModuleChildren
@@ -0,0 +1,32 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+ module OpenNebula
18
+
19
+ # Service Template pool
20
+ class ServiceTemplatePool < DocumentPoolJSON
21
+
22
+ DOCUMENT_TYPE = 101
23
+
24
+ def factory(element_xml)
25
+ s_template = OpenNebula::ServiceTemplate.new(element_xml, @client)
26
+ s_template.load_body
27
+ s_template
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,499 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+ require 'uri'
18
+
19
+ class Hash
20
+ # Returns a new hash containing the contents of other_hash and the
21
+ # contents of self. If the value for entries with duplicate keys
22
+ # is a Hash, it will be merged recursively, otherwise it will be that
23
+ # of other_hash.
24
+ #
25
+ # @param [Hash] other_hash
26
+ #
27
+ # @return [Hash] Containing the merged values
28
+ #
29
+ # @example Merging two hashes
30
+ # h1 = {:a => 3, {:b => 3, :c => 7}}
31
+ # h2 = {:a => 22, c => 4, {:b => 5}}
32
+ #
33
+ # h1.deep_merge(h2) #=> {:a => 22, c => 4, {:b => 5, :c => 7}}
34
+ def deep_merge(other_hash)
35
+ target = dup
36
+
37
+ other_hash.each do |hash_key, hash_value|
38
+ if hash_value.is_a?(Hash) and self[hash_key].is_a?(Hash)
39
+ target[hash_key] = self[hash_key].deep_merge(hash_value)
40
+ elsif hash_value.is_a?(Array) and self[hash_key].is_a?(Array)
41
+ hash_value.each_with_index { |elem, i|
42
+ if self[hash_key][i].is_a?(Hash) and elem.is_a?(Hash)
43
+ target[hash_key][i] = self[hash_key][i].deep_merge(elem)
44
+ else
45
+ target[hash_key] = hash_value
46
+ end
47
+ }
48
+ else
49
+ target[hash_key] = hash_value
50
+ end
51
+ end
52
+
53
+ target
54
+ end
55
+ end
56
+
57
+ module Validator
58
+
59
+ class ParseException < StandardError; end
60
+ class SchemaException < StandardError; end
61
+
62
+ class Validator
63
+
64
+ # @param [Hash] opts the options to validate a body
65
+ # @option opts [Boolean] :default_values Set default values if the schema
66
+ # specifies it (if true)
67
+ # @option opts [Boolean] :delete_extra_properties If the body contains properties
68
+ # not specified in the schema delete them from the body (if true)
69
+ # or raise an exception (if false)
70
+ # @option opts [Boolean] :allow_extra_properties Allow properties
71
+ # not specified in the schema
72
+ def initialize(opts={})
73
+ @opts = {
74
+ :default_values => true,
75
+ :delete_extra_properties => false,
76
+ :allow_extra_properties => false
77
+ }.merge(opts)
78
+ end
79
+
80
+ # Recursively validate and modify a JSON body based on a schema.
81
+ #
82
+ # @see http://tools.ietf.org/html/draft-zyp-json-schema-03
83
+ #
84
+ # @param [Hash, Array, String, nil] body JSON represented as Ruby objects
85
+ # @param [Hash] schema that will be used to validate
86
+ # @param [String] key of the body that will be validated in this step
87
+ #
88
+ # @return [Hash, Array, String, nil] The modified body
89
+ #
90
+ # @raise [SchemaException] If the schema is not correctly defined
91
+ # @raise [ParseException] if the body does not meet the schema definition
92
+ #
93
+ # @example Validate a User
94
+ # schema = {
95
+ # :type => :object,
96
+ # :properties => {
97
+ # 'username' => {
98
+ # :type => :string
99
+ # }
100
+ # }
101
+ # }
102
+ #
103
+ # hash = {
104
+ # 'username' => 'pepe'
105
+ # }
106
+ #
107
+ # Validator.validate!(hash, schema)
108
+ # #=> {'username' => 'pepe'}
109
+ #
110
+ # @note The parameter body will be modified
111
+ # @note Schema options supported
112
+ # :extends
113
+ # :type => [:object, :array, :string, :null]
114
+ #
115
+ def validate!(body, schema, key="")
116
+ if schema[:extends]
117
+ base_schema = schema.delete(:extends)
118
+ schema = base_schema.deep_merge(schema)
119
+ end
120
+
121
+ case schema[:type]
122
+ when :object then validate_object(body, schema, key)
123
+ when :array then validate_array(body, schema, key)
124
+ when :string then validate_string(body, schema, key)
125
+ when :integer then validate_integer(body, schema, key)
126
+ when :null then validate_null(body, schema, key)
127
+ when :boolean then validate_boolean(body, schema, key)
128
+ else raise SchemaException, "type #{schema[:type]} is not a valid type"
129
+ end
130
+ end
131
+
132
+ private
133
+
134
+ # Validate an object type
135
+ #
136
+ # @param [Hash] body to be validated
137
+ # @param [Hash] schema_object of the objectto validate the body
138
+ # @param [String] key of the body that will be validated in this step
139
+ #
140
+ # @return [Hash] The modified body
141
+ #
142
+ # @raise [ParseException] if the body does not meet the schema definition
143
+ #
144
+ # @example Validate with default values
145
+ # schema_body = {
146
+ # :type => :object,
147
+ # :properties => {
148
+ # 'username' => {
149
+ # :type => :string,
150
+ # :default => 'def'
151
+ # }
152
+ # }
153
+ #
154
+ # body = {}
155
+ #
156
+ # Validator.validate_object(body, schema_body)
157
+ # #=> {'username' => 'def'}
158
+ #
159
+ # @note The parameter body will be modified
160
+ # @note Schema options supported
161
+ # :properties
162
+ # :required
163
+ # :default
164
+ #
165
+ def validate_object(body, schema_object, key)
166
+ unless body.is_a?(Hash)
167
+ raise ParseException, "KEY: #{key} must be a Hash; SCHEMA:"
168
+ end
169
+
170
+ new_body = body.dup
171
+
172
+ schema_object[:properties].each{ |schema_key, schema_value|
173
+ body_value = new_body.delete(schema_key)
174
+
175
+ if body_value
176
+ body[schema_key] = validate!(body_value, schema_value,
177
+ schema_key)
178
+ else
179
+ if schema_value[:required]
180
+ raise ParseException, "KEY: '#{schema_key}' is required;"
181
+ end
182
+
183
+ if @opts[:default_values] && schema_value[:default]
184
+ body[schema_key] = schema_value[:default]
185
+ end
186
+ end
187
+ }
188
+
189
+ # raise error if body.keys is not empty
190
+ unless new_body.keys.empty?
191
+ if @opts[:delete_extra_properties]
192
+ new_body.keys.each{ |key|
193
+ body.delete(key)
194
+ }
195
+ else
196
+ if @opts[:allow_extra_properties]
197
+ return body
198
+ else
199
+ raise ParseException, "KEY: #{new_body.keys.join(', ')} not"\
200
+ " allowed;"
201
+ end
202
+ end
203
+ end
204
+
205
+ body
206
+ end
207
+
208
+ # Validate an array type
209
+ #
210
+ # @param [Array] body to be validated
211
+ # @param [Hash] schema_array of the object to validate the body
212
+ # @param [String] schema_key of the body that will be validated in this step
213
+ #
214
+ # @return [Hash] The modified body
215
+ #
216
+ # @raise [ParseException] if the body does not meet the schema definition
217
+ #
218
+ # @example Validate array
219
+ # schema = {
220
+ # :type => :array,
221
+ # :items => {
222
+ # :type => :string
223
+ # }
224
+ # }
225
+ #
226
+ # body = ['pepe', 'luis', 'juan']
227
+ #
228
+ # Validator.validate_array(body, schema)
229
+ # #=> 'username' => ['pepe', 'luis', 'juan']
230
+ #
231
+ # @note The parameter body will be modified
232
+ # @note Schema options supported
233
+ # :items
234
+ #
235
+ def validate_array(body, schema_array, schema_key)
236
+ if body.instance_of?(Array)
237
+ body.collect { |body_item|
238
+ validate!(body_item, schema_array[:items], schema_key)
239
+ }
240
+ else
241
+ raise ParseException, "KEY: '#{schema_key}' must be an Array;"
242
+ end
243
+ end
244
+
245
+ # Validate an integer type
246
+ #
247
+ # @param [Array] body to be validated
248
+ # @param [Hash] schema_array of the object to validate the body
249
+ # @param [String] schema_key of the body that will be validated in this step
250
+ #
251
+ # @return [Hash] The modified body
252
+ #
253
+ # @raise [ParseException] if the body does not meet the schema definition
254
+ #
255
+ # @example Validate array
256
+ # schema = {
257
+ # :type => :integer
258
+ # }
259
+ #
260
+ # body = 5
261
+ #
262
+ # Validator.validate_integer(body, schema)
263
+ # #=> 5
264
+ #
265
+ #
266
+ def validate_integer(body, schema_array, schema_key)
267
+ value = Integer(body)
268
+
269
+ if schema_array[:maximum]
270
+ excl = schema_array[:exclusiveMaximum]
271
+ max = schema_array[:maximum]
272
+ if !(excl ? value < max : value <= max)
273
+ raise ParseException, "KEY: '#{schema_key}' must be "\
274
+ "lower than #{excl ? '' : 'or equal to'} #{max};"
275
+ end
276
+ end
277
+
278
+ if schema_array[:minimum]
279
+ excl = schema_array[:exclusiveMinimum]
280
+ min = schema_array[:minimum]
281
+ if !(excl ? value > min : value >= min)
282
+ raise ParseException, "KEY: '#{schema_key}' must be "\
283
+ "greater than #{excl ? '' : 'or equal to'} #{min};"
284
+ end
285
+ end
286
+
287
+ value
288
+ rescue ArgumentError
289
+ raise ParseException, "KEY: '#{schema_key}' must be an Integer;"
290
+ end
291
+
292
+ # Validate an null type
293
+ #
294
+ # @param [nil] body to be validated
295
+ # @param [Hash] schema_null of the object to validate the body
296
+ # @param [String] schema_key of the body that will be validated in this step
297
+ #
298
+ # @return [nil]
299
+ #
300
+ # @raise [ParseException] if the body is not nil
301
+ #
302
+ # @example Validate array
303
+ # schema = {
304
+ # :type => :null
305
+ # }
306
+ #
307
+ # body = nil
308
+ #
309
+ # Validator.validate_null(body, schema)
310
+ # #=> nil
311
+ #
312
+ #
313
+ def validate_null(body, schema_null, schema_key)
314
+ if body != nil
315
+ raise ParseException, "KEY: '#{schema_key}' is not allowed;"
316
+ end
317
+ end
318
+
319
+ # Validate an boolean type
320
+ #
321
+ # @param [Object] body to be validated
322
+ # @param [Hash] schema_boolean of the object to validate the body
323
+ # @param [String] schema_key of the body that will be validated in this step
324
+ #
325
+ # @return [nil]
326
+ #
327
+ # @raise [ParseException] if the body is not a boolean
328
+ #
329
+ # @example Validate array
330
+ # schema = {
331
+ # :type => :boolean
332
+ # }
333
+ #
334
+ # body = true
335
+ #
336
+ # Validator.validate_boolean(body, schema)
337
+ # #=> nil
338
+ #
339
+ #
340
+ def validate_boolean(body, schema_boolean, schema_key)
341
+ if body != true && body != false
342
+ raise ParseException, "KEY: '#{schema_key}' is not allowed;"
343
+ end
344
+
345
+ body
346
+ end
347
+
348
+ # Validate an string type
349
+ #
350
+ # @param [String] body to be validated
351
+ # @param [Hash] schema_string of the object to validate the body
352
+ # @param [String] schema_key of the body that will be validated in this step
353
+ #
354
+ # @return [String] The modified body
355
+ #
356
+ # @raise [ParseException] if the body does not meet the schema definition
357
+ #
358
+ # @example Validate array
359
+ # schema = {
360
+ # :type => :string
361
+ # }
362
+ #
363
+ # body = "pepe"
364
+ #
365
+ # Validator.validate_string(body, schema)
366
+ # #=> "pepe"
367
+ #
368
+ # @note The parameter body will be modified
369
+ # @note Schema options supported
370
+ # :format
371
+ # :enum
372
+ # :regex
373
+ #
374
+ def validate_string(body, schema_string, schema_key)
375
+ if body.instance_of?(String)
376
+ if schema_string[:format]
377
+ check_format(body, schema_string, schema_key)
378
+ elsif schema_string[:enum]
379
+ check_enum(body, schema_string, schema_key)
380
+ elsif schema_string[:regex]
381
+ check_regex(body, schema_string, schema_key)
382
+ else
383
+ body
384
+ end
385
+ else
386
+ raise ParseException, "KEY: '#{schema_key}' must be a String;"
387
+ end
388
+ end
389
+
390
+ # Validate an string format
391
+ #
392
+ # @param [String] body_value to be validated
393
+ # @param [Hash] schema_string of the object to validate the body
394
+ # @param [String] schema_key of the body that will be validated in this step
395
+ #
396
+ # @return [String] The modified body
397
+ #
398
+ # @raise [ParseException] if the body does not meet the schema definition
399
+ #
400
+ # @example Validate array
401
+ # schema = {
402
+ # :type => :string,
403
+ # :format => :url
404
+ # }
405
+ #
406
+ # body = "http://localhost:4567"
407
+ #
408
+ # Validator.check_format(body, schema)
409
+ # #=> "http://localhost:4567"
410
+ #
411
+ # @note The parameter body will be modified
412
+ # @note Schema options supported
413
+ # :url
414
+ #
415
+ def check_format(body_value, schema_string, schema_key)
416
+ case schema_string[:format]
417
+ when :uri
418
+ begin
419
+ require 'uri'
420
+ uri = URI.parse(body_value)
421
+ rescue
422
+ raise ParseException, "KEY: '#{schema_key}' must be a valid URL;"
423
+ end
424
+
425
+ body_value
426
+ end
427
+
428
+ body_value
429
+ end
430
+
431
+ # Validate an string enum
432
+ #
433
+ # @param [String] body_value to be validated
434
+ # @param [Hash] schema_string of the object to validate the body
435
+ # @param [String] schema_key of the body that will be validated in this step
436
+ #
437
+ # @return [String] The modified body
438
+ #
439
+ # @raise [ParseException] if the body does not meet the schema definition
440
+ #
441
+ # @example Validate array
442
+ # schema = {
443
+ # :type => :string,
444
+ # :enum => ['juan', 'luis']
445
+ # }
446
+ #
447
+ # body = "juan"
448
+ #
449
+ # Validator.check_enum(body, schema)
450
+ # #=> "juan"
451
+ #
452
+ # @note The parameter body will be modified
453
+ # @note Schema options supported
454
+ # :enum
455
+ #
456
+ def check_enum(body_value, schema_string, schema_key)
457
+ if schema_string[:enum].include?(body_value)
458
+ body_value
459
+ else
460
+ raise ParseException, "KEY: '#{schema_key}' must be one of"\
461
+ " #{schema_string[:enum].join(', ')};"
462
+ end
463
+ end
464
+
465
+ # Validate an string regex
466
+ #
467
+ # @param [String] body_value to be validated
468
+ # @param [Hash] schema_string of the object to validate the body
469
+ # @param [String] schema_key of the body that will be validated in this step
470
+ #
471
+ # @return [String] The modified body
472
+ #
473
+ # @raise [ParseException] if the body does not meet the schema definition
474
+ #
475
+ # @example Validate array
476
+ # schema = {
477
+ # :type => :string,
478
+ # :regex => /^\w+$/
479
+ # }
480
+ #
481
+ # body = "juan"
482
+ #
483
+ # Validator.check_regex(body, schema)
484
+ # #=> "juan"
485
+ #
486
+ # @note The parameter body will be modified
487
+ # @note Schema options supported
488
+ # :enum
489
+ #
490
+ def check_regex(body_value, schema_string, schema_key)
491
+ if schema_string[:regex] =~ body_value
492
+ body_value
493
+ else
494
+ raise ParseException, "KEY: '#{schema_key}' must match regexp #{schema_string[:regex].inspect};"
495
+ end
496
+ end
497
+ end
498
+
499
+ end