Class: Fog::Bouncer::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/bouncer/group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, security, &block) ⇒ Group

Returns a new instance of Group.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/bouncer/group.rb', line 15

def initialize(name, description, security, &block)
  @name = name
  @description = description
  @security = security
  @using = []
  if block_given?
    @local = true
    instance_eval(&block)
    apply_definitions
  end
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



4
5
6
# File 'lib/fog/bouncer/group.rb', line 4

def description
  @description
end

#localObject

Returns the value of attribute local.



5
6
7
# File 'lib/fog/bouncer/group.rb', line 5

def local
  @local
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/fog/bouncer/group.rb', line 4

def name
  @name
end

#remoteObject

Returns the value of attribute remote.



5
6
7
# File 'lib/fog/bouncer/group.rb', line 5

def remote
  @remote
end

#securityObject (readonly)

Returns the value of attribute security.



4
5
6
# File 'lib/fog/bouncer/group.rb', line 4

def security
  @security
end

Class Method Details

.log(data, &block) ⇒ Object



7
8
9
# File 'lib/fog/bouncer/group.rb', line 7

def self.log(data, &block)
  Fog::Bouncer.log({ group: true }.merge(data), &block)
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
125
# File 'lib/fog/bouncer/group.rb', line 122

def ==(other)
  name == other.name &&
  description == other.description
end

#add_source(source, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/fog/bouncer/group.rb', line 27

def add_source(source, &block)
  if existing = sources.find { |s| s.match(source) }
    existing.instance_eval(&block)
  else
    sources << Sources.for(source, self, &block)
  end
end

#create_missing_remoteObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/bouncer/group.rb', line 35

def create_missing_remote
  unless remote?
    log(create_missing_remote: true) do
      unless Fog::Bouncer.pretending?
        @remote = Fog::Bouncer.fog.security_groups.create(:name => name, :description => description)
        @remote.reload
      end
    end
  end
end

#destroyObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fog/bouncer/group.rb', line 46

def destroy
  revoke
  if remote?
    if name != "default"
      log(destroy: true) do
        unless Fog::Bouncer.pretending?
          remote.destroy
          @remote = nil
        end
      end
    else
      log(destroy: false)
    end
  end
end

#exceeded?Boolean

Public: Check if it has exceeded the 100 rules limit per group on AWS,

http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/using-network-security.html.

Examples

exceeded?
# => false

Returns a Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/fog/bouncer/group.rb', line 71

def exceeded?
  local_permissions.size > 100
end

#extra_remote_sourcesObject



75
76
77
# File 'lib/fog/bouncer/group.rb', line 75

def extra_remote_sources
  sources.select { |source| !source.local? && source.remote? }
end

#inspectObject



127
128
129
# File 'lib/fog/bouncer/group.rb', line 127

def inspect
  "<#{self.class.name} @name=#{name.inspect} @description=#{description.inspect} @local=#{local} @remote=#{remote} @sources=#{sources.inspect}>"
end

#local?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/fog/bouncer/group.rb', line 79

def local?
  !!local
end

#log(data, &block) ⇒ Object



11
12
13
# File 'lib/fog/bouncer/group.rb', line 11

def log(data, &block)
  self.class.log({ name: name }.merge(data), &block)
end

#missing_remote_sourcesObject



83
84
85
# File 'lib/fog/bouncer/group.rb', line 83

def missing_remote_sources
  sources.select { |source| source.local? && !source.remote? }
end

#remote?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/fog/bouncer/group.rb', line 87

def remote?
  !remote.nil?
end

#revokeObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fog/bouncer/group.rb', line 91

def revoke
  permissions = sources.map do |source|
    source.protocols.select { |p| p.remote? }
  end.flatten.compact

  if remote? && permissions.any?
    log(revoke: true) do
      remote.connection.revoke_security_group_ingress(name, "IpPermissions" => IPPermissions.from(permissions)) unless Fog::Bouncer.pretending?
      permissions.each do |protocol|
        log({revoked: true}.merge(protocol.to_log))
        protocol.source.protocols.delete_if { |p| p == protocol } unless Fog::Bouncer.pretending?
      end
    end
  end
end

#sourcesObject



107
108
109
# File 'lib/fog/bouncer/group.rb', line 107

def sources
  @sources ||= []
end

#syncObject



111
112
113
114
115
116
# File 'lib/fog/bouncer/group.rb', line 111

def sync
  log(sync: true) do
    create_missing_remote
    synchronize_sources
  end
end

#use(name) ⇒ Object



118
119
120
# File 'lib/fog/bouncer/group.rb', line 118

def use(name)
  @using << security.definitions(name)
end