Test connectivity to the Nacos server. Args: namespace: Nacos namespace ID to test connectivity with. Returns: Dict containing: - success: Whether the connection was successful - message: Human-readable message about the result
(
self,
namespace: str = "public"
)
| 514 | return False |
| 515 | |
| 516 | async def test_connectivity( |
| 517 | self, |
| 518 | namespace: str = "public" |
| 519 | ) -> Dict[str, Any]: |
| 520 | """Test connectivity to the Nacos server. |
| 521 | |
| 522 | Args: |
| 523 | namespace: Nacos namespace ID to test connectivity with. |
| 524 | |
| 525 | Returns: |
| 526 | Dict containing: |
| 527 | - success: Whether the connection was successful |
| 528 | - message: Human-readable message about the result |
| 529 | """ |
| 530 | try: |
| 531 | session = await self._get_session() |
| 532 | |
| 533 | access_token = None |
| 534 | if self.username and self.password: |
| 535 | access_token = await self._get_access_token(session) |
| 536 | if not access_token: |
| 537 | return { |
| 538 | "success": False, |
| 539 | "message": "Authentication failed. Please check username and password." |
| 540 | } |
| 541 | |
| 542 | url = f"{self.nacos_addr}/nacos/v3/admin/ns/ops/metrics" |
| 543 | headers = {} |
| 544 | if access_token: |
| 545 | headers["AccessToken"] = access_token |
| 546 | |
| 547 | async with session.get(url, headers=headers) as response: |
| 548 | if response.status == 200: |
| 549 | data = await response.json() |
| 550 | if data.get("code") == 0: |
| 551 | return { |
| 552 | "success": True, |
| 553 | "message": "Successfully connected to Nacos server" |
| 554 | } |
| 555 | else: |
| 556 | return { |
| 557 | "success": False, |
| 558 | "message": f"Nacos API error: {data.get('message', 'unknown')}" |
| 559 | } |
| 560 | elif response.status == 403: |
| 561 | return { |
| 562 | "success": False, |
| 563 | "message": "Authentication failed. Please check username and password." |
| 564 | } |
| 565 | else: |
| 566 | text = await response.text() |
| 567 | return { |
| 568 | "success": False, |
| 569 | "message": f"Nacos server returned status {response.status}: {text}" |
| 570 | } |
| 571 | |
| 572 | except aiohttp.ClientError as e: |
| 573 | logger.error(f"Failed to connect to Nacos at {self.nacos_addr}: {e}") |
no test coverage detected