#!/usr/bin/ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, 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.                                             #
#--------------------------------------------------------------------------- #

ONE_LOCATION = ENV['ONE_LOCATION']

if !ONE_LOCATION
    RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
    GEMS_LOCATION     = '/usr/share/one/gems'
else
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
end

# %%RUBYGEMS_SETUP_BEGIN%%
require 'load_opennebula_paths'
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION + '/cli'

require 'command_parser'
require 'one_helper/onecluster_helper'

CommandParser::CmdParser.new(ARGV) do
    usage '`onecluster` <command> [<args>] [<options>]'
    version OpenNebulaHelper::ONE_VERSION

    helper = OneClusterHelper.new

    before_proc do
        helper.set_client(options)
    end

    ########################################################################
    # Global Options
    ########################################################################
    set :option, CommandParser::OPTIONS + OpenNebulaHelper::CLIENT_OPTIONS

    list_options  = CLIHelper::OPTIONS
    list_options += OpenNebulaHelper::FORMAT
    list_options << OpenNebulaHelper::NUMERIC
    list_options << OpenNebulaHelper::DESCRIBE

    ########################################################################
    # Formatters for arguments
    ########################################################################
    set :format, :clusterid, OneClusterHelper.to_id_desc do |arg|
        helper.to_id(arg)
    end

    set :format, :clusterid_list, OneClusterHelper.list_to_id_desc do |arg|
        helper.list_to_id(arg)
    end

    set :format, :vnetid, OpenNebulaHelper.rname_to_id_desc('VNET') do |arg|
        OpenNebulaHelper.rname_to_id(arg, 'VNET')
    end

    set :format, :hostid, OpenNebulaHelper.rname_to_id_desc('HOST') do |arg|
        OpenNebulaHelper.rname_to_id(arg, 'HOST')
    end

    set :format, :datastoreid,
        OpenNebulaHelper.rname_to_id_desc('DATASTORE') do |arg|
        OpenNebulaHelper.rname_to_id(arg, 'DATASTORE')
    end

    ########################################################################
    # Commands
    ########################################################################

    create_desc = <<-EOT.unindent
        Creates a new Cluster
    EOT

    command :create, create_desc, :name do
        helper.create_resource(options) do |cluster|
            cluster.allocate(args[0])
        end
    end

    delete_desc = <<-EOT.unindent
        Deletes the given Cluster
    EOT

    command :delete, delete_desc, [:range, :clusterid_list] do
        helper.perform_actions(args[0], options, 'deleted') do |obj|
            obj.delete
        end
    end

    list_desc = <<-EOT.unindent
        Lists Clusters in the pool. #{OneClusterHelper.list_layout_help}
    EOT

    command :list, list_desc, :options => list_options do
        helper.list_pool(options)
    end

    show_desc = <<-EOT.unindent
        Shows information for the given Cluster
    EOT

    command :show, show_desc, :clusterid,
            :options => [OpenNebulaHelper::FORMAT, OpenNebulaHelper::DECRYPT] do
        helper.show_resource(args[0], options)
    end

    addhost_desc = <<-EOT.unindent
        Adds a Host to the given Cluster
    EOT

    # TODO: allow the second param to be [:range, :hostid_list]
    command :addhost, addhost_desc, :clusterid, :hostid do
        helper.perform_action(args[0], options, 'updated') do |cluster|
            cluster.addhost(args[1].to_i)
        end
    end

    delhost_desc = <<-EOT.unindent
        Deletes a Host from the given Cluster
    EOT

    # TODO: allow the second param to be [:range, :hostid_list]
    command :delhost, delhost_desc, :clusterid, :hostid do
        helper.perform_action(args[0], options, 'updated') do |cluster|
            cluster.delhost(args[1].to_i)
        end
    end

    adddatastore_desc = <<-EOT.unindent
        Adds a Datastore to the given Cluster
    EOT

    # TODO: allow the second param to be [:range, :datastoreid_list]
    command :adddatastore, adddatastore_desc, :clusterid, :datastoreid do
        helper.perform_action(args[0], options, 'updated') do |cluster|
            cluster.adddatastore(args[1].to_i)
        end
    end

    deldatastore_desc = <<-EOT.unindent
        Deletes a Datastore from the given Cluster
    EOT

    # TODO: allow the second param to be [:range, :datastoreid_list]
    command :deldatastore, deldatastore_desc, :clusterid, :datastoreid do
        helper.perform_action(args[0], options, 'updated') do |cluster|
            cluster.deldatastore(args[1].to_i)
        end
    end

    addvnet_desc = <<-EOT.unindent
        Adds a Virtual Network to the given Cluster
    EOT

    # TODO: allow the second param to be [:range, :vnetid_list]
    command :addvnet, addvnet_desc, :clusterid, :vnetid do
        helper.perform_action(args[0], options, 'updated') do |cluster|
            cluster.addvnet(args[1].to_i)
        end
    end

    delvnet_desc = <<-EOT.unindent
        Deletes a Virtual Network from the given Cluster
    EOT

    # TODO: allow the second param to be [:range, :vnetid_list]
    command :delvnet, delvnet_desc, :clusterid, :vnetid do
        helper.perform_action(args[0], options, 'updated') do |cluster|
            cluster.delvnet(args[1].to_i)
        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.
    EOT

    command :update, update_desc, :clusterid, [:file, nil],
            :options => OpenNebulaHelper::APPEND do
        helper.perform_action(args[0], options, 'modified') do |obj|
            if options[:append]
                str = OpenNebulaHelper.append_template(args[0], obj, args[1])
            else
                str = OpenNebulaHelper.update_template(args[0], obj, args[1])
            end

            helper.set_client(options)
            obj = helper.retrieve_resource(obj.id)

            obj.update(str, options[:append])
        end
    end

    rename_desc = <<-EOT.unindent
        Renames the Cluster
    EOT

    command :rename, rename_desc, :clusterid, :name do
        helper.perform_action(args[0], options, 'renamed') do |o|
            o.rename(args[1])
        end
    end

    optimize_desc = <<-EOT.unindent
        Create optimization plan for Cluster
    EOT

    # TODO: Consider optional parameters
    #  - autostart: Automatically start applying the new optimization plan
    #  - force: Delete old plan if exists
    command :optimize, optimize_desc, :clusterid do
        helper.perform_action(args[0], options, 'creating optimization plan') do |o|
            o.optimize
        end
    end

    planexecute_desc = <<-EOT.unindent
        Start applying the optimization plan
    EOT

    command :planexecute, planexecute_desc, :clusterid do
        helper.perform_action(args[0], options, 'cluster optimization started') do |o|
            o.plan_execute
        end
    end

    plandelete_desc = <<-EOT.unindent
        Delete the optimization plan
    EOT

    # TODO: We should allow to delete a placement plan with id -1
    command :plandelete, plandelete_desc, :clusterid do
        helper.perform_action(args[0], options, 'plan deleted') do |o|
            o.plan_delete
        end
    end
end
