|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Example of using the Usage Alerts API |
| 4 | +# |
| 5 | + |
| 6 | +import os |
| 7 | +import json |
| 8 | +from ns1 import NS1 |
| 9 | +from ns1.config import Config |
| 10 | + |
| 11 | +# Create NS1 client |
| 12 | +config = { |
| 13 | + "endpoint": "https://api.nsone.net", |
| 14 | + "default_key": "test1", |
| 15 | + "keys": { |
| 16 | + "test1": { |
| 17 | + "key": os.environ.get("NS1_APIKEY", "test1"), |
| 18 | + "desc": "test key", |
| 19 | + } |
| 20 | + }, |
| 21 | +} |
| 22 | + |
| 23 | +# Create a config from dictionary and create the client |
| 24 | +c = Config() |
| 25 | +c.loadFromDict(config) |
| 26 | +client = NS1(config=c) |
| 27 | + |
| 28 | +# If no real API key is set, we'll get appropriate errors |
| 29 | +# This is just an example to show the usage pattern |
| 30 | +if not os.environ.get("NS1_APIKEY"): |
| 31 | + print("Using a mock endpoint - for real usage, set the NS1_APIKEY environment variable") |
| 32 | + |
| 33 | + |
| 34 | +# Usage Alerts API Examples |
| 35 | +def usage_alerts_example(): |
| 36 | + print("\n=== Usage Alerts Examples ===\n") |
| 37 | + |
| 38 | + # List all usage alerts |
| 39 | + print("Listing usage alerts:") |
| 40 | + try: |
| 41 | + alerts = client.alerts().usage.list(limit=10) |
| 42 | + print(f"Total alerts: {alerts.get('total_results', 0)}") |
| 43 | + for i, alert in enumerate(alerts.get("results", [])): |
| 44 | + print(f" {i+1}. {alert.get('name')} (id: {alert.get('id')})") |
| 45 | + except Exception as e: |
| 46 | + print(f"Error listing alerts: {e}") |
| 47 | + |
| 48 | + # Create a usage alert |
| 49 | + print("\nCreating a usage alert:") |
| 50 | + try: |
| 51 | + alert = client.alerts().usage.create( |
| 52 | + name="Example query usage alert", |
| 53 | + subtype="query_usage", |
| 54 | + alert_at_percent=85, |
| 55 | + notifier_list_ids=[], |
| 56 | + zone_names=[], |
| 57 | + ) |
| 58 | + alert_id = alert["id"] |
| 59 | + print(f"Created alert: {alert['name']} (id: {alert_id})") |
| 60 | + print(f"Alert details: {json.dumps(alert, indent=2)}") |
| 61 | + except Exception as e: |
| 62 | + print(f"Error creating alert: {e}") |
| 63 | + return |
| 64 | + |
| 65 | + # Update the alert |
| 66 | + print("\nUpdating the alert threshold to 90%:") |
| 67 | + try: |
| 68 | + updated = client.alerts().usage.patch(alert_id, alert_at_percent=90) |
| 69 | + print(f"Updated alert: {updated['name']}") |
| 70 | + print(f"New threshold: {updated['data']['alert_at_percent']}%") |
| 71 | + except Exception as e: |
| 72 | + print(f"Error updating alert: {e}") |
| 73 | + |
| 74 | + # Get alert details |
| 75 | + print("\nGetting alert details:") |
| 76 | + try: |
| 77 | + details = client.alerts().usage.get(alert_id) |
| 78 | + print(f"Alert details: {json.dumps(details, indent=2)}") |
| 79 | + except Exception as e: |
| 80 | + print(f"Error getting alert: {e}") |
| 81 | + |
| 82 | + # Delete the alert |
| 83 | + print("\nDeleting the alert:") |
| 84 | + try: |
| 85 | + client.alerts().usage.delete(alert_id) |
| 86 | + print(f"Alert {alert_id} deleted successfully") |
| 87 | + except Exception as e: |
| 88 | + print(f"Error deleting alert: {e}") |
| 89 | + |
| 90 | + |
| 91 | +# Test validation failures |
| 92 | +def test_validation(): |
| 93 | + print("\n=== Validation Tests ===\n") |
| 94 | + |
| 95 | + # Test invalid subtype |
| 96 | + print("Testing invalid subtype:") |
| 97 | + try: |
| 98 | + client.alerts().usage.create( |
| 99 | + name="Test alert", subtype="invalid_subtype", alert_at_percent=85 |
| 100 | + ) |
| 101 | + except ValueError as e: |
| 102 | + print(f"Validation error (expected): {e}") |
| 103 | + |
| 104 | + # Test threshold too low |
| 105 | + print("\nTesting threshold too low (0):") |
| 106 | + try: |
| 107 | + client.alerts().usage.create( |
| 108 | + name="Test alert", subtype="query_usage", alert_at_percent=0 |
| 109 | + ) |
| 110 | + except ValueError as e: |
| 111 | + print(f"Validation error (expected): {e}") |
| 112 | + |
| 113 | + # Test threshold too high |
| 114 | + print("\nTesting threshold too high (101):") |
| 115 | + try: |
| 116 | + client.alerts().usage.create( |
| 117 | + name="Test alert", subtype="query_usage", alert_at_percent=101 |
| 118 | + ) |
| 119 | + except ValueError as e: |
| 120 | + print(f"Validation error (expected): {e}") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + print("Usage Alerts API Examples") |
| 125 | + print("-" * 30) |
| 126 | + print( |
| 127 | + "Note: To run against the actual API, set the NS1_APIKEY environment variable" |
| 128 | + ) |
| 129 | + print("Otherwise, this will run against a mock API endpoint") |
| 130 | + |
| 131 | + # Run examples |
| 132 | + usage_alerts_example() |
| 133 | + test_validation() |
0 commit comments