-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewrelic_client.js
More file actions
319 lines (280 loc) · 8.06 KB
/
Copy pathnewrelic_client.js
File metadata and controls
319 lines (280 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import axios from "axios";
import dotenv from "dotenv";
dotenv.config();
class NewRelicClient {
constructor() {
this.apiKey = process.env.NEW_RELIC_API_KEY;
this.accountId = process.env.NEW_RELIC_ACCOUNT_ID;
this.region = process.env.NEW_RELIC_REGION || "US";
this.dryRun = process.env.DRY_RUN === "true";
if (!this.apiKey || !this.accountId) {
throw new Error(
"NEW_RELIC_API_KEY and NEW_RELIC_ACCOUNT_ID are required"
);
}
const baseURL =
this.region === "EU"
? "https://api.eu.newrelic.com/graphql"
: "https://api.newrelic.com/graphql";
this.client = axios.create({
baseURL,
headers: {
"Content-Type": "application/json",
"API-Key": this.apiKey,
},
});
// REST API client
const restBaseURL =
this.region === "EU"
? "https://api.eu.newrelic.com"
: "https://api.newrelic.com";
this.restClient = axios.create({
baseURL: restBaseURL,
headers: {
"Content-Type": "application/json",
"X-Api-Key": this.apiKey,
},
});
}
async makeGraphQLRequest(query, variables = {}) {
try {
const response = await this.client.post("", {
query,
variables,
});
if (response.data.errors) {
throw new Error(
`GraphQL errors: ${JSON.stringify(response.data.errors)}`
);
}
return response.data.data;
} catch (error) {
console.error("GraphQL request failed:", error.message);
throw error;
}
}
async getAllAlertPolicies() {
const query = `
query GetAlertPolicies($accountId: Int!) {
actor {
account(id: $accountId) {
alerts {
policiesSearch {
policies {
id
name
incidentPreference
}
}
}
}
}
}
`;
const data = await this.makeGraphQLRequest(query, {
accountId: parseInt(this.accountId),
});
return data.actor.account.alerts.policiesSearch.policies;
}
async deleteAlertPolicy(policyId) {
if (this.dryRun) {
console.log(`[DRY RUN] Would delete alert policy: ${policyId}`);
return { success: true, dryRun: true };
}
const mutation = `
mutation DeleteAlertPolicy($accountId: Int!, $policyId: ID!) {
alertsPolicyDelete(accountId: $accountId, id: $policyId) {
id
}
}
`;
try {
const data = await this.makeGraphQLRequest(mutation, {
accountId: parseInt(this.accountId),
policyId: policyId.toString(),
});
return { success: true, data: data.alertsPolicyDelete };
} catch (error) {
console.error(
`Failed to delete alert policy ${policyId}:`,
error.message
);
return { success: false, error: error.message };
}
}
async getAllWorkflows() {
const query = `
query GetWorkflows($accountId: Int!) {
actor {
account(id: $accountId) {
aiWorkflows {
workflows {
entities {
id
name
}
}
}
}
}
}
`;
const data = await this.makeGraphQLRequest(query, {
accountId: parseInt(this.accountId),
});
return data.actor.account.aiWorkflows.workflows.entities;
}
async deleteWorkflow(workflowId) {
if (this.dryRun) {
console.log(`[DRY RUN] Would delete workflow: ${workflowId}`);
return { success: true, dryRun: true };
}
const mutation = `
mutation DeleteWorkflow($accountId: Int!, $workflowId: ID!) {
aiWorkflowsDeleteWorkflow(accountId: $accountId, id: $workflowId) {
id
}
}
`;
try {
const data = await this.makeGraphQLRequest(mutation, {
accountId: parseInt(this.accountId),
workflowId: workflowId.toString(),
});
return { success: true, data: data.aiWorkflowsDeleteWorkflow };
} catch (error) {
console.error(`Failed to delete workflow ${workflowId}:`, error.message);
return { success: false, error: error.message };
}
}
async getAllChannels() {
const query = `
query GetChannels($accountId: Int!) {
actor {
account(id: $accountId) {
aiNotifications {
channels {
entities {
id
name
destinationId
}
}
}
}
}
}
`;
const data = await this.makeGraphQLRequest(query, {
accountId: parseInt(this.accountId),
});
return data.actor.account.aiNotifications.channels.entities;
}
async deleteChannel(channelId) {
if (this.dryRun) {
console.log(`[DRY RUN] Would delete channel: ${channelId}`);
return { success: true, dryRun: true };
}
const mutation = `
mutation DeleteChannel($accountId: Int!, $channelId: ID!) {
aiNotificationsDeleteChannel(accountId: $accountId, channelId: $channelId) {
ids
error {
details
}
}
}
`;
try {
const data = await this.makeGraphQLRequest(mutation, {
accountId: parseInt(this.accountId),
channelId: channelId.toString(),
});
if (data.aiNotificationsDeleteChannel.error) {
return {
success: false,
error: data.aiNotificationsDeleteChannel.error.details,
};
}
return { success: true, data: data.aiNotificationsDeleteChannel };
} catch (error) {
console.error(`Failed to delete channel ${channelId}:`, error.message);
return { success: false, error: error.message };
}
}
async getAllAlertDestinations() {
const query = `
query GetAlertDestinations($accountId: Int!) {
actor {
account(id: $accountId) {
aiNotifications {
destinations {
entities {
id
name
type
}
}
}
}
}
}
`;
const data = await this.makeGraphQLRequest(query, {
accountId: parseInt(this.accountId),
});
return data.actor.account.aiNotifications.destinations.entities;
}
async deleteAlertDestination(destinationId) {
if (this.dryRun) {
console.log(`[DRY RUN] Would delete alert destination: ${destinationId}`);
return { success: true, dryRun: true };
}
try {
// Try REST API first
const response = await this.restClient.delete(
`/v2/alerts_destinations/${destinationId}.json`
);
if (response.status === 200 || response.status === 204) {
return { success: true, data: { id: destinationId } };
} else {
throw new Error(`REST API returned status ${response.status}`);
}
} catch (restError) {
console.log(
`REST API failed, trying GraphQL for destination ${destinationId}`
);
// Fallback to GraphQL
const mutation = `
mutation DeleteAlertDestination($accountId: Int!, $destinationId: ID!) {
aiNotificationsDeleteDestination(accountId: $accountId, destinationId: $destinationId) {
ids
error {
details
}
}
}
`;
try {
const data = await this.makeGraphQLRequest(mutation, {
accountId: parseInt(this.accountId),
destinationId: destinationId.toString(),
});
if (data.aiNotificationsDeleteDestination.error) {
return {
success: false,
error: data.aiNotificationsDeleteDestination.error.details,
};
}
return { success: true, data: data.aiNotificationsDeleteDestination };
} catch (graphqlError) {
console.error(
`Both REST and GraphQL failed for destination ${destinationId}:`,
graphqlError.message
);
return { success: false, error: graphqlError.message };
}
}
}
}
export default NewRelicClient;