Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroy and info command #91

Merged
merged 3 commits into from
Feb 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,29 @@ def destroy(name=nil)
})
end

desc "destroy_image FUZZY_NAME", "Destroy an image"
method_option "id",
:type => :string,
:aliases => "-i",
:desc => "The ID of the image."
method_option "name",
:type => :string,
:aliases => "-n",
:desc => "The exact name of the image"
method_option "confirm",
:type => :boolean,
:aliases => "-c",
:desc => "Skip confirmation of the action"
def destroy_image(name=nil)
Middleware.sequence_destroy_image.call({
"user_image_id" => options[:id],
"user_image_name" => options[:name],
"user_image_fuzzy_name" => name,
"user_confirm_action" => options[:confirm],
"user_quiet" => options[:quiet]
})
end

desc "restart FUZZY_NAME", "Restart a droplet"
method_option "id",
:type => :string,
Expand Down Expand Up @@ -269,6 +292,24 @@ def info(name=nil)
})
end

desc "info_image FUZZY_NAME [OPTIONS]", "Show an image's information"
method_option "id",
:type => :string,
:aliases => "-i",
:desc => "The ID of the image."
method_option "name",
:type => :string,
:aliases => "-n",
:desc => "The exact name of the image"
def info_image(name=nil)
Middleware.sequence_info_image.call({
"user_image_id" => options[:id],
"user_image_name" => options[:name],
"user_image_fuzzy_name" => name,
"user_quiet" => options[:quiet]
})
end

desc "snapshot SNAPSHOT_NAME FUZZY_NAME [OPTIONS]", "Queue a snapshot of the droplet."
method_option "id",
:type => :string,
Expand Down
25 changes: 25 additions & 0 deletions lib/tugboat/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ module Middleware
autoload :CreateDroplet, "tugboat/middleware/create_droplet"
autoload :RebuildDroplet, "tugboat/middleware/rebuild_droplet"
autoload :DestroyDroplet, "tugboat/middleware/destroy_droplet"
autoload :DestroyImage, "tugboat/middleware/destroy_image"
autoload :FindDroplet, "tugboat/middleware/find_droplet"
autoload :FindImage, "tugboat/middleware/find_image"
autoload :HaltDroplet, "tugboat/middleware/halt_droplet"
autoload :InfoDroplet, "tugboat/middleware/info_droplet"
autoload :InfoImage, "tugboat/middleware/info_image"
autoload :InjectClient, "tugboat/middleware/inject_client"
autoload :InjectConfiguration, "tugboat/middleware/inject_configuration"
autoload :ListDroplets, "tugboat/middleware/list_droplets"
Expand Down Expand Up @@ -121,6 +123,17 @@ def self.sequence_info_droplet
end
end

# Show information about an image
def self.sequence_info_image
::Middleware::Builder.new do
use InjectConfiguration
use CheckConfiguration
use InjectClient
use FindImage
use InfoImage
end
end

# SSH into a droplet
def self.sequence_ssh_droplet
::Middleware::Builder.new do
Expand Down Expand Up @@ -168,6 +181,18 @@ def self.sequence_destroy_droplet
end
end

# Destroy an image
def self.sequence_destroy_image
::Middleware::Builder.new do
use InjectConfiguration
use CheckConfiguration
use InjectClient
use FindImage
use ConfirmAction
use DestroyImage
end
end

# Snapshot a droplet
def self.sequence_snapshot_droplet
::Middleware::Builder.new do
Expand Down
23 changes: 23 additions & 0 deletions lib/tugboat/middleware/destroy_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Tugboat
module Middleware
class DestroyImage < Base
def call(env)
ocean = env["ocean"]

say "Queuing destroy image for #{env["image_id"]} #{env["image_name"]}...", nil, false

req = ocean.images.delete env["image_id"]

if req.status == "ERROR"
say "#{req.status}: #{req.error_message}", :red
exit 1
end

say "done", :green

@app.call(env)
end
end
end
end

25 changes: 25 additions & 0 deletions lib/tugboat/middleware/info_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Tugboat
module Middleware
class InfoImage < Base
def call(env)
ocean = env["ocean"]

req = ocean.images.show env["image_id"]

if req.status == "ERROR"
say "#{req.status}: #{req.error_message}", :red
exit 1
end

image = req.image

say
say "Name: #{image.name}"
say "ID: #{image.id}"
say "Distribution: #{image.distribution}"

@app.call(env)
end
end
end
end
88 changes: 88 additions & 0 deletions spec/cli/destroy_image_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'spec_helper'

describe Tugboat::CLI do
include_context "spec"

describe "destroy image" do
it "destroys an image with a fuzzy name" do
stub_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_images"))

stub_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

$stdin.should_receive(:gets).and_return("y")

@cli.destroy_image("NLP Final")

expect($stdout.string).to eq <<-eos
Image fuzzy name provided. Finding image ID...done\e[0m, 478 (NLP Final)\nWarning! Potentially destructive action. Please confirm [y/n]: Queuing destroy image for 478 (NLP Final)...done
eos

expect(a_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

it "destroys an image with an id" do
stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

stub_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

$stdin.should_receive(:gets).and_return("y")

@cli.options = @cli.options.merge(:id => 478)
@cli.destroy_image

expect($stdout.string).to eq <<-eos
Image id provided. Finding Image...done\e[0m, 478 (NLP Final)\nWarning! Potentially destructive action. Please confirm [y/n]: Queuing destroy image for 478 (NLP Final)...done
eos

expect(a_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end


it "destroys an image with a name" do
stub_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_images"))

stub_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

$stdin.should_receive(:gets).and_return("y")

@cli.options = @cli.options.merge(:name => "NLP Final")
@cli.destroy_image

expect($stdout.string).to eq <<-eos
Image name provided. Finding image ID...done\e[0m, 478 (NLP Final)\nWarning! Potentially destructive action. Please confirm [y/n]: Queuing destroy image for 478 (NLP Final)...done
eos

expect(a_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end


it "destroys an image with confirm flag set" do
stub_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_images"))

stub_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

@cli.options = @cli.options.merge(:name => "NLP Final")
@cli.options = @cli.options.merge(:confirm => true)
@cli.destroy_image("NLP Final")

expect($stdout.string).to eq <<-eos
Image name provided. Finding image ID...done\e[0m, 478 (NLP Final)\nQueuing destroy image for 478 (NLP Final)...done
eos

expect(a_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/images/478/destroy?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end
end

end
61 changes: 61 additions & 0 deletions spec/cli/info_image_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'spec_helper'

describe Tugboat::CLI do
include_context "spec"

describe "show" do
it "shows an image with a fuzzy name" do
stub_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_images"))

stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

@cli.info_image("NLP Final")

expect($stdout.string).to eq <<-eos
Image fuzzy name provided. Finding image ID...done\e[0m, 478 (NLP Final)\n\nName: NLP Final\nID: 478\nDistribution: Ubuntu
eos

expect(a_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

it "shows an image with an id" do
stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

@cli.options = @cli.options.merge(:id => 478)
@cli.info_image

expect($stdout.string).to eq <<-eos
Image id provided. Finding Image...done\e[0m, 478 (NLP Final)\n\nName: NLP Final\nID: 478\nDistribution: Ubuntu
eos

expect(a_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made.times(2)
end

it "shows an image with a name" do
stub_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_images"))

stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_image"))

@cli.options = @cli.options.merge(:name => "NLP Final")
@cli.info_image

expect($stdout.string).to eq <<-eos
Image name provided. Finding image ID...done\e[0m, 478 (NLP Final)\n\nName: NLP Final\nID: 478\nDistribution: Ubuntu
eos

expect(a_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

end

end
6 changes: 3 additions & 3 deletions spec/cli/rebuild_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
to_return(:status => 200, :body => fixture("show_droplets"), :headers => {})

stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => '{"status": "OK", "image": {"id": 478, "name": "NLP Final", "distribution": "Ubuntu"}}', :headers => {})
to_return(:status => 200, :body => fixture("show_image"), :headers => {})

stub_request(:post, "https://api.digitalocean.com/droplets/100823/rebuild?api_key=#{api_key}&client_id=#{client_key}&image_id=478").
to_return(:status => 200, :body => '{ "status": "OK", "event_id": 7504 }', :headers => {})
Expand All @@ -92,7 +92,7 @@
to_return(:status => 200, :body => fixture("show_droplet"))

stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => '{"status": "OK", "image": {"id": 478, "name": "NLP Final", "distribution": "Ubuntu"}}', :headers => {})
to_return(:status => 200, :body => fixture("show_image"), :headers => {})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot! 👍


stub_request(:post, "https://api.digitalocean.com/droplets/100823/rebuild?api_key=#{api_key}&client_id=#{client_key}&image_id=478").
to_return(:status => 200, :body => '{ "status": "OK", "event_id": 7504 }', :headers => {})
Expand All @@ -113,7 +113,7 @@
to_return(:status => 200, :body => fixture("show_droplets"))

stub_request(:get, "https://api.digitalocean.com/images/478?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => '{"status": "OK", "image": {"id": 478, "name": "NLP Final", "distribution": "Ubuntu"}}', :headers => {})
to_return(:status => 200, :body => fixture("show_image"), :headers => {})

stub_request(:post, "https://api.digitalocean.com/droplets/100823/rebuild?api_key=#{api_key}&client_id=#{client_key}&image_id=478").
to_return(:status => 200, :body => '{ "status": "OK", "event_id": 7504 }', :headers => {})
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/show_image.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"image": {
"distribution": "Ubuntu",
"id": 478,
"name": "NLP Final"
},
"status": "OK"
}