From b5b33c2936a86fae4e14d10a3aa0e32eda099b80 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 29 May 2013 18:43:41 +0200 Subject: [PATCH 1/4] feature #2050: Add appflow-template update --- flow/client/bin/appflow-template | 56 ++++++++++++++++++++++++++++++ flow/client/lib/appflow_client.rb | 7 ++++ flow/controllers/appflow-server.rb | 14 ++++++++ 3 files changed, 77 insertions(+) diff --git a/flow/client/bin/appflow-template b/flow/client/bin/appflow-template index bf1912e6f1..432b0fe08d 100755 --- a/flow/client/bin/appflow-template +++ b/flow/client/bin/appflow-template @@ -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 << template.to_json + 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 4d3fcc9d8b..5dde7b63a9 100644 --- a/flow/client/lib/appflow_client.rb +++ b/flow/client/lib/appflow_client.rb @@ -299,6 +299,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..a8be5c8cd5 100644 --- a/flow/controllers/appflow-server.rb +++ b/flow/controllers/appflow-server.rb @@ -286,6 +286,20 @@ delete '/service_template/:id' do status 201 end +put '/service_template/:id' do + service_template = OpenNebula::ServiceTemplate.new_with_id(params[:id], @client) + + rc = service_template.update(request.body.read) + 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, From 23474f17a7dbbe8a6aead7392a88e3184cbcc8a5 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 30 May 2013 12:42:31 +0200 Subject: [PATCH 2/4] feature #2050: Pretty print json when updating a template --- flow/client/bin/appflow-template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow/client/bin/appflow-template b/flow/client/bin/appflow-template index 432b0fe08d..8267c71246 100755 --- a/flow/client/bin/appflow-template +++ b/flow/client/bin/appflow-template @@ -422,7 +422,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do document_hash = JSON.parse(response.body) template = document_hash['DOCUMENT']['TEMPLATE']['BODY'] - tmp << template.to_json + tmp << JSON.pretty_generate(template) tmp.flush editor_path = ENV["EDITOR"] ? ENV["EDITOR"] : EDITOR_PATH From 796862bc68abcf506df2e6c19c8d50067902de67 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 30 May 2013 17:14:02 +0200 Subject: [PATCH 3/4] feature #2050: Add service template update to Sunstone --- flow/client/bin/appflow-template | 2 +- .../public/js/apptools-appflow-templates.js | 144 ++++++++++++++++-- 2 files changed, 132 insertions(+), 14 deletions(-) diff --git a/flow/client/bin/appflow-template b/flow/client/bin/appflow-template index 8267c71246..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], 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 = '\
\

\ '+tr("Create Service Template")+'\ + \

\
\
\ @@ -117,7 +125,7 @@ var create_service_template_tmpl = '\ \
\
\ - \ + \
\
\
'+ tr("Name for this template") +'
\ @@ -144,10 +152,10 @@ var create_service_template_tmpl = '\
\
\
\ - \ + \
\
\ - \ + \
\
\
'+ tr("Number of VMs to instantiate with this role") +'
\ @@ -158,7 +166,7 @@ var create_service_template_tmpl = '\ \
\
\ - \ + \
\
\
'+ tr("Number of VMs to instantiate with this role") +'
\ @@ -205,7 +213,7 @@ var create_service_template_tmpl = '\ '+tr("Card.")+'\ '+tr("Template")+'\ '+tr("Parents")+'\ - '+tr("Delete")+'\ + '+tr("Actions")+'\ \ \ \ @@ -215,8 +223,9 @@ var create_service_template_tmpl = '\ \ @@ -249,6 +258,23 @@ var service_template_actions = { call: popUpCreateServiceTemplateDialog }, + "ServiceTemplate.update_dialog" : { + type : "single", + call: ServiceTemplate.show, + callback: popUpUpdateServiceTemplateDialog, + error: onError + }, + + "ServiceTemplate.update" : { // Update template + type: "single", + call: ServiceTemplate.update, + callback: function(request,response){ + notifyMessage(tr("ServiceTemplate updated correctly")); + Sunstone.runAction('ServiceTemplate.show',response.DOCUMENT.ID); + }, + error: onError + }, + "ServiceTemplate.list" : { type: "list", call: ServiceTemplate.list, @@ -351,12 +377,16 @@ var service_template_buttons = { type: "create_dialog", layout: "create" }, - "ServiceTemplate.instantiate" : { type: "action", layout: "main", text: tr("Instantiate") }, + "ServiceTemplate.update_dialog" : { + type: "action", + layout: "main", + text: tr("Update") + }, "ServiceTemplate.chown" : { type: "confirm_with_select", text: tr("Change owner"), @@ -551,7 +581,6 @@ function setupCreateServiceTemplateDialog(){ select.append( ''); return true; - } var removeParentRole = function(name){ @@ -565,6 +594,26 @@ function setupCreateServiceTemplateDialog(){ return false; }); + $('.role_update_icon').live('click', function(){ + var row = $(this).parents('tr'); + var role = JSON.parse(row.attr('role')) + + removeParentRole(role.name); + + $("#role_name", dialog).val(role.name); + $("#cardinality", dialog).val(role.cardinality); + $('select[name="vm_template"]', dialog).val(role.vm_template); + + $.each(role.parents, function(index, value){ + var option = $('select[name="parents"] option[value="'+ value +'"]', dialog); + option.text(option.text().replace(/☐/g,'☒')); + option.attr('clicked','clicked'); + }) + + row.fadeOut().remove(); + return false; + }); + $('select[name="parents"]', dialog).change(function(){ var option = $('option:selected', this); var clicked = option.attr('clicked'); @@ -611,7 +660,10 @@ function setupCreateServiceTemplateDialog(){ str += ''+ cardinality +''; str += ''+ template +''; str += ''+ parents.join(',') +''; - str += 'Delete'; + str += '\ +  \ + \ + '; str += ''; var ok = addParentRole(name); @@ -621,7 +673,10 @@ function setupCreateServiceTemplateDialog(){ $('input[name="name"]', context).val(""); $('input[name="cardinality"]', context).val("1"); //unselect selected parents - $('select[name="parents"] option[clicked="clicked"]').trigger('click'); + $('select[name="parents"] option[clicked="clicked"]', dialog).each(function(){ + $(this).text($(this).text().replace(/☒/g,'☐')); + $(this).removeAttr('clicked'); + }); } else notifyError(tr("There is already a role with this name!")); @@ -655,16 +710,79 @@ function setupCreateServiceTemplateDialog(){ dialog.trigger("reveal:close"); return false; }); + + $('#create_service_template_reset', dialog).click(function(){ + var dialog = $create_service_template_dialog; + var tpl_select = makeSelectOptions(dataTable_templates, 1, 4, [], [], true); + $('select[name="vm_template"]', dialog).html(tpl_select); + $('select[name="parents"]', dialog).html(""); + $("table#current_roles tbody tr", dialog).remove(); + }) } function popUpCreateServiceTemplateDialog(){ var dialog = $create_service_template_dialog; + $("#create_service_template_reset", dialog).click(); + var tpl_select = makeSelectOptions(dataTable_templates, 1, 4, [], [], true); $('select[name="vm_template"]', dialog).html(tpl_select); + $('select[name="parents"]', dialog).html(""); + $("table#current_roles tbody tr", dialog).remove(); + + $("#create_service_template_header", dialog).show(); + $("#update_service_template_header", dialog).hide(); + $("#create_service_template_submit", dialog).show(); + $("#update_service_template_submit", dialog).hide(); + + + $("#service_name", dialog).removeAttr("disabled"); + dialog.reveal(); } +function popUpUpdateServiceTemplateDialog(request, response){ + var dialog = $create_service_template_dialog; + $("#create_service_template_reset", dialog).click(); + var tpl_select = makeSelectOptions(dataTable_templates, 1, 4, [], [], true); + $('select[name="vm_template"]', dialog).html(tpl_select); + $('select[name="parents"]', dialog).html(""); + $("table#current_roles tbody tr", dialog).remove(); + + $("#create_service_template_header", dialog).hide(); + $("#update_service_template_header", dialog).show(); + $("#create_service_template_submit", dialog).hide(); + $("#update_service_template_submit", dialog).show(); + + + var service_template = response[ServiceTemplate.resource] + $("#service_name", dialog).attr("disabled", "disabled"); + $("#service_name", dialog).val(service_template.NAME); + $('select[name="deployment"]', dialog).val(service_template.TEMPLATE.BODY.deployment); + + $.each(service_template.TEMPLATE.BODY.roles, function(index, value){ + $("#role_name", dialog).val(value.name); + $("#cardinality", dialog).val(value.cardinality); + $('select[name="vm_template"]', dialog).val(value.vm_template); + + if (value.parents){ + $.each(value.parents, function(pindex, parent){ + var option = $('select[name="parents"] option[value="'+ parent +'"]', dialog); + option.text(option.text().replace(/☐/g,'☒')); + option.attr('clicked','clicked'); + }) + } + + $("#add_role", dialog).click(); + }) + + $('select[name="parents"] option', dialog).each(function(){ + $(this).text($(this).text().replace(/☒/g,'☐')); + $(this).removeAttr('clicked'); + }); + + dialog.reveal(); +} // Set the autorefresh interval for the datatable function setServiceTemplateAutorefresh() { From b78a99e496861c9849415489b507ccb5dfc41f04 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 31 May 2013 12:45:37 +0200 Subject: [PATCH 4/4] feature #2050: Validate json when updating a service template --- flow/controllers/appflow-server.rb | 7 ++++++- flow/models/ServiceTemplate.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/flow/controllers/appflow-server.rb b/flow/controllers/appflow-server.rb index a8be5c8cd5..97535ee55a 100644 --- a/flow/controllers/appflow-server.rb +++ b/flow/controllers/appflow-server.rb @@ -289,7 +289,12 @@ end put '/service_template/:id' do service_template = OpenNebula::ServiceTemplate.new_with_id(params[:id], @client) - rc = service_template.update(request.body.read) + 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 diff --git a/flow/models/ServiceTemplate.rb b/flow/models/ServiceTemplate.rb index 000932d33e..6e70dd73ea 100644 --- a/flow/models/ServiceTemplate.rb +++ b/flow/models/ServiceTemplate.rb @@ -85,5 +85,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