-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref(uptime): Move subscription limit check to validator #100904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
MaxManualUptimeSubscriptionsReached, | ||
MaxUrlsForDomainReachedException, | ||
UptimeMonitorNoSeatAvailable, | ||
check_uptime_subscription_limit, | ||
check_url_limits, | ||
create_uptime_detector, | ||
create_uptime_subscription, | ||
|
@@ -202,6 +203,16 @@ class UptimeMonitorValidator(CamelSnakeSerializer): | |
) | ||
|
||
def validate(self, attrs): | ||
# When creating a new uptime monitor, check if we would exceed the organization limit | ||
if not self.instance: | ||
organization = self.context["organization"] | ||
try: | ||
check_uptime_subscription_limit(organization.id) | ||
except MaxManualUptimeSubscriptionsReached: | ||
raise serializers.ValidationError( | ||
f"You may have at most {MAX_MANUAL_SUBSCRIPTIONS_PER_ORG} uptime monitors per organization" | ||
) | ||
|
||
headers = [] | ||
method = "GET" | ||
body = None | ||
|
@@ -246,34 +257,29 @@ def create(self, validated_data): | |
method_headers_body = { | ||
k: v for k, v in validated_data.items() if k in {"method", "headers", "body"} | ||
} | ||
try: | ||
detector = create_uptime_detector( | ||
project=self.context["project"], | ||
environment=environment, | ||
url=validated_data["url"], | ||
interval_seconds=validated_data["interval_seconds"], | ||
timeout_ms=validated_data["timeout_ms"], | ||
name=validated_data["name"], | ||
status=validated_data.get("status"), | ||
mode=validated_data.get("mode", UptimeMonitorMode.MANUAL), | ||
owner=validated_data.get("owner"), | ||
trace_sampling=validated_data.get("trace_sampling", False), | ||
recovery_threshold=validated_data["recovery_threshold"], | ||
downtime_threshold=validated_data["downtime_threshold"], | ||
**method_headers_body, | ||
) | ||
detector = create_uptime_detector( | ||
project=self.context["project"], | ||
environment=environment, | ||
url=validated_data["url"], | ||
interval_seconds=validated_data["interval_seconds"], | ||
timeout_ms=validated_data["timeout_ms"], | ||
name=validated_data["name"], | ||
status=validated_data.get("status"), | ||
mode=validated_data.get("mode", UptimeMonitorMode.MANUAL), | ||
owner=validated_data.get("owner"), | ||
trace_sampling=validated_data.get("trace_sampling", False), | ||
recovery_threshold=validated_data["recovery_threshold"], | ||
downtime_threshold=validated_data["downtime_threshold"], | ||
**method_headers_body, | ||
) | ||
|
||
create_audit_entry( | ||
request=self.context["request"], | ||
organization=self.context["organization"], | ||
target_object=detector.id, | ||
event=audit_log.get_event_id("UPTIME_MONITOR_ADD"), | ||
data=get_audit_log_data(detector), | ||
) | ||
except MaxManualUptimeSubscriptionsReached: | ||
raise serializers.ValidationError( | ||
f"You may have at most {MAX_MANUAL_SUBSCRIPTIONS_PER_ORG} uptime monitors per organization" | ||
) | ||
create_audit_entry( | ||
request=self.context["request"], | ||
organization=self.context["organization"], | ||
target_object=detector.id, | ||
event=audit_log.get_event_id("UPTIME_MONITOR_ADD"), | ||
data=get_audit_log_data(detector), | ||
) | ||
|
||
Comment on lines
+279
to
283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential bug: The
|
||
return detector | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.