def change_lifecycle_state()

in mds_plugin/mysql_database_service.py [0:0]


def change_lifecycle_state(**kwargs):
    """Starts or stops the DbSystem with the given id

    If no id is given, it will prompt the user for the id.

    Args:
        **kwargs: Optional parameters

    Keyword Args:
        db_system_name (str): The name of the DB System.
        db_system_id (str): OCID of the DbSystem.
        await_completion (bool): Whether to wait till the DbSystem reaches
            the desired lifecycle state
        ignore_current (bool): Whether to not default to the current bastion.
        compartment_id (str): OCID of the parent compartment
        config (dict): An OCI config object or None
        config_profile (str): The name of an OCI config profile
        interactive (bool): Indicates whether to execute in interactive mode
        raise_exceptions (bool): If true exceptions are raised
        action (int): The action to execute


    Returns:
       None
    """
    db_system_name = kwargs.get("db_system_name")
    db_system_id = kwargs.get("db_system_id")
    await_completion = kwargs.get("await_completion")
    ignore_current = kwargs.get("ignore_current", False)

    compartment_id = kwargs.get("compartment_id")
    config = kwargs.get("config")
    config_profile = kwargs.get("config_profile")

    interactive = kwargs.get("interactive", core.get_interactive_default())
    raise_exceptions = kwargs.get("raise_exceptions", not interactive)

    action = kwargs.get("action")

    if action == DB_SYSTEM_ACTION_START or action == HW_CLUSTER_ACTION_START:
        action_name = "start"
        action_state = "ACTIVE"
    elif action == DB_SYSTEM_ACTION_STOP or action == HW_CLUSTER_ACTION_STOP:
        action_name = "stop"
        action_state = "INACTIVE"
    elif action == DB_SYSTEM_ACTION_RESTART or action == HW_CLUSTER_ACTION_RESTART:
        action_name = "restart"
        action_state = "ACTIVE"
    else:
        raise ValueError("Unknown action given.")

    db_system_action = (
        action == DB_SYSTEM_ACTION_START or action == DB_SYSTEM_ACTION_STOP or action == DB_SYSTEM_ACTION_RESTART
    )

    action_obj = "DB System" if db_system_action else "HeatWave Cluster"

    # Get the active config and compartment
    try:
        config = configuration.get_current_config(
            config=config, config_profile=config_profile,
            interactive=interactive)
        compartment_id = configuration.get_current_compartment_id(
            compartment_id=compartment_id, config=config)

        # Get the active config and compartment
        try:
            import oci.mysql
            import mysqlsh

            db_system = get_db_system(
                db_system_name=db_system_name, db_system_id=db_system_id,
                compartment_id=compartment_id, config=config,
                interactive=interactive, raise_exceptions=raise_exceptions,
                ignore_current=ignore_current,
                return_python_object=True)
            if db_system is None:
                if db_system_name or db_system_id:
                    raise ValueError("DB System not found.")
                else:
                    raise Exception("Cancelling operation.")
            else:
                db_system_id = db_system.id

            if interactive:
                # Prompt the user for specifying a compartment
                prompt = mysqlsh.globals.shell.prompt(
                    f"Are you sure you want to {action_name} the {action_obj} "
                    f"{db_system.display_name} [yes/NO]: ",
                    {'defaultValue': 'no'}).strip().lower()

                if prompt != "yes":
                    print("Operation cancelled.\n")
                    return

            # Get DbSystem Client
            db_sys = core.get_oci_db_system_client(config=config)
            work_request_id = None

            if action == DB_SYSTEM_ACTION_STOP:
                # Stop the DB System
                work_request_id = db_sys.stop_db_system(
                    db_system_id,
                    oci.mysql.models.StopDbSystemDetails(
                        shutdown_type="IMMEDIATE"
                    )).headers["opc-work-request-id"]
            elif action == DB_SYSTEM_ACTION_START:
                # Start the DB System
                work_request_id = db_sys.start_db_system(db_system_id).headers["opc-work-request-id"]
            elif action == DB_SYSTEM_ACTION_RESTART:
                # Restart the DB System
                work_request_id = db_sys.restart_db_system(
                    db_system_id,
                    oci.mysql.models.RestartDbSystemDetails(
                        shutdown_type="IMMEDIATE"
                    ))
            elif action == HW_CLUSTER_ACTION_STOP:
                # Stop the HW Cluster
                work_request_id = db_sys.stop_heat_wave_cluster(db_system_id).headers["opc-work-request-id"]
            elif action == HW_CLUSTER_ACTION_START:
                # Start the HW Cluster
                work_request_id = db_sys.start_heat_wave_cluster(db_system_id).headers["opc-work-request-id"]
            elif action == HW_CLUSTER_ACTION_RESTART:
                # Restart the HW Cluster
                work_request_id = db_sys.restart_heat_wave_cluster(db_system_id).headers["opc-work-request-id"]

            # If the function should wait till the bastion reaches the correct
            # lifecycle state

            if await_completion:
                if db_system_action:
                    await_lifecycle_state(
                        db_system_id, action_state, action_name,
                        config, interactive, work_request_id)
                else:
                    await_hw_cluster_lifecycle_state(
                        db_system_id, action_state, action_name,
                        config, interactive, work_request_id)
            elif interactive:
                print(f"MySQL {action_obj} '{db_system.display_name}' is being "
                    f"{action_name}{'p' if action_name == 'stop' else ''}ed.")

        except oci.exceptions.ServiceError as e:
            if interactive:
                raise
            print(f'ERROR: {e.message}. (Code: {e.code}; Status: {e.status})')
            return
    except Exception as e:
        if raise_exceptions:
            raise
        print(f'ERROR: {e}')