This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
141 lines (130 loc) · 4.06 KB
/
main.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
# Get Metabase auth token
data "external" "metabase-session" {
program = [
"curl", "-X", "POST", "https://${var.metabase_host}/api/session",
"-H", "Content-Type: application/json", "-d",
jsonencode({
username = var.metabase_username,
password = var.metabase_password
})
]
}
# Get Whistler backend auth token
data "external" "backend-session" {
program = ["python3", "${path.module}/files/backend_auth.py"]
query = {
backend_host = var.backend_host,
account_id = var.backend_account_id,
username = var.backend_user,
password = var.backend_password
}
}
# Metabase REST API provider
provider "restapi" {
uri = "https://${var.metabase_host}/api"
headers = {
X-Metabase-Session = data.external.metabase-session.result.id
}
id_attribute = "id"
write_returns_object = true
}
# Whistler backend REST API provider
provider "restapi" {
alias = "backend"
uri = "${var.backend_host}/cognito"
headers = {
authorization = data.external.backend-session.result.authorization,
Content-Type = "application/vnd.api+json"
}
id_attribute = "data/id"
write_returns_object = true
}
# Create collection and cards on Metabase
resource "restapi_object" "collection" {
path = "/collection"
data = jsonencode(local.metabase_collection)
}
resource "random_uuid" "variable-uuid" {
for_each = local.metabase_cards
}
# Create initial metabase card that will be updated with null_resource.put_metabase_card
resource "restapi_object" "cards" {
for_each = local.metabase_cards
path = "/card"
data = jsonencode({
name = each.value.name
dataset_query = {
native = {
query = ""
}
type = "native"
database = tonumber(each.value.database_id)
}
display = "table"
visualization_settings = {}
})
}
resource "local_file" "json_card" {
for_each = restapi_object.cards
filename = "${local.cards_path}/${each.key}.json"
content = jsonencode({
name = lookup(local.metabase_cards, each.key).name
dataset_query = {
native = {
query = lookup(local.metabase_cards, each.key).native_query
template-tags = {
for index, variable in lookup(local.metabase_cards, each.key).variables :
variable.name => {
id = "${substr(random_uuid.variable-uuid[each.key].result, 0, 35)}${format("%02d", index)}"
name = variable.name
type = variable.type
required = variable.required
display_name = variable.display_name
display-name = variable.display_name
default = variable.default
}
}
}
type = "native"
database = tonumber(lookup(local.metabase_cards, each.key).database_id)
}
display = "table"
description = lookup(local.metabase_cards, each.key).description
collection_id = tonumber(restapi_object.collection.id)
visualization_settings = lookup(lookup(local.metabase_cards, each.key), "visualization_settings", {})
embedding_params = {
for variable in lookup(local.metabase_cards, each.key).variables :
variable.name => variable.embedding_param
}
enable_embedding = lookup(local.metabase_cards, each.key).enable_embedding
})
}
resource "null_resource" "put_metabase_card" {
for_each = restapi_object.cards
provisioner "local-exec" {
command = <<EOS
curl -X PUT https://metabase.perxtech.io/api/card/${each.value.id} \
-H "X-Metabase-Session: ${data.external.metabase-session.result.id}" \
-H "Content-Type: application/json" \
-d @${lookup(local_file.json_card, each.key).filename}
EOS
}
triggers = {
always_run = "${timestamp()}"
}
}
# Cards mapping on Whistler backend
resource "restapi_object" "metabase_cards_mapping" {
provider = restapi.backend
path = "/metabase_cards"
for_each = restapi_object.cards
data = jsonencode({
data = {
type = "metabase_cards",
attributes = {
identifier = each.key,
card_id = each.value.id
}
}
})
}