OpenStackClient Plugin Library

4.4.0 · active · verified Sun Apr 12

osc-lib is a Python library that provides common support modules for writing plugins for the OpenStackClient (OSC), the unified command-line client for OpenStack. It offers functionalities like command definitions, session management, and utility functions that plugin developers can leverage. The library is actively maintained and currently at version 4.4.0, with regular updates aligning with OpenStack development cycles.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a custom OpenStackClient command using `osc-lib`. It outlines extending `osc_lib.command.command.Command` to create a new CLI action, including argument parsing and action execution. Note that `osc-lib` is primarily for building plugins, so this code requires an OpenStackClient environment to run fully, but the example shows the core structure for plugin development. The `MockApp` and `MockAppArgs` classes are included to make the example runnable for demonstrating the `take_action` logic, though they don't replicate a full OpenStackClient runtime environment.

import argparse
from osc_lib.command.command import Command

class MyCustomCommand(Command):
    """My custom OpenStackClient command."""

    def get_parser(self, prog_name):
        parser = super(MyCustomCommand, self).get_parser(prog_name)
        parser.add_argument(
            'name',
            metavar='<name>',
            help='Name to greet'
        )
        return parser

    def take_action(self, parsed_args):
        # In a real OpenStackClient plugin, 'self.app' and 'self.client_manager'
        # would provide access to OpenStack services and configuration.
        # For this example, we'll simulate the output.
        greeting = f"Hello, {parsed_args.name}! This is a custom command from osc-lib."
        return {}, {'Result': greeting}

# Example of how to instantiate and call (for demonstration, not runnable without OSC context)
if __name__ == '__main__':
    # Simulate `app` and `app_args` that `cliff` (which OSC uses) would pass
    class MockApp:
        NAME = 'openstack'
        def __init__(self):
            self.options = argparse.Namespace()
            self.options.debug = False
    
    class MockAppArgs:
        pass

    mock_app = MockApp()
    mock_app_args = MockAppArgs()

    cmd_instance = MyCustomCommand(mock_app, mock_app_args)
    
    # Simulate parsed arguments
    parsed_args = cmd_instance.get_parser('my_command').parse_args(['World'])
    
    # Execute the command's action
    # In a real setup, this would be called by the OpenStackClient framework
    _columns, _data = cmd_instance.take_action(parsed_args)
    print(f"Command output: {_data['Result']}")

view raw JSON →