#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_cluster_hmc.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

#================================================
# The following, commented line enforces hmcing
# standards when this file is edited via vim.
#================================================
# vim:tabstop=4:shiftwidth=4:expandtab:smarttab
#================================================

# Start of POD-formatted documentation. Viewing suggestions:
#      perldoc <FILENAME>
#      pod2text -c <FILENAME>
#      pod2text -c --hmce <FILENAME>
#      pod2html <FILENAME>
function devDoc {
    : <<'=cut' >/dev/null 2>&1

=head1 NAME

 KLIB_HACMP_modify_cluster_hmc

=head1 VERSION

 Version Number:  1.8
 Last Extracted:  10/29/15 19:42:04
 Last Changed:    10/28/15 07:21:01

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_cluster_hmc.sh, hacmp.assist, 61haes_r720, 1544B_hacmp720

=head1 SYNOPSIS

 clmgr manage cluster hmc \
             [ DEFAULT_HMC_TIMEOUT=<MINUTES> ] \
             [ DEFAULT_HMC_RETRY_COUNT=<INTEGER> ]\
             [ DEFAULT_HMC_RETRY_DELAY=<SECONDS> ]\
             [ DEFAULT_HMCS_LIST=<HMCS> ]
             [ CONNECTION_TYPE={ssh|rest} ]
 NOTE: the alias for "cluster" is "cl".

=head1 DESCRIPTION

Attempts to modify the cluster hmc configuration.

=head1 ARGUMENTS

 1. properties [REQUIRED] [hash ref]
    An associative array within which data about the
    modified object can be returned to the caller.

 2. DEFAULT_HMC_TIMEOUT [OPTIONAL] [integer]
    The default value for timeout for DLPAR acquisition/release HMC operations.
    This value is expressed in minutes.
    For memory release operations, 1 minute per GBytes is added to this value.
    The timeout value can also be set on each HMC separately.

 3. DEFAULT_HMC_RETRY_COUNT [OPTIONAL] [integer]
    The default value for the number of retries on HMC operations.
    The HMC operation which timeouts will be retried this number of times.
    The retry count value can also be set on each HMC separately.

 4. DEFAULT_HMC_RETRY_DELAY [OPTIONAL] [integer]
    The default value for duration of delay between two retries
     set on HMC operations.
    This value is expressed in seconds.
    The HMC operation which timeouts will be retried 'default_hmc_retry_count' number
     of times, with 'default_hmc_retry_delay' seconds between each retry.
    The retry delay value can also be set on each HMC separately.

 5. DEFAULT_HMCS_LIST [OPTIONAL] [string]
    The default list of HMCs to be used by all nodes of the cluster.
    HMC list can be set at cluster level, at site level or at node level.
    We cannot modify the list of HMCs set on this DEFAULT_HMCS_LIST parameter.
    We can only change the precedence order of the HMCs of this list.
    For example we had 'DEFAULT_HMCS_LIST=HMC1 HMC2 HMC3',
    we can change this list to 'DEFAULT_HMCS_LIST=HMC2 HMC3 HMC1',
    to indicate that HMC2 has to be tried first.

 6. CONNECTION_TYPE [OPTIONAL] [integer]
    The connection type is used to establish passwordless SSH vs REST API
    based connection to HMC for DLPAR,EPCoD and OnOff CoD operations. 
    Default value for this parameter is SSH.


=head1 RETURN

 0: no errors were detected; the operation appears to have been successful
 1: a general error has occurred
 2: a specified resource does not exist, or could not be found
 3: some required input was missing
 4: some detected input was incorrect in some way
 5: a required dependency does not exist
 6: a specified search failed to match any data

=head1 COPYRIGHT

COPYRIGHT International Business Machines Corp. 2005,2015
All Rights Reserved

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_modify_cluster_hmc {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_cluster_hmc.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
    : INPUTS: $*

    typeset -n properties=$1
    shift

    typeset user_input=$*
    typeset default_timeout=${1//\"/}
    typeset default_retry_count=${2//\"/}
    typeset default_retry_delay=${3//\"/}
    typeset default_hmcs_list=${4//\"/}
    typeset connection_type=${5//\"/}

    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset -A clattrs

    CL=$LINENO KLIB_HACMP_get_cluster_attributes clattrs

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    if [[ -z ${clattrs[CLUSTER_NAME]} ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 35 "\nERROR: no cluster is defined yet.\n\n" 1>&2
        rc=$RC_MISSING_DEPENDENCY

    #=================
    : Validate input
    #=================
    elif [[ $user_input == *([[:space:]]) ]] ; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 401 "\nERROR: no valid modifications were specified for \"%1\$s\".\n\n" "$(cllsclstr -cS | /usr/bin/cut -d: -f2)" 1>&2
        rc=$RC_MISSING_INPUT
    fi

    if [[ -n $default_timeout && $default_timeout != +([[:digit:]]) ]] ; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 111 "\nERROR: \"%1\$s\" requires a positive, integer value.\n\n" DEFAULT_HMC_TIMEOUT 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $default_retry_count && $default_retry_count != +([[:digit:]]) ]] ; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 111 "\nERROR: \"%1\$s\" requires a positive, integer value.\n\n" DEFAULT_HMC_RETRY_COUNT 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $default_retry_delay && $default_retry_delay != +([[:digit:]]) ]] ; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 111 "\nERROR: \"%1\$s\" requires a positive, integer value.\n\n" DEFAULT_HMC_RETRY_DELAY 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ $connection_type != *([[:space:]]) ]]; then
        CL=$LINENO verify_in_set CONNECTION_TYPE "$connection_type" "ssh,rest" connection_type
        (( $? != RC_SUCCESS )) && rc=$RC_INCORRECT_INPUT
    fi



    #=====================================================
    : We cannot modify the contents of the list of HMCs,
    : we can only modify the precedence order of the HMCs.
    #=====================================================
    if [[ -n $default_hmcs_list ]]; then
        #====================================================
        : First, verify that the list of hmcs passed is the
        : exact list of hmcs already set on this cluster.
        #====================================================
        typeset -i error_found=0;
        print "$0()[$LINENO]($SECONDS): clodmget -f default_hmcs_list -n HACMPhmcparam" >>$CLMGR_TMPLOG  # Always log commands
        if [[ $(clodmget -f default_hmcs_list -n HACMPhmcparam | tr ' ' '\n' | sort -u) != \
            $(print -- "$default_hmcs_list" | tr ' ' '\n' | sort -u) ]]; then
            error_found=1
        fi

        #===========================================================
        : If verification step is not ok, then display error message
        #===========================================================
        if (( error_found == 1 )); then
        	typeset VALIDVALUES=$(clodmget -f default_hmcs_list -n HACMPhmcparam | sort -u)
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" DEFAULT_HMCS_LIST "$default_hmcs_list" 1>&2
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "$VALIDVALUES" 1>&2
            rc=$RC_INCORRECT_INPUT
        fi
    fi

    #=================================
    # Tune the cluster, if requested
    #=================================
    if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
        if [[ -n $default_timeout ]]; then
            #================================================
            : Setting default_timeout to "$default_timeout"
            #================================================
            print "HACMPhmcparam:
default_timeout=$default_timeout" | /usr/bin/odmchange -o HACMPhmcparam
            typeset VAL=$(clodmget -n -f default_timeout HACMPhmcparam)
            [[ $VAL == $default_timeout ]] && rc=$RC_SUCCESS || rc=$RC_ERROR
        fi
    fi

    if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
        if [[ -n $default_retry_count ]]; then
            #================================================
            : Setting default_retry_count to "$default_retry_count"
            #================================================
            print "HACMPhmcparam:
default_retry_count=$default_retry_count" | /usr/bin/odmchange -o HACMPhmcparam
            typeset VAL=$(clodmget -n -f default_retry_count HACMPhmcparam)
            [[ $VAL == $default_retry_count ]] && rc=$RC_SUCCESS || rc=$RC_ERROR
        fi
    fi

    if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
        if [[ -n $default_retry_delay ]]; then
            #================================================
            : Setting default_retry_delay to "$default_retry_delay"
            #================================================
            print "HACMPhmcparam:
default_retry_delay=$default_retry_delay" | /usr/bin/odmchange -o HACMPhmcparam
            typeset VAL=$(clodmget -n -f default_retry_delay HACMPhmcparam)
            [[ $VAL == $default_retry_delay ]] && rc=$RC_SUCCESS || rc=$RC_ERROR
        fi
    fi

    if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
        if [[ -n $default_hmcs_list ]]; then
            #================================================
            : Setting default_hmcs_list to "$default_hmcs_list"
            #================================================
            print "HACMPhmcparam:
default_hmcs_list=$default_hmcs_list" | /usr/bin/odmchange -o HACMPhmcparam
            typeset VAL=$(clodmget -n -f default_hmcs_list HACMPhmcparam)
            [[ $VAL == $default_hmcs_list ]] && rc=$RC_SUCCESS || rc=$RC_ERROR
        fi
    fi

    if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
        if [[ -n $connection_type ]]; then
            if [[ $connection_type == "ssh" ]]; then
                connectiontype=0
            else
                connectiontype=1
                CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1142 "\nWARNING: HMC Connection type is updated to REST API,make sure USER_NAME and PASSWORD is set for all HMC(s).\n" 1>&2
            fi
            #==============================================================
            : Setting connection_type to "$connectiontype" \(0=ssh,1=rest\) 
            #==============================================================
            print "HACMPhmcparam:
connection_type=$connectiontype" | /usr/bin/odmchange -o HACMPhmcparam
            typeset VAL=$(clodmget -n -f connection_type HACMPhmcparam)
            [[ $VAL == $connectiontype ]] && rc=$RC_SUCCESS || rc=$RC_ERROR
        fi
    fi

    if (( rc == RC_SUCCESS )); then
        #===========================================================
        # Set the "handle" attribute in the HACMPcluster class
        # to zero to indicate that an unsynced change exists in
        # the cluster configuration. 
        #===========================================================
        if [[ -n $default_timeout || 
                    -n $default_retry_delay || 
                    -n $default_retry_delay || 
                    -n $default_hmcs_list || 
                    -n $connection_type ]]; then
            print "\
HACMPcluster:
\thandle=0" | /usr/bin/odmchange -o HACMPcluster
        fi
    fi

    #===========================================================
    : If output from this operation was requested, retrieve it
    #===========================================================
    if (( $rc == RC_SUCCESS )); then
        if (( CLMGR_VERBOSE )) || [[ -n $CLMGR_ATTRS ]]; then
            CL=$LINENO KLIB_HACMP_get_cluster_attributes properties
        fi
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
       [[ $CLMGR_GUI == *([[:space:]]) ]]
    then
        CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 104 "For more information about available options and syntax, try\n\"$HAUTILS/clmgr %1\$s\". As an\nalternative, if the PowerHA SystemMirror man pages have been installed, invoke\n\"$HAUTILS/clmgr -hv\" (or \"/usr/bin/man clmgr\"),\nsearching for \"%2\$s\" in the displayed text.\n\n" \
        "manage cluster -h" "CLUSTER:" "$CLMGR_PROGNAME" 1>&2
    fi

    log_return_msg "$rc" "$0()" "$LINENO"
    return $?
} # End of "KLIB_HACMP_modify_cluster_hmc()"
