get_switch_list
Retrieve a list of switches for Sakura Cloud infrastructure by specifying the zone, such as "tk1v", "is1a", or "tk1a", to manage network resources effectively.
Instructions
Get list of switches
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| zone | No | The zone to use (e.g., "tk1v", "is1a", "tk1a"). Defaults to "tk1v" if not specified. |
Implementation Reference
- src/server.ts:1470-1488 (handler)The main handler logic for the 'get_switch_list' tool within the CallToolRequestSchema handler. It validates credentials, fetches the switch list from the Sakura Cloud API using the shared fetchFromSakuraCloud helper, and returns the result as JSON text.} else if (request.params.name === 'get_switch_list') { try { validateCredentials(); const zone = request.params.arguments?.zone as string || DEFAULT_ZONE; const switchList = await fetchFromSakuraCloud(`/switch`, false, zone); return { content: [ { type: 'text', text: JSON.stringify(switchList, null, 2) } ] }; } catch (error) { console.error('Error calling tool:', error); throw error; }
- src/server.ts:774-784 (registration)Registration of the 'get_switch_list' tool in the ListToolsRequestSchema handler, including name, description, and input schema definition.name: 'get_switch_list', description: 'Get list of switches', inputSchema: { type: 'object', properties: { zone: { type: 'string', description: 'The zone to use (e.g., "tk1v", "is1a", "tk1a"). Defaults to "tk1v" if not specified.' } }, }
- src/server.ts:777-783 (schema)Input schema definition for the 'get_switch_list' tool, specifying optional zone parameter.type: 'object', properties: { zone: { type: 'string', description: 'The zone to use (e.g., "tk1v", "is1a", "tk1a"). Defaults to "tk1v" if not specified.' } },
- src/server.ts:20-72 (helper)Shared helper function fetchFromSakuraCloud used by the get_switch_list handler to perform authenticated HTTPS requests to the Sakura Cloud API endpoint '/switch'.async function fetchFromSakuraCloud(path: string, isPublicAPI: boolean = false, zone: string = DEFAULT_ZONE, method: string = 'GET', bodyData?: any): Promise<any> { return new Promise((resolve, reject) => { const basePath = isPublicAPI ? '/cloud/api/cloud/1.1' : `/cloud/zone/${zone}/api/cloud/1.1`; const options = { hostname: 'secure.sakura.ad.jp', port: 443, path: `${basePath}${path}`, method: method, headers: { 'Accept': 'application/json', 'Authorization': '', 'Content-Type': 'application/json' } }; // Add authorization for non-public APIs if (!isPublicAPI) { options.headers['Authorization'] = `Basic ${Buffer.from(`${SACLOUD_API_TOKEN}:${SACLOUD_API_SECRET}`).toString('base64')}`; } const req = https.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { if (data) { const parsedData = JSON.parse(data); resolve(parsedData); } else { resolve({}); } } catch (err) { reject(new Error(`Failed to parse response: ${err}`)); } }); }); req.on('error', (error) => { reject(error); }); if (bodyData && (method === 'POST' || method === 'PUT')) { req.write(JSON.stringify(bodyData)); } req.end(); }); }
- src/server.ts:125-129 (helper)Helper function validateCredentials called by the get_switch_list handler to ensure API credentials are set.function validateCredentials(): void { if (!SACLOUD_API_TOKEN || !SACLOUD_API_SECRET) { throw new Error('Missing API credentials. Set SACLOUD_API_TOKEN and SACLOUD_API_SECRET environment variables.'); } }