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

require 'one_helper'

class OneZoneHelper < OpenNebulaHelper::OneHelper

    SERVER_NAME={
        :name => "server_name",
        :short => "-n server_name",
        :large => "--name",
        :format => String,
        :description => "Zone server name"
    }

    SERVER_ENDPOINT={
        :name => "server_rpc",
        :short => "-r rpc endpoint",
        :large => "--rpc",
        :format => String,
        :description => "Zone server RPC endpoint"
    }

    def show_resource(id, options)
        resource = retrieve_resource(id)

        rc = resource.info_extended
        return -1, rc.message if OpenNebula.is_error?(rc)

        if options[:xml]
            return 0, resource.to_xml(true)
        else
            format_resource(resource, options)
            return 0
        end
    end

    def self.rname
        "ZONE"
    end

    def self.conf_file
        "onezone.yaml"
    end

    def format_pool(options)
        config_file = self.class.table_conf

        table = CLIHelper::ShowTable.new(config_file, self) do
            column :CURRENT, "Active Zone", :size=>1 do |d|
                "*" if helper.client.one_endpoint.strip ==
                       d["TEMPLATE"]['ENDPOINT'].strip
            end

            column :ID, "ONE identifier for the Zone", :size=>5 do |d|
                d["ID"]
            end

            column :NAME, "Name of the Zone", :left, :size=>25 do |d|
                d["NAME"]
            end

            column :ENDPOINT, "Endpoint of the Zone", :left, :size=>45 do |d|
                d["TEMPLATE"]['ENDPOINT']
            end

            default :CURRENT, :ID, :NAME, :ENDPOINT
        end

        table
    end

    def set_zone(zone_id, temporary_zone)
        zone = factory(zone_id)
        rc = zone.info

        if OpenNebula.is_error?(rc)
            return -1, rc.message
        end

        if !zone['TEMPLATE/ENDPOINT']
            return -1, "No Endpoint defined for Zone #{zone_id}"
        end

        if temporary_zone
            puts "Type: export ONE_XMLRPC=#{zone['TEMPLATE/ENDPOINT']}"
        else
            File.open(ENV['HOME']+"/.one/one_endpoint", 'w'){|f|<commit_msg>F #3378: add new column to show fed index (#3412)<commit_after># -------------------------------------------------------------------------- #
# Copyright 2002-2019, 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.                                             #
#--------------------------------------------------------------------------- #

require 'one_helper'

class OneZoneHelper < OpenNebulaHelper::OneHelper

    SERVER_NAME={
        :name => "server_name",
        :short => "-n server_name",
        :large => "--name",
        :format => String,
        :description => "Zone server name"
    }

    SERVER_ENDPOINT={
        :name => "server_rpc",
        :short => "-r rpc endpoint",
        :large => "--rpc",
        :format => String,
        :description => "Zone server RPC endpoint"
    }

    def show_resource(id, options)
        resource = retrieve_resource(id)

        rc = resource.info_extended
        return -1, rc.message if OpenNebula.is_error?(rc)

        if options[:xml]
            return 0, resource.to_xml(true)
        else
            format_resource(resource, options)
            return 0
        end
    end

    def self.rname
        "ZONE"
    end

    def self.conf_file
        "onezone.yaml"
    end

    def format_pool(options)
        config_file = self.class.table_conf

        table = CLIHelper::ShowTable.new(config_file, self) do
            column :CURRENT, "Active Zone", :size=>1 do |d|
                "*" if helper.client.one_endpoint.strip ==
                       d["TEMPLATE"]['ENDPOINT'].strip
            end

            column :ID, "ONE identifier for the Zone", :size=>5 do |d|
                d["ID"]
            end

            column :NAME, "Name of the Zone", :left, :size=>25 do |d|
                d["NAME"]
            end

            column :ENDPOINT, "Endpoint of the Zone", :left, :size=>45 do |d|
                d["TEMPLATE"]['ENDPOINT']
            end

            column :FED_INDEX, "Federation index", :left, :size=>10 do |d|
                helper.get_fed_index(d["TEMPLATE"]['ENDPOINT'])
            end

            default :CURRENT, :ID, :NAME, :ENDPOINT, :FED_INDEX
        end

        table
    end

    def get_fed_index(endpoint)
        client = OpenNebula::Client.new(nil, endpoint, :timeout => 5)
        xml    = client.call('zone.raftstatus')

        return '-' if OpenNebula.is_error?(xml)

        xml    = Nokogiri::XML(xml)

        if xml.xpath('RAFT/FEDLOG_INDEX')
            xml.xpath('RAFT/FEDLOG_INDEX').text
        else
            '-'
        end
    end

    def set_zone(zone_id, temporary_zone)
        zone = factory(zone_id)
        rc = zone.info

        if OpenNebula.is_error?(rc)
            return -1, rc.message
        end

        if !zone['TEMPLATE/ENDPOINT']
            return -1, "No Endpoint defined for Zone #{zone_id}"
        end

        if temporary_zone
            puts "Type: export ONE_XMLRPC=#{zone['TEMPLATE/ENDPOINT']}"
        else
            File.open(ENV['HOME']+"/.one/one_endpoint", 'w'){|f|
{'fraction_non_alphanumeric': 0.1382324819013796, 'fraction_numerical': 0.007785821609069799, 'mean_word_length': 1.3185560481317289, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5698555', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1827, 'n_words': 535}