Skip to content

Commit f9e03eb

Browse files
authored
fix(3764/3788): mailjet & sendgrid alerts from notebooks (#3871)
1 parent 7745be4 commit f9e03eb

File tree

2 files changed

+87
-79
lines changed

2 files changed

+87
-79
lines changed

src/flows/pipes/Notification/endpoints/Mailjet/index.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export default register => {
1717
component: View,
1818
readOnlyComponent: ReadOnly,
1919
generateImports: () =>
20-
['http', 'influxdata/influxdb/secrets']
20+
['http', 'influxdata/influxdb/secrets', 'json']
2121
.map(i => `import "${i}"`)
2222
.join('\n'),
2323
generateTestImports: () =>
24-
['array', 'http', 'influxdata/influxdb/secrets']
24+
['array', 'http', 'influxdata/influxdb/secrets', 'json']
2525
.map(i => `import "${i}"`)
2626
.join('\n'),
2727
generateQuery: data => `task_data
@@ -34,50 +34,56 @@ export default register => {
3434
)
3535
|> monitor["notify"](
3636
data: notification,
37-
endpoint: ((r) => {
37+
endpoint: http.endpoint(url: "${data.url}")(
38+
mapFn: (r) => {
3839
apiKey = secrets.get(key: "${data.apiKey}")
3940
apiSecret = secrets.get(key: "${data.apiSecret}")
40-
http.post(
41-
url: "${data.url}",
42-
headers: {
43-
"Content-type": "application/json",
44-
"Authorization": "Basic \${apiKey}:\${apiSecret}"
45-
},
46-
data: bytes(v: "{
47-
\\"Messages\\": [{
48-
\\"From\\": {
49-
\\"Email\\": \\"${data.fromEmail}\\"
50-
},
51-
\\"To\\": [{
52-
\\"Email\\": \\"${data.email}\\"
53-
}],
54-
\\"Subject\\": \\"InfluxDB Alert\\",
55-
\\"TextPart\\": \\"\${ r._message }\\"
56-
}]
57-
}"))
58-
})
41+
auth = http.basicAuth(u: apiKey, p: apiSecret)
42+
43+
return {
44+
headers: {
45+
"Content-Type": "application/json",
46+
"Authorization": auth
47+
},
48+
data: json.encode(v: {
49+
"Messages": [{
50+
"From": {
51+
"Email": "${data.fromEmail}"
52+
},
53+
"To": [{
54+
"Email": "${data.email}"
55+
}],
56+
"Subject": "InfluxDB Alert",
57+
"TextPart": r._message
58+
}]
59+
})
60+
}
61+
}
62+
)
5963
)`,
6064
generateTestQuery: data => `
6165
apiKey = secrets.get(key: "${data.apiKey}")
62-
apiSecrets = secrets.get(key: "${data.apiSecret}")
66+
apiSecret = secrets.get(key: "${data.apiSecret}")
67+
auth = http.basicAuth(u: apiKey, p: apiSecret)
68+
6369
http.post(
6470
url: "${data.url}",
6571
headers: {
66-
"Content-type": "application/json",
67-
"Authorization": "Basic \${apiKey}:\${apiSecret}"
72+
"Content-Type": "application/json",
73+
"Authorization": auth
6874
},
69-
data: bytes(v: "{
70-
\\"Messages\\": [{
71-
\\"From\\": {
72-
\\"Email\\": \\"${data.fromEmail}\\"
75+
data: json.encode(v: {
76+
"Messages": [{
77+
"From": {
78+
"Email": "${data.fromEmail}"
7379
},
74-
\\"To\\": [{
75-
\\"Email\\": \\"${data.email}\\"
80+
"To": [{
81+
"Email": "${data.email}"
7682
}],
77-
\\"Subject\\": \\"InfluxDB Alert\\",
78-
\\"TextPart\\": \\"${TEST_NOTIFICATION}\\"
83+
"Subject": "InfluxDB Alert",
84+
"TextPart": "${TEST_NOTIFICATION}"
7985
}]
80-
}"))
86+
}))
8187
8288
array.from(rows: [{value: 0}])
8389
|> yield(name: "ignore")`,

src/flows/pipes/Notification/endpoints/SendGrid/index.ts

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ export default register => {
1616
component: View,
1717
readOnlyComponent: ReadOnly,
1818
generateImports: () =>
19-
['http', 'influxdata/influxdb/secrets']
19+
['http', 'influxdata/influxdb/secrets', 'json']
2020
.map(i => `import "${i}"`)
2121
.join('\n'),
2222
generateTestImports: () =>
23-
['array', 'http', 'influxdata/influxdb/secrets']
23+
['array', 'http', 'influxdata/influxdb/secrets', 'json']
2424
.map(i => `import "${i}"`)
2525
.join('\n'),
2626
generateQuery: data => `task_data
@@ -31,69 +31,71 @@ export default register => {
3131
messageFn: messageFn,
3232
crit: trigger,
3333
)
34-
|> monitor["notify"](
34+
|> monitor["notify"](
3535
data: notification,
36-
endpoint: ((r) => {
36+
endpoint: http.endpoint(url: "${data.url}")(
37+
mapFn: (r) => {
3738
apiKey = secrets.get(key: "${data.apiKey}")
38-
http.post(
39-
url: "${data.url}",
40-
headers: {
41-
"Content-type": "application/json",
42-
"Authorization": "Bearer \${apiKey}"
39+
return {
40+
headers: {
41+
"Content-Type": "application/json",
42+
"Authorization": "Bearer \${apiKey}"
43+
},
44+
data: json.encode(v: {
45+
"personalizations": [
46+
{
47+
"to": [
48+
{
49+
"email": "${data.email}"
50+
}
51+
],
52+
"subject": "InfluxDB Alert",
53+
}
54+
],
55+
"from": {
56+
"email": "${data.fromEmail}"
4357
},
44-
data: bytes(v: "{
45-
\\"personalizations\\": [
46-
{
47-
\\"to\\": [
48-
{
49-
\\"email\\": \\"${data.email}\\"
50-
}
51-
]
52-
}
53-
],
54-
\\"from\\": {
55-
\\"email\\": \\"${data.fromEmail} \\"
56-
},
57-
\\"subject\\": \\"InfluxDB Alert\\",
58-
\\"content\\": [
59-
{
60-
\\"type\\": \\"text/plain\\",
61-
\\"value\\": \\"\${ r._message }\\"
62-
}
63-
]
64-
}"))
65-
})
58+
"content": [
59+
{
60+
"type": "text/plain",
61+
"value": r._message
62+
}
63+
]
64+
})
65+
}
66+
}
67+
)
6668
)`,
6769
generateTestQuery: data => `
6870
apiKey = secrets.get(key: "${data.apiKey}")
6971
http.post(
7072
url: "${data.url}",
7173
headers: {
72-
"Content-type": "application/json",
74+
"Content-Type": "application/json",
7375
"Authorization": "Bearer \${apiKey}"
7476
},
75-
data: bytes(v: "{
76-
\\"personalizations\\": [
77+
data: json.encode(v: {
78+
"personalizations": [
7779
{
78-
\\"to\\": [
80+
"to": [
7981
{
80-
\\"email\\": \\"${data.email}\\"
82+
"email": "${data.email}"
8183
}
82-
]
84+
],
85+
"subject": "InfluxDB Alert"
8386
}
8487
],
85-
\\"from\\": {
86-
\\"email\\": \\"${data.fromEmail} \\"
88+
"from": {
89+
"email": "${data.fromEmail}"
8790
},
88-
\\"subject\\": \\"InfluxDB Alert\\",
89-
\\"content\\": [
91+
"content": [
9092
{
91-
\\"type\\": \\"text/plain\\",
92-
\\"value\\": \\"${TEST_NOTIFICATION}\\"
93+
"type": "text/plain",
94+
"value": "${TEST_NOTIFICATION}"
9395
}
9496
]
95-
}"))
96-
97+
})
98+
)
9799
array.from(rows: [{value: 0}])
98100
|> yield(name: "ignore")`,
99101
})

0 commit comments

Comments
 (0)