Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jun 16, 2016
0 parents commit f051578
Show file tree
Hide file tree
Showing 154 changed files with 1,783 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: ruby
rvm:
- 2.2.2
before_install: gem install bundler -v 1.11.2
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Kevin Deisz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
@@ -0,0 +1,41 @@
# AwsCF

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/aws_cf`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'aws_cf'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install aws_cf

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/aws_cf.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

10 changes: 10 additions & 0 deletions Rakefile
@@ -0,0 +1,10 @@
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.libs << 'lib'
t.test_files = FileList['test/**/*_test.rb']
end

task default: :test
35 changes: 35 additions & 0 deletions aws_cf.gemspec
@@ -0,0 +1,35 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'aws_cf/version'

Gem::Specification.new do |spec|
spec.name = 'aws_cf'
spec.version = AwsCF::VERSION
spec.authors = ['Kevin Deisz']
spec.email = ['kevin.deisz@gmail.com']

spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency 'bundler', '~> 1.11'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'nokogiri'
spec.add_development_dependency 'pry'
end
7 changes: 7 additions & 0 deletions bin/console
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'aws_cf'

require 'irb'
IRB.start
23 changes: 23 additions & 0 deletions bin/get-docs
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'aws_cf'

require 'fileutils'
require 'net/http'
require 'nokogiri'
require 'uri'

def get_url(page)
base_url = 'http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/'
listing = Net::HTTP.get_response(URI.parse(base_url + page)).body
end

Nokogiri::HTML(get_url('aws-template-resource-type-ref.html')).css('.highlights ul li a').each do |link|
contents = Nokogiri::HTML(get_url(link['href'])).at_css('.programlisting').text.strip.split("\n")
next unless (type_line = contents.grep(/Type/).first) && contents.grep(/Properties/).first

File.open(File.join('specs', type_line.scan(/AWS::(.*?)::(.*?)\"/).first.join('-') + '.cf'), 'w') do |file|
file.write(contents.join("\n"))
end
end
13 changes: 13 additions & 0 deletions lib/aws_cf.rb
@@ -0,0 +1,13 @@
require 'json'
require 'pathname'

require 'aws_cf/parser'
require 'aws_cf/props'
require 'aws_cf/resource'
require 'aws_cf/version'

Dir[File.expand_path(File.join('..', '..', 'specs', '*'), __FILE__)].each do |filepath|
group, resource = Pathname.new(filepath).basename('.cf').to_s.split('-')
spec = File.readlines(filepath).slice_after { |line| line.include?('Properties') }.to_a[1].slice_before { |line| line.strip == '}' }.to_a[0]
AwsCF::Resource.register(group, resource, spec.join)
end
23 changes: 23 additions & 0 deletions lib/aws_cf/parser.rb
@@ -0,0 +1,23 @@
module AwsCF
class Parser

attr_accessor :props, :spec

def initialize(spec)
self.spec = spec
end

def parse
self.props = spec.split("\n").map do |spec_line|
prop = Props.from(spec_line)
[prop.name, prop]
end.to_h
end

def self.parse(spec)
parser = new(spec)
parser.parse
parser
end
end
end
28 changes: 28 additions & 0 deletions lib/aws_cf/props.rb
@@ -0,0 +1,28 @@
module AwsCF
module Props
def self.from(spec_line)
if spec_line.include?(':')
key, type = spec_line.strip.gsub(/,\z/, '').split(' : ')
key = key[1..-2]
else
key, type = nil, spec_line.strip
end

case type
when 'Boolean' then BooleanProp.new(key: key)
when 'Integer' then IntegerProp.new(key: key)
when 'String' then StringProp.new(key: key)
when /\[ (.*?),.*?\]/ then ArrayProp.new(key: key, spec: $1)
else
StringProp.new(key: key)
#TODO fail ArgumentError, "Invalid spec: #{spec_line}"
end
end
end
end

require 'aws_cf/props/base'
require 'aws_cf/props/array_prop'
require 'aws_cf/props/boolean_prop'
require 'aws_cf/props/integer_prop'
require 'aws_cf/props/string_prop'
18 changes: 18 additions & 0 deletions lib/aws_cf/props/array_prop.rb
@@ -0,0 +1,18 @@
module AwsCF
module Props
class ArrayProp < Base

attr_accessor :sub_prop

def valid?(value)
value.is_a?(Array) && value.all? { |sub_value| sub_prop.valid?(sub_value) }
end

private

def after_initialize(args)
self.sub_prop = Props.from(args[:spec])
end
end
end
end
27 changes: 27 additions & 0 deletions lib/aws_cf/props/base.rb
@@ -0,0 +1,27 @@
module AwsCF
module Props
class Base

attr_accessor :key
attr_reader :value

def initialize(args = {})
self.key = args[:key]
after_initialize(args)
end

def name
@name ||= key && key.gsub(/(.)([A-Z])/, '\1_\2').downcase
end

def to_cf(value)
[key, value]
end

private

def after_initialize(args)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/aws_cf/props/boolean_prop.rb
@@ -0,0 +1,9 @@
module AwsCF
module Props
class BooleanProp < Base
def valid?(value)
value.is_a?(TrueClass) || value.is_a?(FalseClass)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/aws_cf/props/integer_prop.rb
@@ -0,0 +1,9 @@
module AwsCF
module Props
class IntegerProp < Base
def valid?(value)
value.is_a?(Fixnum)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/aws_cf/props/string_prop.rb
@@ -0,0 +1,9 @@
module AwsCF
module Props
class StringProp < Base
def valid?(value)
value.is_a?(String)
end
end
end
end
53 changes: 53 additions & 0 deletions lib/aws_cf/resource.rb
@@ -0,0 +1,53 @@
module AwsCF
class Resource

attr_accessor :properties

def initialize
self.properties = {}
end

def method_missing(name, *args)
sname = name.to_s

if (self.class.props.keys & [sname, sname[0..-2]]).empty?
super
elsif self.class.props.key?(sname)
properties[sname]
elsif !self.class.props[sname[0..-2]].valid?(args.first)
fail ArgumentError, "Invalid value for #{sname[0..-2]}: #{args.first.inspect}"
else
properties[sname[0..-2]] = args.first
end
end

def respond_to_missing?(name, include_private = false)
sname = name.to_s
(self.class.props.keys & [sname, sname[0..-2]]).any? || super
end

def to_cf
props_cf = properties.map do |key, value|
self.class.props[key].to_cf(value)
end.to_h

JSON.pretty_generate({ 'Type' => self.class.aws_name, 'Properties' => props_cf })
end

class << self
attr_accessor :aws_name, :props

def register(group, resource, spec)
parser = Parser.parse(spec)
self.props = parser.props

resource_class = Class.new(Resource)
resource_class.props = parser.props
resource_class.aws_name = "AWS::#{group}::#{resource}"

AwsCF.const_set(group, Module.new) unless AwsCF.const_defined?(group)
AwsCF.const_get(group).const_set(resource, resource_class)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/aws_cf/version.rb
@@ -0,0 +1,3 @@
module AwsCF
VERSION = '0.0.1'
end
21 changes: 21 additions & 0 deletions specs/AutoScaling-AutoScalingGroup.cf
@@ -0,0 +1,21 @@
{
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"AvailabilityZones" : [ String, ... ],
"Cooldown" : String,
"DesiredCapacity" : String,
"HealthCheckGracePeriod" : Integer,
"HealthCheckType" : String,
"InstanceId" : String,
"LaunchConfigurationName" : String,
"LoadBalancerNames" : [ String, ... ],
"MaxSize" : String,
"MetricsCollection" : [ MetricsCollection, ... ]
"MinSize" : String,
"NotificationConfigurations" : [ NotificationConfigurations, ... ],
"PlacementGroup" : String,
"Tags" : [ Auto Scaling Tag, ..., ],
"TerminationPolicies" : [ String, ..., ],
"VPCZoneIdentifier" : [ String, ... ]
}
}

0 comments on commit f051578

Please sign in to comment.