VPC を作るナニ

朝から微妙なイベントがあり、モチベーションを激しく落としつつこちら対応に着手。当初は試験が動かずパニクッてたんですが、以下を見つけて事無きを得ました。

とりあえず aws-sdk な手続きを呼び出す wrapper を検討しつつ試験ドリブンで色々書きました。
vpc を create するソレの試験が以下なカンジで

  describe 'create_vpc' do
    it 'calls #create_vpc on the client' do
      ec2.client.should_receive(:create_vpc).
        with(:cidr_block => '10.0.0.0/16', :instance_tenancy => 'default').
        and_return(create_vpc_response)

      vpcc.create_vpc('10.0.0.0/16')
    end

    it 'calls accept a different tenancy value' do
      ec2.client.should_receive(:create_vpc).
        with(:cidr_block => '10.0.0.0/24', :instance_tenancy => 'dedicated').
        and_return(create_vpc_response)

      vpcc.create_vpc('10.0.0.0/24', :instance_tenancy => :dedicated)
    end

    it 'returns a VPC object' do
      vpc = vpcc.create_vpc('192.0.0.0/16')
      vpc.should be_a(AWS::EC2::VPC)
      vpc.vpc_id.should == 'vpc-12345'
      vpc.cidr_block.should == '192.0.0.0/16'
#      vpc.dhcp_options_id.should == 'dopt-12345'
      vpc.instance_tenancy.should == :default
    end
  end

あるいは wrapper が以下。

    def create_vpc cidr_block, options = {}
      @vpc = @ec2.vpcs.create(cidr_block, options)
    end

これらを

  • vpc 作成
  • subnet 作成
  • internet gateway 作成
  • security group 作成
  • route table 作成

について書いてさあどうするか、な状況だったのですが、帰りしな Factory Method 使えば良いのだな、という事に気づいた次第。

ちなみに

credential が云々というエラーが出てたんですがとりあえず config を以下にすることで回避できてはいます。

  let!(:config) { AWS.config.with(:stub_requests => true,
                                 :access_key_id => "AKID",
                                 :secret_access_key => 'b') }

とりあえず

  • VPCFactory なクラスを作って
  • create な static メソドを作成

インクリメンタルに作成できるあたりが色々面白いですね。現時点では以下なカンジで使う方向なのかどうなのか。

require 'aws-vpccreate'

config_file = File.join(File.dirname(__FILE__).'vpc.yml')
AWS::VPCFactory.create(YAML.load(File.read(config_file)))

もう少し弄りつつ云々する方向。

追記

lib/aws-vpccreate.rb にクラス定義を追加。これは require する名前になっているのか。

  class VPCFactory
    def self.create config
      raise "no config" if config == {}
      raise "no vpc" if !config.key?('vpc') 
      raise "no vpc_subnet" if !config["vpc"].key?('vpc_subnet')
      raise "no vpc_subnet" if config["vpc"]["vpc_subnet"] == ""
    end
  end

試験も追加。こちらは spec/aws-vpccreate/aws-vpcfactory_spec.rb です。

require 'spec_helper'

describe AWS::VPCFactory do
  let!(:config) { AWS.config.with(:stub_requests => true,
                                 :access_key_id => "AKID",
                                 :secret_access_key => 'b') }
  let!(:ec2) { AWS::EC2.new(:config => config) }
  let!(:vpcc) { AWS::Vpccreate.new(ec2) }

  let(:create_vpc_response) { ec2.client.stub_for(:create_vpc) }

  let(:vpc_details) {{
      :vpc_id => 'vpc-12345',
      :state => 'pending',
      :cidr_block => '192.0.0.0/16',
      :dhcp_option_id => 'dopt-12345',
      :instance_tenancy => 'default',
    }}

  before(:each) do
    create_vpc_response.data[:vpc] = vpc_details
    ec2.client.stub(:create_vpc).and_return(create_vpc_response)
  end

  describe 'create' do
    let!(:vpc) { vpcc.create_vpc('10.0.0.0/16') }

    it 'no configuration (config is {})' do
      config = {}
      proc { VPCFactory.create(config) }.should raise_error
    end

    it 'no configuration' do
      proc { VPCFactory.create }.should raise_error
    end

    it 'no vpc_subnet' do
      config = {"vpc" => {}}

      proc { VPCFactory.create(config) }.should raise_error
    end

    it 'vpc_subnet is ""' do
      config = {"vpc" => {"vpc_subnet" => ""}}

      proc { VPCFactory.create(config) }.should raise_error
    end
  end
end

不要な部分が残っているカンジですが試験にはパスしてます。