lib/security.rb in forj-0.0.21 vs lib/security.rb in forj-0.0.22

- old
+ new

@@ -17,26 +17,43 @@ require 'require_relative' require_relative 'connection.rb' include Connection +require_relative 'log.rb' +include Logging # # SecurityGroup module # module SecurityGroup def create_security_group(name) - description = format('Security group for blueprint %{name}', name: name) - Connection.network.security_groups.create( - :name => name, - :description => description - ) + begin + sec_groups = get_security_group(name) + if sec_groups.length >= 1 + sec_groups[0] + else + description = format('Security group for blueprint %{name}', name: name) + Logging.info(description) + Connection.network.security_groups.create( + :name => name, + :description => description + ) + end + rescue => e + Logging.error(e.message) + end end - def delete_security_group(security_group_id) - Connection.network.security_groups.get(security_group_id).destroy + def delete_security_group(security_group) + begin + sec_group = get_security_group(security_group) + Connection.network.security_groups.get(sec_group.id).destroy + rescue => e + Logging.error(e.message) + end end def create_security_group_rule(security_group_id, protocol, port_min, port_max) begin Connection.network.security_group_rules.create( @@ -45,14 +62,28 @@ :protocol => protocol, :port_range_min => port_min, :port_range_max => port_max, :remote_ip_prefix => '0.0.0.0/0' ) - rescue StandardError - puts format('error creating the rule for port %{port_min}', port_min: port_min) + rescue StandardError => e + msg = format('error creating the rule for port %{port_min}', port_min: port_min) + puts msg + Logging.error(e.message) end end def delete_security_group_rule(rule_id) - Connection.network.security_group_rules.get(rule_id).destroy + begin + Connection.network.security_group_rules.get(rule_id).destroy + rescue => e + Logging.error(e.message) + end + end + + def get_security_group(name) + begin + Connection.network.security_groups.all({:name => name}) + rescue => e + Logging.error(e.message) + end end end