Sha256: 8086db854ae7b752c9dbcf0a8144cda12360f65f11d92cef10d734072e9bc9b4

Contents?: true

Size: 1.99 KB

Versions: 10

Compression:

Stored size: 1.99 KB

Contents

require 'pathname'

module Builderator
  ##
  # Shared helper methods
  ##
  module Util
    GEM_PATH = Pathname.new(__FILE__).join('../../..').expand_path
    WORKSPACE = '.builderator'.freeze
    VENDOR = 'vendor'.freeze

    class << self
      ##
      # Transform helpers
      ##
      def to_array(arg)
        arg.is_a?(Array) ? arg : [arg]
      end

      def from_tags(aws_tags)
        {}.tap { |tt| aws_tags.each { |t| tt[t.key.to_s] = t.value } }
      end

      ##
      # Relative path from working directory
      ##
      def relative_path(*relative)
        Pathname.pwd.join(*(relative.flatten.map(&:to_s))).expand_path
      end

      def workspace(*relative)
        relative_path(WORKSPACE, relative)
      end

      def vendor(*relative)
        workspace(VENDOR, relative)
      end

      def source_path(*relative)
        GEM_PATH.join(*(relative.flatten.map(&:to_s))).expand_path
      end

      ##
      # Set-filter helpers
      ##
      def filter(resources, filters = {})
        resources.select do |_, r|
          _filter_reduce(r, filters)
        end
      end

      def filter!(resources, filters = {})
        resources.select! do |_, r|
          _filter_reduce(r, filters)
        end

        resources
      end

      ##
      # AWS Clients
      ##
      def ec2
        @ec2 ||= Aws::EC2::Client.new(:region => Config.aws.region)
      end

      def asg
        @asg ||= Aws::AutoScaling::Client.new(:region => Config.aws.region)
      end

      private

      def _filter_reduce(resource, filters)
        filters.reduce(true) do |memo, (k, v)|
          ## Allow for negation with a leading `~`
          if v[0] == '~'
            memo && (!resource[:properties].include?(k.to_s) || resource[:properties][k.to_s] != v[1..-1])
          else
            memo && resource[:properties].include?(k.to_s) && resource[:properties][k.to_s] == v
          end
        end
      end
    end
  end
end

require_relative './util/aws_exception'
require_relative './util/limit_exception'

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
builderator-1.0.0.pre.rc.11 lib/builderator/util.rb
builderator-1.0.0.pre.rc.10 lib/builderator/util.rb
builderator-1.0.0.pre.rc.9 lib/builderator/util.rb
builderator-1.0.0.pre.rc.8 lib/builderator/util.rb
builderator-1.0.0.pre.rc.7 lib/builderator/util.rb
builderator-1.0.0.pre.rc.6 lib/builderator/util.rb
builderator-1.0.0.pre.rc.5 lib/builderator/util.rb
builderator-1.0.0.pre.rc.4 lib/builderator/util.rb
builderator-1.0.0.pre.rc.3 lib/builderator/util.rb
builderator-1.0.0.pre.rc.1 lib/builderator/util.rb