class Tfctl::Config

Attributes

config[R]

Public Class Methods

new(config_name:, yaml_config:, aws_org_config:, use_cache: false) click to toggle source
# File lib/tfctl/config.rb, line 13
def initialize(config_name:, yaml_config:, aws_org_config:, use_cache: false)
    cache_file  = "#{PROJECT_ROOT}/.tfctl/#{config_name}_cache.yaml"

    # Get configuration.  Either load from cache or process fresh.
    if use_cache
        @config = read_cache(cache_file)
    else
        @config = load_config(config_name, yaml_config, aws_org_config)
        write_cache(cache_file)
    end
end

Public Instance Methods

[](key) click to toggle source
# File lib/tfctl/config.rb, line 25
def [](key)
    @config[key]
end
each(&block) click to toggle source
# File lib/tfctl/config.rb, line 33
def each(&block)
    @config.each(&block)
end
fetch(key, default) click to toggle source
# File lib/tfctl/config.rb, line 29
def fetch(key, default)
    @config.fetch(key, default)
end
find_accounts(property_name, property_value) click to toggle source

Filters accounts by an account property

# File lib/tfctl/config.rb, line 52
def find_accounts(property_name, property_value)
    output =[]
    @config[:accounts].each do |account|
        if account[property_name] == property_value
            output << account
        end
    end

    if output.empty?
        raise Tfctl::Error, "Account not found with #{property_name}: #{property_value}"
    end

    output
end
find_accounts_regex(property_name, expr) click to toggle source
# File lib/tfctl/config.rb, line 67
def find_accounts_regex(property_name, expr)
    output =[]
    @config[:accounts].each do |account|
        begin
            if account[property_name] =~ /#{expr}/
                output << account
            end
        rescue RegexpError => e
            raise Tfctl::Error, "Regexp: #{e}"
        end
    end

    if output.empty?
        raise Tfctl::Error, "Account not found with #{property_name} matching regex: #{expr}"
    end

    output
end
has_key?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/tfctl/config.rb, line 37
def key?(key)
    @config.key?(key)
end
Also aliased as: has_key?
to_json(*_args) click to toggle source
# File lib/tfctl/config.rb, line 47
def to_json(*_args)
    @config.to_json
end
to_yaml() click to toggle source
# File lib/tfctl/config.rb, line 43
def to_yaml
    @config.to_yaml
end

Private Instance Methods

import_yaml_config(config, yaml_config) click to toggle source

Import yaml config other than organisation defaults sections which are merged elsewhere.

# File lib/tfctl/config.rb, line 147
def import_yaml_config(config, yaml_config)
    yaml_config.delete(:organization_root)
    yaml_config.delete(:organization_units)
    yaml_config.delete(:account_overrides)
    config.merge(yaml_config)
end
load_config(config_name, yaml_config, aws_org_config) click to toggle source

Retrieves AWS Organizations data and merges it with data from yaml config.

# File lib/tfctl/config.rb, line 90
def load_config(config_name, yaml_config, aws_org_config)
    # AWS Organizations data
    config = aws_org_config
    # Merge organization sections from yaml file
    config = merge_accounts_config(config, yaml_config)
    # Import remaining parameters from yaml file
    config = import_yaml_config(config, yaml_config)
    # Set excluded property on any excluded accounts
    config = mark_excluded_accounts(config)
    # Remove any profiles that are unset
    config = remove_unset_profiles(config)
    # Set config name property (based on yaml config file name)
    config[:config_name] = config_name
    config
end
mark_excluded_accounts(config) click to toggle source

Sets :excluded property on any excluded accounts

# File lib/tfctl/config.rb, line 120
def mark_excluded_accounts(config)
    return config unless config.key?(:exclude_accounts)

    config[:accounts].each_with_index do |account, idx|
        # rubocop:disable Style/IfWithBooleanLiteralBranches
        config[:accounts][idx][:excluded] = config[:exclude_accounts].include?(account[:name]) ? true : false
        # rubocop:enable Style/IfWithBooleanLiteralBranches
    end

    config
end
merge_accounts_config(config, yaml_config) click to toggle source

Merge AWS Organizations accounts config with defaults from yaml config

# File lib/tfctl/config.rb, line 155
def merge_accounts_config(config, yaml_config)

    config[:accounts].each_with_index do |account_config, idx|
        account_name       = account_config[:name].to_sym
        account_ou_parents = account_config[:ou_parents]

        # merge any root settings
        account_config = account_config.deep_merge(yaml_config[:organization_root])

        # merge all OU levels settings
        account_ou_parents.each_with_index do |_, i|
            account_ou = account_ou_parents[0..i].join('/').to_sym
            if yaml_config[:organization_units].key?(account_ou)
                account_config = account_config.deep_merge(yaml_config[:organization_units][account_ou])
            end
        end

        # merge any account overrides
        if yaml_config[:account_overrides].key?(account_name)
            account_config = account_config.deep_merge(yaml_config[:account_overrides][account_name])
        end

        config[:accounts][idx] = account_config
    end
    config
end
read_cache(cache_file) click to toggle source
# File lib/tfctl/config.rb, line 111
def read_cache(cache_file)
    unless File.exist?(cache_file)
        raise Tfctl::Error, "Cached configuration not found in: #{cache_file}"
    end

    YAML.load_file(cache_file)
end
remove_unset_profiles(config) click to toggle source
# File lib/tfctl/config.rb, line 132
def remove_unset_profiles(config)
    config[:accounts].each do |account|
        profiles_to_unset = []
        account[:profiles].each do |profile|
            if profile =~  /\.unset$/
                profiles_to_unset << profile
                profiles_to_unset << profile.chomp('.unset')
            end
        end
        account[:profiles] = account[:profiles] - profiles_to_unset
    end
    config
end
write_cache(cache_file) click to toggle source
# File lib/tfctl/config.rb, line 106
def write_cache(cache_file)
    FileUtils.mkdir_p File.dirname(cache_file)
    File.open(cache_file, 'w') { |f| f.write to_yaml }
end