#!/usr/bin/env python3

# ------------------------------------------------------------------------
# Copyright 2020 IBM Corp. All Rights Reserved.
#
# Licensed 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.
# ------------------------------------------------------------------------

""" Get the status of a pod """

# Global modules

import sys

# Local modules

from modules.args    import (
    getCommonArgsParser,
    addArgHelperUser
)
from modules.command import CmdShell
from modules.config  import getConfig
from modules.logger  import setupLogging
from modules.tools   import ocLogin

# Functions


def _getArgs():
    """ Get command line arguments """
    parser = getCommonArgsParser(
        'Get the status of a pod'
    )

    addArgHelperUser(parser, False)

    return parser.parse_args()


def _getPodStatus(config):
    """ Get status of the pod """
    out = CmdShell().run(
        'oc get pods'
        + f' | grep soos-{config.flavor.nws4.sid.lower()}'
    ).out

    if out:
        return out.split()[2].strip()
    return 'undefined'

# ----------------------------------------------------------------------


def _main():
    """ Main function """

    args = _getArgs()

    setupLogging(args)

    config = getConfig(args.config_file, True)

    ocLogin(config)

    print(f'Status of Pod soos-{config.flavor.nws4.sid.lower()}: {_getPodStatus(config)}')

# ----------------------------------------------------------------------


if __name__ == '__main__':
    _main()
    sys.exit(0)
