Update a Notification policy

Update a Notification policy.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Update a Notification policy
8
 * Update a Notification policy.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_id: string,
13
  policy_id: string,
14
  body: {
15
    alert_type?:
16
      | "access_custom_certificate_expiration_type"
17
      | "advanced_ddos_attack_l4_alert"
18
      | "advanced_ddos_attack_l7_alert"
19
      | "advanced_http_alert_error"
20
      | "bgp_hijack_notification"
21
      | "billing_usage_alert"
22
      | "block_notification_block_removed"
23
      | "block_notification_new_block"
24
      | "block_notification_review_rejected"
25
      | "brand_protection_alert"
26
      | "brand_protection_digest"
27
      | "clickhouse_alert_fw_anomaly"
28
      | "clickhouse_alert_fw_ent_anomaly"
29
      | "custom_ssl_certificate_event_type"
30
      | "dedicated_ssl_certificate_event_type"
31
      | "dos_attack_l4"
32
      | "dos_attack_l7"
33
      | "expiring_service_token_alert"
34
      | "failing_logpush_job_disabled_alert"
35
      | "fbm_auto_advertisement"
36
      | "fbm_dosd_attack"
37
      | "fbm_volumetric_attack"
38
      | "health_check_status_notification"
39
      | "hostname_aop_custom_certificate_expiration_type"
40
      | "http_alert_edge_error"
41
      | "http_alert_origin_error"
42
      | "incident_alert"
43
      | "load_balancing_health_alert"
44
      | "load_balancing_pool_enablement_alert"
45
      | "logo_match_alert"
46
      | "magic_tunnel_health_check_event"
47
      | "maintenance_event_notification"
48
      | "mtls_certificate_store_certificate_expiration_type"
49
      | "pages_event_alert"
50
      | "radar_notification"
51
      | "real_origin_monitoring"
52
      | "scriptmonitor_alert_new_code_change_detections"
53
      | "scriptmonitor_alert_new_hosts"
54
      | "scriptmonitor_alert_new_malicious_hosts"
55
      | "scriptmonitor_alert_new_malicious_scripts"
56
      | "scriptmonitor_alert_new_malicious_url"
57
      | "scriptmonitor_alert_new_max_length_resource_url"
58
      | "scriptmonitor_alert_new_resources"
59
      | "secondary_dns_all_primaries_failing"
60
      | "secondary_dns_primaries_failing"
61
      | "secondary_dns_zone_successfully_updated"
62
      | "secondary_dns_zone_validation_warning"
63
      | "sentinel_alert"
64
      | "stream_live_notifications"
65
      | "tunnel_health_event"
66
      | "tunnel_update_event"
67
      | "universal_ssl_event_type"
68
      | "web_analytics_metrics_update"
69
      | "zone_aop_custom_certificate_expiration_type";
70
    description?: string;
71
    enabled?: boolean;
72
    filters?: {
73
      actions?: string[];
74
      affected_asns?: string[];
75
      affected_components?: string[];
76
      affected_locations?: string[];
77
      airport_code?: string[];
78
      alert_trigger_preferences?: string[];
79
      alert_trigger_preferences_value?: ("99.0" | "98.0" | "97.0")[];
80
      enabled?: string[];
81
      environment?: string[];
82
      event?: string[];
83
      event_source?: string[];
84
      event_type?: string[];
85
      group_by?: string[];
86
      health_check_id?: string[];
87
      incident_impact?: (
88
        | "INCIDENT_IMPACT_NONE"
89
        | "INCIDENT_IMPACT_MINOR"
90
        | "INCIDENT_IMPACT_MAJOR"
91
        | "INCIDENT_IMPACT_CRITICAL"
92
      )[];
93
      input_id?: string[];
94
      limit?: string[];
95
      logo_tag?: string[];
96
      megabits_per_second?: string[];
97
      new_health?: string[];
98
      new_status?: string[];
99
      packets_per_second?: string[];
100
      pool_id?: string[];
101
      product?: string[];
102
      project_id?: string[];
103
      protocol?: string[];
104
      query_tag?: string[];
105
      requests_per_second?: string[];
106
      selectors?: string[];
107
      services?: string[];
108
      slo?: string[];
109
      status?: string[];
110
      target_hostname?: string[];
111
      target_ip?: string[];
112
      target_zone_name?: string[];
113
      traffic_exclusions?: "security_events"[];
114
      tunnel_id?: string[];
115
      tunnel_name?: string[];
116
      where?: string[];
117
      zones?: string[];
118
      [k: string]: unknown;
119
    };
120
    mechanisms?: {
121
      [k: string]: { id?: string | string; [k: string]: unknown }[];
122
    };
123
    name?: string;
124
    [k: string]: unknown;
125
  }
126
) {
127
  const url = new URL(
128
    `https://api.cloudflare.com/client/v4/accounts/${account_id}/alerting/v3/policies/${policy_id}`
129
  );
130

131
  const response = await fetch(url, {
132
    method: "PUT",
133
    headers: {
134
      "X-AUTH-EMAIL": auth.email,
135
      "X-AUTH-KEY": auth.key,
136
      "Content-Type": "application/json",
137
      Authorization: "Bearer " + auth.token,
138
    },
139
    body: JSON.stringify(body),
140
  });
141
  if (!response.ok) {
142
    const text = await response.text();
143
    throw new Error(`${response.status} ${text}`);
144
  }
145
  return await response.json();
146
}
147