---
src/Gemfile | 1 +
src/app/controllers/instances_controller.rb | 9 ++++++++-
src/app/models/instance.rb | 26 ++++++++++++++++++++++++++
src/app/views/instances/_list.haml | 1 +
src/config/routes.rb | 1 +
src/spec/models/instance_spec.rb | 17 +++++++++++++++++
6 files changed, 54 insertions(+), 1 deletions(-)
diff --git a/src/Gemfile b/src/Gemfile
index ce11e9d..05d3b7c 100644
--- a/src/Gemfile
+++ b/src/Gemfile
@@ -21,6 +21,7 @@ gem 'pg'
gem 'thin'
gem 'json'
gem 'railties'
+gem 'fastercsv'
group :development, :test do
#gem 'rspec-rails'
#gem 'factory_girl_rails'
diff --git a/src/app/controllers/instances_controller.rb b/src/app/controllers/instances_controller.rb
index bde8bb3..09046a1 100644
--- a/src/app/controllers/instances_controller.rb
+++ b/src/app/controllers/instances_controller.rb
@@ -1,7 +1,7 @@
class InstancesController < ApplicationController
before_filter :require_user
before_filter :load_instance, :only => [:show, :key, :edit, :update]
- before_filter :set_view_vars, :only => [:show, :index]
+ before_filter :set_view_vars, :only => [:show, :index, :export_events]
def index
@params = params
@@ -12,6 +12,7 @@ class InstancesController < ApplicationController
format.html
format.js { render :partial => 'list' }
format.json { render :json => @instances }
+
end
end
@@ -148,6 +149,12 @@ class InstancesController < ApplicationController
end
end
+ def export_events
+ send_data(Instance.csv_export(load_instances),
+ :type => 'text/csv; charset=utf-8; header=present',
+ :filename => "export.csv")
+ end
+
private
def load_instance
diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb
index aee4750..85454cf 100644
--- a/src/app/models/instance.rb
+++ b/src/app/models/instance.rb
@@ -341,6 +341,32 @@ class Instance < ActiveRecord::Base
read_attribute(:public_addresses)
end
+ def create_auth_key
+ raise "instance provider_account is not set" unless self.provider_account
+ client = self.provider_account.connect
+ return nil unless client && client.feature?(:instances, :authentication_key)
+ kname = "#{self.name}_#{Time.now.to_i}_key_#{self.object_id}".gsub(/[^a-zA-Z0-9\.\-]/, '_')
+ if key = client.create_key(:name => kname)
+ self.update_attribute(:instance_key, InstanceKey.create!(:pem => key.pem.first, :name => key.id, :instance_key_owner => self))
+ end
+ end
+
+ def self.csv_export(instances)
+ csv_string = FasterCSV.generate(:col_sep => ";", :row_sep => "\r\n") do |csv|
+ event_attributes = Event.new.attributes.keys.reject {|key| key if key == "created_at" || key == "updated_at"}
+
+ csv << event_attributes.map {|event| event.capitalize }
+
+ events = instances.map{|i| i.events}.flatten!
+ unless events.nil?
+ events.each do |event|
+ csv << event_attributes.map {|event_attribute| event[event_attribute] }
+ end
+ end
+ end
+ csv_string
+ end
+
scope :with_hardware_profile, lambda {
{:include => :hardware_profile}
}
diff --git a/src/app/views/instances/_list.haml b/src/app/views/instances/_list.haml
index f9545e4..316ad14 100644
--- a/src/app/views/instances/_list.haml
+++ b/src/app/views/instances/_list.haml
@@ -1,4 +1,5 @@
- content_for :form_header do
+ %li= link_to "Export events", export_events_instances_path, :class => 'button'
%li= restful_submit_tag 'Stop Selected Instances', 'stop', multi_stop_instances_path, 'GET', :class => 'button', :id => 'stop_selected_instances'
= hidden_field_tag 'backlink', request.url
%li
diff --git a/src/config/routes.rb b/src/config/routes.rb
index 67262ae..3def92a 100644
--- a/src/config/routes.rb
+++ b/src/config/routes.rb
@@ -95,6 +95,7 @@ Conductor::Application.routes.draw do
get 'start'
get 'multi_stop'
get 'remove_failed'
+ get 'export_events'
end
member do
get 'key'
diff --git a/src/spec/models/instance_spec.rb b/src/spec/models/instance_spec.rb
index 13ccafe..da0f849 100644
--- a/src/spec/models/instance_spec.rb
+++ b/src/spec/models/instance_spec.rb
@@ -194,4 +194,21 @@ describe Instance do
@pool.pool_family.provider_accounts = [FactoryGirl.create(:mock_provider_account, :label => 'testaccount', :provider => provider)]
@instance.matches.first.should_not be_empty
end
+
+ it "should return csv header string for export" do
+ reader = CSV::Reader.create(Instance.csv_export([FactoryGirl.create(:instance)]))
+ header = reader.shift
+ ['Status_code','Event_time','Summary','Source_type','Description','Source_id'].each do |attribute|
+ header[0].split(';').include?(attribute).should be_true
+ end
+ end
+
+ it "should return csv string for export" do
+ instance = FactoryGirl.create(:instance)
+ export_string = Instance.csv_export([instance]).gsub(/\s+/, "")
+
+ export_string.include?(instance.id.to_s).should be_true
+ export_string.include?("Instance").should be_true
+ export_string.include?("created").should be_true
+ end
end
--
1.7.6