Skip to content

Commit 3bffd91

Browse files
committed
Adds script to import dashboards to terraform
1 parent 3869651 commit 3bffd91

File tree

6 files changed

+1329
-0
lines changed

6 files changed

+1329
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:1.22-alpine AS build
2+
3+
RUN apk add --no-cache git
4+
WORKDIR /app
5+
COPY go.mod go.sum ./
6+
RUN go mod download
7+
COPY import.go .
8+
RUN go build -o main .
9+
10+
FROM hashicorp/terraform:1.8
11+
12+
WORKDIR /app
13+
COPY --from=build /app/main .
14+
ENTRYPOINT ["/app/main"]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
IMAGE_NAME ?= import-tf-script
2+
SIGNALFX_API_URL ?= https://app.us0.signalfx.com
3+
RELATIVE_DIR_PATH ?= "generated-dashboards"
4+
5+
6+
ifneq (${MAKEFILE_DIR_LOCATION},${WORKING_DIR})
7+
8+
%:
9+
$(MAKE) -C ${MAKEFILE_DIR_LOCATION} $@
10+
11+
.PHONY: %
12+
13+
else
14+
15+
.PHONY: tidy
16+
tidy:
17+
go mod tidy
18+
19+
.PHONY: fmt
20+
fmt:
21+
go fmt ./...
22+
23+
.PHONY: build
24+
build:
25+
docker buildx build -t $(IMAGE_NAME) .
26+
27+
.PHONY: import-dashboard-group
28+
import-dashboard-group: clean build
29+
@docker run --volume="$(shell pwd):/terraform-state" \
30+
-it $(IMAGE_NAME) --api-url $(SIGNALFX_API_URL) \
31+
--api-token=$(SIGNALFX_AUTH_TOKEN) \
32+
--groups $(GROUP_IDS) \
33+
--dir /terraform-state/$(RELATIVE_DIR_PATH) \
34+
--add-var-file \
35+
--add-versions-file \
36+
--allow-chart-name-conflict
37+
38+
.PHONY: clean
39+
clean:
40+
ifeq ($(shell docker images -q ${IMAGE_NAME} 2> /dev/null),)
41+
@echo "No image to remove"
42+
else
43+
@docker rmi $(IMAGE_NAME) -f
44+
endif
45+
endif
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Terraform Import SignalFx Dashboards
2+
3+
This folder contains helper scripts for importing SignalFx dashboard groups. along with their linked dashboards and charts, into Terraform code. This tool enables users to create dashboards in the SignalFx UI and then convert them into infrastructure-as-code for better version control and automation.
4+
5+
## Usage
6+
7+
This script, given dashboard group ids, will generate mostly usable terraform code for dashboard groups, and any associated dashboards/charts.
8+
9+
### Building the Docker Image
10+
11+
To build docker image with the script binary and terraform installed, run the following command:
12+
13+
```sh
14+
make build
15+
```
16+
17+
It is recommeded to pull the latest version of the repo and run build cmd before running the script.
18+
19+
### Cleaning up the Docker Image
20+
21+
To clean up the docker image, run the following command:
22+
23+
```sh
24+
make clean
25+
```
26+
27+
### Viewing Options
28+
29+
To look at the options for the script, run the following command:
30+
31+
```sh
32+
docker run import-tf-script --help
33+
```
34+
35+
### Quick Start
36+
37+
For a quick start cmd to generate terraform code for dashboard group, run the following make command:
38+
39+
```sh
40+
SIGNALFX_AUTH_TOKEN=<SIGNALFX API TOKEN> GROUP_IDS=<ID1,ID2> RELATIVE_DIR_PATH=resources/Synthetics-Dashboards make import-dashboard-group
41+
```
42+
43+
This will generate a bunch of `.tf` files. There will be a file for each dashboard group, and a different file for dashboards in the groups. The dashboard file will have the dashboard and chart resources.
44+
45+
### Example to run with custom options
46+
47+
```sh
48+
cd ${REPO_ROOT}
49+
docker run -v $(pwd):/app import-tf-script \
50+
--auth-token <TOKEN> \
51+
--api-url <API URL> \
52+
--group-ids <ID1,ID2> \
53+
--dir /app/resources/APM \
54+
--allow-chart-name-conflict
55+
```
56+
57+
### Additional Notes
58+
59+
- The resource type and name must be unique within a TF module. Changing the resource name will be treated as a recreate by TF, which means dashboard/chart urls will be updated if their corresponding resource name is changed. We derive the TF resource name from the SignalFx object names. It is recommended to clean-up sfx names before the first import to avoid recreates when rerunning the script to import updates to dashboards. The resource names generated for the different resource types are explained below. The names will have non-alphanumeric characters replaced with `_`:
60+
- `signalfx_dashboard_group`: `<dashboard group name>`
61+
- `signalfx_dashboard`: `<dashboard group resource name>_<dashboard name>`
62+
- `signalfx_*_chart`: `<dashboard resource name>_<chart name>`
63+
- In case of duplicate chart type and name combos in a dashboards, the script will append a number to the chart's name attribute and the chart resource name to make it unique if the option `--allow-chart-name-conflict` is provided. The additional index in the conflicting chart name on the first run of the script, will ensure the re-runs of the script does not cause TF to recreate chart resource, keeping the url same. If the option is not provided, the script will exit with an error on encountering conflicts. It is recommended to rename charts to make them unique before running the script.
64+
- For `text` charts, the script will fallback to the chart description to generate chart resource name if the chart name is empty. This is useful for Text charts which usually are untitled. Users are recommended to update their text charts to add some small unique description. We recommend using descriptions for text chart since titles for text charts might not be desirable in the UI.
65+
- The `dashboard_group` will only have READ permissions for the org, and the dashboard resource will inherit the group's permission. You can edit the `signalfx_dashboard_group` resource to add more permission blocks. Make sure you update the permissions when you create these for the first time since a session token of an administrator is needed to update write permissions of existing groups.
66+
- The script will overwrite any existing files in the directory with same names. If you want to keep the existing files, move them to a different directory before running the script.
67+
- Charts with detector links will have their `program_text` updated to replace the detector_id with terraform resource reference to the detector. The reference is in the format `signalfx_detector.<detector resource name>.id`. The detector resource name is generated by replacing non-alphanumeric characters with `_` in the detector name. If this resource is not found, plan will fail with an error. You can update the detector resource reference in the chart resource to fix the error.
68+
- The script orders the resources alphabetically by their resource type and name in the final terraform file. This is to ensure the order of resources is kind of deterministic and does not change between reruns of import script generating unnecessary diffs in the PRs.

import-dashboards-terraform/go.mod

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module main
2+
3+
go 1.22
4+
5+
toolchain go1.22.8
6+
7+
require (
8+
github.com/hashicorp/go-version v1.7.0
9+
github.com/hashicorp/hcl/v2 v2.22.0
10+
github.com/hashicorp/terraform-exec v0.21.0
11+
github.com/signalfx/signalfx-go v1.42.0
12+
github.com/zclconf/go-cty v1.15.0
13+
)
14+
15+
require (
16+
github.com/agext/levenshtein v1.2.1 // indirect
17+
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
18+
github.com/golang/protobuf v1.5.2 // indirect
19+
github.com/google/go-cmp v0.6.0 // indirect
20+
github.com/hashicorp/terraform-json v0.22.1 // indirect
21+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
22+
golang.org/x/mod v0.16.0 // indirect
23+
golang.org/x/net v0.23.0 // indirect
24+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
25+
golang.org/x/sys v0.18.0 // indirect
26+
golang.org/x/text v0.14.0 // indirect
27+
golang.org/x/tools v0.13.0 // indirect
28+
google.golang.org/appengine v1.6.7 // indirect
29+
google.golang.org/protobuf v1.33.0 // indirect
30+
)
31+
32+
// https://github.com/hashicorp/terraform-exec/pull/446 can be removed after the PR is merged
33+
replace github.com/hashicorp/terraform-exec v0.21.0 => github.com/hrmsk66/terraform-exec v0.21.0

0 commit comments

Comments
 (0)