#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2024, 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%%
if File.directory?(GEMS_LOCATION)
    real_gems_path = File.realpath(GEMS_LOCATION)
    if !defined?(Gem) || Gem.path != [real_gems_path]
        $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }

        # Suppress warnings from Rubygems
        # https://github.com/OpenNebula/one/issues/5379
        begin
            verb = $VERBOSE
            $VERBOSE = nil
            require 'rubygems'
            Gem.use_paths(real_gems_path)
        ensure
            $VERBOSE = verb
        end
    end
end
# %%RUBYGEMS_SETUP_END%%

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

################################################################################
# Required libraries
################################################################################

require 'base64'
require 'csv'
require 'date'
require 'digest/md5'
require 'erb'
require 'json'
require 'nokogiri'
require 'openssl'
require 'ox'
require 'set'
require 'socket'
require 'sqlite3'
require 'tempfile'
require 'time'
require 'uri'
require 'yaml'

require 'opennebula'
require 'vcenter_driver'

# Include OpenNebula to avoid having to use OpenNebula:: namespace
include OpenNebula

################################################################################
# vCenter helper functions
################################################################################

# Get VIClient object from host
#
# @param id [Integer] Host ID
def vi_client_host(id)
    VCenterDriver::VIClient.new_from_host(id)
end

# Get VM vCenter object
#
# @param id [Integer] VM ID
def vm(id)
    one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, id)

    did = one_vm['DEPLOY_ID']
    hid = one_vm.retrieve_xmlelements(
        'HISTORY_RECORDS/HISTORY/HID'
    ).last.text.to_i

    VCenterDriver::VirtualMachine.new_one(vi_client_host(hid), did, one_vm)
end

# Get host vCenter object
#
# @param id [Integer] Host ID
def host(id)
    vi_client = vi_client_host(id)
    one_h     = VCenterDriver::VIHelper.one_item(OpenNebula::Host, hid)

    VCenterDriver::ClusterComputeResource.new_from_ref(
        one_h['TEMPLATE/VCENTER_CCR_REF'],
        vi_client
    )
end

################################################################################
# Open irb session
################################################################################

puts '* You can use the function vi_client_host(id) to get vCenter client'
puts '* You can use the function vm(id) to get vCenter VM'
puts '* You can use the function host(id) to get vCenter host'

@client = Client.new
version = '>= 0'

gem 'pry', version
load Gem.bin_path('pry', 'pry', version)
