#!/bin/bash

#set -x

usage() {
  printf "\nUsage: [-d] [-u] [-p] \n\n -d - cloud DB server ip address, defaulted to localhost if not specified \n -u - user name to access cloud DB, defaulted to "cloud" if not specified \n -p - cloud DB user password, defaulted to "cloud" if not specified \n -z - zone id \n\nExample: zone_delete.sh -dlocalhost -ucloud -pcloud -z1\n\n" 
}

user=cloud
password=cloud
host=localhost
help=
zone=

while getopts “d:u:z:ph” OPTION
do
     case $OPTION in
         u)
             user=$OPTARG
             ;;
         p)
             password=$OPTARG
             ;;
	 d)
	     host=$OPTARG
	     ;;
	 z)
	     zone=$OPTARG
	     ;;
	 h)
	     help=1
	     ;;
     esac
done

if  [ "$help" == "1" ]
then
    usage
    exit 3
fi

if  [ "$zone" == "" ]
then
    printf "\nZone Id is required\n"
    usage
    exit 3
fi

query[0]="update networks set removed = now() where data_center_id"
query[1]="update networks set removed = now() where data_center_id"
query[2]="update cluster set removed = now() where data_center_id"
query[3]="update volumes set removed = now() where data_center_id"
query[4]="update snapshots set removed = now() where data_center_id"
query[5]="delete from vlan where data_center_id"
query[6]="delete from op_dc_ip_address_alloc where data_center_id"
query[7]="delete from op_dc_link_local_ip_address_alloc where data_center_id"
query[8]="delete from dc_storage_network_ip_range where data_center_id"
query[9]="update host_pod_ref set removed = now() where data_center_id"
query[10]="delete from op_dc_vnet_alloc where data_center_id"
query[11]="update host set removed = now() where data_center_id"
query[12]="delete from user_ip_address where data_center_id"
query[13]="delete from user_statistics where data_center_id"
query[14]="update vm_instance set removed = now() where data_center_id"
query[15]="update template_zone_ref set removed = now() where zone_id"
query[16]="update account set default_zone_id = null where default_zone_id"
query[17]="delete from op_host_capacity where data_center_id"
query[18]="delete from alert where data_center_id"
query[19]="update storage_pool set removed = now() where data_center_id"
query[20]="delete from op_pod_vlan_alloc where data_center_id"
query[21]="delete from data_center_details where dc_id"
query[22]="update physical_network set removed = now() where data_center_id"
query[23]="update vpc_gateways set removed = now() where zone_id"
query[24]="delete from volume_host_ref where zone_id"
query[25]="delete from usage_event where zone_id"
query[26]="update vpc set removed = now() where zone_id"
query[27]="update data_center set removed = now() where id"
query[28]="update autoscale_vmprofiles set removed = now() where zone_id"
query[29]="update autoscale_vmgroups set removed = now() where zone_id"
query[30]="update volume_view set removed = now() where data_center_id"
query[31]="update account_view set removed = now() where data_center_id"
query[32]="delete from  ucs_manager where zone_id"
query[33]="delete from  user_ipv6_address where data_center_id"
query[34]="update user_vm_view set removed = now() where data_center_id"
query[35]="update domain_router_view set removed = now() where data_center_id"
query[36]="update host_view set removed = now() where data_center_id"
query[37]="update storage_pool_view set removed = now() where data_center_id"
query[38]="update vm_reservation set removed = now() where data_center_id"
query[39]="delete from  volume_store_ref where zone_id"
query[40]="delete from vmware_data_center_zone_map where zone_id"
query[41]="delete from  legacy_zones where zone_id"
query[42]="delete from  portable_ip_address where data_center_id"
query[43]="update template_view set removed = now() where data_center_id"
query[44]="update data_center_view set removed = now() where id"
query[45]="delete from  external_stratosphere_ssp_tenants where zone_id"
query[46]="update image_store_view set removed = now() where data_center_id"
query[47]="delete from image_store where data_center_id"

zone_exits=(`mysql -h $host --user=$user --password=$password --skip-column-names -U cloud -e "select id from data_center where id = $zone and removed is null"`)
if [ "${#zone_exits[@]}" == "0" ];then
	echo "Zone : $zone does not exist or is removed"
	exit 2
fi

echo "Removing Zone: $zone"
for ((i=0;i<${#query[*]};i++)) 
do
	echo "Executing: ${query[i]} = $zone"
	`mysql -h $host --user=$user --password=$password --skip-column-names cloud -e "${query[i]} = $zone"`
	if [ "$?" != 0 ];then
		echo "Error while removing zone: $zone"
		exit 2
	fi
done


removed_zone=(`mysql -h $host --user=$user --password=$password --skip-column-names -U cloud -e "select id from data_center where id = $zone and removed is null"`)
if [ "${#removed_zone[@]}" == "0" ];then
	echo "Successfully removed Zone: $zone"
else
	echo "Failed to remove Zone: $zone"
fi

