3636from linode_api4 .objects import Capability , Region
3737
3838ENV_TOKEN_NAME = "LINODE_TOKEN"
39- ENV_API_URL_NAME = "LINODE_API_URL"
39+ ENV_API_URL_NAME = "LINODE_URL"
40+ ENV_API_VERSION_NAME = "LINODE_API_VERSION"
4041ENV_REGION_OVERRIDE = "LINODE_TEST_REGION_OVERRIDE"
4142ENV_API_CA_NAME = "LINODE_API_CA"
4243RUN_LONG_TESTS = "RUN_LONG_TESTS"
@@ -57,7 +58,9 @@ def get_token():
5758
5859
5960def get_api_url ():
60- return os .environ .get (ENV_API_URL_NAME , "https://api.linode.com/v4beta" )
61+ api_url = os .environ .get (ENV_API_URL_NAME , "https://api.linode.com" )
62+ api_version = os .environ .get (ENV_API_VERSION_NAME , "v4beta" )
63+ return f"{ api_url .rstrip ('/' )} /{ api_version } "
6164
6265
6366def get_regions (
@@ -847,6 +850,72 @@ def test_monitor_client(get_monitor_token_for_db_entities):
847850 return client , entity_ids
848851
849852
853+ def get_alert_channels_list (test_linode_client ):
854+ channels = list (test_linode_client .monitor .alert_channels ())
855+ if len (channels ) == 0 :
856+ pytest .fail (
857+ "No alert channels available for testing. Please create an alert channel and try again."
858+ )
859+
860+ return channels
861+
862+
863+ @pytest .fixture (scope = "session" )
864+ def create_alert_service_definition (test_linode_client ):
865+ rule_criteria = {
866+ "rules" : [
867+ {
868+ "aggregate_function" : "min" ,
869+ "dimension_filters" : [
870+ {
871+ "dimension_label" : "node_type" ,
872+ "label" : "Node Type" ,
873+ "operator" : "eq" ,
874+ "value" : "primary" ,
875+ }
876+ ],
877+ "label" : "Memory Usage" ,
878+ "metric" : "memory_usage" ,
879+ "operator" : "eq" ,
880+ "threshold" : 95 ,
881+ "unit" : "percent" ,
882+ }
883+ ]
884+ }
885+ trigger_conditions = {
886+ "criteria_condition" : "ALL" ,
887+ "evaluation_period_seconds" : 300 ,
888+ "polling_interval_seconds" : 900 ,
889+ "trigger_occurrences" : 3 ,
890+ }
891+ alert = test_linode_client .monitor .create_alert_definition (
892+ service_type = "dbaas" ,
893+ label = get_test_label () + "-service-definition" ,
894+ severity = 1 ,
895+ description = "description" ,
896+ channel_ids = [get_alert_channels_list (test_linode_client )[0 ].id ],
897+ rule_criteria = rule_criteria ,
898+ trigger_conditions = trigger_conditions ,
899+ )
900+
901+ yield alert
902+
903+ alert .delete ()
904+
905+
906+ def get_system_alerts (client : LinodeClient ):
907+ alerts = client .monitor .alert_definitions ()
908+ system_alerts = []
909+ for alert in alerts .lists [0 ]:
910+ if alert .type == "system" :
911+ system_alerts .append (alert )
912+ if len (system_alerts ) == 0 :
913+ raise Exception (
914+ "No system alert definitions found. Cannot run tests dependent on system alert definitions."
915+ )
916+ return system_alerts
917+
918+
850919@pytest .fixture
851920def create_reserved_ip (test_linode_client ):
852921 client = test_linode_client
0 commit comments