-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Add network router interface tools #83
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 1 commit
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |||
| Network, | ||||
| Port, | ||||
| Router, | ||||
| RouterInterface, | ||||
| Subnet, | ||||
| ) | ||||
|
|
||||
|
|
@@ -52,6 +53,9 @@ def register_tools(self, mcp: FastMCP): | |||
| mcp.tool()(self.get_router_detail) | ||||
| mcp.tool()(self.update_router) | ||||
| mcp.tool()(self.delete_router) | ||||
| mcp.tool()(self.add_router_interface) | ||||
| mcp.tool()(self.get_router_interfaces) | ||||
| mcp.tool()(self.remove_router_interface) | ||||
|
|
||||
| def get_networks( | ||||
| self, | ||||
|
|
@@ -1038,6 +1042,93 @@ def delete_router(self, router_id: str) -> None: | |||
| conn.network.delete_router(router_id, ignore_missing=False) | ||||
| return None | ||||
|
|
||||
| def add_router_interface( | ||||
| self, | ||||
| router_id: str, | ||||
| subnet_id: str | None = None, | ||||
| port_id: str | None = None, | ||||
| ) -> RouterInterface: | ||||
| """ | ||||
| Add an interface to a Router by subnet or port. | ||||
| Provide either subnet_id or port_id. | ||||
|
|
||||
| :param router_id: Target router ID | ||||
| :param subnet_id: Subnet ID to attach (mutually exclusive with port_id) | ||||
| :param port_id: Port ID to attach (mutually exclusive with subnet_id) | ||||
| :return: Created/attached router interface information as RouterInterface | ||||
| """ | ||||
| conn = get_openstack_conn() | ||||
| args: dict = {} | ||||
| if subnet_id is not None: | ||||
| args["subnet_id"] = subnet_id | ||||
| if port_id is not None: | ||||
| args["port_id"] = port_id | ||||
| res = conn.network.add_interface_to_router(router_id, **args) | ||||
| return RouterInterface( | ||||
| router_id=res.get("router_id", router_id), | ||||
| port_id=res.get("port_id"), | ||||
| subnet_id=res.get("subnet_id"), | ||||
| ) | ||||
|
|
||||
| def get_router_interfaces(self, router_id: str) -> list[RouterInterface]: | ||||
| """ | ||||
| List interfaces attached to a Router. | ||||
|
|
||||
| :param router_id: Target router ID | ||||
| :return: List of RouterInterface objects representing router-owned ports | ||||
| """ | ||||
| conn = get_openstack_conn() | ||||
| filters = { | ||||
| "device_id": router_id, | ||||
| "device_owner": "network:router_interface", | ||||
| } | ||||
| ports = conn.network.ports(**filters) | ||||
| result: list[RouterInterface] = [] | ||||
| for p in ports: | ||||
| subnet_id = None | ||||
| if getattr(p, "fixed_ips", None): | ||||
| first = p.fixed_ips[0] | ||||
| if isinstance(first, dict): | ||||
| subnet_id = first.get("subnet_id") | ||||
| else: | ||||
| subnet_id = None | ||||
|
||||
| subnet_id = None |
반복문 시작 시 subnet_id=None으로 초기화하므로, else 블록의 초기화 로직은 제거해도 될 것 같습니다.
Collaborator
Author
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.
넵! 반영되었습니다 ㅎㅎ
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
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.
제가 보기엔 다 훌륭하신데 궁금해서 댓글 드립니다.
_proxy.py를 확인해보니 subnet_id와 port_id를 전달하지 않아도 None으로 처리하던데, not None 조건문을 태우는게 더 우아한방식일까요?
Uh oh!
There was an error while loading. Please reload this page.
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.
프록시 전달 목적의 argument를 만들기 위해 처리되는 로직입니다.
tools는 string으로 받지만 프록시 인터페이스는 dict로 받고 있으니, 형변환 과정이라고 봐주시면 되겠습니다.
(보충설명)
조금 이야기를 보태자면, 라우터에 인터페이스를 추가하기 위해서는 port나 subnet 중 무조건 둘 중에 하나를 지정해야 합니다.
서브넷을 지정하는 경우 해당 서브넷에서 포트를 생성해서 라우터에 붙이는 방식이고,
포트를 지정하는 경우는 별도 과정 없이 라우터에 붙입니다.
즉, 확인하신 로직은 포트와 서브넷이 바디에 있을때, 어느것을 우선으로 지정해서 보낼건지 결정하는 부분입니다.
Uh oh!
There was an error while loading. Please reload this page.
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.
서브넷 지정시 포트가 생성되는지 몰랐네요~ 보충설명까지 감사합니다!!
근데 제가 질문이 구체적이지 않아 의도 전달이 불충분하지 않았나 한데요
현재 코드에서
부분을
로 작성한다면 openstacksdk/.../_proxy.py의 add_interface_to_router 함수가 실행될 때 해당 함수가 subnet_id, port_id 매개변수를 기본값 None으로 설정하기때문에 두 코드 다 결론적으로는 같은 동작을 할 것으로 보이는데,
그래도 if문을 통해 철옹성 같은?ㅎㅎ 안전한 코드를 짜는게 좋은가 궁금하여 질문드렸습니다
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.
이부분은 수정해서 subnet, port 둘 다 테스트해보니 잘 되네요.
반영하도록 하겠습니다!