1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

feature #817: Command line options addhost, delhost for onevdc

(cherry picked from commit c42545943ed2dd77d592a15720a766047a6099e9)
This commit is contained in:
Ruben S. Montero 2011-09-23 01:22:54 +02:00
parent 72e55b394f
commit 82d94ce496
3 changed files with 92 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -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)