class Piculet::EC2Wrapper::SecurityGroupCollection::SecurityGroup

Public Class Methods

new(security_group, options) click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 12
def initialize(security_group, options)
  @security_group = security_group
  @options = options
end

Public Instance Methods

delete() click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 32
def delete
  log(:info, 'Delete SecurityGroup', :red, "#{vpc_id || :classic} > #{name}")

  if name == 'default'
    log(:warn, 'SecurityGroup `default` is reserved', :yellow)
  else
    unless @options.dry_run
      @security_group.delete
      @options.updated = true
    end
  end
end
egress_ip_permissions() click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 59
def egress_ip_permissions
  PermissionCollection.new(@security_group, :egress, @options)
end
eql?(dsl) click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 17
def eql?(dsl)
  description_eql?(dsl) and tags_eql?(dsl)
end
ingress_ip_permissions() click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 55
def ingress_ip_permissions
  PermissionCollection.new(@security_group, :ingress, @options)
end
tags() click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 49
def tags
  h = {}
  @security_group.tags.map {|k, v| h[k] = v }
  h
end
update(dsl) click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 21
def update(dsl)
  unless description_eql?(dsl)
    log(:warn, '`description` cannot be updated', :yellow, "#{vpc_id || :classic} > #{name}")
  end

  unless tags_eql?(dsl)
    log(:info, 'Update SecurityGroup', :green, "#{vpc_id || :classic} > #{name}")
    update_tags(dsl)
  end
end
vpc?() click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 45
def vpc?
  !!@security_group
end

Private Instance Methods

description_eql?(dsl) click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 64
def description_eql?(dsl)
  @security_group.description == dsl.description
end
normalize_tags(src) click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 95
def normalize_tags(src)
  normalized = {}
  src.map {|k, v| normalized[k.to_s] = v.to_s }
  normalized
end
tags_eql?(dsl) click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 68
def tags_eql?(dsl)
  self_tags = normalize_tags(self.tags)
  dsl_tags = normalize_tags(dsl.tags)
  self_tags == dsl_tags
end
update_tags(dsl) click to toggle source
# File lib/piculet/wrapper/security-group.rb, line 74
def update_tags(dsl)
  self_tags = normalize_tags(self.tags)
  dsl_tags = normalize_tags(dsl.tags)

  log(:info, "  tags:\n".green + Piculet::Utils.diff(self_tags, dsl_tags, :color => @options.color, :indent => '    '), false)

  unless @options.dry_run
    if dsl_tags.empty?
      @security_group.tags.clear
    else
      delete_keys = self_tags.keys - dsl_tags.keys
      # XXX: `delete` method does not remove the tag. It's seems a bug in the API
      #@security_group.tags.delete(delete_keys) unless delete_keys.empty?
      @security_group.tags.clear unless delete_keys.empty?
      @security_group.tags.set(dsl_tags)
    end

    @options.updated = true
  end
end