opennebula 6.10.3 → 6.99.85.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cloud/CloudClient.rb +3 -3
  3. data/lib/models/role.rb +349 -823
  4. data/lib/models/service.rb +156 -80
  5. data/lib/models/vmrole.rb +703 -0
  6. data/lib/models/vrrole.rb +284 -0
  7. data/lib/models.rb +3 -1
  8. data/lib/opennebula/acl.rb +1 -1
  9. data/lib/opennebula/acl_pool.rb +1 -1
  10. data/lib/opennebula/backupjob.rb +1 -1
  11. data/lib/opennebula/backupjob_pool.rb +1 -1
  12. data/lib/opennebula/client.rb +1 -1
  13. data/lib/opennebula/cluster.rb +45 -2
  14. data/lib/opennebula/cluster_pool.rb +1 -1
  15. data/lib/opennebula/datastore.rb +1 -1
  16. data/lib/opennebula/datastore_pool.rb +1 -1
  17. data/lib/opennebula/document.rb +1 -1
  18. data/lib/opennebula/document_json.rb +1 -1
  19. data/lib/opennebula/document_pool.rb +1 -1
  20. data/lib/opennebula/document_pool_json.rb +1 -1
  21. data/lib/opennebula/error.rb +1 -1
  22. data/lib/opennebula/flow/grammar.rb +1 -1
  23. data/lib/opennebula/flow/service_pool.rb +1 -1
  24. data/lib/opennebula/flow/service_template.rb +353 -97
  25. data/lib/opennebula/flow/service_template_ext.rb +3 -3
  26. data/lib/opennebula/flow/service_template_pool.rb +1 -1
  27. data/lib/opennebula/flow/validator.rb +458 -410
  28. data/lib/opennebula/flow.rb +1 -1
  29. data/lib/opennebula/group.rb +1 -1
  30. data/lib/opennebula/group_pool.rb +1 -1
  31. data/lib/opennebula/hook.rb +1 -1
  32. data/lib/opennebula/hook_log.rb +1 -1
  33. data/lib/opennebula/hook_pool.rb +1 -1
  34. data/lib/opennebula/host.rb +1 -60
  35. data/lib/opennebula/host_pool.rb +1 -1
  36. data/lib/opennebula/image.rb +1 -1
  37. data/lib/opennebula/image_pool.rb +1 -1
  38. data/lib/opennebula/ldap_auth.rb +1 -1
  39. data/lib/opennebula/ldap_auth_spec.rb +1 -1
  40. data/lib/opennebula/lockable_ext.rb +1 -1
  41. data/lib/opennebula/marketplace.rb +1 -1
  42. data/lib/opennebula/marketplace_pool.rb +1 -1
  43. data/lib/opennebula/marketplaceapp.rb +1 -1
  44. data/lib/opennebula/marketplaceapp_ext.rb +14 -211
  45. data/lib/opennebula/marketplaceapp_pool.rb +1 -1
  46. data/lib/opennebula/oneflow_client.rb +11 -9
  47. data/lib/opennebula/pool.rb +1 -1
  48. data/lib/opennebula/pool_element.rb +1 -1
  49. data/lib/opennebula/security_group.rb +1 -1
  50. data/lib/opennebula/security_group_pool.rb +1 -1
  51. data/lib/opennebula/server_cipher_auth.rb +1 -1
  52. data/lib/opennebula/server_x509_auth.rb +1 -1
  53. data/lib/opennebula/ssh_auth.rb +1 -1
  54. data/lib/opennebula/system.rb +1 -1
  55. data/lib/opennebula/template.rb +1 -1
  56. data/lib/opennebula/template_ext.rb +1 -1
  57. data/lib/opennebula/template_pool.rb +1 -1
  58. data/lib/opennebula/user.rb +1 -1
  59. data/lib/opennebula/user_pool.rb +1 -1
  60. data/lib/opennebula/utils.rb +2 -2
  61. data/lib/opennebula/vdc.rb +1 -1
  62. data/lib/opennebula/vdc_pool.rb +1 -1
  63. data/lib/opennebula/virtual_machine.rb +3 -12
  64. data/lib/opennebula/virtual_machine_ext.rb +2 -31
  65. data/lib/opennebula/virtual_machine_pool.rb +1 -1
  66. data/lib/opennebula/virtual_network.rb +1 -1
  67. data/lib/opennebula/virtual_network_pool.rb +1 -1
  68. data/lib/opennebula/virtual_router.rb +1 -1
  69. data/lib/opennebula/virtual_router_pool.rb +1 -1
  70. data/lib/opennebula/vm_group.rb +1 -1
  71. data/lib/opennebula/vm_group_pool.rb +1 -1
  72. data/lib/opennebula/vntemplate.rb +1 -1
  73. data/lib/opennebula/vntemplate_pool.rb +1 -1
  74. data/lib/opennebula/wait_ext.rb +1 -1
  75. data/lib/opennebula/x509_auth.rb +1 -1
  76. data/lib/opennebula/xml_element.rb +2 -2
  77. data/lib/opennebula/xml_pool.rb +1 -1
  78. data/lib/opennebula/xml_utils.rb +1 -1
  79. data/lib/opennebula/zone.rb +1 -1
  80. data/lib/opennebula/zone_pool.rb +1 -1
  81. data/lib/opennebula.rb +2 -2
  82. metadata +6 -67
  83. data/lib/ActionManager.rb +0 -280
  84. data/lib/CommandManager.rb +0 -328
  85. data/lib/DriverExecHelper.rb +0 -213
  86. data/lib/HostSyncManager.rb +0 -111
  87. data/lib/OpenNebulaDriver.rb +0 -223
  88. data/lib/VirtualMachineDriver.rb +0 -404
  89. data/lib/datacenter.rb +0 -1319
  90. data/lib/datastore.rb +0 -1049
  91. data/lib/distributed_firewall.rb +0 -293
  92. data/lib/file_helper.rb +0 -374
  93. data/lib/host.rb +0 -1518
  94. data/lib/logical_port.rb +0 -50
  95. data/lib/logical_switch.rb +0 -77
  96. data/lib/memoize.rb +0 -74
  97. data/lib/network.rb +0 -705
  98. data/lib/nsx_client.rb +0 -157
  99. data/lib/nsx_component.rb +0 -28
  100. data/lib/nsx_constants.rb +0 -162
  101. data/lib/nsx_driver.rb +0 -91
  102. data/lib/nsx_error.rb +0 -77
  103. data/lib/nsx_rule.rb +0 -206
  104. data/lib/nsxt_client.rb +0 -189
  105. data/lib/nsxt_dfw.rb +0 -196
  106. data/lib/nsxt_logical_port.rb +0 -94
  107. data/lib/nsxt_rule.rb +0 -188
  108. data/lib/nsxt_tz.rb +0 -38
  109. data/lib/nsxv_client.rb +0 -189
  110. data/lib/nsxv_dfw.rb +0 -202
  111. data/lib/nsxv_logical_port.rb +0 -107
  112. data/lib/nsxv_rule.rb +0 -172
  113. data/lib/nsxv_tz.rb +0 -41
  114. data/lib/opaque_network.rb +0 -134
  115. data/lib/rest_client.rb +0 -191
  116. data/lib/scripts_common.rb +0 -176
  117. data/lib/transport_zone.rb +0 -43
  118. data/lib/vcenter_driver.rb +0 -152
  119. data/lib/vcenter_importer.rb +0 -626
  120. data/lib/vi_client.rb +0 -273
  121. data/lib/vi_helper.rb +0 -328
  122. data/lib/virtual_machine.rb +0 -3574
  123. data/lib/virtual_wire.rb +0 -158
  124. data/lib/vm_device.rb +0 -80
  125. data/lib/vm_disk.rb +0 -202
  126. data/lib/vm_folder.rb +0 -69
  127. data/lib/vm_helper.rb +0 -30
  128. data/lib/vm_monitor.rb +0 -305
  129. data/lib/vm_nic.rb +0 -70
  130. data/lib/vm_template.rb +0 -2112
  131. data/lib/vmm_importer.rb +0 -165
@@ -1,5 +1,5 @@
1
1
  # -------------------------------------------------------------------------- #
2
- # Copyright 2002-2024, OpenNebula Project, OpenNebula Systems #
2
+ # Copyright 2002-2025, OpenNebula Project, OpenNebula Systems #
3
3
  # #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
5
  # not use this file except in compliance with the License. You may obtain #
@@ -15,12 +15,15 @@
15
15
  #--------------------------------------------------------------------------- #
16
16
 
17
17
  require 'uri'
18
+ require 'json'
18
19
 
20
+ # Overwriting hash class with new methods
19
21
  class Hash
22
+
20
23
  # 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
+ # contents of self. If the value for entries with duplicate keys
25
+ # is a Hash, it will be merged recursively, otherwise it will be that
26
+ # of other_hash.
24
27
  #
25
28
  # @param [Hash] other_hash
26
29
  #
@@ -31,348 +34,392 @@ class Hash
31
34
  # h2 = {:a => 22, c => 4, {:b => 5}}
32
35
  #
33
36
  # 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
37
+ def deep_merge(other_hash, merge_array = true)
38
+ target = clone
39
+
40
+ other_hash.each do |key, value|
41
+ current = target[key]
42
+
43
+ target[key] =
44
+ if value.is_a?(Hash) && current.is_a?(Hash)
45
+ current.deep_merge(value)
46
+ elsif (value.is_a?(Array) && current.is_a?(Array)) && merge_array
47
+ current + value
48
+ else
49
+ value
50
+ end
51
+ end
52
+
53
+ target
54
+ end
55
+
56
+ class << self
57
+
58
+ # Converts a hash to a raw String in the form KEY = VAL
59
+ #
60
+ # @param template [String] Hash content
61
+ #
62
+ # @return [Hash, OpenNebula::Error] String representation in the form KEY = VALUE of
63
+ # the hash, or an OpenNebula Error if the conversion fails
64
+ def to_raw(content_hash)
65
+ return '' if content_hash.nil? || content_hash.empty?
66
+
67
+ content = ''
68
+ content_hash.each do |key, value|
69
+ case value
70
+ when Hash
71
+ sub_content = to_raw(value)
72
+
73
+ content += "#{key} = [\n"
74
+ content_lines = sub_content.split("\n")
75
+
76
+ content_lines.each_with_index do |line, index|
77
+ content += line.to_s
78
+ content += ",\n" unless index == content_lines.size - 1
46
79
  end
47
- }
48
- else
49
- target[hash_key] = hash_value
80
+
81
+ content += "\n]\n"
82
+ when Array
83
+ value.each do |element|
84
+ content += to_raw({ key.to_s => element })
85
+ end
86
+ else
87
+ content += "#{key} = \"#{value.to_s.gsub('"', '\"')}\"\n"
88
+ end
50
89
  end
90
+
91
+ content
92
+ rescue StandardError => e
93
+ return OpenNebula::Error.new("Error wrapping the hash: #{e.message}")
51
94
  end
52
95
 
53
- target
54
- end
96
+ end
97
+
55
98
  end
56
99
 
57
100
  module Validator
58
101
 
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)
102
+ class ParseException < StandardError; end
103
+ class SchemaException < StandardError; end
104
+
105
+ # The Validator class is used to validate a JSON body based on a schema
106
+ # which is a Hash that describes the structure of the body.
107
+ class Validator
108
+
109
+ # @param [Hash] opts the options to validate a body
110
+ # @option opts [Boolean] :default_values Set default values if the schema
111
+ # specifies it (if true)
112
+ # @option opts [Boolean] :delete_extra_properties If the body contains properties
113
+ # not specified in the schema delete them from the body (if true)
114
+ # or raise an exception (if false)
115
+ # @option opts [Boolean] :allow_extra_properties Allow properties
116
+ # not specified in the schema
117
+ def initialize(opts = {})
118
+ @opts = {
119
+ :default_values => true,
120
+ :delete_extra_properties => false,
121
+ :allow_extra_properties => false
122
+ }.merge(opts)
119
123
  end
120
124
 
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"
125
+ # Recursively validate and modify a JSON body based on a schema.
126
+ #
127
+ # @see http://tools.ietf.org/html/draft-zyp-json-schema-03
128
+ #
129
+ # @param [Hash, Array, String, nil] body JSON represented as Ruby objects
130
+ # @param [Hash] schema that will be used to validate
131
+ # @param [String] key of the body that will be validated in this step
132
+ #
133
+ # @return [Hash, Array, String, nil] The modified body
134
+ #
135
+ # @raise [SchemaException] If the schema is not correctly defined
136
+ # @raise [ParseException] if the body does not meet the schema definition
137
+ #
138
+ # @example Validate a User
139
+ # schema = {
140
+ # :type => :object,
141
+ # :properties => {
142
+ # 'username' => {
143
+ # :type => :string
144
+ # }
145
+ # }
146
+ # }
147
+ #
148
+ # hash = {
149
+ # 'username' => 'pepe'
150
+ # }
151
+ #
152
+ # Validator.validate!(hash, schema)
153
+ # #=> {'username' => 'pepe'}
154
+ #
155
+ # @note The parameter body will be modified
156
+ # @note Schema options supported
157
+ # :extends
158
+ # :type => [:object, :array, :string, :null]
159
+ #
160
+ def validate!(body, schema, key = '')
161
+ if schema[:extends]
162
+ base_schema = schema.delete(:extends)
163
+ schema = base_schema.deep_merge(schema)
164
+ end
165
+
166
+ case schema[:type]
167
+ when :object then validate_object(body, schema, key)
168
+ when :array then validate_array(body, schema, key)
169
+ when :string then validate_string(body, schema, key)
170
+ when :integer then validate_integer(body, schema, key)
171
+ when :null then validate_null(body, schema, key)
172
+ when :boolean then validate_boolean(body, schema, key)
173
+ else raise SchemaException, "Unsupported type #{schema[:type]}"
174
+ end
129
175
  end
130
- end
131
176
 
132
- private
177
+ private
178
+
179
+ # Validate an object type
180
+ #
181
+ # @param [Hash] body to be validated
182
+ # @param [Hash] schema_object of the objectto validate the body
183
+ # @param [String] key of the body that will be validated in this step
184
+ #
185
+ # @return [Hash] The modified body
186
+ #
187
+ # @raise [ParseException] if the body does not meet the schema definition
188
+ #
189
+ # @example Validate with default values
190
+ # schema_body = {
191
+ # :type => :object,
192
+ # :properties => {
193
+ # 'username' => {
194
+ # :type => :string,
195
+ # :default => 'def'
196
+ # }
197
+ # }
198
+ #
199
+ # body = {}
200
+ #
201
+ # Validator.validate_object(body, schema_body)
202
+ # #=> {'username' => 'def'}
203
+ #
204
+ # @note The parameter body will be modified
205
+ # @note Schema options supported
206
+ # :properties
207
+ # :required
208
+ # :default
209
+ #
210
+ def validate_object(body, schema_object, key)
211
+ unless body.is_a?(Hash)
212
+ raise ParseException, "KEY: '#{key}' must be an Object;"
213
+ end
133
214
 
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
215
+ return body if schema_object[:properties].empty?
169
216
 
170
- new_body = body.dup
217
+ new_body = body.dup
171
218
 
172
- schema_object[:properties].each{ |schema_key, schema_value|
173
- body_value = new_body.delete(schema_key)
219
+ schema_object[:properties].each do |schema_key, schema_value|
220
+ body_value = new_body.delete(schema_key)
174
221
 
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
222
+ if body_value
223
+ body[schema_key] = validate!(body_value, schema_value, schema_key)
224
+ else
225
+ if schema_value[:required]
226
+ raise ParseException,
227
+ "Mandatory document key '#{schema_key} (#{schema_value[:type]})' is missing from #{JSON.dump(body)}"
228
+ end
182
229
 
183
- if @opts[:default_values] && schema_value[:default]
184
- body[schema_key] = schema_value[:default]
230
+ if @opts[:default_values] && schema_value[:default]
231
+ body[schema_key] = schema_value[:default]
232
+ end
185
233
  end
186
234
  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
235
+
236
+ # raise error if body.keys is not empty
237
+ unless new_body.keys.empty?
238
+ if @opts[:delete_extra_properties]
239
+ new_body.keys.each {|key| body.delete(key) }
198
240
  else
199
- raise ParseException, "KEY: #{new_body.keys.join(', ')} not"\
200
- " allowed;"
241
+ return body if @opts[:allow_extra_properties]
242
+
243
+ raise ParseException, "The following keys are not allowed: #{new_body.keys.join(', ')};"
201
244
  end
202
245
  end
246
+
247
+ body
203
248
  end
204
249
 
205
- body
206
- end
250
+ # Validate an array type
251
+ #
252
+ # @param [Array] body to be validated
253
+ # @param [Hash] schema_array of the object to validate the body
254
+ # @param [String] schema_key of the body that will be validated in this step
255
+ #
256
+ # @return [Hash] The modified body
257
+ #
258
+ # @raise [ParseException] if the body does not meet the schema definition
259
+ #
260
+ # @example Validate array
261
+ # schema = {
262
+ # :type => :array,
263
+ # :items => {
264
+ # :type => :string
265
+ # }
266
+ # }
267
+ #
268
+ # body = ['pepe', 'luis', 'juan']
269
+ #
270
+ # Validator.validate_array(body, schema)
271
+ # #=> 'username' => ['pepe', 'luis', 'juan']
272
+ #
273
+ # @note The parameter body will be modified
274
+ # @note Schema options supported
275
+ # :items
276
+ #
277
+ def validate_array(body, schema_array, schema_key)
278
+ unless body.instance_of?(Array)
279
+ raise ParseException, "KEY: '#{schema_key}' must be an Array;"
280
+ end
207
281
 
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|
282
+ return body if schema_array[:items].empty?
283
+
284
+ body.collect do |body_item|
238
285
  validate!(body_item, schema_array[:items], schema_key)
239
- }
240
- else
241
- raise ParseException, "KEY: '#{schema_key}' must be an Array;"
286
+ end
242
287
  end
243
- end
244
288
 
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};"
289
+ # Validate an integer type
290
+ #
291
+ # @param [Array] body to be validated
292
+ # @param [Hash] schema_array of the object to validate the body
293
+ # @param [String] schema_key of the body that will be validated in this step
294
+ #
295
+ # @return [Hash] The modified body
296
+ #
297
+ # @raise [ParseException] if the body does not meet the schema definition
298
+ #
299
+ # @example Validate array
300
+ # schema = {
301
+ # :type => :integer
302
+ # }
303
+ #
304
+ # body = 5
305
+ #
306
+ # Validator.validate_integer(body, schema)
307
+ # #=> 5
308
+ #
309
+ #
310
+ def validate_integer(body, schema_array, schema_key)
311
+ value = Integer(body)
312
+
313
+ if schema_array[:maximum]
314
+ excl = schema_array[:exclusiveMaximum]
315
+ max = schema_array[:maximum]
316
+ if !(excl ? value < max : value <= max)
317
+ raise ParseException, "KEY: '#{schema_key}' must be "\
318
+ "lower than #{excl ? '' : 'or equal to'} #{max};"
319
+ end
275
320
  end
276
- end
277
321
 
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};"
322
+ if schema_array[:minimum]
323
+ excl = schema_array[:exclusiveMinimum]
324
+ min = schema_array[:minimum]
325
+ if !(excl ? value > min : value >= min)
326
+ raise ParseException, "KEY: '#{schema_key}' must be "\
327
+ "greater than #{excl ? '' : 'or equal to'} #{min};"
328
+ end
284
329
  end
285
- end
286
330
 
287
- value
288
- rescue ArgumentError
289
- raise ParseException, "KEY: '#{schema_key}' must be an Integer;"
290
- end
331
+ value
332
+ rescue ArgumentError
333
+ raise ParseException, "KEY: '#{schema_key}' must be an Integer;"
334
+ end
291
335
 
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;"
336
+ # Validate an null type
337
+ #
338
+ # @param [nil] body to be validated
339
+ # @param [Hash] schema_null of the object to validate the body
340
+ # @param [String] schema_key of the body that will be validated in this step
341
+ #
342
+ # @return [nil]
343
+ #
344
+ # @raise [ParseException] if the body is not nil
345
+ #
346
+ # @example Validate array
347
+ # schema = {
348
+ # :type => :null
349
+ # }
350
+ #
351
+ # body = nil
352
+ #
353
+ # Validator.validate_null(body, schema)
354
+ # #=> nil
355
+ #
356
+ #
357
+ def validate_null(body, schema_null, schema_key)
358
+ return if body.nil?
359
+
360
+ raise ParseException, "KEY: '#{schema_key}' is not allowed in #{schema_null};"
316
361
  end
317
- end
318
362
 
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;"
363
+ # Validate an boolean type
364
+ #
365
+ # @param [Object] body to be validated
366
+ # @param [Hash] schema_boolean of the object to validate the body
367
+ # @param [String] schema_key of the body that will be validated in this step
368
+ #
369
+ # @return [nil]
370
+ #
371
+ # @raise [ParseException] if the body is not a boolean
372
+ #
373
+ # @example Validate array
374
+ # schema = {
375
+ # :type => :boolean
376
+ # }
377
+ #
378
+ # body = true
379
+ #
380
+ # Validator.validate_boolean(body, schema)
381
+ # #=> nil
382
+ #
383
+ #
384
+ def validate_boolean(body, schema_boolean, schema_key)
385
+ if body != true && body != false
386
+ raise ParseException, "KEY: '#{schema_key}' is not allowed in #{schema_boolean};"
387
+ end
388
+
389
+ body
343
390
  end
344
391
 
345
- body
346
- end
392
+ # Validate an string type
393
+ #
394
+ # @param [String] body to be validated
395
+ # @param [Hash] schema_string of the object to validate the body
396
+ # @param [String] schema_key of the body that will be validated in this step
397
+ #
398
+ # @return [String] The modified body
399
+ #
400
+ # @raise [ParseException] if the body does not meet the schema definition
401
+ #
402
+ # @example Validate array
403
+ # schema = {
404
+ # :type => :string
405
+ # }
406
+ #
407
+ # body = "pepe"
408
+ #
409
+ # Validator.validate_string(body, schema)
410
+ # #=> "pepe"
411
+ #
412
+ # @note The parameter body will be modified
413
+ # @note Schema options supported
414
+ # :format
415
+ # :enum
416
+ # :regex
417
+ #
418
+ def validate_string(body, schema_string, schema_key)
419
+ unless body.instance_of?(String)
420
+ raise ParseException, "KEY: '#{schema_key}' must be a String;"
421
+ end
347
422
 
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
423
  if schema_string[:format]
377
424
  check_format(body, schema_string, schema_key)
378
425
  elsif schema_string[:enum]
@@ -382,118 +429,119 @@ class Validator
382
429
  else
383
430
  body
384
431
  end
385
- else
386
- raise ParseException, "KEY: '#{schema_key}' must be a String;"
387
432
  end
388
- end
389
433
 
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;"
434
+ # Validate an string format
435
+ #
436
+ # @param [String] body_value to be validated
437
+ # @param [Hash] schema_string of the object to validate the body
438
+ # @param [String] schema_key of the body that will be validated in this step
439
+ #
440
+ # @return [String] The modified body
441
+ #
442
+ # @raise [ParseException] if the body does not meet the schema definition
443
+ #
444
+ # @example Validate array
445
+ # schema = {
446
+ # :type => :string,
447
+ # :format => :url
448
+ # }
449
+ #
450
+ # body = "http://localhost:4567"
451
+ #
452
+ # Validator.check_format(body, schema)
453
+ # #=> "http://localhost:4567"
454
+ #
455
+ # @note The parameter body will be modified
456
+ # @note Schema options supported
457
+ # :url
458
+ #
459
+ def check_format(body_value, schema_string, schema_key)
460
+ case schema_string[:format]
461
+ when :uri
462
+ begin
463
+ require 'uri'
464
+ URI.parse(body_value)
465
+ rescue URI::InvalidURIError
466
+ raise ParseException, "KEY: '#{schema_key}' must be a valid URL;"
467
+ end
468
+
469
+ body_value
423
470
  end
424
471
 
425
472
  body_value
426
473
  end
427
474
 
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(', ')};"
475
+ # Validate an string enum
476
+ #
477
+ # @param [String] body_value to be validated
478
+ # @param [Hash] schema_string of the object to validate the body
479
+ # @param [String] schema_key of the body that will be validated in this step
480
+ #
481
+ # @return [String] The modified body
482
+ #
483
+ # @raise [ParseException] if the body does not meet the schema definition
484
+ #
485
+ # @example Validate array
486
+ # schema = {
487
+ # :type => :string,
488
+ # :enum => ['juan', 'luis']
489
+ # }
490
+ #
491
+ # body = "juan"
492
+ #
493
+ # Validator.check_enum(body, schema)
494
+ # #=> "juan"
495
+ #
496
+ # @note The parameter body will be modified
497
+ # @note Schema options supported
498
+ # :enum
499
+ #
500
+ def check_enum(body_value, schema_string, schema_key)
501
+ if schema_string[:enum].include?(body_value)
502
+ body_value
503
+ else
504
+ raise ParseException, "KEY: '#{schema_key}' must be one of"\
505
+ " #{schema_string[:enum].join(', ')};"
506
+ end
462
507
  end
463
- end
464
508
 
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};"
509
+ # Validate an string regex
510
+ #
511
+ # @param [String] body_value to be validated
512
+ # @param [Hash] schema_string of the object to validate the body
513
+ # @param [String] schema_key of the body that will be validated in this step
514
+ #
515
+ # @return [String] The modified body
516
+ #
517
+ # @raise [ParseException] if the body does not meet the schema definition
518
+ #
519
+ # @example Validate array
520
+ # schema = {
521
+ # :type => :string,
522
+ # :regex => /^\w+$/
523
+ # }
524
+ #
525
+ # body = "juan"
526
+ #
527
+ # Validator.check_regex(body, schema)
528
+ # #=> "juan"
529
+ #
530
+ # @note The parameter body will be modified
531
+ # @note Schema options supported
532
+ # :enum
533
+ #
534
+ def check_regex(body_value, schema_string, schema_key)
535
+ if schema_string[:regex] =~ body_value
536
+ body_value
537
+ else
538
+ raise(
539
+ ParseException,
540
+ "KEY: '#{schema_key}' must match regexp #{schema_string[:regex].inspect};"
541
+ )
542
+ end
495
543
  end
544
+
496
545
  end
497
- end
498
546
 
499
547
  end