Sha256: 151f2eeef7b9d9b112e363b05125afdb4ed6b2e1ab5739a6d4b0d415ccd97ce5

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'json'
module Kakine
  class SecurityGroup
    attr_reader :id, :name, :tenant_name, :description, :rules

    def initialize(tenant_name, params)
      @name        = params[0]
      @tenant_name = tenant_name
      @id          = params[1]["id"] || ""
      @description = params[1]["description"] || ""
      @rules       = get_rule_instances(params) || []
    end

    def tenant_id
      Kakine::Resource.get(:openstack).tenant(@tenant_name).id
    end

    def ==(target_sg)
      same_group?(target_sg) && same_rule?(self, target_sg) && same_rule?(target_sg, self)
    end

    def !=(target_sg)
      !(self == target_sg)
    end

    def same_group?(target_sg)
      %i(@name @tenant_name @description).all? do |val|
        instance_variable_get(val) == target_sg.instance_variable_get(val)
      end
    end

    def same_rule?(a, b)
      a.rules.all? do |rule|
        b.find_by_rule(rule)
      end
    end

    def get_rule_instances(params)
      params[1]["rules"].map do |rule|
        SecurityRule.new(rule, @tenant_name, @name)
      end unless params[1]["rules"].nil?
    end

    def find_by_rule(target_rule)
      @rules.find { |rule| rule == target_rule }
    end

    def has_rules?
      @rules.detect {|v| !v.nil?}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kakine-0.4.0 lib/kakine/security_group.rb