#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,2019,2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_storage_system.sh 1.5
#
# Licensed Materials - Property of IBM
#
# COPYRIGHT International Business Machines Corp. 2011
# All Rights Reserved
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# IBM_PROLOG_END_TAG
#
# @(#)  5f56dbd 43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_storage_system.sh, 726, 2147A_aha726, Mar 08 2021 04:44 PM

# 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_modify_storage_system

=head1 SYNOPSIS

 clmgr modify storage_system <storage_system_name> \
             [ NAME=<new_storage_system_name> ] \
             [ SITE=<site> ] \
             [ AGENTS=<agent>[,<agent#2>,...] ] \
             [ WWNN=<world_wide_node_name> ] \
             [ VENDOR_ID=<identifier> ] \
             [ ADDRESSES=<IP>[<IP#2>,...] ] \
             [ MASTER=<Master/Auxiliary> ] \
             [ PARTNER=<Remote Partner> ] \
             [ USER=<username> ] \
             [ BACKUP_PROFILE=true ] \
             [ ATTRIBUTES=<NAME>@<VALUE>[,<NAME#2>@<VALUE#2>,...] ]
 NOTE: the alias for "storage_system" is "sts".

=head1 DESCRIPTION

Attempts to modify a storage system.

=head1 ARGUMENTS

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

 2. storage_system [REQUIRED] [string]
    The label of the object to be modified.

 3. new_name [OPTIONAL] [set]
    A new label to apply to this object

 4. agents [OPTIONAL] [string]
    One or more storage agents to be associated with this
    storage system.

 5. vendor_id [OPTIONAL] [string]
    The vendor ID of the specified storage system.

 6. wwnn [OPTIONAL] [string]
    The World Wide Node Name for the specified storage system.

 7. site [OPTIONAL] [string]
    The site that the specified storage system should be
    associated with.

 8. addresses [OPTIONAL] [string]
    SVC-only inputs.

 9. master [OPTIONAL] [string]
    SVC-only inputs.

 10. partner [OPTIONAL] [string]
    SVC-only inputs.

 11. attributes [OPTIONAL] [string]
   Storage-specific configuration parameters.

 12. user [OPTIONAL] [string]
     Input for svc remote backup.
 
 13. backup_profile [OPTIONAL] [boolean]
     A Boolean-like indicator. 
     Enabling creates svc cluster definition for remote backup

=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,2010
All Rights Reserved

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_modify_storage_system {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  5f56dbd 43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_storage_system.sh, 726, 2147A_aha726, Mar 08 2021 04:44 PM
    : INPUTS: $*

    typeset -n properties=$1
    typeset storage_system=${2//\"/}
    typeset new_name=${3//\"/}
    typeset agents=${4//\"/}
    typeset vendor_id=${5//\"/}
    typeset wwnn=${6//\"/}
    typeset site=${7//\"/}
    typeset addresses=${8//\"/}
    typeset master=${9//\"/}
    typeset partner=${10//\"/}
    typeset attributes=${11//\"/}
    typeset username=${12//\"/}
    typeset backup=${13//\"/}

    addresses=${addresses// /,}
    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        log_return_msg "$RC_MISSING_DEPENDENCY" "$0()" "$LINENO"
        return $?
    fi
 
    typeset -i backup_profile=0
    if [[ -n $backup ]]; then
        CL=$LINENO verify_in_set BACKUP_PROFILE "$backup" "enable, true, yes, 1" backup
        if (( $? == RC_SUCCESS )); then
            [[ $backup == @(t|y|1|e)* ]] && backup_profile=1
        else
            log_return_msg "$RC_INCORRECT_INPUT" "$0()" "$LINENO"
            return $?
        fi
    fi

    if (( $backup_profile == 0 )); then 
        CL=$LINENO isEnterprise
        if (( $? != 1 )); then
            CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 184 "\nERROR: this operation requires IBM PowerHA SystemMirror for AIX Enterprise Edition.\n\n" 1>&2
            log_return_msg "$RC_MISSING_DEPENDENCY" "$0()" "$LINENO"
            return $?
        fi
    fi

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

    typeset existing
    if (( $backup_profile == 0 )); then 
        CL=$LINENO KLIB_HACMP_list_storage_systems existing 2>>$CLMGR_TMPLOG
    else
        CL=$LINENO KLIB_HACMP_list_storage_systems existing BACKUP=1 2>>$CLMGR_TMPLOG
    fi

    #=================
    : Validate input
    #=================
    if [[ -z $storage_system ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
        rc=$RC_MISSING_INPUT

    elif [[ " ${existing[*]} " != *\ $storage_system\ * ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$storage_system" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 164 "Available Storage Systems:\n\n" 1>&2

        for (( i=0; i<${#existing[*]}; i++ )); do
            if [[ ${existing[$i]} != *([[:space:]]) ]]; then
                print -u2 "\t${existing[$i]}"
            fi
        done
        print -u2 ""
        rc=$RC_NOT_FOUND
 
    elif [[ -n $new_name ]];then

        if [[ " ${existing[*]} " == *\ $new_name\ * ]] && [[ $new_name != $storage_system ]]; then
            dspmsg -s $CLMGR_SET $CLMGR_MSGS 1215 "\nERROR: storage system with the specified new name already exists: \"%1\$s\"\nDuplicate storage name entries are not allowed\n\n" "$new_name" 1>&2 
            rc=$RC_INCORRECT_INPUT

        elif [[ -n "${new_name//[a-zA-Z0-9_-]/}" ]] || [[ "${new_name/#[0-9-]/}" != $new_name ]]; then
            dspmsg -s $CLMGR_SET $CLMGR_MSGS 1451 "\nERROR: one or more invalid characters were detected in \"%1\$s\".\nValid characters include letters, numbers, hyphen and underscores only.\n.A name cannot begin with a number or hyphen.\n\n" "$new_name" 1>&2
            rc=$RC_INCORRECT_INPUT
        fi
    fi

    if [[ $user == *([[:space:]]) && $password != *([[:space:]]) ]]; then
        password=""
    fi

    #==================================================================
    : Modify the storage system if no input errors have been detected
    #==================================================================
    if (( $rc == RC_UNKNOWN )); then
        typeset -A ssAttrs
        if (( $backup_profile == 0 )); then 
            CL=$LINENO KLIB_HACMP_get_storage_system_attributes "$storage_system" ssAttrs 2>>$CLMGR_TMPLOG
        else
            # Get Storage Systems from backup management XML file
            CL=$LINENO KLIB_HACMP_get_storage_system_attributes "$storage_system" ssAttrs BACKUP=1 2>>$CLMGR_TMPLOG
        fi
        typeset type=${ssAttrs[TYPE]}

        typeset value="" aOpts="" oOpts="" vOpt="" wOpt="" NOpt="" tOpt=""
        typeset rOpt="" pOpt="" iOpt="" IOpt="" bOpt="" uOpt=""

        [[ $new_name  != *([[:space:]]) ]] && NOpt=" -N $new_name"
        [[ $vendor_id != *([[:space:]]) ]] && vOpt=" -v $vendor_id"
        [[ $wwnn      != *([[:space:]]) ]] && wOpt=" -w $wwnn"
        [[ $site      != *([[:space:]]) ]] && tOpt=" -t $site"
        [[ $master    != *([[:space:]]) ]] && rOpt=" -r $master"
        [[ $partner   != *([[:space:]]) ]] && pOpt=" -p $partner"

        if [[ -n $agents ]]; then
            for value in ${agents//,/ }; do
                aOpts="$aOpts -a $value"
            done
        fi

        if [[ -n $attributes ]]; then
            for value in ${attributes//,/ }; do
                oOpts="$oOpts -o${value//@/ }"
            done
        fi

        if [[ -n $addresses ]]; then
            typeset ip1="" ip2="" rest=""
            print -- "$addresses" | IFS=, read ip1 ip2 rest
            [[ -n $ip1 ]] && iOpt=" -i $ip1"
            [[ -n $ip2 ]] && IOpt=" -I $ip2"
        fi
        if [[ -n $username ]];then
            uOpt=" -u $username"
        fi
        if [[ $backup_profile == 0 ]];then
            print -- "$0()[$LINENO]($SECONDS): $HAXDWIZ/clxd_change_ss_director -n $storage_system -s $type$tOpt$NOpt$vOpt$wOpt$aOpts$rOpt$pOpt$iOpt$IOpt$oOpts$uOpt" >>$CLMGR_TMPLOG  # Always log commands
            $HAXDWIZ/clxd_change_ss_director -n $storage_system -s $type$tOpt$NOpt$vOpt$wOpt$aOpts$rOpt$pOpt$iOpt$IOpt$oOpts$uOpt
            rc=$?
            print "$0()[$LINENO]($SECONDS): clxd_change_ss_director RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        else
            [[ $new_name != *([[:space:]]) ]] && bOpt="New_name=$new_name"
            [[ -n $addresses ]] && bOpt="$bOpt IP_address=$addresses"
            [[ -n $username ]] && bOpt="$bOpt Username=$username"
            print -- "$0()[$LINENO]($SECONDS): cl_cbm_modify_storage_profile \"Storage_name=$storage_system\" \"Storage_type=$type\" $bOpt" >>$CLMGR_TMPLOG
            LANG=C cl_cbm_modify_storage_profile "Storage_name=$storage_system" "Storage_type=$type" $bOpt
            rc=$?
            print "$0()[$LINENO]($SECONDS): cl_cbm_modify_storage_profile \"Storage_name=$storage_system\" \"Storage_type=$type\" $bOpt RC: $rc" >>$CLMGR_TMPLOG

        fi
 
        if (( $rc != RC_SUCCESS )); then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 400 "\nERROR: failed to modify \"%1\$s\".\n\n" "$storage_system" 1>&2
            rc=$RC_ERROR
        fi

        #===========================================================
        : If output from this operation was requested, retrieve it
        #===========================================================
        if (( $rc == RC_SUCCESS )); then
            if (( CLMGR_VERBOSE )) || [[ -n $CLMGR_ATTRS ]]; then
                if (( $backup_profile == 0 )); then 
                    CL=$LINENO KLIB_HACMP_get_storage_system_attributes "$storage_system" ssAttrs 2>>$CLMGR_TMPLOG
                else
                    # Get Storage Systems from backup management XML file
                    CL=$LINENO KLIB_HACMP_get_storage_system_attributes "$storage_system" ssAttrs BACKUP=1 2>>$CLMGR_TMPLOG
                fi
            fi
        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" \
        "add storage_system -h" "STORAGE SYSTEM:" "$CLMGR_PROGNAME" 1>&2
    fi

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