Added enum type in the parser file

added enum type with the aim to use a circuit-breaker logic implemented
by the argparser itself, with a fail-fast behavior when trying to cast
the input parameters with values different than the ones expected.

Test Plan:
PASS: Run install_vbox.py with expected values for modified parameters

Failure Path:
PASS: Run install_vbox.py with non-expected values fails right away

Task: 47907
Story: 2005051
Change-Id: I30d283947200019d2472a7fdd8804026db924155
Signed-off-by: Tomás Barros <tomas.barros@encora.com>
Signed-off-by: Bruno Muniz <bruno.muniz@encora.com>
This commit is contained in:
Tomás Barros 2023-05-15 14:30:12 -03:00 committed by Lindley Werner
parent 252481411f
commit 9ff33116f2

View File

@ -1,5 +1,5 @@
# pylint: disable=invalid-name
#!/usr/bin/python3
# !/usr/bin/python3
#
# SPDX-License-Identifier: Apache-2.0
#
@ -7,10 +7,56 @@
"""
Parser to handle command line arguments
"""
import argparse
import re
from argparse import ArgumentParser, RawTextHelpFormatter
import getpass
from enum import Enum
class StringEnum(Enum):
"""This class aims to serve as a parent class for a parser
type checker"""
@classmethod
def get_members_values(cls):
"""Static method that returns a list of enum members values"""
return [v.value for v in cls.__members__.values()]
class SetupType(StringEnum):
"""Class aimed to help parser choose between installation
predefined types"""
AIO_SX = 'AIO-SX'
AIO_DX = 'AIO-DX'
STANDARD = 'STANDARD'
STORAGE = 'STORAGE'
class VboxNetworkType(StringEnum):
"""Class aimed to help parser choose between virtual box
network types"""
HOST_ONLY = 'hostonly'
NAT = 'nat'
class SecurityProfile(StringEnum):
"""Class aimed to help parser choose between
security profile types"""
STANDARD = 'standard'
EXTENDED = 'extended'
class LabInstallationType(StringEnum):
"""Class aimed to help parser choose between
lab installation types"""
GRAPHICAL = 'graphical'
SERIAL = 'serial'
def validate_password(pwd: str):
@ -55,16 +101,9 @@ def parse_setup_config(parser: ArgumentParser):
"""
Mutate parser with CLI arguments related to the setup type
"""
parser.add_argument("--setup-type", help=
"""
Type of setup:
AIO-SX
AIO-DX
STANDARD
STORAGE
""",
choices=['AIO-SX', 'AIO-DX', 'STANDARD', 'STORAGE'],
type=str,
parser.add_argument("--setup-type",
help=f"Expected value is one of {SetupType.get_members_values()}",
type=SetupType,
required=True)
parser.add_argument("--controllers", help=
"""
@ -174,7 +213,8 @@ def parse_config_location(parser: ArgumentParser):
parser.add_argument("--ansible-controller-config", help=
"""
Path to a local YAML file to be copied as localhost.yml
to the home directory of the controller-0. NOTE: System password value is updated dynamically with user defined --password.
to the home directory of the controller-0.
NOTE: System password value is updated dynamically with user defined --password.
""",
type=str)
parser.add_argument("--vbox-home-dir", help=
@ -244,7 +284,7 @@ def parse_networking(parser: ArgumentParser):
parser.add_argument("--vboxnet-name", help=
"""
Which host only network to use for setup.
Which network to use for setup.
""",
type=str)
parser.add_argument("--vboxnet-ip", help=
@ -258,13 +298,10 @@ def parse_networking(parser: ArgumentParser):
Add a new NAT interface to hosts.
""",
action='store_true')
parser.add_argument("--vboxnet-type", help=
"""
Type of vbox network, either hostonly on nat
""",
choices=['hostonly', 'nat'],
type=str,
default='hostonly')
parser.add_argument("--vboxnet-type",
help=f"Expected value is one of {VboxNetworkType.get_members_values()}",
type=VboxNetworkType,
default=VboxNetworkType.HOST_ONLY)
parser.add_argument("--nat-controller-floating-local-ssh-port", help=
"""
When oam network is configured as 'nat' a port on