-
Notifications
You must be signed in to change notification settings - Fork 0
/
ef_slack.tf
156 lines (128 loc) · 3.05 KB
/
ef_slack.tf
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
resource "snowflake_external_function" "slack_snowflake" {
count = contains(var.handlers, "slack") == true ? 1 : 0
provider = snowflake.alerting_role
database = local.snowalert_database_name
schema = local.results_schema
name = "SLACK_SNOWFLAKE"
# Function arguments
arg {
name = "METHOD"
type = "VARCHAR"
}
arg {
name = "PATH"
type = "VARCHAR"
}
arg {
name = "PARAMS"
type = "VARCHAR"
}
# Function headers
header {
name = "method"
value = "{0}"
}
header {
name = "base-url"
value = "https://slack.com"
}
header {
name = "url"
value = "/api/{1}"
}
header {
name = "data"
value = "{2}"
}
header {
name = "auth"
value = var.slack_secrets_arn
}
return_null_allowed = true
max_batch_rows = 1
api_integration = module.geff_snowalert[0].api_integration_name
url_of_proxy_and_resource = "${module.geff_snowalert[0].api_gateway_invoke_url}${var.env}/https"
return_type = "VARIANT"
return_behavior = "VOLATILE"
comment = <<COMMENT
slack_snowflake: (method, path, params) -> response
COMMENT
depends_on = [
module.snowalert_grants
]
}
locals {
slack_snowflake_function = contains(var.handlers, "slack") == true ? join(".", [
local.snowalert_database_name,
local.results_schema,
snowflake_external_function.slack_snowflake[0].name,
]) : null
url_encode_function = join(".", [
local.snowalert_database_name,
local.data_schema,
snowflake_function.urlencode.name,
])
}
resource "snowflake_function" "slack_snowflake_chat_post_message" {
count = contains(var.handlers, "slack") == true ? 1 : 0
provider = snowflake.alerting_role
name = "SLACK_SNOWFLAKE_CHAT_POST_MESSAGE"
database = local.snowalert_database_name
schema = local.results_schema
# Function arguments
arguments {
name = "CHANNEL"
type = "VARCHAR"
}
arguments {
name = "TEXT"
type = "VARCHAR"
}
return_type = "VARIANT"
statement = <<SQL
${local.slack_snowflake_function}(
'post',
'chat.postMessage',
${local.url_encode_function}(OBJECT_CONSTRUCT(
'channel', channel::STRING,
'text', text::STRING
))
)
SQL
depends_on = [
module.snowalert_grants
]
}
locals {
slack_post_message_function = contains(var.handlers, "slack") == true ? join(".", [
local.snowalert_database_name,
local.results_schema,
snowflake_function.slack_snowflake_chat_post_message[0].name,
]) : null
}
resource "snowflake_function" "slack_handler" {
count = contains(var.handlers, "slack") == true ? 1 : 0
provider = snowflake.alerting_role
database = local.snowalert_database_name
schema = local.results_schema
name = "SLACK_HANDLER"
# Function arguments
arguments {
name = "ALERT"
type = "VARIANT"
}
arguments {
name = "PAYLOAD"
type = "VARIANT"
}
return_type = "VARIANT"
statement = <<SQL
${local.slack_post_message_function}(
payload['channel'],
payload['message']
)
SQL
depends_on = [
module.snowalert_grants
]
}