#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
#                                                                            #
# 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.                                             #
#--------------------------------------------------------------------------- #

ONE_LOCATION=ENV["ONE_LOCATION"]

if !ONE_LOCATION
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
else
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end

$: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cli"

require 'command_parser'
require 'ozones_helper/zones_helper.rb'

cmd=CommandParser::CmdParser.new(ARGV) do
    usage "`onezone` <command> [<args>] [<options>]"
    version OpenNebulaHelper::ONE_VERSION

    set :format, :zoneid, "Zone ID" do |arg|
        arg.match(/^[0123456789]+$/) ? [0,arg] : [-1]
    end

    ########################################################################
    # Global Options
    ########################################################################
    set :option, CommandParser::OPTIONS

    begin
        helper = ZonesHelper.new "zone"
    rescue Exception => e
        warn e.message
        exit -1
    end

    command :create, 'Create a new Zone', :file do
        helper.create_resource(args[0])
    end

    show_desc = <<-EOT.unindent
        Show information of a particular Zone
        Available resources: host, vm, image, vn, template, user
        Examples:
          onezone show 4
          onezone show 4 host
    EOT

    command :show, show_desc, :zoneid, [:resource, nil] do
        zone=helper.show_resource(args[0],options)[1]

        case args[1]
            when "host"
                aux_helper  = OneHostHelper.new(
                                zone['onename'] + ":" + zone['onepass'],
                                zone['endpoint'],
                                false)

                aux_helper.list_pool(options)
            when "vm"
                aux_helper  = OneVMHelper.new(
                                zone['onename'] + ":" + zone['onepass'],
                                zone['endpoint'],
                                false)

                aux_helper.list_pool(options)
            when "image"
                aux_helper  = OneImageHelper.new(
                                zone['onename'] + ":" + zone['onepass'],
                                zone['endpoint'],
                                false)

                aux_helper.list_pool(options)
            when "vn"
                aux_helper  = OneVNetHelper.new(
                                zone['onename'] + ":" + zone['onepass'],
                                zone['endpoint'],
                                false)

                aux_helper.list_pool(options)

            when "template"
                aux_helper  = OneTemplateHelper.new(
                                zone['onename'] + ":" + zone['onepass'],
                                zone['endpoint'],
                                false)

                aux_helper.list_pool(options)

            when "user"
                aux_helper  = OneUserHelper.new(
                                zone['onename'] + ":" + zone['onepass'],
                                zone['endpoint'],
                                false)

                aux_helper.list_pool(options)
        end
        0
    end

    command :list, 'Lists Zones in the pool',
                :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
        helper.list_pool(options)
    end

    command :delete, 'Deletes a Zone', :zoneid do
        helper.delete_resource(args[0],options)
    end
end
