File: create_vpc.rb

package info (click to toggle)
ruby-fog-aws 3.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,140 kB
  • sloc: ruby: 73,328; javascript: 14; makefile: 9; sh: 4
file content (124 lines) | stat: -rw-r--r-- 5,635 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
module Fog
  module AWS
    class Compute
      class Real
        require 'fog/aws/parsers/compute/create_vpc'

        # Creates a VPC with the CIDR block you specify.
        #
        # ==== Parameters
        # * cidrBlock<~String> - The CIDR block you want the VPC to cover (e.g., 10.0.0.0/16).
        # * options<~Hash>:
        #   * InstanceTenancy<~String> - The allowed tenancy of instances launched into the VPC. A value of default
        #     means instances can be launched with any tenancy; a value of dedicated means instances must be launched with tenancy as dedicated.
        #
        # === Returns
        # * response<~Excon::Response>:
        # * body<~Hash>:
        # * 'requestId'<~String> - Id of request
        # * 'vpc'<~Array>:
        # * 'vpcId'<~String> - The VPC's ID
        # * 'state'<~String> - The current state of the VPC. ['pending', 'available']
        # * 'cidrBlock'<~String> - The CIDR block the VPC covers.
        # * 'dhcpOptionsId'<~String> - The ID of the set of DHCP options.
        # * 'tagSet'<~Array>: Tags assigned to the resource.
        # * 'key'<~String> - Tag's key
        # * 'value'<~String> - Tag's value
        #
        # {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/2011-07-15/APIReference/index.html?ApiReference-query-CreateVpc.html]
        def create_vpc(cidrBlock, options = {})
          request({
            'Action' => 'CreateVpc',
            'CidrBlock' => cidrBlock,
            :parser => Fog::Parsers::AWS::Compute::CreateVpc.new
          }.merge!(options))
        end
      end

      class Mock
        def create_vpc(cidrBlock, options = {})
          Excon::Response.new.tap do |response|
            if cidrBlock
              response.status = 200
              vpc_id = Fog::AWS::Mock.vpc_id
              vpc = {
                'vpcId'                       => vpc_id,
                'state'                       => 'pending',
                'cidrBlock'                   => cidrBlock,
                'dhcpOptionsId'               => Fog::AWS::Mock.request_id,
                'tagSet'                      => {},
                'enableDnsSupport'            => true,
                'enableDnsHostnames'          => false,
                'mapPublicIpOnLaunch'         => false,
                'classicLinkEnabled'          => false,
                'classicLinkDnsSupport'       => false,
                'cidrBlockAssociationSet'     => [{ 'cidrBlock' => cidrBlock, 'state' => 'associated', 'associationId' => "vpc-cidr-assoc-#{vpc_id}" }],
                'ipv6CidrBlockAssociationSet' => [],
                'instanceTenancy'             => options['InstanceTenancy'] || 'default'
              }
              self.data[:vpcs].push(vpc)

              #Creates a default route for the subnet
              default_route = self.route_tables.new(:vpc_id => vpc_id)
              default_route.save

              # You are not able to push a main route in the normal AWS, so we are re-implementing some of the
              # associate_route_table here in order to accomplish this.
              route_table = self.data[:route_tables].find { |routetable| routetable["routeTableId"].eql? default_route.id }

              # This pushes a main route to the associationSet
              # add_route_association(routeTableId, subnetId, main=false) is declared in assocate_route_table.rb
              assoc = add_route_association(default_route.id, nil, true)
              route_table["associationSet"].push(assoc)

              # Create a default network ACL
              default_nacl = self.network_acls.new(:vpc_id => vpc_id)
              default_nacl.save
              # Manually override since Amazon doesn't let you create a default one
              self.data[:network_acls][default_nacl.network_acl_id]['default'] = true


              # create default security groups
              default_elb_group_name = "default_elb_#{Fog::Mock.random_hex(6)}"
              default_elb_group_id = Fog::AWS::Mock.security_group_id

              Fog::AWS::Compute::Mock.data[region][@aws_access_key_id][:security_groups][default_elb_group_id] = {
                'groupDescription'    => 'default_elb security group',
                'groupName'           => default_elb_group_name,
                'groupId'             => default_elb_group_id,
                'ipPermissions'       => [],
                'ownerId'             => self.data[:owner_id],
                'vpcId'               => vpc_id
              }

              default_group_name = 'default'
              default_group_id = Fog::AWS::Mock.security_group_id

              Fog::AWS::Compute::Mock.data[region][@aws_access_key_id][:security_groups][default_group_id] = {
                'groupDescription'    => default_group_name,
                'groupName'           => default_group_name,
                'groupId'             => default_group_id,
                'ipPermissions'       => [],
                'ownerId'             => self.data[:owner_id],
                'vpcId'               => vpc_id
              }

              response.body = {
                'requestId' => Fog::AWS::Mock.request_id,
                'vpcSet'    => [vpc]
              }
            else
              response.status = 400
              response.body = {
                'Code' => 'InvalidParameterValue'
              }
              if cidrBlock.empty?
                response.body['Message'] = "Invalid value '' for cidrBlock. Must be specified."
              end
            end
          end
        end
      end
    end
  end
end