Sha256: 5b027131d2ae789af31af661d7a1fc85db1a55d24a7977a0cbc5a60257ff40fc

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

require 'awspec'

module SelfHeal

'''this function takes a previously captured EC2 instance name and chacks it against the current
inventory. If the resource is found or can be pattern matched to a matching one with a dynamic element updated
the function returns the resource name, otherwise its false'''

  def dynamic_resource(item)
    unless ec2(item).exists?
      resource = Aws::EC2::Resource.new(region: 'eu-west-2')
      capture_group = item.match(/([a-z]+-)([a-z|0-9]+)(\..+)/)

      if capture_group
        network_item = capture_group[1]
        environment = capture_group[3]

        resource.instances.each do | i |
          i.tags.each do | tag |
            if tag.key == 'Name'
              name = tag.value
              regex = /\b#{Regexp.quote(network_item)}[0-9|a-z]+#{Regexp.quote(environment)}\b/
              if name.match(regex)  #unless
                return name
              end
            end
          end
        end
      end
    end
    item
  end
  module_function :dynamic_resource
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aws-spec-etl-0.1.5 lib/self_heal.rb
aws-spec-etl-0.1.4 lib/self_heal.rb
aws-spec-etl-0.1.3 lib/self_heal.rb
aws-spec-etl-0.1.2 lib/self_heal.rb
aws-spec-etl-0.1.1 lib/self_heal.rb
aws-spec-etl-0.1.0 lib/self_heal.rb