-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
377 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "dsl" | ||
|
||
module Terraform | ||
module Config | ||
class Base | ||
include Dsl | ||
|
||
def to_tf | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
module Terraform | ||
module Config | ||
module Dsl | ||
extend Forwardable | ||
|
||
def_delegators :current_context, :put, :output | ||
|
||
def block(name, *labels) | ||
switch_context do | ||
put("#{block_declaration(name, labels)} {\n") | ||
yield | ||
put("}\n") | ||
end | ||
|
||
# There is extra indent for whole output that needs to be removed | ||
output.unindent(2) | ||
end | ||
|
||
def argument(name, value, optional: false) | ||
return if value.nil? && optional | ||
|
||
content = | ||
if value.is_a?(Hash) | ||
"{\n#{value.map { |n, v| "#{n} = #{tf_value(v)}" }.join("\n").indent(2)}\n}\n" | ||
else | ||
"#{tf_value(value)}\n" | ||
end | ||
|
||
put("#{name} = #{content}", indent: 2) | ||
end | ||
|
||
private | ||
|
||
def tf_value(value) | ||
value = value.to_s if value.is_a?(Symbol) | ||
|
||
case value | ||
when String | ||
expression?(value) ? value : "\"#{value}\"" | ||
else | ||
value | ||
end | ||
end | ||
|
||
def expression?(value) | ||
value.start_with?("var.") || value.start_with?("locals.") | ||
end | ||
|
||
def block_declaration(name, labels) | ||
result = name | ||
return result unless labels.any? | ||
|
||
result + " #{labels.map { |label| tf_value(label) }.join(' ')}" | ||
end | ||
|
||
class Context | ||
attr_accessor :output | ||
|
||
def initialize | ||
@output = "" | ||
end | ||
|
||
def put(content, indent: 0) | ||
@output += content.indent(indent) | ||
end | ||
end | ||
|
||
def switch_context | ||
old_context = current_context | ||
@current_context = Context.new | ||
yield | ||
ensure | ||
old_context.put(current_context.output, indent: 2) | ||
@current_context = old_context | ||
end | ||
|
||
def current_context | ||
@current_context ||= Context.new | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Terraform | ||
module Config | ||
class RequiredProvider < Base | ||
attr_reader :name, :options | ||
|
||
def initialize(name, **options) | ||
super() | ||
|
||
@name = name | ||
@options = options | ||
end | ||
|
||
def to_tf | ||
block :terraform do | ||
block :required_providers do | ||
argument name, options | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Style/OptionalBooleanParameter, Lint/UnderscorePrefixedVariableName | ||
class String | ||
# Copied from Rails | ||
def indent(amount, indent_string = nil, indent_empty_lines = false) | ||
dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) } | ||
end | ||
|
||
# Copied from Rails | ||
def indent!(amount, indent_string = nil, indent_empty_lines = false) | ||
indent_string = indent_string || self[/^[ \t]/] || " " | ||
re = indent_empty_lines ? /^/ : /^(?!$)/ | ||
gsub!(re, indent_string * amount) | ||
end | ||
|
||
def unindent(amount, indent_string = " ") | ||
lines.map { |line| line.delete_prefix(indent_string * amount) }.join | ||
end | ||
end | ||
# rubocop:enable Style/OptionalBooleanParameter, Lint/UnderscorePrefixedVariableName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require "pathname" | ||
|
||
GEM_ROOT_PATH = Pathname.new(Dir.pwd) | ||
GEM_TEMP_PATH = GEM_ROOT_PATH.join("tmp") | ||
GENERATOR_PLAYGROUND_PATH = GEM_TEMP_PATH.join("sample-project") | ||
TERRAFORM_CONFIG_DIR_PATH = GENERATOR_PLAYGROUND_PATH.join("terraform") | ||
|
||
describe Command::Terraform::Generate do | ||
before do | ||
FileUtils.rm_r(GENERATOR_PLAYGROUND_PATH) if Dir.exist?(GENERATOR_PLAYGROUND_PATH) | ||
FileUtils.mkdir_p GENERATOR_PLAYGROUND_PATH | ||
|
||
allow(Cpflow).to receive(:root_path).and_return(GENERATOR_PLAYGROUND_PATH) | ||
end | ||
|
||
after do | ||
FileUtils.rm_r GENERATOR_PLAYGROUND_PATH | ||
end | ||
|
||
it "generates terraform config files" do | ||
providers_config_file_path = TERRAFORM_CONFIG_DIR_PATH.join("providers.tf") | ||
|
||
expect(providers_config_file_path).not_to exist | ||
run_cpflow_command(described_class::SUBCOMMAND_NAME, described_class::NAME) | ||
expect(providers_config_file_path).to exist | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe Terraform::Config::Base do | ||
let(:config) { described_class.new } | ||
|
||
describe "#to_tf" do | ||
subject(:to_tf) { config.to_tf } | ||
|
||
it "raises NotImplementedError" do | ||
expect { to_tf }.to raise_error(NotImplementedError) | ||
end | ||
end | ||
end |
Oops, something went wrong.