class Terraspace::Mod

Example properties:

name: vpc
root: app/modules/vpc, app/stacks/vpc, vendor/modules/vpc or vendor/stacks/vpc
type: module or stack

Attributes

consider_stacks[R]
instance[R]
name[R]
options[R]
resolved[RW]
root_module[RW]

Public Class Methods

new(name, options={}) click to toggle source
# File lib/terraspace/mod.rb, line 14
def initialize(name, options={})
  @name, @options = placeholder(name), options
  @consider_stacks = options[:consider_stacks].nil? ? true : options[:consider_stacks]
  @instance = options[:instance]
end

Public Instance Methods

build_dir(disable_instance: false) click to toggle source

Relative folder path without app or vendor. For example, the actual location can be found in a couple of places

app/modules/vpc
app/stacks/vpc
vendor/modules/vpc
vendor/stacks/vpc

The build folder does not include the app or vendor info.

modules/vpc
# File lib/terraspace/mod.rb, line 101
def build_dir(disable_instance: false)
  if !@instance.nil? && type_dir == "stacks" && !disable_instance
    # add _ in front so instance doesnt collide with other default stacks
    # never add for app/modules sources
    instance_name = [name, @instance].compact.join('.')
  else
    instance_name = name
  end
  [type_dir, instance_name].compact.join('/')
end
cache_dir() click to toggle source

Full path with build_dir

# File lib/terraspace/mod.rb, line 113
def cache_dir
  # config.build.cache_dir is a String or object that respond_to call. IE:
  #   :CACHE_ROOT/:REGION/:ENV/:BUILD_DIR
  #   CustomBuildDir.call
  # The call method should return a String pattern used for substitutions
  object = Terraspace.config.build.cache_dir
  pattern = if object.is_a?(String)
      object
    elsif object.respond_to?(:call)
      object.call(self)
    elsif object.public_instance_methods.include?(:call)
      instance = object.new
      instance.call(self)
    else
      raise "ERROR: config.build.cache_dir is not a String or responds to the .call method."
    end

  expander = Terraspace::Compiler::Expander.autodetect(self)
  expander.expansion(pattern) # pattern is a String that contains placeholders for substitutions
end
check_exist!() click to toggle source
# File lib/terraspace/mod.rb, line 33
    def check_exist!
      return if root

      pretty_paths = paths.map { |p| Terraspace::Util.pretty_path(p) }.join(", ")
      logger.error <<~EOL
        ERROR: Unable to find #{@name.color(:green)}. Searched paths:

            #{pretty_paths}

        To see available stacks, try running:

            terraspace list

      EOL
      ENV['TS_TEST'] ? raise : exit(1)
    end
exist?() click to toggle source
# File lib/terraspace/mod.rb, line 86
def exist?
  !!root
end
placeholder(name) click to toggle source
# File lib/terraspace/mod.rb, line 20
def placeholder(name)
  if name == "placeholder"
    Terraspace::CLI::Build::Placeholder.new(@options).find_stack
  else
    name
  end
end
possible_fake_root() click to toggle source

If the app/stacks/NAME has been removed in source code but stack still exist in the cloud. allow user to delete by materializing an empty stack with the backend.tf Note this does not seem to work for Terraform Cloud as terraform init doesnt seem to download the plugins required. SIt only works for s3, azurerm, and gcs backends. On TFC, you can delete the stack via the GUI though.

down - so user can delete stacks w/o needing to create an empty app/stacks/demo folder
null - for the terraspace summary command when there are zero stacks.
       Also useful for terraspace tfc list_workspaces
# File lib/terraspace/mod.rb, line 80
def possible_fake_root
  if @options[:command] == "down"
    "#{Terraspace.root}/app/stacks/#{@name}" # fake stack root
  end
end
root() click to toggle source
# File lib/terraspace/mod.rb, line 61
def root
  root = paths.find { |p| File.exist?(p) }
  if root.nil?
    possible_fake_root
  else
    root
  end
end
root_module?() click to toggle source
# File lib/terraspace/mod.rb, line 29
def root_module?
  @root_module
end
to_info() click to toggle source
# File lib/terraspace/mod.rb, line 50
def to_info
  {
    build_dir: build_dir,
    cache_dir: Terraspace::Util.pretty_path(cache_dir),
    name: name,
    root: Terraspace::Util.pretty_path(root),
    type: type,
    type_dir: type_dir,
  }
end
type() click to toggle source
# File lib/terraspace/mod.rb, line 135
def type
  root.include?("/stacks/") ? "stack" : "module"
end
type_dir() click to toggle source
# File lib/terraspace/mod.rb, line 139
def type_dir
  type.pluralize
end

Private Instance Methods

paths() click to toggle source
# File lib/terraspace/mod.rb, line 144
def paths
  paths = []
  root = Terraspace.root
  paths << "#{root}/app/stacks/#{@name}" if @consider_stacks
  paths << "#{root}/app/modules/#{@name}"
  paths << "#{root}/vendor/stacks/#{@name}" if @consider_stacks
  paths << "#{root}/vendor/modules/#{@name}"
  paths
end