-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,21 @@ | ||
| import openstack | ||
|
|
||
| from openstack import connection | ||
|
|
||
| from openstack_mcp_server import config | ||
| from openstack_mcp_server.tools.connection import ConnectionManager | ||
|
|
||
|
|
||
| _connection_manager = ConnectionManager() | ||
|
|
||
| class OpenStackConnectionManager: | ||
| """OpenStack Connection Manager""" | ||
| openstack.enable_logging(debug=config.MCP_DEBUG_MODE) | ||
|
|
||
| _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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| 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 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 | ||
| 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 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
staticmethod와 classmethod를 사용하신 이유가 궁금합니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mask하는 경우 인스턴스 변수를 사용하지 않아 staticmethod로도 충분하다고 생각했습니다.
_cloud_name은 클래스 변수로 인스턴스 전역에서 공유하도록 의도하여 getter, setter도 classmethod로 정의하였습니다.나머지 함수들은
register_tools에서 tool로 등록하기 위해 self 사용하는 인스턴스 메서드로 정의했습니다.