Skip to content

Commit

Permalink
Feature flag terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
claytongentry committed Nov 26, 2024
1 parent 3722978 commit e07c435
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 39 deletions.
19 changes: 3 additions & 16 deletions lib/manifold/api/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions lib/manifold/api/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/manifold/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 28 additions & 19 deletions spec/manifold/api/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions spec/manifold/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down

0 comments on commit e07c435

Please sign in to comment.