Skip to content

Commit

Permalink
add spec for adding textarea fields in dialog, for terraform list & m…
Browse files Browse the repository at this point in the history
…ap JSON types

* make the JSON string const variables for regex validation public in the class, to that same can is accessed in the spec files
  • Loading branch information
putmanoj committed Dec 5, 2024
1 parent d453c58 commit af9e0a4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/models/dialog/terraform_template_service_dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def create_dialog(label, terraform_template, extra_vars)
end
end

JSONSTR_LIST_REGEX = '^\[[\W\w]*\]$'.freeze # list of strings or objects
JSONSTR_OBJECT_REGEX = '^\{[\W\w]*\}$'.freeze # map or object

private

def add_template_variables_group(tab, position, terraform_template)
Expand Down Expand Up @@ -103,9 +106,6 @@ def add_variable_field(key, value, group, position, label, description, required
)
end

JSONSTR_LIST_REGEX = '^\[[\W\w]*\]$'.freeze # list of strings or objects
JSONSTR_OBJECT_REGEX = '^\{[\W\w]*\}$'.freeze # map or object

def add_json_variable_field(key, value, group, position, label, description, required, read_only, is_list: false)
value = JSON.pretty_generate(value) if [Hash, Array].include?(value.class)
description = key if description.blank?
Expand Down
2 changes: 1 addition & 1 deletion lib/terraform/runner/api_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.to_cam_parameters(vars)
# @param input_vars [Hash] key/value pairs as input variables for the terraform-runner run job.
# @param type_constraints [Array] array of type constraints objects, from Terraform Runner
#
# @return [Array] Array of param objects [{:name,:value}]
# @return [Array] Array of param objects [{name,value,secured}]
def self.to_normalized_cam_parameters(input_vars, type_constraints)
require 'json'

Expand Down
91 changes: 86 additions & 5 deletions spec/models/dialog/terraform_template_service_dialog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
end
end

context "with terraform boolean input variable" do
context "with terraform input variable of type boolean" do
let(:dialog_label) { "mydialog-with-boolean-field" }
let(:input_vars) do
[{"name" => "set_password", "label" => "set_password", "type" => "boolean", "description" => "Do you want to set the password ?", "required" => false, "secured" => false, "hidden" => false, "immutable" => false, "default" => true}]
Expand All @@ -136,7 +136,7 @@
expect(dialog).to have_attributes(:label => dialog_label, :buttons => "submit,cancel")

group1 = assert_terraform_template_variables_tab(dialog, :group_size => 1)
assert_terraform_variables_group(group1, input_vars, :assert_default_value => {:value => "t"})
assert_terraform_variables_group(group1, input_vars, :assert_default_values => [{:position => 0, :value => "t"}])
end

it "create_dialog with checkbox field, when default value is empty" do
Expand All @@ -148,7 +148,7 @@
expect(dialog).to have_attributes(:label => dialog_label, :buttons => "submit,cancel")

group1 = assert_terraform_template_variables_tab(dialog, :group_size => 1)
assert_terraform_variables_group(group1, input_vars_copy, :assert_default_value => {:value => nil})
assert_terraform_variables_group(group1, input_vars_copy, :assert_default_values => [{:position => 0, :value => nil}])
end

it "create_dialog with checkbox field, when default value is not available" do
Expand All @@ -160,7 +160,59 @@
expect(dialog).to have_attributes(:label => dialog_label, :buttons => "submit,cancel")

group1 = assert_terraform_template_variables_tab(dialog, :group_size => 1)
assert_terraform_variables_group(group1, input_vars_copy, :assert_default_value => {:value => nil})
assert_terraform_variables_group(group1, input_vars_copy, :assert_default_values => [{:position => 0, :value => nil}])
end
end

context "with terraform input variable of type list" do
let(:dialog_label) { "mydialog-with-boolean-field" }
let(:input_vars) do
[
{"name" => "list_with_no_default_value", "label" => "list_with_no_default_value", "type" => "list", "description" => "a list with no default value", "required" => true, "secured" => false, "hidden" => false, "immutable" => false},
{"name" => "list_of_object_with_nested_structures", "label" => "list_of_object_with_nested_structures", "type" => "list", "description" => "list with nested structures", "required" => true, "secured" => false, "hidden" => false, "immutable" => false, "default" => [{"name" => "Production", "website" => {"routing_rules"=>"[\n {\n \"Condition\" = { \"KeyPrefixEquals\": \"img/\" },\n \"Redirect\" = { \"ReplaceKeyPrefixWith\": \"images/\" }\n }\n]\n"}}, {"enabled" => false, "name" => "archived"}]},
{"name" => "list_of_objects", "label" => "list_of_objects", "type" => "list", "description" => "list of objects", "required" => true, "secured" => false, "hidden" => false, "immutable" => false, "default" => [{"external" => 8300, "internal" => 8300, "protocol" => "tcp"}]},
{"name" => "list_of_strings", "label" => "list_of_strings", "type" => "list", "description" => "", "required" => true, "secured" => false, "hidden" => false, "immutable" => false, "default" => ["micro", "large", "xlarge"]},
]
end

it "create_dialog with textarea fields, with prettified default values" do
terraform_template = FactoryBot.create(:terraform_template, :payload => "{\"input_vars\": #{input_vars.to_json}}")
assert_default_values = [
{:position => 0, :value => nil},
{:position => 1, :value => JSON.pretty_generate(input_vars[1]['default'])},
{:position => 2, :value => JSON.pretty_generate(input_vars[2]['default'])},
{:position => 3, :value => JSON.pretty_generate(input_vars[3]['default'])}
]

dialog = described_class.create_dialog(dialog_label, terraform_template, {})
expect(dialog).to have_attributes(:label => dialog_label, :buttons => "submit,cancel")

group1 = assert_terraform_template_variables_tab(dialog, :group_size => 1)
assert_terraform_variables_group(group1, input_vars, :assert_default_values => assert_default_values)
end

context "with terraform input variable of type map (ie object)" do
let(:dialog_label) { "mydialog-with-boolean-field" }
let(:input_vars) do
[
{"name" => "map_without_default_value", "label" => "map_without_default_value", "type" => "map", "description" => "a json map", "required" => true, "secured" => false, "hidden" => false, "immutable" => false},
{"name" => "a_object", "label" => "a_object", "type" => "map", "description" => "", "required" => true, "secured" => false, "hidden" => false, "immutable" => false, "default" => {"age" => 30, "email" => "sam@example.com", "name" => "Sam"}},
]
end

it "create_dialog with textarea fields, with prettified default values" do
terraform_template = FactoryBot.create(:terraform_template, :payload => "{\"input_vars\": #{input_vars.to_json}}")
assert_default_values = [
{:position => 0, :value => nil},
{:position => 1, :value => JSON.pretty_generate(input_vars[1]['default'])}
]

dialog = described_class.create_dialog(dialog_label, terraform_template, {})
expect(dialog).to have_attributes(:label => dialog_label, :buttons => "submit,cancel")

group1 = assert_terraform_template_variables_tab(dialog, :group_size => 1)
assert_terraform_variables_group(group1, input_vars, :assert_default_values => assert_default_values)
end
end
end
end
Expand Down Expand Up @@ -218,7 +270,7 @@ def assert_default_variables_group(group, field_value)
assert_field(fields[0], DialogFieldTextBox, :name => 'name', :default_value => field_value, :data_type => 'string')
end

def assert_terraform_variables_group(group, input_vars, assert_default_value: nil)
def assert_terraform_variables_group(group, input_vars, assert_default_values: [])
expect(group).to have_attributes(:label => "Terraform Template Variables", :display => "edit")

fields = group.dialog_fields
Expand All @@ -229,6 +281,7 @@ def assert_terraform_variables_group(group, input_vars, assert_default_value: ni
"name", "default", "required", "immutable", "hidden", "label", "description", "type"
)

assert_default_value = assert_default_values.find { |e| e[:position] == index }
value = assert_default_value[:value] if assert_default_value.present?
description = name if description.blank?

Expand All @@ -243,6 +296,34 @@ def assert_terraform_variables_group(group, input_vars, assert_default_value: ni
:reconfigurable => true,
:position => index,
:read_only => readonly)
when 'list'
assert_field(fields[index], DialogFieldTextAreaBox,
:name => name,
:default_value => value,
:data_type => 'string',
:display => "edit",
:label => name,
:description => description,
:reconfigurable => true,
:position => index,
:read_only => readonly,
:validator_type => 'regex',
:validator_rule => described_class::JSONSTR_LIST_REGEX,
:validator_message => "This field value must be a JSON List")
when 'map'
assert_field(fields[index], DialogFieldTextAreaBox,
:name => name,
:default_value => value,
:data_type => 'string',
:display => "edit",
:label => name,
:description => description,
:reconfigurable => true,
:position => index,
:read_only => readonly,
:validator_type => 'regex',
:validator_rule => described_class::JSONSTR_OBJECT_REGEX,
:validator_message => "This field value must be a JSON Object or Map")
else
assert_field(fields[index], DialogFieldTextBox,
:name => name,
Expand Down

0 comments on commit af9e0a4

Please sign in to comment.