diff --git a/flow/client/bin/appflow-template b/flow/client/bin/appflow-template index bf1912e6f1..9c404fe2c9 100755 --- a/flow/client/bin/appflow-template +++ b/flow/client/bin/appflow-template @@ -266,7 +266,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Show detailed information of a given Service Template EOT - command :show, show_desc, :template_id do + command :show, show_desc, :template_id, :options => Service::JSON_FORMAT do client = Service::Client.new( :username => options[:username], :password => options[:password], @@ -392,4 +392,60 @@ cmd=CommandParser::CmdParser.new(ARGV) do client.post("#{RESOURCE_PATH}/#{service_id}/action", json_action) } end + + update_desc = <<-EOT.unindent + Update the template contents. If a path is not provided the editor will + be launched to modify the current content. + EOT + + command :update, update_desc, :templateid, [:file, nil] do + template_id = args[0] + client = Service::Client.new( + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) + + if args[1] + path = args[1] + else + require 'tempfile' + + tmp = Tempfile.new(template_id.to_s) + path = tmp.path + + response = client.get("#{RESOURCE_PATH}/#{template_id}") + + if CloudClient::is_error?(response) + exit_with_code response.code.to_i, response.to_s + else + document_hash = JSON.parse(response.body) + template = document_hash['DOCUMENT']['TEMPLATE']['BODY'] + + tmp << JSON.pretty_generate(template) + tmp.flush + + editor_path = ENV["EDITOR"] ? ENV["EDITOR"] : EDITOR_PATH + system("#{editor_path} #{path}") + + unless $?.exitstatus == 0 + puts "Editor not defined" + exit -1 + end + + tmp.close + end + end + + exit_code = 0 + + t_str = File.read(path) + response = client.put("#{RESOURCE_PATH}/#{template_id}", t_str) + if CloudClient::is_error?(response) + puts response.to_s + exit_code = response.code.to_i + end + + exit_code + end end \ No newline at end of file diff --git a/flow/client/lib/appflow_client.rb b/flow/client/lib/appflow_client.rb index c9ff61dab7..13078669e2 100644 --- a/flow/client/lib/appflow_client.rb +++ b/flow/client/lib/appflow_client.rb @@ -307,6 +307,13 @@ module Service do_request(req) end + def put(path, body) + req = Net::HTTP::Proxy(@host, @port)::Put.new(path) + req.body = body + + do_request(req) + end + def login req = Net::HTTP::Proxy(@host, @port)::Post.new('/login') diff --git a/flow/controllers/appflow-server.rb b/flow/controllers/appflow-server.rb index 7b868ccd62..97535ee55a 100644 --- a/flow/controllers/appflow-server.rb +++ b/flow/controllers/appflow-server.rb @@ -286,6 +286,25 @@ delete '/service_template/:id' do status 201 end +put '/service_template/:id' do + service_template = OpenNebula::ServiceTemplate.new_with_id(params[:id], @client) + + begin + rc = service_template.update(request.body.read) + rescue Validator::ParseException, JSON::ParserError + error 400, $!.message + end + + if OpenNebula.is_error?(rc) + error CloudServer::HTTP_ERROR_CODE[rc.errno], rc.message + end + + service_template.info + + status 200 + body service_template.to_json +end + post '/service_template' do s_template = OpenNebula::ServiceTemplate.new( OpenNebula::ServiceTemplate.build_xml, diff --git a/flow/models/ServiceTemplate.rb b/flow/models/ServiceTemplate.rb index 57c06e56b1..bd85907f42 100644 --- a/flow/models/ServiceTemplate.rb +++ b/flow/models/ServiceTemplate.rb @@ -148,5 +148,18 @@ module OpenNebula @body.to_json end + def update(template_json) + template = JSON.parse(template_json) + + validator = Validator::Validator.new( + :default_values => true, + :delete_extra_properties => false + ) + + validator.validate!(template, SCHEMA) + + super(template.to_json) + end + end end diff --git a/flow/sunstone/public/js/apptools-appflow-templates.js b/flow/sunstone/public/js/apptools-appflow-templates.js index 948e0914e1..699b9922dd 100644 --- a/flow/sunstone/public/js/apptools-appflow-templates.js +++ b/flow/sunstone/public/js/apptools-appflow-templates.js @@ -18,8 +18,7 @@ var ServiceTemplate = { "resource" : 'DOCUMENT', "path" : 'service_template', "create": function(params){ - OpenNebula.Action.create(params, ServiceTemplate.resource, - ServiceTemplate.path); + OpenNebula.Action.create(params, ServiceTemplate.resource, ServiceTemplate.path); }, "instantiate": function(params){ @@ -30,6 +29,14 @@ var ServiceTemplate = { action_obj, ServiceTemplate.path); }, + "update": function(params){ + var action_obj = {"template_raw" : params.data.extra_param }; + OpenNebula.Action.simple_action(params, + ServiceTemplate.resource, + "update", + action_obj, + ServiceTemplate.path); + }, "del": function(params){ OpenNebula.Action.del(params,ServiceTemplate.resource, ServiceTemplate.path); }, @@ -107,6 +114,7 @@ var create_service_template_tmpl = '\