-
Notifications
You must be signed in to change notification settings - Fork 6
feat: connection manager related tool #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,16 @@ | ||
| import openstack | ||
| from openstack_mcp_server.tools.connection import ConnectionManager | ||
|
|
||
| from openstack import connection | ||
|
|
||
| from openstack_mcp_server import config | ||
| _connection_manager = ConnectionManager() | ||
|
|
||
|
|
||
| class OpenStackConnectionManager: | ||
| """OpenStack Connection Manager""" | ||
|
|
||
| _connection: connection.Connection | None = None | ||
|
|
||
| @classmethod | ||
| def get_connection(cls) -> connection.Connection: | ||
| """OpenStack Connection""" | ||
| if cls._connection is None: | ||
| openstack.enable_logging(debug=config.MCP_DEBUG_MODE) | ||
| cls._connection = openstack.connect(cloud=config.MCP_CLOUD_NAME) | ||
| return cls._connection | ||
| def get_openstack_conn(): | ||
| return _connection_manager.get_connection() | ||
|
|
||
|
|
||
| _openstack_connection_manager = OpenStackConnectionManager() | ||
| def set_openstack_cloud_name(cloud_name: str) -> None: | ||
| _connection_manager.set_cloud_name(cloud_name) | ||
|
|
||
|
|
||
| def get_openstack_conn(): | ||
| """Get OpenStack Connection""" | ||
| return _openstack_connection_manager.get_connection() | ||
| def get_openstack_cloud_name() -> str: | ||
| return _connection_manager.get_cloud_name() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import openstack | ||
|
|
||
| from fastmcp import FastMCP | ||
| from openstack import connection | ||
| from openstack.config.loader import OpenStackConfig | ||
|
|
||
| from openstack_mcp_server import config | ||
|
|
||
|
|
||
| class ConnectionManager: | ||
| _cloud_name = config.MCP_CLOUD_NAME | ||
|
|
||
| def __init__(self): | ||
| openstack.enable_logging(debug=config.MCP_DEBUG_MODE) | ||
|
|
||
| def register_tools(self, mcp: FastMCP): | ||
| mcp.tool(self.get_cloud_config) | ||
| mcp.tool(self.get_cloud_names) | ||
| mcp.tool(self.get_cloud_name) | ||
| mcp.tool(self.set_cloud_name) | ||
|
|
||
| def get_connection(self) -> connection.Connection: | ||
| return openstack.connect(cloud=self._cloud_name) | ||
|
|
||
| def get_cloud_names(self) -> list[str]: | ||
| """List available cloud configurations. | ||
|
|
||
| :return: Names of OpenStack clouds from user's config file. | ||
| """ | ||
| config = OpenStackConfig() | ||
| return list(config.get_cloud_names()) | ||
|
|
||
| def get_cloud_config(self) -> dict: | ||
| """Provide cloud configuration with secrets masked of current user's config file. | ||
|
|
||
| :return: Cloud configuration dictionary with credentials masked. | ||
| """ | ||
| config = OpenStackConfig() | ||
| return ConnectionManager._mask_credential( | ||
| config.cloud_config, ["password"] | ||
| ) | ||
|
|
||
| @staticmethod | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. staticmethod와 classmethod를 사용하신 이유가 궁금합니다.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mask하는 경우 인스턴스 변수를 사용하지 않아 staticmethod로도 충분하다고 생각했습니다.
나머지 함수들은 |
||
| def _mask_credential( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 비밀번호를 없애는 것이 아닌 마스킹하신 이유가 궁금합니다.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clouds.yaml에는 비밀번호 없이도 입력이 가능한데, 그것과 구분하기 위해 마스킹하도록 하였습니다. |
||
| config_dict: dict, credential_keys: list[str] | ||
| ) -> dict: | ||
| masked = {} | ||
| for k, v in config_dict.items(): | ||
| if k in credential_keys: | ||
| masked[k] = "****" | ||
| elif isinstance(v, dict): | ||
| masked[k] = ConnectionManager._mask_credential( | ||
| v, credential_keys | ||
| ) | ||
| else: | ||
| masked[k] = v | ||
| return masked | ||
|
|
||
| @classmethod | ||
| def get_cloud_name(cls) -> str: | ||
| """Return the currently selected cloud name. | ||
|
|
||
| :return: current OpenStack cloud name. | ||
| """ | ||
| return cls._cloud_name | ||
|
|
||
| @classmethod | ||
| def set_cloud_name(cls, cloud_name: str) -> None: | ||
| """Set cloud name to use for later connections. Must set name from currently valid cloud config file. | ||
|
|
||
| :param cloud_name: Name of the OpenStack cloud profile to activate. | ||
| """ | ||
| cls._cloud_name = cloud_name | ||
Uh oh!
There was an error while loading. Please reload this page.