aws-vpccreate 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +12 -0
- data/aws-vpccreate.gemspec +19 -0
- data/lib/aws-vpccreate.rb +132 -0
- data/lib/aws-vpccreate/version.rb +5 -0
- data/spec/aws-vpccreate/aws-vpccreate_spec.rb +248 -0
- data/spec/aws-vpccreate/aws-vpcfactory_spec.rb +305 -0
- data/spec/spec_helper.rb +8 -0
- data/tasks/rspec.rake +26 -0
- metadata +60 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 YAMANE Toshiaki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Aws::Vpccreate
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'aws-vpccreate'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install aws-vpccreate
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'yaml'
|
22
|
+
require 'aws-sdk'
|
23
|
+
require 'aws-vpccreate'
|
24
|
+
|
25
|
+
AWS.config(YAML.load(File.read(File.join(File.dirname(__FILE__). 'aws.yml'))))
|
26
|
+
ec2 = AWS::EC2.new
|
27
|
+
vpcf = AWS::VPCFactory.new(ec2)
|
28
|
+
|
29
|
+
vpc_config = YAML.load(File.read(File.join(File.dirname(__FILE__). 'vpc.yml'))))
|
30
|
+
vpcf.create(vpc_config)
|
31
|
+
|
32
|
+
puts vpcf.vpcc.logger.config_log
|
33
|
+
|
34
|
+
vpc.yml example is the following
|
35
|
+
|
36
|
+
vpc:
|
37
|
+
vpc_subnet: '10.0.0.0/16'
|
38
|
+
subnets:
|
39
|
+
- subnet_addr: '10.0.0.0/24'
|
40
|
+
availability_zone: 'ap-northeast-1a'
|
41
|
+
- subnet_addr: '10.0.1.0/24'
|
42
|
+
availability_zone: 'ap-northeast-1b'
|
43
|
+
- subnet_addr: '10.0.2.0/24'
|
44
|
+
availability_zone: 'ap-northeast-1b'
|
45
|
+
- subnet_addr: '10.0.3.0/24'
|
46
|
+
availability_zone: 'ap-northeast-1c'
|
47
|
+
security_group:
|
48
|
+
- name: 'abc'
|
49
|
+
description: 'NAT'
|
50
|
+
- name: 'def'
|
51
|
+
description: 'WEB'
|
52
|
+
- name: 'ghi'
|
53
|
+
description: 'DB'
|
54
|
+
|
55
|
+
aws.yml example is the following
|
56
|
+
|
57
|
+
access_key_id: my_acess_key
|
58
|
+
secret_access_key: my_secret_access_key
|
59
|
+
ec2_endpoint: ec2.ap-northeast-1.amazonaws.com
|
60
|
+
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
root = File.dirname(__FILE__)
|
4
|
+
tasks_dir = File.join(root, "tasks")
|
5
|
+
$:.unshift(tasks_dir)
|
6
|
+
$:.unshift(File.join(root, "lib"))
|
7
|
+
|
8
|
+
Dir[File.join(tasks_dir, "**", "*.rake")].each do |task_file|
|
9
|
+
load task_file
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :spec
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'aws-vpccreate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "aws-vpccreate"
|
8
|
+
gem.version = Aws::Vpccreate::VERSION
|
9
|
+
gem.authors = ["YAMANE Toshiaki"]
|
10
|
+
gem.email = ["yamanetoshi@gmail.com"]
|
11
|
+
gem.description = %q{AWS VPC object creator}
|
12
|
+
gem.summary = %q{Simply generates AWS VPC objects}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require "aws-vpccreate/version"
|
2
|
+
|
3
|
+
require 'aws-sdk'
|
4
|
+
#require 'singleton'
|
5
|
+
|
6
|
+
module AWS
|
7
|
+
class Logger
|
8
|
+
attr_reader :config_log
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@config_log = { :vpc => { :vpc_subnet => "",
|
12
|
+
:vpc_id => "",
|
13
|
+
:subnets => [],
|
14
|
+
:security_group => []}}
|
15
|
+
end
|
16
|
+
|
17
|
+
def put hash
|
18
|
+
if hash[:key] == :vpc_subnet
|
19
|
+
@config_log[:vpc][hash[:key]] = hash[:value]
|
20
|
+
elsif hash[:key] == :vpc_id
|
21
|
+
@config_log[:vpc][hash[:key]] = hash[:value]
|
22
|
+
else
|
23
|
+
@config_log[:vpc][hash[:key]].push hash[:value]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Vpccreate
|
29
|
+
attr_reader :vpc, :logger
|
30
|
+
|
31
|
+
def initialize ec2
|
32
|
+
@logger = Logger.new
|
33
|
+
@ec2 = ec2
|
34
|
+
@vpc = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_vpc cidr_block, options = {}
|
38
|
+
@vpc = @ec2.vpcs.create(cidr_block, options)
|
39
|
+
|
40
|
+
@logger.put({:key => :vpc_subnet, :value => cidr_block})
|
41
|
+
@logger.put({:key => :vpc_id, :value => @vpc.id})
|
42
|
+
|
43
|
+
@vpc
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_subnet cidr_block, options = {}
|
47
|
+
raise "no vpc instance" if @vpc == nil
|
48
|
+
|
49
|
+
@logger.put({:key => :subnets,
|
50
|
+
:value => {:subnet_addr => cidr_block,
|
51
|
+
:availability_zone => options[:availability_zone]}})
|
52
|
+
|
53
|
+
options[:vpc] = @vpc
|
54
|
+
@ec2.subnets.create(cidr_block, options)
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_ig
|
58
|
+
raise "no vpc instance" if @vpc == nil
|
59
|
+
|
60
|
+
ig = @ec2.internet_gateways.create
|
61
|
+
ig.attach @vpc
|
62
|
+
ig
|
63
|
+
end
|
64
|
+
|
65
|
+
def create_sg name, options = {}
|
66
|
+
raise "no vpc instance" if @vpc == nil
|
67
|
+
|
68
|
+
@logger.put({ :key => :security_group,
|
69
|
+
:value => { :name => name,
|
70
|
+
:description => options[:description]}})
|
71
|
+
|
72
|
+
options[:vpc] = @vpc
|
73
|
+
@ec2.security_groups.create(name, options)
|
74
|
+
end
|
75
|
+
|
76
|
+
def create_rt options = {}
|
77
|
+
raise "no vpc instance" if @vpc == nil
|
78
|
+
|
79
|
+
options[:vpc] = @vpc
|
80
|
+
@ec2.route_tables.create(options)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class VPCFactory
|
85
|
+
attr_reader :vpcc
|
86
|
+
# include Singleton
|
87
|
+
|
88
|
+
# def initialize
|
89
|
+
def initialize ec2
|
90
|
+
@vpc = nil
|
91
|
+
@vpcc = AWS::Vpccreate.new(ec2)
|
92
|
+
end
|
93
|
+
|
94
|
+
# def setec2 ec2
|
95
|
+
# @vpcc = AWS::Vpccreate.new(ec2)
|
96
|
+
# end
|
97
|
+
|
98
|
+
def create config
|
99
|
+
raise "no config" if config == {}
|
100
|
+
raise "no vpc" if !config.key?('vpc')
|
101
|
+
raise "no vpc_subnet" if !config["vpc"].key?('vpc_subnet')
|
102
|
+
raise "no vpc_subnet" if config["vpc"]["vpc_subnet"] == ""
|
103
|
+
|
104
|
+
raise "vpc already exist!" if @vpc != nil
|
105
|
+
|
106
|
+
@vpc = create_vpc_ig(config)
|
107
|
+
create_subnet @vpc, config if config["vpc"].key?('subnets')
|
108
|
+
create_sg config if config["vpc"].key?('security_group')
|
109
|
+
end
|
110
|
+
|
111
|
+
@private
|
112
|
+
def create_vpc_ig config
|
113
|
+
vpc = @vpcc.create_vpc(config["vpc"]["vpc_subnet"])
|
114
|
+
@vpcc.create_ig
|
115
|
+
vpc
|
116
|
+
end
|
117
|
+
|
118
|
+
def create_subnet vpc, config
|
119
|
+
config["vpc"]["subnets"].each { |tmp|
|
120
|
+
@vpcc.create_subnet(tmp["subnet_addr"],
|
121
|
+
{:availability_zone => tmp["availability_zone"]})
|
122
|
+
@vpcc.create_rt
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
def create_sg config
|
127
|
+
config["vpc"]["security_group"].each { |tmp|
|
128
|
+
@vpcc.create_sg(tmp["name"], {:description => tmp["description"]})
|
129
|
+
}
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,248 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AWS::Vpccreate do
|
4
|
+
let!(:config) { AWS.config.with(:stub_requests => true,
|
5
|
+
:access_key_id => "AKID",
|
6
|
+
:secret_access_key => 'b') }
|
7
|
+
let!(:ec2) { AWS::EC2.new(:config => config) }
|
8
|
+
let!(:vpcc) { AWS::Vpccreate.new(ec2) }
|
9
|
+
|
10
|
+
let(:create_vpc_response) { ec2.client.stub_for(:create_vpc) }
|
11
|
+
|
12
|
+
let(:vpc_details) {{
|
13
|
+
:vpc_id => 'vpc-12345',
|
14
|
+
:state => 'pending',
|
15
|
+
:cidr_block => '192.0.0.0/16',
|
16
|
+
:dhcp_option_id => 'dopt-12345',
|
17
|
+
:instance_tenancy => 'default',
|
18
|
+
}}
|
19
|
+
|
20
|
+
before(:each) do
|
21
|
+
create_vpc_response.data[:vpc] = vpc_details
|
22
|
+
ec2.client.stub(:create_vpc).and_return(create_vpc_response)
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'create_vpc' do
|
26
|
+
it 'calls #create_vpc on the client' do
|
27
|
+
ec2.client.should_receive(:create_vpc).
|
28
|
+
with(:cidr_block => '10.0.0.0/16', :instance_tenancy => 'default').
|
29
|
+
and_return(create_vpc_response)
|
30
|
+
|
31
|
+
vpcc.create_vpc('10.0.0.0/16')
|
32
|
+
|
33
|
+
vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
34
|
+
:vpc_id => 'vpc-12345',
|
35
|
+
:subnets => [],
|
36
|
+
:security_group => []}}
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'calls accept a different tenancy value' do
|
40
|
+
ec2.client.should_receive(:create_vpc).
|
41
|
+
with(:cidr_block => '10.0.0.0/24', :instance_tenancy => 'dedicated').
|
42
|
+
and_return(create_vpc_response)
|
43
|
+
|
44
|
+
vpcc.create_vpc('10.0.0.0/24', :instance_tenancy => :dedicated)
|
45
|
+
|
46
|
+
vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/24',
|
47
|
+
:vpc_id => 'vpc-12345',
|
48
|
+
:subnets => [],
|
49
|
+
:security_group => []}}
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns a VPC object' do
|
53
|
+
vpc = vpcc.create_vpc('192.0.0.0/16')
|
54
|
+
|
55
|
+
vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '192.0.0.0/16',
|
56
|
+
:vpc_id => 'vpc-12345',
|
57
|
+
:subnets => [],
|
58
|
+
:security_group => []}}
|
59
|
+
|
60
|
+
vpc.should be_a(AWS::EC2::VPC)
|
61
|
+
vpc.vpc_id.should == 'vpc-12345'
|
62
|
+
vpc.cidr_block.should == '192.0.0.0/16'
|
63
|
+
# vpc.dhcp_options_id.should == 'dopt-12345'
|
64
|
+
vpc.instance_tenancy.should == :default
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'create_subnet' do
|
69
|
+
let!(:vpc) { vpcc.create_vpc('10.0.0.0/16') }
|
70
|
+
|
71
|
+
let(:subnet_details) {{
|
72
|
+
:subnet_id => 'subnet-12345',
|
73
|
+
:vpc_id => 'vpc-12345',
|
74
|
+
:state => 'pending',
|
75
|
+
:cidr_block => '192.0.0.0/16',
|
76
|
+
:available_ip_address_count => 50,
|
77
|
+
:availability_zone => 'us-east-1c',
|
78
|
+
}}
|
79
|
+
|
80
|
+
let(:response) { ec2.client.stub_for(:create_subnet) }
|
81
|
+
|
82
|
+
before(:each) do
|
83
|
+
response.data[:subnet] = subnet_details
|
84
|
+
ec2.client.stub(:create_subnet).and_return(response)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'vpc in vpcc is valid' do
|
88
|
+
vpcc.vpc.should be_a(AWS::EC2::VPC)
|
89
|
+
vpcc.vpc.id.should == 'vpc-12345'
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'calls #create_subnet on the client' do
|
93
|
+
ec2.client.should_receive(:create_subnet).
|
94
|
+
with(:vpc_id => vpc.id, :cidr_block => '10.0.0.0/16').
|
95
|
+
and_return(response)
|
96
|
+
vpcc.create_subnet('10.0.0.0/16')
|
97
|
+
|
98
|
+
vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
99
|
+
:vpc_id => 'vpc-12345',
|
100
|
+
:subnets => [{:subnet_addr => '10.0.0.0/16',
|
101
|
+
:availability_zone => nil}],
|
102
|
+
:security_group => []}}
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'accepts an availability zone name' do
|
106
|
+
ec2.client.should_receive(:create_subnet).with(
|
107
|
+
:vpc_id => vpc.id,
|
108
|
+
:cidr_block => 'cidr-block',
|
109
|
+
:availability_zone => 'abc'
|
110
|
+
).and_return(response)
|
111
|
+
subnet = vpcc.create_subnet('cidr-block',
|
112
|
+
:availability_zone => 'abc')
|
113
|
+
|
114
|
+
vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
115
|
+
:vpc_id => 'vpc-12345',
|
116
|
+
:subnets => [{:subnet_addr => 'cidr-block',
|
117
|
+
:availability_zone => 'abc'}],
|
118
|
+
:security_group => []}}
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'returns a populated subnet' do
|
122
|
+
subnet = vpcc.create_subnet('192.0.0.0/16')
|
123
|
+
|
124
|
+
subnet.should be_a(AWS::EC2::Subnet)
|
125
|
+
subnet.subnet_id.should == 'subnet-12345'
|
126
|
+
subnet.vpc_id.should == 'vpc-12345'
|
127
|
+
subnet.cidr_block.should == '192.0.0.0/16'
|
128
|
+
subnet.available_ip_address_count.should == 50
|
129
|
+
subnet.availability_zone_name.should == 'us-east-1c'
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'throw exception when object has no vpc object' do
|
133
|
+
tmp_vpcc = AWS::Vpccreate.new(ec2)
|
134
|
+
proc { tmp_vpcc.create_subnet('10.0.0.0/16') }.should raise_error
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe 'create_ig' do
|
139
|
+
let!(:vpc) { vpcc.create_vpc('10.0.0.0/16') }
|
140
|
+
|
141
|
+
let(:create_response) { ec2.client.stub_for(:create_internet_gateway) }
|
142
|
+
let(:attach_response) { ec2.client.stub_for(:attach_internet_gateway) }
|
143
|
+
|
144
|
+
before(:each) do
|
145
|
+
create_response.data[:internet_gateway] = {
|
146
|
+
:internet_gateway_id => 'igw-123',
|
147
|
+
}
|
148
|
+
ec2.client.stub(:create_internet_gateway).and_return(create_response)
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'calls #create_internet_gateway on the client' do
|
152
|
+
ec2.client.should_receive(:create_internet_gateway).
|
153
|
+
with(no_args).and_return(create_response)
|
154
|
+
ig = vpcc.create_ig
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'calls #attach_internet_gateway on the client' do
|
158
|
+
ec2.client.should_receive(:attach_internet_gateway).with(
|
159
|
+
:internet_gateway_id => 'igw-123',
|
160
|
+
:vpc_id => vpc.id).and_return(attach_response)
|
161
|
+
ig = vpcc.create_ig
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'returns an internet gateway' do
|
165
|
+
ig = vpcc.create_ig
|
166
|
+
ig.should be_an(AWS::EC2::InternetGateway)
|
167
|
+
ig.internet_gateway_id.should == 'igw-123'
|
168
|
+
ig.config.should == config
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'throw exception when object has no vpc object' do
|
172
|
+
tmp_vpcc = AWS::Vpccreate.new(ec2)
|
173
|
+
proc { tmp_vpcc.create_ig }.should raise_error
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
describe 'create_security_group' do
|
178
|
+
let!(:vpc) { vpcc.create_vpc('10.0.0.0/16') }
|
179
|
+
|
180
|
+
let(:response) { ec2.client.stub_for(:create_security_group) }
|
181
|
+
|
182
|
+
before(:each) do
|
183
|
+
response.data[:group_id] = 'group-id'
|
184
|
+
ec2.client.stub(:create_security_group).and_return(response)
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'calls #create_security_group on the client' do
|
188
|
+
ec2.client.should_receive(:create_security_group).
|
189
|
+
with(:group_name => 'abc',
|
190
|
+
:description => 'xyz',
|
191
|
+
:vpc_id => vpc.id).and_return(response)
|
192
|
+
sg = vpcc.create_sg('abc', :description => 'xyz')
|
193
|
+
|
194
|
+
vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
195
|
+
:vpc_id => 'vpc-12345',
|
196
|
+
:subnets => [],
|
197
|
+
:security_group => [{:name => 'abc', :description => 'xyz'}]}}
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'returns an security gateway' do
|
201
|
+
sg = vpcc.create_sg('abc', :description => 'xyz')
|
202
|
+
sg.should be_a(AWS::EC2::SecurityGroup)
|
203
|
+
sg.name.should == 'abc'
|
204
|
+
sg.description.should == 'xyz'
|
205
|
+
sg.vpc_id.should == vpc.id
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'throw exception when object has no vpc object' do
|
209
|
+
tmp_vpcc = AWS::Vpccreate.new(ec2)
|
210
|
+
proc { tmp_vpcc.create_sg('name') }.should raise_error
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
describe 'create_rt' do
|
215
|
+
let!(:vpc) { vpcc.create_vpc('10.0.0.0/16') }
|
216
|
+
|
217
|
+
let(:details) {{
|
218
|
+
:route_table_id => 'rt-123',
|
219
|
+
:vpc_id => vpc.id,
|
220
|
+
}}
|
221
|
+
|
222
|
+
let(:response) { ec2.client.stub_for(:create_route_table) }
|
223
|
+
|
224
|
+
before(:each) do
|
225
|
+
response.data[:route_table] = details
|
226
|
+
ec2.client.stub(:create_route_table).and_return(response)
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'calls #create_route_table on the client' do
|
230
|
+
ec2.client.should_receive(:create_route_table).
|
231
|
+
with(:vpc_id => vpc.id).and_return(response)
|
232
|
+
|
233
|
+
vpcc.create_rt
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'returns an route table' do
|
237
|
+
rt = vpcc.create_rt
|
238
|
+
rt.should be_a(AWS::EC2::RouteTable)
|
239
|
+
rt.vpc.should be_a(AWS::EC2::VPC)
|
240
|
+
rt.vpc.vpc_id.should == vpc.id
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'throw exception when object has no vpc object' do
|
244
|
+
tmp_vpcc = AWS::Vpccreate.new(ec2)
|
245
|
+
proc { tmp_vpcc.create_rt(:vpc => 'vpc-id') }.should raise_error
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
@@ -0,0 +1,305 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AWS::VPCFactory do
|
4
|
+
let!(:config) { AWS.config.with(:stub_requests => true,
|
5
|
+
:access_key_id => "AKID",
|
6
|
+
:secret_access_key => 'b') }
|
7
|
+
let!(:ec2) { AWS::EC2.new(:config => config) }
|
8
|
+
|
9
|
+
let(:create_vpc_response) { ec2.client.stub_for(:create_vpc) }
|
10
|
+
|
11
|
+
let(:vpc_details) {{
|
12
|
+
:vpc_id => 'vpc-12345',
|
13
|
+
:state => 'pending',
|
14
|
+
:cidr_block => '192.0.0.0/16',
|
15
|
+
:dhcp_option_id => 'dopt-12345',
|
16
|
+
:instance_tenancy => 'default',
|
17
|
+
}}
|
18
|
+
|
19
|
+
before(:each) do
|
20
|
+
create_vpc_response.data[:vpc] = vpc_details
|
21
|
+
ec2.client.stub(:create_vpc).and_return(create_vpc_response)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'create' do
|
25
|
+
it 'no configuration (config is {})' do
|
26
|
+
config = {}
|
27
|
+
proc { AWS::VPCFactory.create(ec2, config) }.should raise_error
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'no configuration' do
|
31
|
+
proc { AWS::VPCFactory.create(ec2) }.should raise_error
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'no vpc_subnet' do
|
35
|
+
config = {"vpc" => {}}
|
36
|
+
|
37
|
+
proc { AWS::VPCFactory.create(ec2, config) }.should raise_error
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'vpc_subnet is null-string' do
|
41
|
+
config = {"vpc" => {"vpc_subnet" => ""}}
|
42
|
+
|
43
|
+
proc { AWS::VPCFactory.create(ec2, config) }.should raise_error
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'makes vpc and internet_gateway only' do
|
47
|
+
let(:create_response) { ec2.client.stub_for(:create_internet_gateway) }
|
48
|
+
let(:attach_response) { ec2.client.stub_for(:attach_internet_gateway) }
|
49
|
+
|
50
|
+
before(:each) do
|
51
|
+
create_response.data[:internet_gateway] = {
|
52
|
+
:internet_gateway_id => 'igw-123',
|
53
|
+
}
|
54
|
+
ec2.client.stub(:create_internet_gateway).and_return(create_response)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'create vpc and create internet gateway and attach it to vpc' do
|
58
|
+
vpc_config = {"vpc" => {"vpc_subnet" => "10.0.0.0/16"}}
|
59
|
+
|
60
|
+
ec2.client.should_receive(:create_vpc).
|
61
|
+
with(:cidr_block => '10.0.0.0/16', :instance_tenancy => 'default').
|
62
|
+
and_return(create_vpc_response)
|
63
|
+
|
64
|
+
ec2.client.should_receive(:create_internet_gateway).
|
65
|
+
with(no_args).and_return(create_response)
|
66
|
+
|
67
|
+
ec2.client.should_receive(:attach_internet_gateway).
|
68
|
+
with(:internet_gateway_id => 'igw-123',
|
69
|
+
:vpc_id => 'vpc-12345').and_return(attach_response)
|
70
|
+
|
71
|
+
ec2.client.should_not_receive(:create_subnet)
|
72
|
+
ec2.client.should_not_receive(:create_route_table)
|
73
|
+
|
74
|
+
ec2.client.should_not_receive(:create_security_group)
|
75
|
+
|
76
|
+
vpcf = AWS::VPCFactory.new ec2
|
77
|
+
vpcf.create(vpc_config)
|
78
|
+
|
79
|
+
vpcf.vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
80
|
+
:vpc_id => 'vpc-12345',
|
81
|
+
:subnets => [],
|
82
|
+
:security_group => []}}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe 'makes vpc and ig and security_gateway' do
|
87
|
+
let(:create_response) { ec2.client.stub_for(:create_internet_gateway) }
|
88
|
+
let(:attach_response) { ec2.client.stub_for(:attach_internet_gateway) }
|
89
|
+
let(:create_sg_response) { ec2.client.stub_for(:create_security_group) }
|
90
|
+
|
91
|
+
before(:each) do
|
92
|
+
create_response.data[:internet_gateway] = {
|
93
|
+
:internet_gateway_id => 'igw-123',
|
94
|
+
}
|
95
|
+
ec2.client.stub(:create_internet_gateway).and_return(create_response)
|
96
|
+
|
97
|
+
create_sg_response.data[:group_id] = 'group-id'
|
98
|
+
ec2.client.stub(:create_security_group).and_return(create_sg_response)
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'create security gateway' do
|
102
|
+
vpc_config = {"vpc" => {"vpc_subnet" => "10.0.0.0/16",
|
103
|
+
"security_group" => [{"name" =>"abc",
|
104
|
+
"description" => "NAT"},
|
105
|
+
{"name" =>"def",
|
106
|
+
"description" => "WEB"},
|
107
|
+
{"name" =>"ghi",
|
108
|
+
"description" => "DB"},
|
109
|
+
]}}
|
110
|
+
|
111
|
+
ec2.client.should_receive(:create_vpc).
|
112
|
+
with(:cidr_block => '10.0.0.0/16', :instance_tenancy => 'default').
|
113
|
+
and_return(create_vpc_response)
|
114
|
+
|
115
|
+
ec2.client.should_receive(:create_internet_gateway).
|
116
|
+
with(no_args).and_return(create_response)
|
117
|
+
|
118
|
+
ec2.client.should_receive(:attach_internet_gateway).
|
119
|
+
with(:internet_gateway_id => 'igw-123',
|
120
|
+
:vpc_id => 'vpc-12345').and_return(attach_response)
|
121
|
+
|
122
|
+
ec2.client.should_not_receive(:create_subnet)
|
123
|
+
ec2.client.should_not_receive(:create_route_table)
|
124
|
+
|
125
|
+
ec2.client.should_receive(:create_security_group)
|
126
|
+
|
127
|
+
vpcf = AWS::VPCFactory.new ec2
|
128
|
+
vpcf.create(vpc_config)
|
129
|
+
|
130
|
+
vpcf.vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
131
|
+
:vpc_id => 'vpc-12345',
|
132
|
+
:subnets => [],
|
133
|
+
:security_group => [{:name => "abc", :description => "NAT"},
|
134
|
+
{:name => "def", :description => "WEB"},
|
135
|
+
{:name => "ghi", :description => "DB"}]}}
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe 'makes vpc and ig and subnet and route' do
|
140
|
+
let(:create_response) { ec2.client.stub_for(:create_internet_gateway) }
|
141
|
+
let(:attach_response) { ec2.client.stub_for(:attach_internet_gateway) }
|
142
|
+
let(:create_subnet_response) { ec2.client.stub_for(:create_subnet) }
|
143
|
+
let(:create_route_response) { ec2.client.stub_for(:create_route_table) }
|
144
|
+
let(:create_sg_response) { ec2.client.stub_for(:create_security_group) }
|
145
|
+
|
146
|
+
let(:subnet_details) {{
|
147
|
+
:subnet_id => 'subnet-12345',
|
148
|
+
:vpc_id => 'vpc-12345',
|
149
|
+
:state => 'pending',
|
150
|
+
:cidr_block => '192.0.0.0/16',
|
151
|
+
:available_ip_address_count => 50,
|
152
|
+
:availability_zone => 'us-east-1c',
|
153
|
+
}}
|
154
|
+
|
155
|
+
let(:route_details) {{
|
156
|
+
:route_table_id => 'rt-123',
|
157
|
+
:vpc_id => 'vpc-12345',
|
158
|
+
}}
|
159
|
+
|
160
|
+
before(:each) do
|
161
|
+
create_response.data[:internet_gateway] = {
|
162
|
+
:internet_gateway_id => 'igw-123',
|
163
|
+
}
|
164
|
+
ec2.client.stub(:create_internet_gateway).and_return(create_response)
|
165
|
+
|
166
|
+
create_sg_response.data[:group_id] = 'group-id'
|
167
|
+
ec2.client.stub(:create_security_group).and_return(create_sg_response)
|
168
|
+
|
169
|
+
create_subnet_response.data[:subnet] = subnet_details
|
170
|
+
ec2.client.stub(:create_subnet).and_return(create_subnet_response)
|
171
|
+
|
172
|
+
create_route_response.data[:route_table] = route_details
|
173
|
+
ec2.client.stub(:create_route_table).and_return(create_route_response)
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'create subnet and route' do
|
177
|
+
vpc_config = {"vpc" => {"vpc_subnet" => "10.0.0.0/16",
|
178
|
+
"subnets" => [{"subnet_addr" => "10.0.0.0/24",
|
179
|
+
"availability_zone" => "ap-northeast-1a"},
|
180
|
+
{"subnet_addr" => "10.0.0.1/24",
|
181
|
+
"availability_zone" => "ap-northeast-1b"},
|
182
|
+
{"subnet_addr" => "10.0.0.2/24",
|
183
|
+
"availability_zone" => "ap-northeast-1c"}]}}
|
184
|
+
|
185
|
+
ec2.client.should_receive(:create_vpc).
|
186
|
+
with(:cidr_block => '10.0.0.0/16', :instance_tenancy => 'default').
|
187
|
+
and_return(create_vpc_response)
|
188
|
+
|
189
|
+
ec2.client.should_receive(:create_internet_gateway).
|
190
|
+
with(no_args).and_return(create_response)
|
191
|
+
|
192
|
+
ec2.client.should_receive(:attach_internet_gateway).
|
193
|
+
with(:internet_gateway_id => 'igw-123',
|
194
|
+
:vpc_id => 'vpc-12345').and_return(attach_response)
|
195
|
+
|
196
|
+
ec2.client.should_receive(:create_subnet)
|
197
|
+
ec2.client.should_receive(:create_route_table)
|
198
|
+
|
199
|
+
ec2.client.should_not_receive(:create_security_group)
|
200
|
+
|
201
|
+
vpcf = AWS::VPCFactory.new ec2
|
202
|
+
vpcf.create(vpc_config)
|
203
|
+
|
204
|
+
vpcf.vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
205
|
+
:vpc_id => 'vpc-12345',
|
206
|
+
:subnets => [{:subnet_addr => "10.0.0.0/24",
|
207
|
+
:availability_zone => "ap-northeast-1a"},
|
208
|
+
{:subnet_addr => "10.0.0.1/24",
|
209
|
+
:availability_zone => "ap-northeast-1b"},
|
210
|
+
{:subnet_addr => "10.0.0.2/24",
|
211
|
+
:availability_zone => "ap-northeast-1c"}],
|
212
|
+
:security_group => []}}
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe 'makes vpc and ig and security_gateway' do
|
217
|
+
let(:create_response) { ec2.client.stub_for(:create_internet_gateway) }
|
218
|
+
let(:attach_response) { ec2.client.stub_for(:attach_internet_gateway) }
|
219
|
+
let(:create_sg_response) { ec2.client.stub_for(:create_security_group) }
|
220
|
+
let(:create_subnet_response) { ec2.client.stub_for(:create_subnet) }
|
221
|
+
let(:create_route_response) { ec2.client.stub_for(:create_route_table) }
|
222
|
+
|
223
|
+
let(:subnet_details) {{
|
224
|
+
:subnet_id => 'subnet-12345',
|
225
|
+
:vpc_id => 'vpc-12345',
|
226
|
+
:state => 'pending',
|
227
|
+
:cidr_block => '192.0.0.0/16',
|
228
|
+
:available_ip_address_count => 50,
|
229
|
+
:availability_zone => 'us-east-1c',
|
230
|
+
}}
|
231
|
+
|
232
|
+
let(:route_details) {{
|
233
|
+
:route_table_id => 'rt-123',
|
234
|
+
:vpc_id => 'vpc-12345',
|
235
|
+
}}
|
236
|
+
|
237
|
+
before(:each) do
|
238
|
+
create_response.data[:internet_gateway] = {
|
239
|
+
:internet_gateway_id => 'igw-123',
|
240
|
+
}
|
241
|
+
ec2.client.stub(:create_internet_gateway).and_return(create_response)
|
242
|
+
|
243
|
+
create_sg_response.data[:group_id] = 'group-id'
|
244
|
+
ec2.client.stub(:create_security_group).and_return(create_sg_response)
|
245
|
+
|
246
|
+
create_subnet_response.data[:subnet] = subnet_details
|
247
|
+
ec2.client.stub(:create_subnet).and_return(create_subnet_response)
|
248
|
+
|
249
|
+
create_route_response.data[:route_table] = route_details
|
250
|
+
ec2.client.stub(:create_route_table).and_return(create_route_response)
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'create all' do
|
254
|
+
vpc_config = {"vpc" => {"vpc_subnet" => "10.0.0.0/16",
|
255
|
+
"subnets" => [{"subnet_addr" => "10.0.0.0/24",
|
256
|
+
"availability_zone" => "ap-northeast-1a"},
|
257
|
+
{"subnet_addr" => "10.0.0.1/24",
|
258
|
+
"availability_zone" => "ap-northeast-1b"},
|
259
|
+
{"subnet_addr" => "10.0.0.2/24",
|
260
|
+
"availability_zone" => "ap-northeast-1c"}],
|
261
|
+
"security_group" => [{"name" =>"abc",
|
262
|
+
"description" => "NAT"},
|
263
|
+
{"name" =>"def",
|
264
|
+
"description" => "WEB"},
|
265
|
+
{"name" =>"ghi",
|
266
|
+
"description" => "DB"},
|
267
|
+
]}}
|
268
|
+
|
269
|
+
ec2.client.should_receive(:create_vpc).
|
270
|
+
with(:cidr_block => '10.0.0.0/16', :instance_tenancy => 'default').
|
271
|
+
and_return(create_vpc_response)
|
272
|
+
|
273
|
+
ec2.client.should_receive(:create_internet_gateway).
|
274
|
+
with(no_args).and_return(create_response)
|
275
|
+
|
276
|
+
ec2.client.should_receive(:attach_internet_gateway).
|
277
|
+
with(:internet_gateway_id => 'igw-123',
|
278
|
+
:vpc_id => 'vpc-12345').and_return(attach_response)
|
279
|
+
|
280
|
+
ec2.client.should_receive(:create_subnet)
|
281
|
+
ec2.client.should_receive(:create_route_table)
|
282
|
+
|
283
|
+
ec2.client.should_receive(:create_security_group)
|
284
|
+
|
285
|
+
vpcf = AWS::VPCFactory.new ec2
|
286
|
+
vpcf.create(vpc_config)
|
287
|
+
|
288
|
+
vpcf.vpcc.logger.config_log.should == {:vpc => {:vpc_subnet => '10.0.0.0/16',
|
289
|
+
:vpc_id => 'vpc-12345',
|
290
|
+
:subnets => [{:subnet_addr => "10.0.0.0/24",
|
291
|
+
:availability_zone => "ap-northeast-1a"},
|
292
|
+
{:subnet_addr => "10.0.0.1/24",
|
293
|
+
:availability_zone => "ap-northeast-1b"},
|
294
|
+
{:subnet_addr => "10.0.0.2/24",
|
295
|
+
:availability_zone => "ap-northeast-1c"}],
|
296
|
+
:security_group => [{:name =>"abc",
|
297
|
+
:description => "NAT"},
|
298
|
+
{:name =>"def",
|
299
|
+
:description => "WEB"},
|
300
|
+
{:name =>"ghi",
|
301
|
+
:description => "DB"}]}}
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"). You
|
4
|
+
# may not use this file except in compliance with the License. A copy of
|
5
|
+
# the License is located at
|
6
|
+
#
|
7
|
+
# http://aws.amazon.com/apache2.0/
|
8
|
+
#
|
9
|
+
# or in the "license" file accompanying this file. This file is
|
10
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
11
|
+
# ANY KIND, either express or implied. See the License for the specific
|
12
|
+
# language governing permissions and limitations under the License.
|
13
|
+
|
14
|
+
desc 'Run RSpec code examples'
|
15
|
+
task :spec do
|
16
|
+
opts = ['rspec', '-c']
|
17
|
+
if ENV['DEBUG']
|
18
|
+
$DEBUG = true
|
19
|
+
opts += ['-d']
|
20
|
+
end
|
21
|
+
opts += FileList["spec/**/*_spec.rb"].sort
|
22
|
+
cmd = opts.join(' ')
|
23
|
+
puts cmd if Rake.application.options.trace
|
24
|
+
system(cmd)
|
25
|
+
raise "Command failed with status (#{$?.to_i}): #{cmd}" if $?.to_i != 0
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aws-vpccreate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- YAMANE Toshiaki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: AWS VPC object creator
|
15
|
+
email:
|
16
|
+
- yamanetoshi@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- aws-vpccreate.gemspec
|
27
|
+
- lib/aws-vpccreate.rb
|
28
|
+
- lib/aws-vpccreate/version.rb
|
29
|
+
- spec/aws-vpccreate/aws-vpccreate_spec.rb
|
30
|
+
- spec/aws-vpccreate/aws-vpcfactory_spec.rb
|
31
|
+
- spec/spec_helper.rb
|
32
|
+
- tasks/rspec.rake
|
33
|
+
homepage: ''
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.23
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Simply generates AWS VPC objects
|
57
|
+
test_files:
|
58
|
+
- spec/aws-vpccreate/aws-vpccreate_spec.rb
|
59
|
+
- spec/aws-vpccreate/aws-vpcfactory_spec.rb
|
60
|
+
- spec/spec_helper.rb
|