Skip to content

Commit

Permalink
fix more lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
megan07 committed May 12, 2020
1 parent b0f1a91 commit da123f2
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 171 deletions.
10 changes: 4 additions & 6 deletions compile/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def ansible_style_yaml(obj, options = {})
# Refer to Compile::Core.compile for full details about the compilation
# process.
def compile_file(ctx, source)
$pwd ||= Dir.pwd
compile_string(ctx, File.read($pwd + '/' + source))
compile_string(ctx, File.read(source))
rescue StandardError => e
puts "Error compiling file: #{source}"
raise e
Expand Down Expand Up @@ -227,9 +226,9 @@ def compile_string(ctx, source)
end
end

def autogen_notice(lang)
def autogen_notice(lang, pwd=Dir.pwd)
Thread.current[:autogen] = true
comment_block(compile('templates/autogen_notice.erb').split("\n"), lang)
comment_block(compile(pwd + '/templates/autogen_notice.erb').split("\n"), lang)
end

def autogen_exception
Expand Down Expand Up @@ -261,8 +260,7 @@ def autogen_notice_contrib
end

def get_helper_file(file, remove_copyright_notice = true)
$pwd ||= Dir.pwd
content = IO.read($pwd + '/' + file)
content = IO.read(file)
remove_copyright_notice ? strip_copyright_notice(content) : content
end

Expand Down
32 changes: 16 additions & 16 deletions provider/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def generate(output_folder, types, product_path, dump_yaml)
unless @config.datasources.nil?

FileUtils.mkpath output_folder unless Dir.exist?(output_folder)
$pwd = Dir.pwd
pwd = Dir.pwd
Dir.chdir output_folder
generate_operation(output_folder, types)
Dir.chdir $pwd
generate_operation(pwd, output_folder, types)
Dir.chdir pwd

# Write a file with the final version of the api, after overrides
# have been applied.
Expand Down Expand Up @@ -187,15 +187,15 @@ def compile_common_files(

def compile_file_list(output_folder, files, file_template)
FileUtils.mkpath output_folder unless Dir.exist?(output_folder)
$pwd = Dir.pwd
pwd = Dir.pwd
Dir.chdir output_folder
files.map do |target, source|
Thread.new do
Google::LOGGER.debug "Compiling #{source} => #{target}"
file_template.generate(source, target, self)
file_template.generate(pwd, source, target, self)
end
end.map(&:join)
Dir.chdir $pwd
Dir.chdir pwd
end

def generate_objects(output_folder, types)
Expand Down Expand Up @@ -223,17 +223,17 @@ def generate_objects(output_folder, types)

def generate_object(object, output_folder, version_name)
data = build_object_data(object, output_folder, version_name)
$pwd = Dir.pwd
pwd = Dir.pwd
unless object.exclude_resource
FileUtils.mkpath output_folder unless Dir.exist?(output_folder)
Dir.chdir output_folder
Google::LOGGER.debug "Generating #{object.name} resource"
generate_resource data.clone
generate_resource(pwd, data.clone)
Google::LOGGER.debug "Generating #{object.name} tests"
generate_resource_tests data.clone
generate_resource_sweepers data.clone
generate_resource_files data.clone
Dir.chdir $pwd
generate_resource_tests(pwd, data.clone)
generate_resource_sweepers(pwd, data.clone)
generate_resource_files(pwd, data.clone)
Dir.chdir pwd
end

# if iam_policy is not defined or excluded, don't generate it
Expand All @@ -242,12 +242,12 @@ def generate_object(object, output_folder, version_name)
FileUtils.mkpath output_folder unless Dir.exist?(output_folder)
Dir.chdir output_folder
Google::LOGGER.debug "Generating #{object.name} IAM policy"
generate_iam_policy data.clone
Dir.chdir $pwd
generate_iam_policy(pwd, data.clone)
Dir.chdir pwd
end

# Generate files at a per-resource basis.
def generate_resource_files(data) end
def generate_resource_files(pwd, data) end

def generate_datasources(output_folder, types)
# We need to apply overrides for datasources
Expand Down Expand Up @@ -343,7 +343,7 @@ def update_uri(resource, url_part)
url_part
end

def generate_iam_policy(data) end
def generate_iam_policy(pwd, data) end

# TODO(nelsonjr): Review all object interfaces and move to private methods
# that should not be exposed outside the object hierarchy.
Expand Down
7 changes: 4 additions & 3 deletions provider/file_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FileTemplate
#
# Once the file's contents are written, set the proper [chmod] mode and
# format the file with a language-appropriate formatter.
def generate(template, path, provider)
def generate(pwd, template, path, provider)
# If we've modified a file since starting an MM run, it's a reasonable
# assumption that it was this run that modified it.
if File.exist?(path) && File.mtime(path) > @env[:start_time]
Expand All @@ -57,16 +57,17 @@ def generate(template, path, provider)
# This variable is used in ansible/resource.erb
ctx.local_variable_set('file_relative',
relative_path(@output_folder + '/' + path, @output_folder).to_s)
ctx.local_variable_set('pwd', pwd)

Google::LOGGER.debug "Generating #{path}"
File.open(path, 'w') { |f| f.puts compile_file(ctx, template) }
File.open(path, 'w') { |f| f.puts compile_file(ctx, pwd + '/' + template) }

# Files are often generated in parallel.
# We can use thread-local variables to ensure that autogen checking
# stays specific to the file each thred represents.
raise "#{path} missing autogen" unless Thread.current[:autogen]

old_file_chmod_mode = File.stat($pwd + '/' + template).mode
old_file_chmod_mode = File.stat(pwd + '/' + template).mode
FileUtils.chmod(old_file_chmod_mode, path)

format_output_file(path)
Expand Down
38 changes: 22 additions & 16 deletions provider/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,19 @@ def folder_name(version)
# This function uses the resource.erb template to create one file
# per resource. The resource.erb template forms the basis of a single
# GCP Resource on Terraform.
def generate_resource(data)
def generate_resource(pwd, data)
name = data.object.name.underscore
product_name = data.product.name.underscore

FileUtils.mkpath folder_name(data.version) unless Dir.exist?(folder_name(data.version))
data.generate('/templates/terraform/resource.erb',
data.generate(pwd,
'/templates/terraform/resource.erb',
"#{folder_name(data.version)}/resource_#{product_name}_#{name}.go",
self)
generate_documentation(data)
generate_documentation(pwd, data)
end

def generate_documentation(data)
def generate_documentation(pwd, data)
target_folder = data.output_folder
target_folder = File.join(target_folder, 'website', 'docs', 'r')
FileUtils.mkpath target_folder
Expand All @@ -198,10 +199,10 @@ def generate_documentation(data)

filepath =
File.join(target_folder, "#{product_name}_#{name}.html.markdown")
data.generate('templates/terraform/resource.html.markdown.erb', filepath, self)
data.generate(pwd, 'templates/terraform/resource.html.markdown.erb', filepath, self)
end

def generate_resource_tests(data)
def generate_resource_tests(pwd, data)
return if data.object.examples
.reject(&:skip_test)
.reject do |e|
Expand All @@ -216,12 +217,13 @@ def generate_resource_tests(data)
data.product = data.product.name
data.resource_name = data.object.name.camelize(:upper)
FileUtils.mkpath folder_name(data.version) unless Dir.exist?(folder_name(data.version))
data.generate('templates/terraform/examples/base_configs/test_file.go.erb',
data.generate(pwd,
'templates/terraform/examples/base_configs/test_file.go.erb',
"#{folder_name(data.version)}/resource_#{product_name}_#{name}_generated_test.go",
self)
end

def generate_resource_sweepers(data)
def generate_resource_sweepers(pwd, data)
return if data.object.skip_sweeper ||
data.object.custom_code.custom_delete ||
data.object.custom_code.pre_delete ||
Expand All @@ -233,12 +235,13 @@ def generate_resource_sweepers(data)
data.product = data.product.name
data.resource_name = data.object.name.camelize(:upper)
FileUtils.mkpath folder_name(data.version) unless Dir.exist?(folder_name(data.version))
data.generate('templates/terraform/sweeper_file.go.erb',
data.generate(pwd,
'templates/terraform/sweeper_file.go.erb',
"#{folder_name(data.version)}/resource_#{product_name}_#{name}_sweeper_test.go",
self)
end

def generate_operation(output_folder, _types)
def generate_operation(pwd, output_folder, _types)
return if @api.objects.select(&:autogen_async).empty?

product_name = @api.name.underscore
Expand All @@ -247,35 +250,38 @@ def generate_operation(output_folder, _types)
data.object = @api.objects.select(&:autogen_async).first
data.async = data.object.async
FileUtils.mkpath folder_name(data.version) unless Dir.exist?(folder_name(data.version))
data.generate('templates/terraform/operation.go.erb',
data.generate(pwd,
'templates/terraform/operation.go.erb',
"#{folder_name(data.version)}/#{product_name}_operation.go",
self)
end

# Generate the IAM policy for this object. This is used to query and test
# IAM policies separately from the resource itself
def generate_iam_policy(data)
def generate_iam_policy(pwd, data)
name = data.object.name.underscore
product_name = data.product.name.underscore

FileUtils.mkpath folder_name(data.version) unless Dir.exist?(folder_name(data.version))
data.generate('templates/terraform/iam_policy.go.erb',
data.generate(pwd,
'templates/terraform/iam_policy.go.erb',
"#{folder_name(data.version)}/iam_#{product_name}_#{name}.go",
self)

# Only generate test if testable examples exist.
unless data.object.examples.reject(&:skip_test).empty?
data.generate(
pwd,
'templates/terraform/examples/base_configs/iam_test_file.go.erb',
"#{folder_name(data.version)}/iam_#{product_name}_#{name}_generated_test.go",
self
)
end

generate_iam_documentation(data)
generate_iam_documentation(pwd, data)
end

def generate_iam_documentation(data)
def generate_iam_documentation(pwd, data)
target_folder = data.output_folder
target_folder = File.join(target_folder, 'website', 'docs', 'r')
FileUtils.mkpath target_folder
Expand All @@ -284,7 +290,7 @@ def generate_iam_documentation(data)

filepath =
File.join(target_folder, "#{product_name}_#{name}_iam.html.markdown")
data.generate('templates/terraform/resource_iam.html.markdown.erb', filepath, self)
data.generate(pwd, 'templates/terraform/resource_iam.html.markdown.erb', filepath, self)
end

def build_object_data(object, output_folder, version)
Expand Down
16 changes: 8 additions & 8 deletions provider/terraform/examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Examples < Api::Object
# Defaults to `templates/terraform/examples/{{name}}.tf.erb`
attr_reader :config_path

def config_documentation
def config_documentation(pwd)
docs_defaults = {
PROJECT_NAME: 'my-project-name',
FIRESTORE_PROJECT_NAME: 'my-project-name',
Expand All @@ -135,26 +135,26 @@ def config_documentation
test_env_vars: test_env_vars.map { |k, v| [k, docs_defaults[v]] }.to_h,
primary_resource_id: primary_resource_id
},
config_path
pwd + '/' + config_path
))
lines(compile_file(
{ content: body },
'templates/terraform/examples/base_configs/documentation.tf.erb'
pwd + '/templates/terraform/examples/base_configs/documentation.tf.erb'
))
end

def config_test
body = config_test_body
def config_test(pwd)
body = config_test_body(pwd)
lines(compile_file(
{
content: body
},
'templates/terraform/examples/base_configs/test_body.go.erb'
pwd + '/templates/terraform/examples/base_configs/test_body.go.erb'
))
end

# rubocop:disable Style/FormatStringToken
def config_test_body
def config_test_body(pwd)
@vars ||= {}
@test_env_vars ||= {}
@test_vars_overrides ||= {}
Expand Down Expand Up @@ -185,7 +185,7 @@ def config_test_body
test_env_vars: test_env_vars.map { |k, _| [k, "%{#{k}}"] }.to_h,
primary_resource_id: primary_resource_id
},
config_path
pwd + '/' + config_path
))

substitute_test_paths body
Expand Down
49 changes: 28 additions & 21 deletions provider/terraform/sub_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,62 @@ module Provider
class Terraform < Provider::AbstractCore
# Functions to compile sub-templates.
module SubTemplate
def build_schema_property(property, object)
compile_template'templates/terraform/schema_property.erb',
def build_schema_property(property, object, pwd)
compile_template pwd + '/templates/terraform/schema_property.erb',
property: property,
object: object
object: object,
pwd: pwd
end

def build_subresource_schema(property, object)
compile_template'templates/terraform/schema_subresource.erb',
def build_subresource_schema(property, object, pwd)
compile_template pwd + '/templates/terraform/schema_subresource.erb',
property: property,
object: object
object: object,
pwd: pwd
end

# Transforms a Cloud API representation of a property into a Terraform
# schema representation.
def build_flatten_method(prefix, property, object)
compile_template 'templates/terraform/flatten_property_method.erb',
def build_flatten_method(prefix, property, object, pwd)
compile_template pwd + '/templates/terraform/flatten_property_method.erb',
prefix: prefix,
property: property,
object: object
object: object,
pwd: pwd
end

# Transforms a Terraform schema representation of a property into a
# representation used by the Cloud API.
def build_expand_method(prefix, property, object)
compile_template 'templates/terraform/expand_property_method.erb',
def build_expand_method(prefix, property, object, pwd)
compile_template pwd + '/templates/terraform/expand_property_method.erb',
prefix: prefix,
property: property,
object: object
object: object,
pwd: pwd
end

def build_expand_resource_ref(var_name, property)
compile_template 'templates/terraform/expand_resource_ref.erb',
def build_expand_resource_ref(var_name, property, pwd)
compile_template pwd + '/templates/terraform/expand_resource_ref.erb',
var_name: var_name,
property: property
property: property,
pwd: pwd
end

def build_property_documentation(property)
def build_property_documentation(property, pwd)
return if property.removed?

compile_template 'templates/terraform/property_documentation.erb',
property: property
compile_template pwd + '/templates/terraform/property_documentation.erb',
property: property,
pwd: pwd
end

def build_nested_property_documentation(property)
def build_nested_property_documentation(property, pwd)
return if property.removed?

compile_template(
'templates/terraform/nested_property_documentation.erb',
property: property
pwd + '/templates/terraform/nested_property_documentation.erb',
property: property,
pwd: pwd
)
end

Expand Down
Loading

0 comments on commit da123f2

Please sign in to comment.