#!/bin/ksh93
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_nova.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#  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                                                      
#                                                                              

#================================================
# The following, commented line enforces coding
# 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 --code <FILENAME>
#      pod2html <FILENAME>
function devDoc {
  : <<'=cut' >/dev/null 2>&1

=head1 NAME

 KLIB_HACMP_delete_nova

=head1 SYNOPSIS

 clmgr delete nova {[NovaLink] | ALL}

=head1 DESCRIPTION

 Attempts to delete the specified NovaLink, from its name, or All NovaLinks.

=head1 ARGUMENTS

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

 2. NovaLink [REQUIRED] [string]
    Either hostname of IP address for the NovaLink.
    IPv4 and IPv6 format address are supported.
    All NovaLinks can be deleted if 'All' is specified.

=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

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_delete_nova {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : INPUTS: $*

    typeset novas=${1//\"}
    novas=${novas//,/ }

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset nova=
    typeset -u uc_val=$novas

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        rc=$RC_MISSING_DEPENDENCY

    #=================
    : Validate input
    #=================
    elif [[ $novas == *([[:space:]]) ]]; then
        CL=$LINENO dspmsg -s $CLVT_SET $CLVT_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
        rc=$RC_MISSING_INPUT
    else
        typeset -u ALL=$uc_val
        if [[ $ALL == "ALL" ]]; then
            novas=""
            typeset available
            CL=$LINENO KLIB_HACMP_list_novas available
            for (( i=0; i<${#available[*]}; i++ )); do
                        novas=${novas:+$novas }${available[$i]}
            done
            # If no NOVA to delete, consider it is ok
            if [[ -z $novas ]]; then
                rc=$RC_SUCCESS
            fi
        else
            typeset NOVAS=""
            for nova in $novas; do
                CL=$LINENO KLIB_HACMP_is_known_nova nova >/dev/null
                if (( $? != RC_SUCCESS )); then
                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$nova" 1>&2
                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 1146 "Available NovaLinks:\n\n" 1>&2 

                    typeset available
                    CL=$LINENO KLIB_HACMP_list_novas available
                    for (( i=0; i<${#available[*]}; i++ )); do
                                if [[ ${available[$i]} != *([[:space:]]) ]]; then
                                    print -u2 "\t${available[$i]}"
                                fi
                    done
                    print -u2 ""
                    rc=$RC_NOT_FOUND
                else
                    NOVAS="${NOVAS:+$NOVAS }$nova"
                fi
            done
            novas=$NOVAS
        fi
    fi

    if (( $rc == RC_UNKNOWN )) ; then
        typeset NODES_OF_NOVA= managed_system=
        for nova in $novas; do
            #=====================================================
            : Get managed system attribute from nova
            #=====================================================
            print -- "$0()[$LINENO]($SECONDS): managed_system=$(clodmget -q name=$nova -f managed_system -n HACMPnova)" >>$CLMGR_TMPLOG
            managed_system=$(clodmget -q name=$nova -f managed_system -n HACMPnova)

            print -- "$0()[$LINENO]($SECONDS): clrmnova -n \"$nova\"" >>$CLMGR_TMPLOG
            clrmnova -n "$nova"
            rc=$?
            print -- "$0()[$LINENO]($SECONDS): clrmnova -n \"$nova\" rc=$rc" >>$CLMGR_TMPLOG

            if (( $rc != RC_SUCCESS )); then
                rc=$RC_ERROR
                dspmsg -s $CLMGR_SET $CLMGR_MSGS 1147 "\nERROR: failed to remove the \"%1\$s\" NovaLink.\n\n" "$nova" 1>&2 
                break
            else
                #===========================================================
                : Remove this nova managed system entries from HACMPnode ODM
                #===========================================================
                if [[ -n $managed_system ]]; then
                    NODES_OF_NOVA=$(clodmget -q "object=NOVA_MANAGED_SYSTEM  and value=$managed_system" -f name -n HACMPnode)
                    rc=$?                
                    if (( $rc == RC_SUCCESS )); then
                        if [[ -n $NODES_OF_NOVA ]]; then
                            for NODE_OF_NOVA in $NODES_OF_NOVA; do
                                print -- "$0()[$LINENO]($SECONDS): clchnovaparam -n \"$NODE_OF_NOVA\" -r NOVA_MANAGED_SYSTEM=$managed_system" >>$CLMGR_TMPLOG  # Always log commands
                                clchnovaparam -n "$NODE_OF_NOVA" -r NOVA_MANAGED_SYSTEM=$managed_system
                                rc=$?
                                print -- "clchnovaparam -n \"$NODE_OF_NOVA\" -r NOVA_MANAGED_SYSTEM=$managed_system rc=$rc" >>$CLMGR_TMPLOG  # Always log command result
                                if (( $rc != RC_SUCCESS )); then
                                    rc=$RC_ERROR
                                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 1147 "\nERROR: failed to remove the \"%1\$s\" NovaLink.\n\n" "$nova" 1>&2
                                fi
                            done
                        fi
                    fi
                fi                
            fi
        done
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
        [[ $CLVT_GUI == *([[:space:]]) ]]; then
        CL=$LINENO cl_dspmsg -s $CLVT_SET $CLVT_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" \
            "delete nova -h" "NOVA:" "$CLVT_PROGNAME" 1>&2
    fi

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