fluke/devops-service/app/api3/routes/stack.rb

249 lines
7.8 KiB
Ruby
Raw Permalink Normal View History

module Devops
2018-04-04 22:44:39 +03:00
module API3
module Routes
module StackRoutes
def self.registered(app)
2018-04-04 22:44:39 +03:00
app.define_policy :read_stacks, 'Read stacks'
app.define_policy :create_stacks, 'Create stacks'
app.define_policy :update_stacks, 'Create stacks'
app.define_policy :delete_stacks, 'Delete stacks'
app.get_with_headers '/stacks' do
check_policy(:read_stacks)
json Devops::API3::Handler::Stack.new(request).stacks.map(&:to_hash)
2015-07-27 11:14:01 +03:00
end
2018-04-04 22:44:39 +03:00
app.get_with_headers '/stacks/provider/:provider' do |provider|
check_policy(:read_stacks)
2015-07-27 11:14:01 +03:00
check_provider(provider)
2018-04-04 22:44:39 +03:00
json Devops::API3::Handler::Stack.new(request).stacks_for_provider(provider).map(&:to_hash)
2015-07-27 11:14:01 +03:00
end
2018-04-04 22:44:39 +03:00
app.get_with_headers '/stack/:name/servers' do |name|
check_policy(:read_stacks)
json Devops::API3::Handler::Stack.new(request).stack_servers(name).map(&:to_hash)
2015-08-19 16:52:01 +03:00
end
2016-02-26 14:27:14 +03:00
# Create stack
#
# * *Request*
2016-03-09 21:39:26 +03:00
# - method : POST
2016-02-26 14:27:14 +03:00
# - headers :
# - Accept: application/json
# - Content-Type: application/json
# - body :
# {
2018-04-04 22:44:39 +03:00
# "stack_attributes": {
# "project": "project_name",
# "deploy_env": "test",
# "provider": "ec2",
# "tags": {
# "tagName": "tagValue"
# },
# "parameters": {
# "KeyName": "Value"
# }
2016-02-26 14:27:14 +03:00
# }
2018-04-04 22:44:39 +03:00
# "without_bootstrap": false,
# "skip_rollback": false
2016-02-26 14:27:14 +03:00
# }
#
# * *Returns* :
2018-04-04 22:44:39 +03:00
# [task_id]
app.post_with_headers "/stack" do
check_policy(:create_stacks)
json Devops::API3::Handler::Stack.new(request).create_stack
#create_response "Created", nil, 201
2015-07-27 11:14:01 +03:00
end
hash = {}
2015-07-27 11:14:01 +03:00
hash['GET'] = lambda { |stack_id|
2018-04-04 22:44:39 +03:00
check_policy(:read_stacks)
json Devops::API3::Handler::Stack.new(request).stack(stack_id).to_hash
2015-07-27 11:14:01 +03:00
}
2015-07-27 11:14:01 +03:00
hash['DELETE'] = lambda { |stack_id|
2018-04-04 22:44:39 +03:00
check_policy(:delete_stacks)
Devops::API3::Handler::Stack.new(request).delete_stack(stack_id)
2015-07-27 11:14:01 +03:00
create_response("Stack '#{stack_id}' has been removed")
}
2018-04-04 22:44:39 +03:00
app.multi_routes '/stack/:stack_id', hash
app.post_with_headers "/stack/:stack_id/sync" do |stack_id|
check_policy(:update_stacks)
json Devops::API3::Handler::Stack.new(request).sync(stack_id).to_hash
end
=begin
TODO: delete?
app.get_with_headers "/stack/:stack_id/resources" do |stack_id|
check_policy(:read_stacks)
json Devops::API3::Handler::Stack.new(request).resources(stack_id)
end
2018-04-04 22:44:39 +03:00
app.get_with_headers "/stack/:stack_id/resources/:resource_id" do |stack_id, resource_id|
check_policy(:read_stacks)
json Devops::API3::Handler::Stack.new(request).resource(stack_id, resource_id)
end
2018-04-04 22:44:39 +03:00
=end
2015-08-04 12:36:10 +03:00
# Set run_list to stack
#
# * *Request*
# - method : PATCH
# - headers :
# - Accept: application/json
# - Content-Type: application/json
# - body :
# [
# "role[role_1]",
# "recipe[recipe_1]"
# ]
#
# * *Returns* :
# 200 - Updated
2018-04-04 22:44:39 +03:00
app.patch_with_headers "/stack/:id/run_list" do |id|
check_policy(:update_stacks)
info = Devops::API3::Handler::Stack.new(request).set_run_list(id)
2015-08-04 12:36:10 +03:00
create_response(info)
end
2015-08-14 13:28:03 +03:00
# Deploy stack instances
#
# * *Request*
# - method : POST
# - headers :
# - Accept: application/json
# - Content-Type: application/json
# - body :
# {
# "tags" : ["tag1", "tag2"]
# }
#
# * *Returns* :
# 200 - Updated
2018-04-04 22:44:39 +03:00
app.post_with_headers "/stack/:id/deploy" do |id|
2015-08-14 13:28:03 +03:00
check_privileges("stack", "x")
if request["X-Stream"]
stream() do |out|
status = []
begin
2018-04-04 22:44:39 +03:00
status = Devops::API3::Handler::Stack.new(request).deploy_stream(id, out)
2015-08-14 13:28:03 +03:00
out << create_status(status)
rescue IOError => e
logger.error e.message
break
end
end # stream
else
2018-04-04 22:44:39 +03:00
files = Devops::API3::Handler::Stack.new(request).deploy(id)
2015-08-14 13:28:03 +03:00
sleep 1
json files
end
end
2015-08-04 12:36:10 +03:00
# Reserve stack instances
#
# * *Request*
# - method : POST
# - headers :
# - Accept: application/json
# - Content-Type: application/json
#
# * *Returns* :
# 200 - OK
2018-04-04 22:44:39 +03:00
app.post_with_headers "/stack/:id/reserve" do |id|
check_policy(:read_stacks)
check_privileges("server", "w")
2018-04-04 22:44:39 +03:00
Devops::API3::Handler::Stack.new(request).reserve_servers(id)
create_response("Servers of stack '#{id}' has been reserved")
end
# Unreserve stack instances
#
# * *Request*
# - method : POST
# - headers :
# - Accept: application/json
# - Content-Type: application/json
#
# * *Returns* :
# 200 - OK
2018-04-04 22:44:39 +03:00
app.post_with_headers "/stack/:id/unreserve" do |id|
check_policy(:read_stacks)
check_privileges("server", "w")
2018-04-04 22:44:39 +03:00
Devops::API3::Handler::Stack.new(request).unreserve_servers(id)
create_response("Servers of stack '#{id}' has been unreserved")
end
2015-09-15 17:14:26 +03:00
hash = {}
# Add tags to stack instances
#
# * *Request*
# - method : PUT
# - headers :
# - Content-Type: application/json
# - body :
# {
# "tag name": "tag value"
# }
#
# * *Returns* :
# 200 - Added
hash["PUT"] = lambda {|id|
2018-04-04 22:44:39 +03:00
check_policy(:update_stacks)
Devops::API3::Handler::Stack.new(request).set_tags(id)
2015-09-15 17:14:26 +03:00
create_response("Added")
}
# Delete tags from stack instances
#
# * *Request*
# - method : DELETE
# - headers :
# - Content-Type: application/json
# - body :
# {
# "tag name": "tag value"
# }
#
# * *Returns* :
# 200 - Deleted
hash["DELETE"] = lambda {|id|
2018-04-04 22:44:39 +03:00
check_policy(:update_stacks)
Devops::API3::Handler::Stack.new(request).unset_tags(id)
2015-09-15 17:14:26 +03:00
create_response("Deleted")
}
2018-04-04 22:44:39 +03:00
app.multi_routes "/stack/:id/tags", hash
2015-09-15 17:14:26 +03:00
# Change stack stack_template
#
# * *Request*
# - method : PUT
# - headers :
# - Accept: application/json
# - Content-Type: application/json
# - body :
# {
# "stack_template": "template_id"
# }
#
# * *Returns* :
# 200 - OK
2018-04-04 22:44:39 +03:00
app.put_with_headers "/stack/:id/stack_template" do |id|
check_policy(:read_stacks)
json Devops::API3::Handler::Stack.new(request).change_stack_template(id)
end
2018-04-04 22:44:39 +03:00
puts 'Stack routes initialized'
end
end
end
end
2015-07-27 11:14:01 +03:00
end