Sha256: 4658c8a86471384baa4f7d11dbab1a82dbf15baa15abb81577a36a17eb21bf11

Contents?: true

Size: 1.97 KB

Versions: 10

Compression:

Stored size: 1.97 KB

Contents

module Fog
  module Bouncer
    class SourceManager
      def self.log(data, &block)
        Fog::Bouncer.log({source_manager: true}.merge(data), &block)
      end

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

      def initialize(group)
        @group = group
      end

      def synchronize
        log(synchronize: true) do
          create_missing_source_permissions
          remove_extra_source_permissions
          @group.sources.each { |s| s.remote = true } unless Fog::Bouncer.pretending?
        end
      end

      private

      def create_missing_source_permissions
        if missing_source_permissions.any?
          @group.remote.connection.authorize_security_group_ingress(@group.name, "IpPermissions" => IPPermissions.from(missing_source_permissions, :local_only => true)) unless Fog::Bouncer.pretending?
          missing_source_permissions.each do |protocol|
            log({authorized: true}.merge(protocol.to_log))
            protocol.remote = true unless Fog::Bouncer.pretending?
          end
        end
      end

      def missing_source_permissions
        @group.sources.map do |source|
          source.protocols.select { |p| p.local? && !p.remote? }
        end.flatten.compact
      end

      def remove_extra_source_permissions
        if extra_source_permissions.any?
          @group.remote.connection.revoke_security_group_ingress(@group.name, "IpPermissions" => IPPermissions.from(extra_source_permissions, :remote_only => true)) unless Fog::Bouncer.pretending?
          extra_source_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

      def extra_source_permissions
        @group.sources.map do |source|
          source.protocols.select { |p| !p.local? && p.remote? }
        end.flatten.compact
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fog-bouncer-0.2.5 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.2.4 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.2.3 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.2.2 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.2.1 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.2.0 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.1.1 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.1.0 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.0.8 lib/fog/bouncer/source_manager.rb
fog-bouncer-0.0.6 lib/fog/bouncer/source_manager.rb