Skip to content

Commit

Permalink
Merge pull request #59 from k1LoW/credentials-load
Browse files Browse the repository at this point in the history
Support  `awspec generate --profile [profile_name]`
  • Loading branch information
k1LoW committed Nov 5, 2015
2 parents 1e1275f + 6a787a5 commit 474cdda
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,24 @@ end

$ bundle exec rake spec

### Advanced Usage: Spec generate command
### Advanced Tips: Spec generate command

Generate spec from AWS resources already exists.

```sh
$ awspec generate ec2 vpc-ab123cde >> spec/ec2_spec.rb
```

### Advanced Tips: Use Shared credentials (~/.aws/config ~/.aws/credentials)

```sh
$ awspec generate ec2 vpc-ab123cde --profile mycreds
```

```sh
$ export AWS_PROFILE=mycreds; bundle exec rake spec
```

## Support AWS Resources

- [x] EC2 (`ec2`)
Expand Down
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', '~> 3.0'
spec.add_runtime_dependency 'rspec-its'
spec.add_runtime_dependency 'aws-sdk', '~> 2.1.20'
spec.add_runtime_dependency 'aws_config'
spec.add_runtime_dependency 'thor'
spec.add_runtime_dependency 'activesupport'
spec.add_development_dependency 'bundler', '~> 1.9'
Expand Down
22 changes: 6 additions & 16 deletions lib/awspec/command/generate.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
require 'thor'
require 'awspec/setup'
require 'awspec/helper/credentials_loader'

module Awspec
class Generate < Thor
class_option :profile

types = %w(
vpc ec2 rds security_group elb
)

types.each do |type|
desc type + ' [vpc_id]', "Generate #{type} spec from VPC ID (or VPC \"Name\" tag)"
define_method type do |*args|
load_secrets
Awspec::Helper::CredentialsLoader.load(options[:profile])
vpc_id = args.first
eval "puts Awspec::Generator::Spec::#{type.camelize}.new.generate_by_vpc_id(vpc_id)"
end
Expand All @@ -22,7 +25,7 @@ class Generate < Thor

desc 'route53_hosted_zone [example.com.]', 'Generate route53_hosted_zone spec from Domain name'
def route53_hosted_zone(hosted_zone)
load_secrets
Awspec::Helper::CredentialsLoader.load(options[:profile])
puts Awspec::Generator::Spec::Route53HostedZone.new.generate_by_domain_name(hosted_zone)
end

Expand All @@ -37,22 +40,9 @@ def route53_hosted_zone(hosted_zone)
desc type, "Generate #{type} spec"
end
define_method type do
load_secrets
Awspec::Helper::CredentialsLoader.load(options[:profile])
eval "puts Awspec::Generator::Spec::#{type.camelize}.new.generate_all"
end
end

no_commands do
def load_secrets
creds = YAML.load_file('spec/secrets.yml') if File.exist?('spec/secrets.yml')
creds = YAML.load_file('secrets.yml') if File.exist?('secrets.yml')
Aws.config.update({
region: creds['region'],
credentials: Aws::Credentials.new(
creds['aws_access_key_id'],
creds['aws_secret_access_key'])
}) if creds
end
end
end
end
1 change: 1 addition & 0 deletions lib/awspec/helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'awspec/helper/type'
require 'awspec/helper/finder'
require 'awspec/helper/credentials_loader'
extend Awspec::Helper::Type
class RSpec::Core::ExampleGroup
extend Awspec::Helper::Type
Expand Down
27 changes: 27 additions & 0 deletions lib/awspec/helper/credentials_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'aws-sdk'
require 'aws_config'

module Awspec::Helper
module CredentialsLoader
def self.load(profile = nil)
profile = ENV['AWS_PROFILE'] if profile.nil? && ENV.key?('AWS_PROFILE')
if profile
# SharedCredentials
aws_config = AWSConfig.profiles[profile]
aws_config = AWSConfig.profiles['default'] unless aws_config
Aws.config[:region] = aws_config.config_hash[:region] if aws_config
Aws.config[:credentials] = Aws::SharedCredentials.new(profile_name: profile)
else
# secrets.yml
creds = YAML.load_file('spec/secrets.yml') if File.exist?('spec/secrets.yml')
creds = YAML.load_file('secrets.yml') if File.exist?('secrets.yml')
Aws.config.update({
region: creds['region'],
credentials: Aws::Credentials.new(
creds['aws_access_key_id'],
creds['aws_secret_access_key'])
}) if creds
end
end
end
end
10 changes: 1 addition & 9 deletions lib/awspec/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ def self.run
def self.generate_spec_helper
content = <<-'EOF'
require 'awspec'
if File.exist?('spec/secrets.yml')
creds = YAML.load_file('spec/secrets.yml')
Aws.config.update({
region: creds['region'],
credentials: Aws::Credentials.new(
creds['aws_access_key_id'],
creds['aws_secret_access_key'])
})
end
Awspec::Helper::CredentialsLoader.load
EOF
dir = 'spec'
if File.exist? dir
Expand Down
10 changes: 1 addition & 9 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,4 @@
'secret')
})

if File.exist?('spec/secrets.yml')
creds = YAML.load_file('spec/secrets.yml')
Aws.config.update({
region: creds['region'],
credentials: Aws::Credentials.new(
creds['aws_access_key_id'],
creds['aws_secret_access_key'])
})
end
Awspec::Helper::CredentialsLoader.load

0 comments on commit 474cdda

Please sign in to comment.