ec2 instance tags

This commit is contained in:
amartynov 2015-08-03 18:01:50 +03:00
parent 249d08d60e
commit 1592939068
5 changed files with 111 additions and 26 deletions

View File

@ -233,6 +233,26 @@ module Devops
"Server '#{s.id}' has been added"
end
def set_tags node_name
prepare_tags do |id, provider|
provider.set_tags id, parser.tags
end
end
def unset_tags node_name
prepare_tags do |id, provider|
provider.unset_tags id, parser.tags
end
end
def prepare_tags node_name
s = get_server_by_key(node_name, parser.instance_key)
user = parser.current_user
Devops::Db.connector.check_project_auth s.project, s.deploy_env, user
provider = ::Provider::ProviderFactory.get(s.provider)
yield s.id, provider
end
def get_server_by_key id, key
mongo = Devops::Db.connector
key == "instance" ? mongo.server_by_instance_id(id) : mongo.server_by_chef_node_name(id)

View File

@ -20,46 +20,51 @@ module Devops
end
def instance_key
body = create_object_from_json_body(Hash, true)
(body.nil? ? nil : body["key"])
@body ||= create_object_from_json_body(Hash, true)
(@body.nil? ? nil : @body["key"])
end
def create
body = create_object_from_json_body
project_name = check_string(body["project"], "Parameter 'project' must be a not empty string")
env_name = check_string(body["deploy_env"], "Parameter 'deploy_env' must be a not empty string")
server_name = check_string(body["name"], "Parameter 'name' should be null or not empty string", true)
without_bootstrap = body["without_bootstrap"]
force = body["force"]
@body ||= create_object_from_json_body
project_name = check_string(@body["project"], "Parameter 'project' must be a not empty string")
env_name = check_string(@body["deploy_env"], "Parameter 'deploy_env' must be a not empty string")
server_name = check_string(@body["name"], "Parameter 'name' should be null or not empty string", true)
without_bootstrap = @body["without_bootstrap"]
force = @body["force"]
raise InvalidRecord.new("Parameter 'without_bootstrap' should be a null or true") unless without_bootstrap.nil? or without_bootstrap == true
raise InvalidRecord.new("Parameter 'force' should be a null or true") unless force.nil? or force == true
groups = check_array(body["groups"], "Parameter 'groups' should be null or not empty array of string", String, true)
key_name = check_string(body["key"], "Parameter 'key' should be null or not empty string", true)
body
groups = check_array(@body["groups"], "Parameter 'groups' should be null or not empty array of string", String, true)
key_name = check_string(@body["key"], "Parameter 'key' should be null or not empty string", true)
@body
end
def bootstrap
body = create_object_from_json_body(Hash, true)
id = check_string(body["instance_id"], "Parameter 'instance_id' must be a not empty string")
name = check_string(body["name"], "Parameter 'name' should be a not empty string", true)
rl = check_array(body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, true)
@body ||= create_object_from_json_body(Hash, true)
id = check_string(@body["instance_id"], "Parameter 'instance_id' must be a not empty string")
name = check_string(@body["name"], "Parameter 'name' should be a not empty string", true)
rl = check_array(@body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, true)
unless rl.nil?
validator = Validators::Helpers::RunList.new(rl)
halt_response(validator.message) unless validator.valid?
end
t = check_string(body["bootstrap_template"], "Parameter 'bootstrap_template' should be a not empty string", true)
body
t = check_string(@body["bootstrap_template"], "Parameter 'bootstrap_template' should be a not empty string", true)
@body
end
def add_server
body = create_object_from_json_body
project = check_string(body["project"], "Parameter 'project' must be a not empty string")
deploy_env = check_string(body["deploy_env"], "Parameter 'deploy_env' must be a not empty string")
key = check_string(body["key"], "Parameter 'key' must be a not empty string")
remote_user = check_string(body["remote_user"], "Parameter 'remote_user' must be a not empty string")
private_ip = check_string(body["private_ip"], "Parameter 'private_ip' must be a not empty string")
public_ip = check_string(body["public_ip"], "Parameter 'public_ip' should be a not empty string", true)
body
@body ||= create_object_from_json_body
project = check_string(@body["project"], "Parameter 'project' must be a not empty string")
deploy_env = check_string(@body["deploy_env"], "Parameter 'deploy_env' must be a not empty string")
key = check_string(@body["key"], "Parameter 'key' must be a not empty string")
remote_user = check_string(@body["remote_user"], "Parameter 'remote_user' must be a not empty string")
private_ip = check_string(@body["private_ip"], "Parameter 'private_ip' must be a not empty string")
public_ip = check_string(@body["public_ip"], "Parameter 'public_ip' should be a not empty string", true)
@body
end
def tags
@body ||= create_object_from_json_body
@body["tags"]
end
end

View File

@ -304,6 +304,48 @@ module Devops
create_response(info)
end
hash = {}
# Add devops server tags
#
# * *Request*
# - method : PUT
# - headers :
# - Content-Type: application/json
# - body :
# {
# "tags": {"tag name": "tag value"}
# "key": "instance", -> search server by instance_id rather then chef_node_name
# }
#
# * *Returns* :
# 200 - Added
hash["PUT"] = lambda {|id|
check_privileges("server", "w")
Devops::API2_0::Handler::Server.new(request).set_tags(id)
"Added"
}
# Delete devops server tags
#
# * *Request*
# - method : DELETE
# - headers :
# - Content-Type: application/json
# - body :
# {
# "tags": {"tag name": "tag value"}
# "key": "instance", -> search server by instance_id rather then chef_node_name
# }
#
# * *Returns* :
# 200 - Deleted
hash["DELETE"] = lambda {|id|
check_privileges("server", "w")
Devops::API2_0::Handler::Server.new(request).unset_tags(id)
"Deleted"
}
app.multi_routes "/server/:id/tags", {:headers => [:content_type]}, hash
puts "Server routes initialized"
end

View File

@ -23,5 +23,11 @@ module Provider
param.nil? or param.empty?
end
def set_tags instance_id, tags
end
def unset_tags instance_id, tags
end
end
end

View File

@ -144,7 +144,7 @@ module Provider
end
s.public_ip = details["ipAddress"]
s.private_ip = details["privateIpAddress"]
compute.create_tags(s.id, {"Name" => s.chef_node_name})
set_tags(s.id, {"Name" => s.chef_node_name})
out << "\nDone\n\n"
out << s.info
@ -183,6 +183,18 @@ module Provider
end
end
def set_tags instance_id, tags
#TODO: raise error, unavailable tag name
# tags.delete("Name")
compute.create_tags(instance_id, tags)
end
def unset_tags instance_id, tags
#TODO: raise error, unavailable tag name
# tags.delete("Name")
compute.delete_tags(instance_id, tags)
end
def compute
connection_compute(connection_options)
end