diff --git a/lib/manifold/api/project.rb b/lib/manifold/api/project.rb index 3d1b143..3e71ad4 100644 --- a/lib/manifold/api/project.rb +++ b/lib/manifold/api/project.rb @@ -22,9 +22,9 @@ def workspaces @workspaces ||= workspace_directories.map { |dir| Workspace.from_directory(dir, logger:) } end - def generate - workspaces.each(&:generate) - generate_terraform_entrypoint + def generate(generate_terraform: false) + workspaces.each { |w| w.generate(generate_terraform:) } + generate_terraform_entrypoint if generate_terraform end def workspaces_directory @@ -45,19 +45,6 @@ def generate_terraform_entrypoint config = Terraform::ProjectConfiguration.new(workspaces) config.write(directory.join("main.tf.json")) end - - def generate_workspace_modules - workspaces.each_with_object({}) do |workspace, modules| - modules[workspace.name] = { - "source" => "./workspaces/#{workspace.name}", - "project_id" => "${var.PROJECT_ID}" - } - end - end - - def terraform_path - directory.join("main.tf.json") - end end end end diff --git a/lib/manifold/api/workspace.rb b/lib/manifold/api/workspace.rb index 4b1b86f..038745c 100644 --- a/lib/manifold/api/workspace.rb +++ b/lib/manifold/api/workspace.rb @@ -26,11 +26,11 @@ def add FileUtils.cp(template_path, manifold_path) end - def generate + def generate(generate_terraform: false) return unless manifold_exists? && any_vectors? generate_dimensions - generate_terraform + do_generate_terraform if generate_terraform logger.info("Generated BigQuery dimensions table schema for workspace '#{name}'.") end @@ -101,7 +101,7 @@ def vectors manifold_yaml["vectors"] end - def generate_terraform + def do_generate_terraform config = Terraform::WorkspaceConfiguration.new(name) config.write(terraform_main_path) end diff --git a/lib/manifold/cli.rb b/lib/manifold/cli.rb index 0d7a7f7..675bb12 100644 --- a/lib/manifold/cli.rb +++ b/lib/manifold/cli.rb @@ -46,11 +46,12 @@ def add(name) end desc "generate", "Generate BigQuery schema for all workspaces in the project" + method_option :tf, type: :boolean, desc: "Generate Terraform configurations" def generate path = Pathname.pwd name = path.basename.to_s project = API::Project.new(name, directory: path, logger:) - project.generate + project.generate(generate_terraform: options[:tf]) logger.info "Generated BigQuery schema for all workspaces in the project." end end diff --git a/spec/manifold/api/project_spec.rb b/spec/manifold/api/project_spec.rb index 4a4cc29..2625bfb 100644 --- a/spec/manifold/api/project_spec.rb +++ b/spec/manifold/api/project_spec.rb @@ -60,31 +60,40 @@ end end - it "calls generate on each workspace" do - project.generate - expect([workspace_one, workspace_two]).to all(have_received(:generate)) + context "with generate_terraform: false" do + it "does not generate terraform configurations" do + project.generate(generate_terraform: false) + expect(project.directory.join("main.tf.json")).not_to be_file + end end - it "creates a terraform configuration file" do - project.generate - expect(project.directory.join("main.tf.json")).to be_file - end + context "with generate_terraform: true" do + it "creates a terraform configuration file" do + project.generate(generate_terraform: true) + expect(project.directory.join("main.tf.json")).to be_file + end - it "includes workspace modules in the terraform configuration" do - project.generate - config = parse_terraform_config(project) - expect(config["module"]).to include(expected_workspace_modules) - end + it "includes workspace modules in the terraform configuration" do + project.generate(generate_terraform: true) + config = parse_terraform_config(project) + expect(config["module"]).to include(expected_workspace_modules) + end + + def parse_terraform_config(project) + JSON.parse(project.directory.join("main.tf.json").read) + end - def parse_terraform_config(project) - JSON.parse(project.directory.join("main.tf.json").read) + def expected_workspace_modules + { + "workspace_one" => { "source" => "./workspaces/workspace_one", "project_id" => "${var.PROJECT_ID}" }, + "workspace_two" => { "source" => "./workspaces/workspace_two", "project_id" => "${var.PROJECT_ID}" } + } + end end - def expected_workspace_modules - { - "workspace_one" => { "source" => "./workspaces/workspace_one", "project_id" => "${var.PROJECT_ID}" }, - "workspace_two" => { "source" => "./workspaces/workspace_two", "project_id" => "${var.PROJECT_ID}" } - } + it "calls generate on each workspace" do + project.generate + expect([workspace_one, workspace_two]).to all(have_received(:generate)) end end end diff --git a/spec/manifold/cli_spec.rb b/spec/manifold/cli_spec.rb index 33310db..8550e81 100644 --- a/spec/manifold/cli_spec.rb +++ b/spec/manifold/cli_spec.rb @@ -39,6 +39,34 @@ end end + describe "#generate" do + subject(:cli) { described_class.new(logger: null_logger) } + + context "when called with --tf option" do + before do + allow(mock_project).to receive(:generate).with(generate_terraform: true) + end + + it "generates terraform configurations" do + cli.options = { tf: true } + cli.generate + expect(mock_project).to have_received(:generate).with(generate_terraform: true) + end + end + + context "when called without --tf option" do + before do + allow(mock_project).to receive(:generate).with(generate_terraform: false) + end + + it "does not generate terraform configurations" do + cli.options = { tf: false } + cli.generate + expect(mock_project).to have_received(:generate).with(generate_terraform: false) + end + end + end + describe "#add" do subject(:cli) { described_class.new(logger: null_logger) }