diff --git a/include/RequestManagerZone.h b/include/RequestManagerZone.h new file mode 100644 index 0000000000..806932fd0c --- /dev/null +++ b/include/RequestManagerZone.h @@ -0,0 +1,70 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#ifndef REQUEST_MANAGER_ZONE_H +#define REQUEST_MANAGER_ZONE_H + +#include "Request.h" +#include "Nebula.h" + +using namespace std; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class RequestManagerZone: public Request +{ +protected: + RequestManagerZone(const string& method_name, + const string& help, + const string& params) + :Request(method_name,params,help) + { + Nebula& nd = Nebula::instance(); + pool = nd.get_zonepool(); + + auth_object = PoolObjectSQL::ZONE; + auth_op = AuthRequest::ADMIN; + }; + + ~RequestManagerZone(){}; + + /* -------------------------------------------------------------------- */ + + virtual void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att) = 0; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class ZoneAddServer : public RequestManagerZone +{ +public: + ZoneAddServer(): + RequestManagerZone("ZoneAddServer", "Add a server to zone", "A:sis"){}; + + ~ZoneAddServer(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +#endif diff --git a/include/Zone.h b/include/Zone.h index 18aa0882eb..179365c9a2 100644 --- a/include/Zone.h +++ b/include/Zone.h @@ -46,6 +46,15 @@ public: */ int from_xml(const string &xml_str); + /** + * Add servers to this zone + * @param tmpl with SERVER definitions + * @param error + * + * @return 0 on success, -1 otherwise + */ + int add_servers(Template& tmpl, string& error); + private: // ------------------------------------------------------------------------- // Friends diff --git a/include/ZoneServer.h b/include/ZoneServer.h index 6f8e0191bb..11685bd544 100644 --- a/include/ZoneServer.h +++ b/include/ZoneServer.h @@ -34,6 +34,18 @@ public: int init(string& error) { + if ( vector_value("NAME").empty() ) + { + error = "Missing NAME in SERVER"; + return -1; + } + + if ( vector_value("ENDPOINT").empty() ) + { + error = "Missing ENDPOINT in SERVER"; + return -1; + } + return 0; } @@ -147,6 +159,8 @@ public: return -1; } + va->replace(SERVER_ID_NAME, next_id); + add_attribute(server, next_id); next_id += 1; @@ -154,15 +168,14 @@ public: return 0; }; - protected: - ExtendedAttribute * attribute_factory(VectorAttribute * va, int id) const { return new ZoneServer(va, id); }; private: + friend class Zone; static const char * SERVER_NAME; //"SERVER" diff --git a/src/cli/one_helper/onezone_helper.rb b/src/cli/one_helper/onezone_helper.rb index 6901d8f494..23a1996c5b 100644 --- a/src/cli/one_helper/onezone_helper.rb +++ b/src/cli/one_helper/onezone_helper.rb @@ -18,6 +18,22 @@ require 'one_helper' class OneZoneHelper < OpenNebulaHelper::OneHelper + SERVER_NAME={ + :name => "server_name", + :short => "-n server_name", + :large => "--name", + :format => String, + :description => "Zone server name" + } + + SERVER_ENDPOINT={ + :name => "server_rpc", + :short => "-r rpc endpoint", + :large => "--rpc", + :format => String, + :description => "Zone server RPC endpoint" + } + def self.rname "ZONE" end @@ -98,6 +114,31 @@ class OneZoneHelper < OpenNebulaHelper::OneHelper puts str % ["NAME", zone.name] puts + zone_hash=zone.to_hash + + if zone.has_elements?("/ZONE/SERVER_POOL/SERVER") + puts + CLIHelper.print_header(str_h1 % "SERVERS",false) + + CLIHelper::ShowTable.new(nil, self) do + + column :"ID", "", :size=>2 do |d| + d["ID"] if !d.nil? + end + + column :"NAME", "", :left, :size=>15 do |d| + d["NAME"] if !d.nil? + end + + column :"ENDPOINT", "", :left, :size=>30 do |d| + d["ENDPOINT"] if !d.nil? + end + + end.show([zone_hash['ZONE']['SERVER_POOL']['SERVER']].flatten, {}) + end + + puts + CLIHelper.print_header(str_h1 % "ZONE TEMPLATE", false) puts zone.template_str end diff --git a/src/cli/onezone b/src/cli/onezone index eb87e43b1d..035e22af57 100755 --- a/src/cli/onezone +++ b/src/cli/onezone @@ -91,6 +91,33 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + addserver_desc = <<-EOT.unindent + Add an OpenNebula server to this zone. + EOT + + command :"server-add", addserver_desc, :zoneid, :options => + [ OneZoneHelper::SERVER_NAME, OneZoneHelper::SERVER_ENDPOINT] do + + if options[:server_name].nil? || options[:server_rpc].nil? + STDERR.puts "To add a server set:" + STDERR.puts "\t-n " + STDERR.puts "\t-r " + exit -1 + end + + template = <<-EOT + SERVER = [ + NAME="#{options[:server_name]}", + ENDPOINT="#{options[:server_rpc]}" + ] + EOT + + helper.perform_action(args[0], options, "server added") do |o| + o.add_servers(template) + end + + 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. diff --git a/src/oca/ruby/opennebula/zone.rb b/src/oca/ruby/opennebula/zone.rb index 87b80c5ba2..8412eecc97 100644 --- a/src/oca/ruby/opennebula/zone.rb +++ b/src/oca/ruby/opennebula/zone.rb @@ -28,7 +28,8 @@ module OpenNebula :allocate => "zone.allocate", :update => "zone.update", :rename => "zone.rename", - :delete => "zone.delete" + :delete => "zone.delete", + :addserver => "zone.addserver" } # Creates a Zone description with just its identifier @@ -103,5 +104,16 @@ module OpenNebula def rename(name) return call(ZONE_METHODS[:rename], @pe_id, name) end + + # Adds servers to this Zone + # + # @param name [String] Template with zone servers + # SERVER = [ NAME = "", ENDPOINT = "" ] + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def add_servers(servers) + return call(ZONE_METHODS[:addserver], @pe_id, servers) + end end end diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index d93592eeec..7b41a37eab 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -30,6 +30,7 @@ #include "RequestManagerChmod.h" #include "RequestManagerClone.h" #include "RequestManagerRename.h" +#include "RequestManagerZone.h" #include "RequestManagerLock.h" #include "RequestManagerVirtualNetwork.h" @@ -779,6 +780,8 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr zone_delete(zone_delete_pt); xmlrpc_c::methodPtr zone_rename(zone_rename_pt); + xmlrpc_c::methodPtr zone_addserver(new ZoneAddServer()); + xmlrpc_c::methodPtr zone_info(new ZoneInfo()); xmlrpc_c::methodPtr zonepool_info(new ZonePoolInfo()); @@ -788,6 +791,8 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.zone.info", zone_info); RequestManagerRegistry.addMethod("one.zone.rename", zone_rename); + RequestManagerRegistry.addMethod("one.zone.addserver", zone_addserver); + RequestManagerRegistry.addMethod("one.zonepool.info",zonepool_info); /* Security Group objects related methods*/ diff --git a/src/rm/RequestManagerZone.cc b/src/rm/RequestManagerZone.cc new file mode 100644 index 0000000000..fb9ac4a2dc --- /dev/null +++ b/src/rm/RequestManagerZone.cc @@ -0,0 +1,74 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "RequestManagerZone.h" +#include "Nebula.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void ZoneAddServer::request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + int id = xmlrpc_c::value_int(paramList.getInt(1)); + string zs_str = xmlrpc_c::value_string(paramList.getString(2)); + + string error_str; + + if ( basic_authorization(id, att) == false ) + { + return; + } + + Template zs_tmpl; + + int rc = zs_tmpl.parse_str_or_xml(zs_str, error_str); + + if ( rc != 0 ) + { + att.resp_msg = error_str; + failure_response(ACTION, att); + + return; + } + + Zone * zone = (static_cast(pool))->get(id, true); + + if ( zone == 0 ) + { + att.resp_id = id; + failure_response(NO_EXISTS, att); + + return; + } + + if ( zone->add_servers(zs_tmpl, att.resp_msg) == -1 ) + { + failure_response(ACTION, att); + + return; + } + + pool->update(zone); + + zone->unlock(); + + success_response(id, att); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + diff --git a/src/rm/SConstruct b/src/rm/SConstruct index 9c2ea353e7..b43e3e9ca7 100644 --- a/src/rm/SConstruct +++ b/src/rm/SConstruct @@ -45,6 +45,7 @@ source_files=[ 'RequestManagerRename.cc', 'RequestManagerProxy.cc', 'RequestManagerVdc.cc', + 'RequestManagerZone.cc', 'RequestManagerDatastore.cc', 'RequestManagerLock.cc', 'RequestManagerMarketPlaceApp.cc', diff --git a/src/zone/Zone.cc b/src/zone/Zone.cc index 6cd4da9edc..b966b34d9f 100644 --- a/src/zone/Zone.cc +++ b/src/zone/Zone.cc @@ -299,3 +299,30 @@ int Zone::post_update_template(string& error) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int Zone::add_servers(Template& tmpl, string& error) +{ + vector vs; + vector::iterator it; + + VectorAttribute * server; + + tmpl.get(ZoneServers::SERVER_NAME, vs); + + for ( it = vs.begin() ; it != vs.end() ; ++it ) + { + server = new VectorAttribute(*it); + + if ( servers->add_server(server, error) == -1 ) + { + delete server; + + return -1; + } + + servers_template.set(server); + } + + return 0; +} +