I had to add a lot of resources with terraform, so it was hard to copy and paste, so that's a memo.
First, prepare a template file.
Set the place you want to make a variable to @ NAME @
@ PATH @
@ HOST @
.
template.tf
resource "google_monitoring_uptime_check_config" "@NAME@" {
display_name = "@NAME@"
timeout = "10s"
http_check {
use_ssl = true
path = "@PATH@"
port = "443"
}
monitored_resource {
type = "uptime_url"
labels = {
project_id = var.project_id
host = "@HOST@"
}
}
}
Create the value you want to pass to the variable with csv.
source.csv
aaa_bbb_com,/,aaa.bbb.com
aaa_bbb_jp,/hoge,aaa.bbb.jp
aaa_bbb_co_jp,/hage,aaa.bbb.co.jp
Run python
# python mkfile.py source.csv template.txt
It is one of the created files
resource "google_monitoring_uptime_check_config" "aaa_bbb_co_jp" {
display_name = "aaa_bbb_co_jp"
timeout = "10s"
http_check {
use_ssl = true
path = "/hage"
port = "443"
}
monitored_resource {
type = "uptime_url"
labels = {
project_id = var.project_id
host = "aaa.bbb.co.jp"
}
}
}
If you want to combine the files,
cat create* > result.tf
mkfile.py
# -*- coding: utf-8 -*-
import os, sys ,csv
def mkFile(srcFname, tmplFname):
with open(srcFname) as c:
reader = csv.reader(c)
for row in reader:
f1 = open(tmplFname, "r")
f2 = open("create" + row[0] + ".txt", "w")
for row2 in f1:
temp = row2.replace("@NAME@", row[0]) \
.replace("@PATH@", row[1]) \
.replace("@HOST@", row[2])
f2.write(temp)
f1.close
f2.close
if __name__ == '__main__':
if(len(sys.argv)!=3):
print("usage: python mkfile.py source.csv template.tf")
srcFname = sys.argv[1]
tmplFname = sys.argv[2]
#start func
mkFile(srcFname, tmplFname)
print("finished")
temp = row2.replace("@NAME@", row[0]) \
.replace("@PATH@", row[1]) \
.replace("@HOST@", row[2])
Recommended Posts