Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add active_support/inflector #42

Merged
merged 2 commits into from
Sep 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions awspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'rspec-its'
spec.add_runtime_dependency 'aws-sdk', '~> 2'
spec.add_runtime_dependency 'thor'
spec.add_runtime_dependency 'activesupport'
spec.add_development_dependency 'bundler', '~> 1.9'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rubocop'
Expand Down
1 change: 1 addition & 0 deletions lib/awspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'rspec'
require 'rspec/its'
require 'time'
require 'active_support/inflector'
require 'awspec/version'
require 'awspec/cli'
require 'awspec/stub'
Expand Down
2 changes: 1 addition & 1 deletion lib/awspec/command/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Generate < Thor
define_method type do |*args|
load_secrets
vpc_id = args.first
eval "puts Awspec::Generator::Spec::#{type.to_camel_case}.new.generate_by_vpc_id(vpc_id)"
eval "puts Awspec::Generator::Spec::#{type.camelize}.new.generate_by_vpc_id(vpc_id)"
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/awspec/ext.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
require 'awspec/ext/string'
require 'awspec/ext/struct'
14 changes: 0 additions & 14 deletions lib/awspec/ext/string.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/awspec/generator/doc/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.generate_doc
doc = ERB.new(header, nil, '-').result(binding)

types.map do |type|
doc += eval "Awspec::Generator::Doc::Type::#{type.to_camel_case}.new.generate_doc"
doc += eval "Awspec::Generator::Doc::Type::#{type.camelize}.new.generate_doc"
end
doc
end
Expand Down
6 changes: 3 additions & 3 deletions lib/awspec/generator/doc/type/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Doc
module Type
class Base
def initialize
Awspec::Stub.load type_name.to_snake_case
Awspec::Stub.load type_name.underscore
@type_name = type_name
end

Expand All @@ -26,7 +26,7 @@ def generate_doc
end

@descriptions = {}
merge_file = File.dirname(__FILE__) + '/../../../../../doc/_resource_types/' + type_name.to_snake_case + '.md'
merge_file = File.dirname(__FILE__) + '/../../../../../doc/_resource_types/' + type_name.underscore + '.md'
if File.exist?(merge_file)
matcher = nil
File.foreach(merge_file) do |line|
Expand Down Expand Up @@ -56,7 +56,7 @@ def collect_matchers

def doc_template
template = <<-'EOF'
## <a name="<%= @type_name.to_snake_case %>"><%= @type_name.to_snake_case %></a>
## <a name="<%= @type_name.underscore %>"><%= @type_name.underscore %></a>

<%= @type_name %> resource type.
<%- if @descriptions.include?('first') -%><%= @descriptions['first'] %><%- end -%>
Expand Down
24 changes: 12 additions & 12 deletions lib/awspec/generator/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ def self.generate(type)
end

def self.generate_type
path = 'lib/awspec/type/' + @type.to_snake_case + '.rb'
path = 'lib/awspec/type/' + @type.underscore + '.rb'
full_path = @root_path + path
content = <<-"EOF"
module Awspec::Type
class #{@type.to_camel_case} < Base
class #{@type.camelize} < Base
def initialize(id)
super
# @id = # @FIXME
Expand All @@ -27,7 +27,7 @@ def initialize(id)
end

def self.generate_stub
path = 'lib/awspec/stub/' + @type.to_snake_case + '.rb'
path = 'lib/awspec/stub/' + @type.underscore + '.rb'
full_path = @root_path + path
content = <<-"EOF"
# Aws.config[:ec2] = {
Expand All @@ -38,31 +38,31 @@ def self.generate_stub
end

def self.generate_type_spec
path = 'spec/type/' + @type.to_snake_case + '_spec.rb'
path = 'spec/type/' + @type.underscore + '_spec.rb'
full_path = @root_path + path
content = <<-"EOF"
require 'spec_helper'
Awspec::Stub.load '#{@type.to_snake_case}'
Awspec::Stub.load '#{@type.underscore}'

describe #{@type.to_snake_case}('my-#{@type.to_snake_case.tr('_', '-')}') do
describe #{@type.underscore}('my-#{@type.underscore.tr('_', '-')}') do
it { should exist }
end
EOF
self.file_check_and_puts(path, content)
end

def self.generate_generator_doc
path = 'lib/awspec/generator/doc/type/' + @type.to_snake_case + '.rb'
path = 'lib/awspec/generator/doc/type/' + @type.underscore + '.rb'
full_path = @root_path + path
content = <<-"EOF"
module Awspec::Generator
module Doc
module Type
class #{@type.to_camel_case} < Base
class #{@type.camelize} < Base
def initialize
super
@type_name = '#{@type.to_camel_case}'
@type = Awspec::Type::#{@type.to_camel_case}.new('my-#{@type.to_snake_case.tr('_', '-')}')
@type_name = '#{@type.camelize}'
@type = Awspec::Type::#{@type.camelize}.new('my-#{@type.underscore.tr('_', '-')}')
@ret = @type.resource
@matchers = []
@ignore_matchers = []
Expand All @@ -79,9 +79,9 @@ def initialize
def self.put_message
content = <<-"EOF"

Genarate #{@type.to_camel_case} template files.
Genarate #{@type.camelize} template files.

* !! AND add '#{@type.to_snake_case}' to Awspec::Helper::Type::TYPES in awspec/lib/helper/type.rb *
* !! AND add '#{@type.underscore}' to Awspec::Helper::Type::TYPES in awspec/lib/helper/type.rb *

EOF
end
Expand Down
64 changes: 13 additions & 51 deletions lib/awspec/helper/finder/iam.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,22 @@
module Awspec::Helper
module Finder
module Iam
def find_iam_user(id)
selected = []
res = @iam_client.list_users

loop do
selected += res.users.select do |u|
u.user_name == id || u.user_id == id || u.arn == id
end
(res.next_page? && res = res.next_page) || break
end

selected.first if selected.count == 1
end

def find_iam_group(id)
selected = []
res = @iam_client.list_groups

loop do
selected += res.groups.select do |g|
g.group_name == id || g.group_id == id || g.arn == id
end
(res.next_page? && res = res.next_page) || break
end

selected.first if selected.count == 1
end

def find_iam_role(id)
selected = []
res = @iam_client.list_roles

loop do
selected += res.roles.select do |r|
r.role_name == id || r.role_id == id || r.arn == id
# find_iam_user, find_iam_group find_iam_role find_iam_policy
role_types = %w(user group role policy)
role_types.each do |type|
define_method 'find_iam_' + type do |*args|
id = args.first
selected = []
res = @iam_client.method('list_' + type.pluralize).call
loop do
selected += res[type.pluralize].select do |u|
u[type + '_name'] == id || u[type + '_id'] == id || u.arn == id
end
(res.next_page? && res = res.next_page) || break
end
(res.next_page? && res = res.next_page) || break
end

selected.first if selected.count == 1
end

def find_iam_policy(id)
selected = []
res = @iam_client.list_policies

loop do
selected += res.policies.select do |p|
p.policy_name == id || p.policy_id == id || p.arn == id
end
(res.next_page? && res = res.next_page) || break
selected.first if selected.count == 1
end

selected.first if selected.count == 1
end

def select_iam_group_by_user_name(user_name)
Expand Down
2 changes: 1 addition & 1 deletion lib/awspec/helper/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Type
require "awspec/type/#{type}"
define_method type do |*args|
name = args.first
eval "Awspec::Type::#{type.to_camel_case}.new(name)"
eval "Awspec::Type::#{type.camelize}.new(name)"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/generator/doc/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
types.delete('base')
types.each do |type|
describe 'Resource Type ' + type do
let(:generator) { eval "Awspec::Generator::Doc::Type::#{type.to_camel_case}.new" }
let(:generator) { eval "Awspec::Generator::Doc::Type::#{type.camelize}.new" }
it 'should generate doc' do
generated = generator.generate_doc
expect(generated).to include(type)
Expand Down