bazaarvoice / cloudformation-ruby-dsl

Ruby DSL for creating Cloudformation templates
Apache License 2.0
210 stars 76 forks source link

Added get_hash method to table library #63

Closed iAnomaly closed 8 years ago

iAnomaly commented 8 years ago

Useful for creating multi-column value hash keyed off the primary column key.

jonaf commented 8 years ago

Hi @iAnomaly , can you give an example use-case for this addition? Thanks!

iAnomaly commented 8 years ago

Sure thing @jonaf. Should I do that here or in https://github.com/bazaarvoice/cloudformation-ruby-dsl/blob/master/examples/cloudformation-ruby-script.rb?

jonaf commented 8 years ago

It would be helpful if you did include a example as well, yes; but actually, I was just curious what you might need something like this for.

iAnomaly commented 8 years ago

We needed this for iteratively creating resources from tabular data. For example, we're managing our Route53 DNS entries as a table and loading multiple columns for use within the DSL. Here is an example:

  domains = Table.load 'maps/domains.txt'
  # CNAMEs
  domains.get_multihash(:purpose, {:product => 'configure', :alias => 'false'}, :prefix, :target).each_pair do |key, value|
    resource key+'Cname', :Type => 'AWS::Route53::RecordSet', :Properties => {
        :Comment => '',
        :HostedZoneName => HOSTED_ZONE,
        :Name => value[:prefix]+'.'+HOSTED_ZONE,
        :Type => 'CNAME',
        :TTL => '900',
        :ResourceRecords => [
          value[:target]+'.'+HOSTED_ZONE
        ]
    }

maps/domains.txt

purpose             product    prefix                     target                         alias  alias_hosted_zone_id  has_origin_domain  origin_target
CDN                 demo       code                       dabc123.cloudfront.net         true   Z2FDTNDATAQYW2        false
Assets              demo       assets                     ddef456.cloudfront.net         true   Z2FDTNDATAQYW2        false
API                 demo       api                        prod-api                       false                        false              prod-api-origin.whatever.net.
iAnomaly commented 8 years ago

@jonaf Done! I have incorporated your requests and added an example.

jonaf commented 8 years ago

@iAnomaly This looks good to me! Would you mind squashing+rebasing according to https://github.com/bazaarvoice/cloudformation-ruby-dsl/blob/master/docs/Contributing.md#git for a nice, clean merge of this PR?

iAnomaly commented 8 years ago

Done @jonaf! Rebase squashed to a single commit as requested.