diff --git a/src/ozones/Client/bin/onevdc b/src/ozones/Client/bin/onevdc index e93478416e..dfff6750d9 100755 --- a/src/ozones/Client/bin/onevdc +++ b/src/ozones/Client/bin/onevdc @@ -50,4 +50,14 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :delete, 'Deletes a VDC', :vdcid do helper.delete_resource(args[0],options) end + + command :addhost, 'Adds the set of hosts to the VDC', + :vdcid, :range do + helper.addhost(args[0], args[1], options) + end + + command :delhost, 'Deletes the set of hosts to the VDC', + :vdcid, :range do + helper.delhost(args[0], args[1], options) + end end diff --git a/src/ozones/Client/lib/OZonesClient.rb b/src/ozones/Client/lib/OZonesClient.rb index bc8294259d..551d4628c7 100644 --- a/src/ozones/Client/lib/OZonesClient.rb +++ b/src/ozones/Client/lib/OZonesClient.rb @@ -90,19 +90,10 @@ EOT # :zonetemplate ###################################################################### def post_resource(kind, template) - template=File.read(template) - - body_str = "" - - template.strip.each_line{|line| - line.strip! - key,value = line.split("=") - body_str = body_str + key + "=" + URI.escape(value) + "&" - } - - body_str = body_str[0..-1] - - url = URI.parse(@endpoint+"/"+kind) + tmpl_str = File.read(template) + body_str = OzonesClient::to_body(tmpl_str) + + url = URI.parse("#{@endpoint}/#{kind}") req = Net::HTTP::Post.new(url.path) req.body=body_str @@ -116,9 +107,25 @@ EOT return OZonesClient::parse_error(res, kind) end + def put_resource(kind, id, tmpl_str) + body_str = OZonesClient::to_body(tmpl_str) + + url = URI.parse("#{@endpoint}/#{kind}/#{id}") + + req = Net::HTTP::Put.new(url.path) + req.body=body_str + + req.basic_auth @ozonesauth[0], @ozonesauth[1] + + res = OZonesClient::http_start(url, @timeout) do |http| + http.request(req) + end + + return OZonesClient::parse_error(res, kind) + end def get_resource(kind, id) - url = URI.parse(@endpoint+"/#{kind}/" + id.to_s) + url = URI.parse("#{@endpoint}/#{kind}/#{id}") req = Net::HTTP::Get.new(url.path) req.basic_auth @ozonesauth[0], @ozonesauth[1] @@ -131,7 +138,7 @@ EOT end def delete_resource(kind, id) - url = URI.parse(@endpoint+"/#{kind}/" + id.to_s) + url = URI.parse("#{@endpoint}/#{kind}/#{id}") req = Net::HTTP::Delete.new(url.path) req.basic_auth @ozonesauth[0], @ozonesauth[1] @@ -237,8 +244,20 @@ EOT end ########################################################################## - # JSON utils + # JSON & Template utils ########################################################################## + + def self.to_body(tmpl_str) + body_str = "" + + tmpl_str.strip.each_line{|line| + line.strip! + key,value = line.split("=") + body_str = body_str + key + "=" + URI.escape(value) + "&" + } + + body_str = body_str[0..-1] + end def self.parse_json(json_str, root_element) begin diff --git a/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb b/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb index 1c7dbf4906..9f82bd6d83 100644 --- a/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb +++ b/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb @@ -39,7 +39,54 @@ class VDCHelper < OZonesHelper::OZHelper def delete_resource(id, options) super(@vdc_str,id, options) end + + def addhost(id, host_array, options) + rc = @client.get_resource(@vdc_str, id) + if OZonesClient::is_error?(rc) + return [-1, rc.message] + else + vdc = OZonesClient::parse_json(rc.body, @vdc_str.upcase) + end + + hosts = vdc['hosts'].split(',').collect!{|x| x.to_i} + host_array.concat(hosts).uniq! + + new_host = host_array.join(',') + template = "ID=#{id}\nHOSTS=#{new_host}\n" + + rc = @client.put_resource(@vdc_str, id, template) + + if OZonesClient::is_error?(rc) + return [-1, rc.message] + end + + [0, ""] + end + + def delhost(id, host_array, options) + rc = @client.get_resource(@vdc_str, id) + + if OZonesClient::is_error?(rc) + return [-1, rc.message] + else + vdc = OZonesClient::parse_json(rc.body, @vdc_str.upcase) + end + + hosts = vdc['hosts'].split(',').collect!{|x| x.to_i} + + new_host = (hosts - host_array).join(',') + template = "ID=#{id}\nHOSTS=#{new_host}\n" + + rc = @client.put_resource(@vdc_str, id, template) + + if OZonesClient::is_error?(rc) + return [-1, rc.message] + end + + [0, ""] + end + private def format_resource(vdc, options)