#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 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/db2/KLIB_DB2_add_instance_to_metadata.sh 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,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 
# @(#)  7d4c34b 43haes/lib/ksh93/db2/KLIB_DB2_add_instance_to_metadata.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
#=head1 NAME
#
# KLIB_DB2_add_instance_to_metadata - Add the DB2 discovery data to the HACMPsa_metadata class
#
#=head1 SYNOPSIS
#
# KLIB_DB2_add_instance_to_metadata "db2inst1" || {
# 	echo "ERROR: unable to add metadata!"
# }
#
#=head1 DESCRIPTION
#
# Adds the HACMPsa_metadata fields from the db2.disc discovery file for
# the specified instance
#
# The following fields are added to the metadata:
#
#	HOME_FILESYSTEM - instance home filesystem
#	HOME_VOLUME_GROUP - instance home volume group
#	USER_NAME 	- instance user
#	GROUP_NAME	- instance user parent group
#	UID 		- instance user UID
#	GID 		- instance user GID
#
#	DASADM_USER 	- das administrator user name
#	DASADM_GROUP 	- das admin group (pgrp)
#	DASADM_UID 	- das admin uid
#	DASADM_GID	- das admin gid
#
#	FENCED_USER	- fenced db2 user name
#	FENCED_GROUP
#	FENCED_UID
#	FENCED_GID
#
# 	DATABASES
#
#=head1 ARGUMENTS
#
# 	1: [scalar] name of DB2 instance to add from discovery file
#
#=head1 RETURN
#
#   0: success, HACMPsa_metadata populated correctly
#   1: failed, unable to populate HACMPsa_metadata
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_add_instance_to_metadata
{
	. /usr/es/lib/ksh93/func_include
	typeset instance=$1
	typeset -n appname=$2
	if [[ -z $instance ]]; then
		return 1
	fi

	typeset -A odm_fields
	# These are the ODM fields and mappings to the discovery content
	odm_fields=( \
		[HOME_FILESYSTEM]="INSTHOME"
		[HOME_VOLUME_GROUP]="DB2HOME_VG"
		[USER_NAME]="INSTANCE_OWNER_USER"
		[GROUP_NAME]="INSTANCE_OWNER_GROUP"
		[UID]="INSTANCE_OWNER_UID"
		[GID]="INSTANCE_OWNER_GID"
		[DASADM_USER]="DASADM_USER"
		[DASADM_GROUP]="DASADM_GROUP"
		[DASADM_UID]="DASADM_UID"
		[DASADM_GID]="DASADM_GID"
		[FENCED_USER]="FENCED_USER"
		[FENCED_GROUP]="FENCED_GROUP"
		[FENCED_UID]="FENCED_UID"
		[FENCED_GID]="FENCED_GID"
		[DATABASES]="DATABASES"
	)
        for name in ${!odm_fields[*]}; do
            # Add the database that user has selected.
            # DATABASE_NAME is exported by cl_db2import.sh
            if [ ${odm_fields[$name]} == "DATABASES" ]; then
                value=$DATABASE_NAME
                if [[ -z $DATABASE_NAME ]]; then
                    KLIB_DB2_print_message 2 60 "INFO: Failed to read \"DATABASE_NAME\" variable.\n"
                    value=$(KLIB_DB2_disc_get_instance_value $instance "${odm_fields[$name]}")
                fi
            else
                value=$(KLIB_DB2_disc_get_instance_value $instance "${odm_fields[$name]}")
            fi
            /usr/es/sbin/cluster/sa/sbin/claddsaapp \
                -a "$appname" $name="$value"
        done
        
	return 0
}
