Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #4127 - Global Parameters with types
- Loading branch information
Showing
31 changed files
with
238 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module KeyTypesHelper | ||
def param_type_selector(f, only_inline_select = false, options = {}) | ||
common_extra_options = { :size => "col-md-4", :class => "parameter_type_selection without_select2", | ||
:label_help => _("<dl>" + | ||
"<dt>String</dt> <dd>Everything turns into a string.</dd>" + | ||
"<dt>Boolean</dt> <dd>Common representation of boolean values are accepted.</dd>" + | ||
"<dt>Integer</dt> <dd>Integer numbers only, can be negative.</dd>" + | ||
"<dt>Real</dt> <dd>Accept any numerical input.</dd>" + | ||
"<dt>Array</dt> <dd>A valid JSON or YAML input, that must evaluate to an array.</dd>" + | ||
"<dt>Hash</dt> <dd>A valid JSON or YAML input, that must evaluate to an object/map/dict/hash.</dd>" + | ||
"<dt>YAML</dt> <dd>Any valid YAML input.</dd>" + | ||
"<dt>JSON</dt> <dd>Any valid JSON input.</dd>" + | ||
"</dl>").html_safe, | ||
:label_help_options => { :title => _("How values are validated") }} | ||
if lookup_keys_table?(f) | ||
common_extra_options[:disabled] = (f.object.puppet? && !f.object.override) | ||
common_extra_options[:size] = 'col-md-8' | ||
end | ||
method_for_select_f = only_inline_select ? 'selectable_f_inline' : 'selectable_f' | ||
|
||
send(method_for_select_f, f, :parameter_type, | ||
options_for_select(LookupKey::KEY_TYPES.map { |e| [_(e), e] }, f.object.parameter_type), {}, | ||
options.merge(common_extra_options)) | ||
end | ||
|
||
def lookup_keys_table?(f) | ||
f.object.class.table_name == 'lookup_keys' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module KeyType | ||
extend ActiveSupport::Concern | ||
KEY_TYPES = [N_("string"), N_("boolean"), N_("integer"), N_("real"), N_("array"), N_("hash"), N_("yaml"), N_("json")] | ||
|
||
included do | ||
validates :key_type, :inclusion => {:in => KEY_TYPES, :message => N_("invalid")}, :allow_blank => true, :allow_nil => true | ||
|
||
alias_attribute :parameter_type, :key_type | ||
end | ||
|
||
def value_before_type_casting(val) | ||
return val if val.nil? || val.contains_erb? | ||
if key_type.present? | ||
case key_type.to_sym | ||
when :json, :array | ||
val = JSON.dump(val) | ||
when :yaml, :hash | ||
val = YAML.dump val | ||
val.sub!(/\A---\s*$\n/, '') | ||
end | ||
end | ||
val | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module KeyValueValidation | ||
extend ActiveSupport::Concern | ||
|
||
def validate_and_cast_value(object_for_key_type = nil) | ||
object_for_key_type ||= self | ||
return if !self.value.is_a?(String) || value.contains_erb? | ||
Foreman::Parameters::Caster.new(self, :attribute_name => :value, :to => object_for_key_type.key_type).cast! | ||
rescue StandardError, SyntaxError => e | ||
Foreman::Logging.exception("Error while parsing #{object_for_key_type}", e) | ||
errors.add(:value, _("is invalid %s") % object_for_key_type.key_type) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.