Skip to content

Commit a70a97e

Browse files
committed
Resolving review comments
- Changed plugin base from 'linode-cli get_metrics' to 'linode-cli monitor-api' with 'get-metrics' as a subcommand - Refactored API_BASE_URL to be more generic, allowing API version specification - Updated all help text and examples to reflect new command structure - Updated unit and integration tests accordingly
1 parent 79f90d7 commit a70a97e

3 files changed

Lines changed: 149 additions & 150 deletions

File tree

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"""
2-
This plugin allows users to query metrics from the monitoring service for various services.
2+
This plugin provides access to the Linode Monitor API.
33
4+
Commands:
5+
get-metrics: Query metrics from the monitoring service for various services.
6+
7+
Usage:
8+
linode-cli monitor-api get-metrics <service> [options]
49
"""
510

611
import json
@@ -16,10 +21,11 @@
1621
from linodecli.help_formatter import SortingHelpFormatter
1722
from linodecli.helpers import register_debug_arg
1823

19-
PLUGIN_BASE = "linode-cli get_metrics"
24+
PLUGIN_BASE = "linode-cli monitor-api"
2025

2126
# API Configuration
22-
API_BASE_URL = "https://monitor-api.linode.com/v2/monitor/services"
27+
API_BASE_URL = "https://monitor-api.linode.com"
28+
API_VERSION = "v2"
2329

2430

2531
def get_auth_token():
@@ -80,7 +86,7 @@ def make_api_request(
8086
Returns:
8187
Tuple of (status_code, response_data)
8288
"""
83-
url = f"{API_BASE_URL}/{service_name}/{endpoint}"
89+
url = f"{API_BASE_URL}/{API_VERSION}/monitor/services/{service_name}/{endpoint}"
8490

8591
headers = {
8692
"Authorization": f"Bearer {token or get_auth_token()}",
@@ -263,7 +269,7 @@ def print_help(parser: ArgumentParser):
263269
print("\nExamples:")
264270
print(" # Get metrics with relative time duration")
265271
print(
266-
" linode-cli get_metrics dbaas --entity-ids 123 --duration 15 "
272+
" linode-cli monitor-api get-metrics dbaas --entity-ids 123 --duration 15 "
267273
"--duration-unit min --metrics cpu_usage:avg"
268274
)
269275

@@ -272,42 +278,42 @@ def print_help(parser: ArgumentParser):
272278
"(only allowed for objectstorage service)"
273279
)
274280
print(
275-
" linode-cli get_metrics objectstorage --duration 15 "
281+
" linode-cli monitor-api get-metrics objectstorage --duration 15 "
276282
"--duration-unit min --metrics obj_requests_num:avg "
277283
"--entity-region us-east-1"
278284
)
279285

280286
print("\n # Get metrics with absolute time duration")
281287
print(
282-
" linode-cli get_metrics dbaas --entity-ids 123 "
288+
" linode-cli monitor-api get-metrics dbaas --entity-ids 123 "
283289
"--start-time 2024-10-10T00:00:01Z --end-time 2024-10-10T23:59:59Z "
284290
"--metrics cpu_usage:avg,memory_usage:sum"
285291
)
286292

287293
print("\n # Get metrics with filters")
288294
print(
289-
" linode-cli get_metrics dbaas --entity-ids 123 --duration 15 "
295+
" linode-cli monitor-api get-metrics dbaas --entity-ids 123 --duration 15 "
290296
"--duration-unit min --metrics cpu_usage:avg "
291297
"--filters 'node_type:in:primary,secondary'"
292298
)
293299

294300
print("\n # Get metrics with multiple filters")
295301
print(
296-
" linode-cli get_metrics dbaas --entity-ids 123 --duration 15 "
302+
" linode-cli monitor-api get-metrics dbaas --entity-ids 123 --duration 15 "
297303
"--duration-unit min --metrics cpu_usage:avg "
298304
"--filters 'node_type:in:primary,secondary;status:eq:active'"
299305
)
300306

301307
print("\n # Get metrics with granularity")
302308
print(
303-
" linode-cli get_metrics netloadbalancer --entity-ids 123 "
309+
" linode-cli monitor-api get-metrics netloadbalancer --entity-ids 123 "
304310
"--duration 1 --duration-unit hour --metrics nlb_ingress_traffic:sum "
305311
"--granularity 10 --granularity-unit min"
306312
)
307313

308314
print("\n # Get metrics with entity region (required ObjectStorage)")
309315
print(
310-
" linode-cli get_metrics objectstorage --entity-region us-east-1 "
316+
" linode-cli monitor-api get-metrics objectstorage --entity-region us-east-1 "
311317
"--duration 15 --duration-unit min --metrics obj_requests_num:sum"
312318
)
313319

@@ -316,7 +322,7 @@ def print_help(parser: ArgumentParser):
316322
"(mandatory for cloud firewall service)"
317323
)
318324
print(
319-
" linode-cli get_metrics firewall --entity-region us-east-1 "
325+
" linode-cli monitor-api get-metrics firewall --entity-region us-east-1 "
320326
"--associated-entity-region us-west-1 --duration 15 "
321327
"--duration-unit min --metrics fw_active_connections:sum"
322328
)
@@ -332,12 +338,19 @@ def get_metrics_parser():
332338

333339
register_debug_arg(parser)
334340

335-
# Service name as positional argument
341+
# Command as first positional argument
342+
parser.add_argument(
343+
"command",
344+
nargs="?",
345+
help="Command to execute (get-metrics)",
346+
)
347+
348+
# Service name as second positional argument
336349
parser.add_argument(
337350
"service",
338351
nargs="?",
339352
help="Service name (Dbaas, Nodebalancer, NetLoadBalancer, Linode, "
340-
"Firewall, ObjectStorage, Blockstorage,LKE)",
353+
"Firewall, ObjectStorage, Blockstorage, LKE)",
341354
)
342355

343356
# Optional arguments for get-metrics functionality
@@ -457,10 +470,23 @@ def call(args, context=None): # pylint: disable=unused-argument
457470
parsed, remaining_args = parser.parse_known_args(args)
458471

459472
# Handle help cases
460-
if not parsed.service or parsed.service == "help" or "--help" in args:
473+
if not parsed.command or parsed.command == "help" or "--help" in args:
461474
print_help(parser)
462475
sys.exit(ExitCodes.SUCCESS)
463476

477+
# Validate command
478+
if parsed.command != "get-metrics":
479+
print(f"Unknown command: {parsed.command}", file=sys.stderr)
480+
print("Available commands: get-metrics", file=sys.stderr)
481+
print_help(parser)
482+
sys.exit(ExitCodes.REQUEST_FAILED)
483+
484+
# Validate service is provided
485+
if not parsed.service:
486+
print("Service name is required", file=sys.stderr)
487+
print_help(parser)
488+
sys.exit(ExitCodes.REQUEST_FAILED)
489+
464490
if remaining_args:
465491
print(f"Unknown arguments: {' '.join(remaining_args)}", file=sys.stderr)
466492
print_help(parser)

tests/integration/monitor/test_plugin_get_metrics.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
import json
2-
import os
3-
4-
import pytest
5-
6-
from tests.integration.helpers import (
7-
exec_failing_test_command,
8-
exec_test_command,
9-
get_random_text,
10-
)
11-
from linodecli.exit_codes import ExitCodes
12-
13-
# Base command for get_metrics plugin
14-
BASE_CMD = ["linode-cli", "get_metrics"]
15-
16-
171
"""
182
Integration tests for the get_metrics plugin
193
"""
@@ -28,8 +12,8 @@
2812
)
2913
from linodecli.exit_codes import ExitCodes
3014

31-
# Base command for get_metrics plugin
32-
BASE_CMD = ["linode-cli", "get_metrics"]
15+
# Base command for monitor-api plugin
16+
BASE_CMD = ["linode-cli", "monitor-api", "get-metrics"]
3317

3418

3519
def test_missing_required_args():

0 commit comments

Comments
 (0)