From ae80102acc99b1c9d29bfe9eb8930abe0d3a9f58 Mon Sep 17 00:00:00 2001 From: Gilbert Cherrie Date: Thu, 11 Jul 2024 18:24:52 -0400 Subject: [PATCH] Fix terraform template name --- .../configuration_script_source.rb | 9 ++++----- .../configuration_script_source_spec.rb | 20 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source.rb index 2774d868..3eba437a 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source.rb @@ -46,13 +46,12 @@ def sync # eg. # https://github.ibm.com/manoj-puthran/sample-scripts/tree/v2.0/terraform/templates/hello-world # is converted as - # "hello-world(v2.0):github.ibm.com/manoj-puthran/sample-scripts/terraform/templates" - def self.template_name_from_git_repo_url(git_repo_url, branch_name, relative_path) + # templates/hello-world + def self.template_name_from_git_repo_url(git_repo_url, relative_path) temp_url = git_repo_url # URI library cannot handle git urls, so just convert it to a standard url. temp_url = temp_url.sub(':', '/').sub('git@', 'https://') if temp_url.start_with?('git@') temp_uri = URI.parse(temp_url) - hostname = temp_uri.hostname path = temp_uri.path path = path[0...-4] if path.end_with?('.git') path = path[0...-5] if path.end_with?('.git/') @@ -64,7 +63,7 @@ def self.template_name_from_git_repo_url(git_repo_url, branch_name, relative_pat basename = File.basename(relative_path) parent_path = File.dirname(relative_path) end - "#{basename}(#{branch_name}):#{hostname}#{path}/#{parent_path}" + "#{parent_path}/#{basename}" end private @@ -87,7 +86,7 @@ def find_templates_in_git_repo .select { |_dir, files| files.any? { |f| f.end_with?(".tf", ".tf.json") } } .transform_values! { |files| files.map { |f| File.basename(f) } } .each do |parent_dir, files| - name = self.class.template_name_from_git_repo_url(git_repository.url, scm_branch, parent_dir) + name = self.class.template_name_from_git_repo_url(git_repository.url, parent_dir) # TODO: add parsing for input/output vars input_vars = nil diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source_spec.rb index 5624859a..18af050a 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source_spec.rb @@ -82,7 +82,7 @@ def files_in_repository(git_repo_dir) names = names_and_payloads.collect(&:first) payloads = names_and_payloads.collect(&:second) - expect(names.first).to(eq("hello_world_local(master):#{local_repo}/")) + expect(names.first).to(eq("/hello_world_local")) expected_hash = { "relative_path" => File.dirname(*repo_dir_structure), @@ -114,7 +114,7 @@ def files_in_repository(git_repo_dir) names = names_and_payloads.collect(&:first) payloads = names_and_payloads.collect(&:second) - expect(names.first).to(eq("hello-world(master):#{nested_repo}/templates")) + expect(names.first).to(eq("templates/hello-world")) expected_hash = { "relative_path" => File.dirname(*nested_repo_structure), @@ -167,8 +167,8 @@ def files_in_repository(git_repo_dir) expect(names).to( eq( [ - "hello-world(master):#{multiple_templates_repo}/templates", - "single-vm(master):#{multiple_templates_repo}/templates" + "templates/hello-world", + "templates/single-vm" ] ) ) @@ -213,7 +213,7 @@ def files_in_repository(git_repo_dir) names = names_and_payloads.collect(&:first) payloads = names_and_payloads.collect(&:second) - expect(names.first).to(eq("hello-world(master):#{nested_repo}/templates")) + expect(names.first).to(eq("templates/hello-world")) files = JSON.parse(payloads.first)["files"] @@ -241,15 +241,15 @@ def files_in_repository(git_repo_dir) record = build_record names = record.configuration_script_payloads.pluck(:name) - expect(names).to(match_array(["hello-world(master):#{nested_repo}/templates"])) + expect(names).to(match_array(["templates/hello-world"])) end end end describe "#template_name_from_git_repo_url" do - let(:git_url_branch_path) { ["git@example.com:manoj-puthran/sample-scripts.git", "v2.0", "terraform/templates/hello-world"] } - let(:https_url_branch_path) { ["https://example.com/manoj-puthran/sample-scripts.git", "v2.0", "terraform/templates/hello-world"] } - let(:expected_result) { "hello-world(v2.0):example.com/manoj-puthran/sample-scripts/terraform/templates" } + let(:git_url_branch_path) { ["git@example.com:manoj-puthran/sample-scripts.git", "terraform/templates/hello-world"] } + let(:https_url_branch_path) { ["https://example.com/manoj-puthran/sample-scripts.git", "terraform/templates/hello-world"] } + let(:expected_result) { "terraform/templates/hello-world" } it "supports https urls" do expect(described_class.template_name_from_git_repo_url(*https_url_branch_path)).to(eq(expected_result)) @@ -279,7 +279,7 @@ def files_in_repository(git_repo_dir) names = names_and_payloads.collect(&:first) payloads = names_and_payloads.collect(&:second) - expect(names.first).to(eq("hello_world_local(other_branch):#{local_repo}/")) + expect(names.first).to(eq("/hello_world_local")) expected_hash = { "relative_path" => File.dirname(*repo_dir_structure),