#!/usr/local/sbin/charm-env bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

if ! charms.reactive is_state 'hbase.installed'; then
    action-fail 'HBase is not yet ready'
    exit
fi

tbl=$1`date +%s`
echo "create '$tbl', 'cf'" > hbase_table.txt
echo "list '$tbl'" >> hbase_table.txt
echo "put '$tbl', 'row1', 'cf:a', 'value1'" >> hbase_table.txt
echo "put '$tbl', 'row2', 'cf:b', 'value2'" >> hbase_table.txt
echo "put '$tbl', 'row3', 'cf:c', 'value3'" >> hbase_table.txt
echo "put '$tbl', 'row4', 'cf:d', 'value4'" >> hbase_table.txt
echo "scan '$tbl'" >> hbase_table.txt
echo "get '$tbl', 'row1'" >> hbase_table.txt
echo "disable '$tbl'" >> hbase_table.txt
echo "enable '$tbl'" >> hbase_table.txt
echo "exit;" >> hbase_table.txt

hbase shell hbase_table.txt

echo "describe '$tbl'" | hbase shell
status=$?
echo "HBase shell return code was: " $status

if [ $status -eq 0 ]; then
  action-set outcome="success"
else
  action-set outcome="failure"
  action-fail "smoke-test failed with return code ${status}"
fi
