From 43b2eaf75d9813c37ef1e10a068a244b1b28d362 Mon Sep 17 00:00:00 2001 From: Kenneth Mayer Date: Tue, 24 Apr 2012 13:01:29 -0700 Subject: [PATCH] Use heroku API for addons --- lib/heroku_san/stage.rb | 11 +++------ spec/heroku_san/stage_spec.rb | 46 +++++++++++++++-------------------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/lib/heroku_san/stage.rb b/lib/heroku_san/stage.rb index 598d894..1dacdbe 100644 --- a/lib/heroku_san/stage.rb +++ b/lib/heroku_san/stage.rb @@ -103,19 +103,14 @@ def push_config(options = nil) heroku.put_config_vars(app, params).body end - def get_installed_addons + def installed_addons heroku.get_addons(app).body end def install_addons - return if addons.empty? - installed_addons = get_installed_addons addons_to_install = addons - installed_addons.map{|a|a['name']} - if addons_to_install.any? - (addons - installed_addons.map{|a|a['name']}).each do |addon| - sh_heroku "addons:add #{addon}" rescue nil - end - installed_addons = get_installed_addons + addons_to_install.each do |addon| + heroku.post_addon(app, addon) end installed_addons end diff --git a/spec/heroku_san/stage_spec.rb b/spec/heroku_san/stage_spec.rb index 6cf7b26..129a88b 100644 --- a/spec/heroku_san/stage_spec.rb +++ b/spec/heroku_san/stage_spec.rb @@ -3,7 +3,7 @@ describe HerokuSan::Stage do include Git - subject { HerokuSan::Stage.new('production', {"app" => "awesomeapp", "stack" => "bamboo-ree-1.8.7", "addons" => ["one", "two"]})} + subject { HerokuSan::Stage.new('production', {"app" => "awesomeapp", "stack" => "bamboo-ree-1.8.7"})} STOCK_CONFIG = {"BUNDLE_WITHOUT"=>"development:test", "LANG"=>"en_US.UTF-8", "RACK_ENV"=>"production"} before do Heroku::Auth.stub(:api_key) { 'API_KEY' } @@ -40,7 +40,7 @@ describe "#stack" do it "returns the name of the stack from Heroku" do subject = HerokuSan::Stage.new('production', {"app" => "awesomeapp"}) - with_app(subject, 'name' => 'awesomeapp') do |app_data| + with_app(subject, 'name' => subject.app) do |app_data| subject.stack.should == 'bamboo-mri-1.9.2' end end @@ -245,35 +245,29 @@ subject.revision.should == '' end end + + describe "#installed_addons" do + it "returns the list of installed addons" do + with_app(subject, 'name' => subject.app) do |app_data| + subject.installed_addons.map{|a|a['name']}.should =~ %w[logging:basic shared-database:5mb] + end + end + end describe '#install_addons' do + subject { HerokuSan::Stage.new('production', {"app" => "awesomeapp", "stack" => "bamboo-ree-1.8.7", "addons" => ["custom_domains:basic", "ssl:piggyback"]})} + it "installs the addons" do - subject.stub(:get_installed_addons => []) - subject.should_receive(:sh_heroku).with("addons:add one") - subject.should_receive(:sh_heroku).with("addons:add two") - subject.install_addons - end - it "tries to install all addons" do - subject.stub(:get_installed_addons => []) - subject.should_receive(:sh_heroku).with("addons:add one").and_raise('boom 1') - subject.should_receive(:sh_heroku).with("addons:add two").and_raise('boom 2') - subject.install_addons + with_app(subject, 'name' => subject.app) do |app_data| + subject.install_addons.map{|a| a['name']}.should =~ %w[logging:basic shared-database:5mb custom_domains:basic ssl:piggyback] + subject.installed_addons.map{|a|a['name']}.should =~ subject.install_addons.map{|a| a['name']} + end end it "only installs missing addons" do - subject.stub(:get_installed_addons => [{'name' => 'one'}]) - subject.should_receive(:sh_heroku).with("addons:add two") - subject.install_addons - end - it "returns a list of installed addons" do - addons = [{'name' => 'one'}, {'name' => 'two'}] - subject.stub(:get_installed_addons => addons) - subject.install_addons.should == addons - end - it "re-queries addons after installing them" do - subject.should_receive(:get_installed_addons).and_return([]) - subject.stub(:sh_heroku => nil) - subject.should_receive(:get_installed_addons).and_return("FINAL RESULT") - subject.install_addons.should == "FINAL RESULT" + subject = HerokuSan::Stage.new('production', {"app" => "awesomeapp", "stack" => "bamboo-ree-1.8.7", "addons" => ["logging:basic","custom_domains:basic", "ssl:piggyback"]}) + with_app(subject, 'name' => subject.app) do |app_data| + subject.install_addons.map{|a| a['name']}.should =~ %w[logging:basic shared-database:5mb custom_domains:basic ssl:piggyback] + end end end end