From af9e0a42ea6b5490b673c9a75346d26d5855914c Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Thu, 5 Dec 2024 19:27:33 +0530 Subject: [PATCH] add spec for adding textarea fields in dialog, for terraform list & map 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 --- .../terraform_template_service_dialog.rb | 6 +- lib/terraform/runner/api_params.rb | 2 +- .../terraform_template_service_dialog_spec.rb | 91 ++++++++++++++++++- 3 files changed, 90 insertions(+), 9 deletions(-) diff --git a/app/models/dialog/terraform_template_service_dialog.rb b/app/models/dialog/terraform_template_service_dialog.rb index c2653dad..e455bf0c 100644 --- a/app/models/dialog/terraform_template_service_dialog.rb +++ b/app/models/dialog/terraform_template_service_dialog.rb @@ -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) @@ -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? diff --git a/lib/terraform/runner/api_params.rb b/lib/terraform/runner/api_params.rb index 7dacab22..a22abda2 100644 --- a/lib/terraform/runner/api_params.rb +++ b/lib/terraform/runner/api_params.rb @@ -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' diff --git a/spec/models/dialog/terraform_template_service_dialog_spec.rb b/spec/models/dialog/terraform_template_service_dialog_spec.rb index b9c4f38c..9a97c117 100644 --- a/spec/models/dialog/terraform_template_service_dialog_spec.rb +++ b/spec/models/dialog/terraform_template_service_dialog_spec.rb @@ -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}] @@ -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 @@ -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 @@ -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 @@ -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 @@ -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? @@ -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,