Skip to content

Commit

Permalink
adding redeployment of existing deployments
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Paige <[email protected]>
  • Loading branch information
dharmamike authored and argvader committed Dec 11, 2014
1 parent db8d38e commit d2c5e81
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 13 deletions.
12 changes: 12 additions & 0 deletions app/controllers/deployments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ def create
def destroy
respond_with Deployment.find(params[:id]).destroy
end

def redeploy
deployment = Deployment.find(params[:id])
if deployment.redeployable?
deployment.destroy
template_attrs = JSON.parse(deployment.template)
template = Template.new(template_attrs)
respond_with Deployment.deploy(template)
else
raise t(:not_redeployable_error, deployment: deployment.name)
end
end
end
10 changes: 7 additions & 3 deletions app/models/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class Deployment < ActiveRecord::Base

class << self

def deploy(template, override)
template.override(override)
def deploy(template, override=nil)
template.override(override) if override

services = services_from_template(template)
deployed_services = adapter_client.create_services(services)

create(name: template.name, service_ids: service_ids(deployed_services))
create(name: template.name, template: template.to_json, service_ids: service_ids(deployed_services))
end

private
Expand Down Expand Up @@ -46,6 +46,10 @@ def status
}
end

def redeployable?
template?
end

private

def update_service_states(desired_state)
Expand Down
6 changes: 5 additions & 1 deletion app/serializers/deployment_detail_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class DeploymentDetailSerializer < ActiveModel::Serializer
self.root = false

attributes :id, :status, :name
attributes :id, :status, :name, :redeployable

def redeployable
object.redeployable?
end

end
9 changes: 9 additions & 0 deletions app/serializers/deployment_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class DeploymentSerializer < ActiveModel::Serializer
self.root = false

attributes :id, :name, :service_ids, :redeployable

def redeployable
object.redeployable?
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
en:
hello: "Hello world"
adapter_connection_error: "Adapter service is not responding"
not_redeployable_error: "%{deployment} cannot be redeployed"
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Rails.application.routes.draw do
resources :deployments, only: [:index, :create, :show, :destroy]
resources :deployments, only: [:index, :create, :show, :destroy] do
member do
post :redeploy
end
end

resource :metadata, only: :show
end
50 changes: 48 additions & 2 deletions spec/controllers/deployments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

it 'returns the deployments' do
get :index, format: :json
expect(response.body).to eq [deployment].to_json
json = ActiveModel::ArraySerializer.new([deployment], each_serializer: DeploymentSerializer).to_json
expect(response.body).to eq json
end

it 'returns a 200 status code' do
Expand Down Expand Up @@ -55,7 +56,7 @@

it 'returns the new deployment' do
post :create, template: template, override: override, format: :json
expect(response.body).to eq deployment.to_json
expect(response.body).to eq DeploymentSerializer.new(deployment).to_json
end

it 'returns a 204 status code' do
Expand All @@ -82,4 +83,49 @@
end

end

describe '#redeploy' do

context 'when the deployment is redeployable' do
let(:deployment_template) { { 'name' => 'some template' }.to_json }
before do
deployment.template = deployment_template
allow(deployment).to receive(:destroy) { true }
allow(Deployment).to receive(:find) { deployment }
end

it 'destroys the deployment' do
expect(deployment).to receive(:destroy)
post :redeploy, id: deployment, format: :json
end

it 'creates a new template from the old deployment template' do
expect(Template).to receive(:new).with(JSON.parse(deployment_template))
post :redeploy, id: deployment, format: :json
end

it 'creates a new deployment using the old deployment template' do
expect(Deployment).to receive(:deploy) do |t|
expect(t.name).to eq 'some template'
end
post :redeploy, id: deployment, format: :json
end
end

context 'when the deployment is not redeployable' do
before do
allow(deployment).to receive(:redeployable?) { false }
end

it 'returns a 500 status code' do
post :redeploy, id: deployment, format: :json
expect(response.status).to eq 500
end

it 'includes' do
post :redeploy, id: deployment, format: :json
expect(response.body).to eq({ message: I18n.t(:not_redeployable_error, deployment: deployment.name) }.to_json)
end
end
end
end
36 changes: 33 additions & 3 deletions spec/models/deployment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@
client.stub(:create_services).and_return(deployed_services)
end

it 'overrides the template with the deployment descriptor' do
expect(template).to receive(:override).with(override)
described_class.deploy(template, override)
context 'when an override is provided' do
it 'overrides the template with the deployment descriptor' do
expect(template).to receive(:override).with(override)
described_class.deploy(template, override)
end
end

context 'when an override is not provided' do
it 'does not override the template' do
expect(template).not_to receive(:override).with(override)
described_class.deploy(template)
end
end

it 'calls create_services on the client' do
Expand Down Expand Up @@ -71,6 +80,10 @@
template.name = 'foo'
expect(described_class.deploy(template, override).name).to eq template.name
end

it 'persists the json representation of the template to the Deployment instance' do
expect(described_class.deploy(template).template).to eq template.to_json
end
end

describe '#stop' do
Expand Down Expand Up @@ -141,4 +154,21 @@
end

end

describe '#redeployable?' do
context 'when the deployment has no template' do
it 'is not redeployable?' do
expect(subject.redeployable?).to be false
end
end

context 'when the deployment has a template' do
before do
subject.template = "I'm a little template short and stout"
end
it 'is not redeployable?' do
expect(subject.redeployable?).to be true
end
end
end
end
7 changes: 4 additions & 3 deletions spec/serializers/deployment_detail_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
it 'exposes the attributes to be jsonified' do
serialized = described_class.new(deployment_model).as_json
expected_keys = [
:id,
:name,
:status
:id,
:name,
:status,
:redeployable
]
expect(serialized.keys).to match_array expected_keys
end
Expand Down
16 changes: 16 additions & 0 deletions spec/serializers/deployment_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe DeploymentSerializer do
let(:deployment_model) { Deployment.new }

it 'exposes the attributes to be jsonified' do
serialized = described_class.new(deployment_model).as_json
expected_keys = [
:id,
:name,
:service_ids,
:redeployable
]
expect(serialized.keys).to match_array expected_keys
end
end

0 comments on commit d2c5e81

Please sign in to comment.