Skip to content
Browse files

[Vcloud] Handle Internet Service Monitors for Terremark::Ecloud

  • Loading branch information...
1 parent 24955b0 commit c3365f337db73eac9c2b94ec1699bdf60c01207d freeformz committed with geemus
View
23 lib/fog/vcloud/terremark/ecloud/models/internet_service.rb
@@ -34,6 +34,26 @@ def save
end
end
+ def monitor=(new_monitor = {})
+ if new_monitor.nil?
+ @monitor = nil
+ elsif new_monitor.is_a?(Hash)
+ @monitor = {}
+ @monitor[:type] = new_monitor[:MonitorType] || new_monitor[:type]
+ @monitor[:url_send_string] = new_monitor[:UrlSendString] || new_monitor[:url_send_string]
+ @monitor[:http_headers] = new_monitor[:HttpHeader] || new_monitor[:http_headers]
+ @monitor[:http_headers] = @monitor[:http_headers].split("\n") unless @monitor[:http_headers].is_a?(Array)
+ @monitor[:receive_string] = new_monitor[:ReceiveString] || new_monitor[:receive_string]
+ @monitor[:interval] = new_monitor[:Interval] || new_monitor[:interval]
+ @monitor[:response_timeout] = new_monitor[:ResponseTimeOut] || new_monitor[:response_timeout]
+ @monitor[:downtime] = new_monitor[:DownTime] || new_monitor[:downtime]
+ @monitor[:retries] = new_monitor[:Retries] || new_monitor[:retries]
+ @monitor[:is_enabled] = new_monitor[:IsEnabled] || new_monitor[:is_enabled]
+ else
+ raise RuntimeError.new("monitor needs to either be nil or a Hash")
+ end
+ end
+
def nodes
@nodes ||= Fog::Vcloud::Terremark::Ecloud::Nodes.new( :connection => connection, :href => href + "/nodeServices" )
end
@@ -41,8 +61,9 @@ def nodes
private
def _compose_service_data
+ #For some reason inject didn't work
service_data = {}
- self.class.attributes.select{ |attribute| !send(attribute).nil? }.each { |attribute| service_data[attribute] = send(attribute).to_s }
+ self.class.attributes.select{ |attribute| !send(attribute).nil? }.each { |attribute| service_data[attribute] = send(attribute) }
service_data
end
View
77 lib/fog/vcloud/terremark/ecloud/requests/add_internet_service.rb
@@ -15,17 +15,23 @@ def generate_internet_service_request(service_data)
builder.Enabled(service_data[:enabled])
builder.Description(service_data[:description])
builder.RedirectURL(service_data[:redirect_url])
- #builder.Monitor {
- # builder.MonitorType {}
- # builder.UrlSendString {}
- # builder.HttpHeader {}
- # builder.ReceiveString {}
- # builder.Interval {}
- # builder.ResponseTimeOut {}
- # builder.DownTime {}
- # builder.Retries {}
- # builder.IsEnabled {}
- #}
+ if monitor = service_data[:monitor]
+ generate_monitor_section(builder,monitor)
+ end
+ }
+ end
+
+ def generate_monitor_section(builder, monitor)
+ builder.Monitor {
+ builder.MonitorType(monitor[:type])
+ builder.UrlSendString(monitor[:url_send_string])
+ builder.HttpHeader(monitor[:http_headers].join("\n"))
+ builder.ReceiveString(monitor[:receive_string])
+ builder.Interval(monitor[:interval])
+ builder.ResponseTimeOut(monitor[:response_timeout])
+ builder.DownTime(monitor[:downtime])
+ builder.Retries(monitor[:retries])
+ builder.IsEnabled(monitor[:is_enabled])
}
end
@@ -39,8 +45,55 @@ def validate_internet_service_data(service_data, configure=false)
end
end
+ def validate_internet_service_monitor(monitor)
+ #FIXME: Refactor this type of function into something generic
+ required_opts = [:type, :url_send_string, :http_headers, :receive_string, :is_enabled]
+
+ unless required_opts.all? { |opt| monitor.keys.include?(opt) && monitor[opt] }
+ raise ArgumentError.new("Required Monitor data missing: #{(required_opts - monitor.keys).map(&:inspect).join(", ")}")
+ end
+
+ unless ['HTTP','ECV'].include?(monitor[:type])
+ raise ArgumentError.new("Supported monitor types are: ECV & HTTP")
+ end
+
+ unless monitor[:http_headers].is_a?(Array) || monitor[:http_headers].is_a?(String)
+ raise ArgumentError.new("Monitor :http_headers must be a String or Array")
+ end
+
+ unless [true, false, "true", "false"].include?(monitor[:is_enabled])
+ raise ArgumentError.new("Monitor :is_enabled must be true or false")
+ end
+ end
+
+ def ensure_monitor_defaults!(monitor)
+ if monitor[:http_headers].is_a?(String)
+ monitor[:http_headers] = [ monitor[:http_headers] ]
+ end
+
+ unless monitor[:retries]
+ monitor[:retries] = 3
+ end
+
+ unless monitor[:response_timeout]
+ monitor[:response_timeout] = 2
+ end
+
+ unless monitor[:down_time]
+ monitor[:down_time] = 30
+ end
+
+ unless monitor[:interval]
+ monitor[:interval] = 5
+ end
+ end
+
def add_internet_service(internet_services_uri, service_data)
validate_internet_service_data(service_data)
+ if monitor = service_data[:monitor]
+ validate_internet_service_monitor(monitor)
+ ensure_monitor_defaults!(monitor)
+ end
request(
:body => generate_internet_service_request(service_data),
@@ -64,7 +117,7 @@ def add_internet_service(internet_services_uri, service_data)
validate_internet_service_data(service_data)
internet_services_uri = ensure_unparsed(internet_services_uri)
-
+
if ip = ip_from_uri(internet_services_uri)
id = rand(1000)
new_service = service_data.merge!( { :href => Fog::Vcloud::Terremark::Ecloud::Mock.internet_service_href( { :id => id } ),
View
8 lib/fog/vcloud/terremark/ecloud/requests/configure_internet_service.rb
@@ -22,6 +22,9 @@ def generate_internet_service_response(service_data,ip_address_data)
builder.Href(ip_address_data[:href].to_s)
builder.Name(ip_address_data[:name])
}
+ if monitor = service_data[:monitor]
+ generate_monitor_section(builder,monitor)
+ end
}
end
@@ -37,6 +40,11 @@ def configure_internet_service(internet_service_uri, service_data, ip_address_da
validate_public_ip_address_data(ip_address_data)
+ if monitor = service_data[:monitor]
+ validate_internet_service_monitor(monitor)
+ ensure_monitor_defaults!(monitor)
+ end
+
request(
:body => generate_internet_service_response(service_data, ip_address_data),
:expects => 200,

0 comments on commit c3365f3

Please sign in to comment.
Something went wrong with that request. Please try again.