Class: BuildCloud::SecurityGroup

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/build-cloud/securitygroup.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 = {}) ⇒ SecurityGroup

Returns a new instance of SecurityGroup.



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

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

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

    @log.debug( options.inspect )

    required_options(:name, :description)
    require_one_of(:vpc_id, :vpc_name)

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/securitygroup.rb', line 7

def self.get_id_by_name( name )

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

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

    sg_fog = sg.read

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

    sg_fog.group_id

end

Instance Method Details

#createObject



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

def create
    
    return if exists?

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

    options = @options.dup

    unless options[:vpc_id]

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

    end

    authorized_ranges = []
    if options[:authorized_ranges]
        authorized_ranges = options[:authorized_ranges]
        options.delete(:authorized_ranges)
    end

    security_group = @compute.security_groups.new( options )
    security_group.save

    authorized_ranges.each do |r|

        security_group.authorize_port_range(
            r.delete(:min_port)..r.delete(:max_port), r
        )

    end

    @log.debug( security_group.inspect )

end

#deleteObject



80
81
82
83
84
85
86
87
88
# File 'lib/build-cloud/securitygroup.rb', line 80

def delete

    return unless exists?

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

    fog_object.destroy

end

#readObject Also known as: fog_object



74
75
76
# File 'lib/build-cloud/securitygroup.rb', line 74

def read
    @compute.security_groups.select { |sg| sg.name == @options[:name] }.first
end