lib/kakine/adapter/real.rb in kakine-0.3.0 vs lib/kakine/adapter/real.rb in kakine-0.4.0
- old
+ new
@@ -1,53 +1,66 @@
-require 'json'
module Kakine
class Adapter
class Real
+ include Kakine::Adapter::Base
def create_rule(security_group_id, direction, security_rule)
- attributes = {}
- %w(protocol port_range_max port_range_min remote_ip ethertype).each do |k|
- attributes[k] = eval("security_rule.#{k}")
- end
- if attributes["remote_ip"]
- attributes["remote_ip_prefix"] = attributes.delete("remote_ip")
- end
-
- data = {}
- attributes.each{|k,v| data[k.to_sym] = v}
begin
- Fog::Network[:openstack].create_security_group_rule(security_group_id, direction, data)
- rescue Excon::Errors::Conflict => e
- JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
- rescue Excon::Errors::BadRequest => e
- JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
+ security_rule = symbolized_rule(security_rule)
+ Yao::SecurityGroupRule.create(security_rule.merge({'security_group_id' => security_group_id, 'direction' => direction}))
+ rescue Excon::Errors::Conflict, Excon::Errors::BadRequest => e
+ error_message(e.response[:body])
+ rescue Kakine::SecurityRuleError => e
+ puts e
end
end
def delete_rule(security_group_rule_id)
- Fog::Network[:openstack].delete_security_group_rule(security_group_rule_id)
+ Yao::SecurityGroupRule.destroy(security_group_rule_id)
end
def create_security_group(attributes)
- data = {}
- attributes.each{|k,v| data[k.to_sym] = v}
begin
- response = Fog::Network[:openstack].create_security_group(data)
- response.data[:body]["security_group"]["id"]
- rescue Excon::Errors::Conflict => e
- JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
- rescue Excon::Errors::BadRequest => e
- JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
+ security_group = Yao::SecurityGroup.create(symbolized_group(attributes))
+ {"id" => security_group.id}
+ rescue Excon::Errors::Conflict, Excon::Errors::BadRequest => e
+ error_message(e.response[:body])
end
end
def delete_security_group(security_group_id)
begin
- Fog::Network[:openstack].delete_security_group(security_group_id)
- rescue Excon::Errors::Conflict => e
- JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
- rescue Excon::Errors::BadRequest => e
- JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
+ Yao::SecurityGroup.destroy(security_group_id)
+ rescue Excon::Errors::Conflict, Excon::Errors::BadRequest => e
+ error_message(e.response[:body])
end
+ end
+
+ private
+
+ def error_message(errors)
+ if errors.kind_of?(JSON)
+ JSON.parse(errors.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
+ else
+ puts errors
+ end
+ end
+
+ def symbolized_group(attributes)
+ attributes.inject({}){|data,(k,v)|data[k.to_sym] = v; data }
+ end
+
+ def symbolized_rule(security_rule)
+ attributes = {}
+ %w(protocol port_range_max port_range_min remote_ip ethertype).each do |k|
+ attributes[k.to_sym] = security_rule.send(k)
+ end
+
+ if security_rule.remote_group
+ attributes[:remote_group_id] = security_rule.remote_group_id
+ else
+ attributes[:remote_ip_prefix] = attributes.delete(:remote_ip) if attributes[:remote_ip]
+ end
+ attributes
end
end
end
end