from argparse import _SubParsersAction
from enum import Enum
from functools import partial
from typing import List
from lxml.objectify import BoolElement, IntElement, StringElement
from q2_sdk.core.cli.textui import colored, puts
from q2_sdk.core.dynamic_imports import (
api_ExecuteStoredProcedure as ExecuteStoredProcedure,
)
from q2_sdk.entrypoints import invalidate_hq_cache
from .db_object import DbObject
from .representation_row_base import RepresentationRowBase
D_TYPES = ExecuteStoredProcedure.DataType
[docs]
class ExternalAuthenticationConfigRow(RepresentationRowBase):
ExternalAuthenticationConfigID: IntElement = "ExternalAuthenticationConfigID"
ShortName: StringElement = "ShortName"
Enabled: BoolElement = "Enabled"
[docs]
class Action(Enum):
Enable = "enable"
Disable = "disable"
[docs]
class ExternalAuth(DbObject):
# GET_BY_NAME_KEY = "column in the db response"
NAME = "ExternalAuth"
[docs]
def add_arguments(self, parser: _SubParsersAction):
subparser = parser.add_parser("get_external_auth_config")
subparser.set_defaults(parser="get_configs")
subparser.set_defaults(func=partial(self.get_configs, serialize_for_cli=True))
subparser = parser.add_parser("set_external_auth_config")
subparser.set_defaults(parser="set_config")
subparser.set_defaults(func=partial(self.set_config, serialize_for_cli=True))
subparser.add_argument(
"short_name", help="Q2.ExternalAuthenticationConfig.ShortName"
)
subparser.add_argument(
"--enabled",
action="store_true",
help="Q2.ExternalAuthenticationConfig.Enabled",
)
[docs]
async def get_configs(
self, serialize_for_cli=False
) -> List[ExternalAuthenticationConfigRow]:
response = await self.call_hq("sdk_GetExternalAuthConfiguration")
if serialize_for_cli:
columns = [
"ExternalAuthenticationConfigID",
"ShortName",
"Enabled",
]
response = self.serialize_for_cli(response, columns)
return response
[docs]
async def set_config(self, short_name: str, enabled: bool, serialize_for_cli=False):
if serialize_for_cli:
match enabled:
case True:
puts(colored.yellow(f"Enabling {short_name}"))
case False:
puts(colored.yellow(f"Disabling {short_name}"))
await self.call_hq(
"sdk_UpdateExternalAuthConfiguration",
ExecuteStoredProcedure.SqlParameters([
ExecuteStoredProcedure.SqlParam(
D_TYPES.VarChar, "short_name", short_name
),
ExecuteStoredProcedure.SqlParam(D_TYPES.Bit, "enabled", enabled),
]),
)
await invalidate_hq_cache.invalidate(
self.logger,
self.hq_credentials,
refresh_type=["ExternalAuthenticationConfig"],
)