Class: BuildCloud::RouteTable

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/build-cloud/routetable.rb

Constant Summary collapse

@@objects =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Component

included

Constructor Details

#initialize(fog_interfaces, log, options = {}) ⇒ RouteTable

Returns a new instance of RouteTable.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/build-cloud/routetable.rb', line 25

def initialize ( fog_interfaces, log, options = {} )

    @compute = fog_interfaces[:compute]
    @log     = log
    @options = options

    @log.debug( options.inspect )

    required_options(:name)
    require_one_of(:vpc_id, :vpc_name)
    require_one_of(:subnet_ids, :subnet_names)


end

Class Method Details

.get_id_by_name(name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/build-cloud/routetable.rb', line 7

def self.get_id_by_name( name )

    route_table = self.search( :name => name ).first

    unless route_table
        raise "Couldn't get a RouteTable object for #{name} - is it defined?"
    end

    route_table_fog = route_table.read

    unless route_table_fog
        raise "Couldn't get a RouteTable fog object for #{name} - is it created?"
    end

    route_table_fog.route_table_id

end

Instance Method Details

#createObject



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
# File 'lib/build-cloud/routetable.rb', line 40

def create

    return if exists?

    @log.info("Creating route table #{@options[:name]}")

    options = @options.dup

    unless options[:subnet_ids]

        options[:subnet_ids] = []

        options[:subnet_names].each do |sn|
            options[:subnet_ids] << BuildCloud::Subnet.get_id_by_name( sn )
        end

        options.delete(:subnet_names)

    end

    unless options[:vpc_id]

        options[:vpc_id] = BuildCloud::VPC.get_id_by_name( options[:vpc_name] )
        options.delete(:vpc_name)

    end

    options[:tags] = { 'Name' => options.delete(:name) }

    # Using requests instead of model here, because the model
    #  doesn't support associations.
 
    rt = @compute.route_tables.new ( options )
    rt.save
    @log.debug(rt.inspect)

    @compute.create_tags( rt.id, options[:tags] )
   
    options[:subnet_ids].each do |s|
        @compute.associate_route_table( rt.id, s )
    end

end

#deleteObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/build-cloud/routetable.rb', line 90

def delete

    return unless exists?

    @log.info("Deleting route table #{@options[:name]}")

    read.associations.each do |ra|
        @compute.disassociate_route_table( ra['routeTableAssociationId'] )
    end

    fog_object.destroy

end

#readObject Also known as: fog_object



84
85
86
# File 'lib/build-cloud/routetable.rb', line 84

def read
    @compute.route_tables.select { |r| r.tags['Name'] == @options[:name] }.first
end