Skip to content

Commit

Permalink
Merge pull request #7 from t0mmili/feature/6-update-tools-and-refacto…
Browse files Browse the repository at this point in the history
…r-terraform

feature/6-update-tools-and-refactor-terraform
  • Loading branch information
t0mmili authored Jun 22, 2023
2 parents ad3eed3 + a310056 commit 0abaae9
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Base image
FROM alpine:3.17
FROM alpine:3.18

# Install applications
RUN apk add --no-cache \
Expand All @@ -13,7 +13,7 @@ RUN apk add --no-cache \
RUN /bin/ash -c 'adduser -h /home/jenkins -g jenkins -s /bin/ash -D -u 1000 jenkins;'

# Install Terraform
RUN RELEASE="1.3.9" && \
RUN RELEASE="1.5.0" && \
wget https://releases.hashicorp.com/terraform/${RELEASE}/terraform_${RELEASE}_linux_amd64.zip && \
unzip terraform_${RELEASE}_linux_amd64.zip && \
rm terraform_${RELEASE}_linux_amd64.zip && \
Expand Down
3 changes: 3 additions & 0 deletions Jenkinsfile.export
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pipeline {
reuseNode true
}
}
environment {
TF_IN_AUTOMATION = 'true'
}
steps {
dir('./dashboards') {
checkout([$class: 'GitSCM', branches: [[name: '*/$gitBranch']], extensions: [[$class: 'LocalBranch', localBranch: gitBranch]], userRemoteConfigs: [[credentialsId: gitCredentials, url: gitUrl]]])
Expand Down
3 changes: 3 additions & 0 deletions Jenkinsfile.import
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pipeline {
agent {
dockerfile true
}
environment {
TF_IN_AUTOMATION = 'true'
}
steps {
dir('./dashboards') {
checkout([$class: 'GitSCM', branches: [[name: '*/$gitBranch']], extensions: [[$class: 'LocalBranch', localBranch: gitBranch]], userRemoteConfigs: [[credentialsId: gitCredentials, url: gitUrl]]])
Expand Down
4 changes: 2 additions & 2 deletions scripts/terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ done
cd "$tf_dir"

terraform init -input=false -no-color
terraform plan -no-color -out=tfplan $([ ! -z ${tf_vars#${tf_vars%?}} ] && printf "$tf_vars")
terraform apply -auto-approve -no-color tfplan
terraform plan -input=false -no-color -out=tfplan $([ ! -z ${tf_vars#${tf_vars%?}} ] && printf "$tf_vars")
terraform apply -input=false -auto-approve -no-color tfplan
13 changes: 7 additions & 6 deletions terraform/dashboard-export/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5 |
| <a name="requirement_grafana"></a> [grafana](#requirement\_grafana) | ~> 1.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_grafana"></a> [grafana](#provider\_grafana) | ~> 1.0 |
| <a name="provider_grafana"></a> [grafana](#provider\_grafana) | 1.41.0 |

## Modules

Expand All @@ -19,16 +20,16 @@ No modules.

| Name | Type |
|------|------|
| [grafana_dashboard.from_dashboard_uids](https://registry.terraform.io/providers/grafana/grafana/latest/docs/data-sources/dashboard) | data source |
| [grafana_dashboards.from_folder_ids](https://registry.terraform.io/providers/grafana/grafana/latest/docs/data-sources/dashboards) | data source |
| [grafana_folders.all](https://registry.terraform.io/providers/grafana/grafana/latest/docs/data-sources/folders) | data source |
| [grafana_dashboard.this](https://registry.terraform.io/providers/grafana/grafana/latest/docs/data-sources/dashboard) | data source |
| [grafana_dashboards.this](https://registry.terraform.io/providers/grafana/grafana/latest/docs/data-sources/dashboards) | data source |
| [grafana_folders.this](https://registry.terraform.io/providers/grafana/grafana/latest/docs/data-sources/folders) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_grafana_auth"></a> [grafana\_auth](#input\_grafana\_auth) | Grafana authentication. Can be api key or basic auth. | `string` | n/a | yes |
| <a name="input_grafana_url"></a> [grafana\_url](#input\_grafana\_url) | Grafana instance url | `string` | n/a | yes |
| <a name="input_grafana_auth"></a> [grafana\_auth](#input\_grafana\_auth) | Grafana authentication. Can be api key or basic auth. | `string` | `null` | no |
| <a name="input_grafana_url"></a> [grafana\_url](#input\_grafana\_url) | Grafana instance url | `string` | `null` | no |

## Outputs

Expand Down
10 changes: 10 additions & 0 deletions terraform/dashboard-export/checks.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
check "grafana_health_check" {
data "http" "grafana" {
url = var.grafana_url
}

assert {
condition = data.http.grafana.status_code == 200
error_message = "${data.http.grafana.url} returned an unhealthy status code"
}
}
17 changes: 9 additions & 8 deletions terraform/dashboard-export/main.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
provider "grafana" {
url = var.grafana_url
auth = var.grafana_auth
auth = var.grafana_auth
retries = 3
url = var.grafana_url
}

data "grafana_folders" "all" {}
data "grafana_folders" "this" {}

data "grafana_dashboards" "from_folder_ids" {
folder_ids = concat(data.grafana_folders.all.folders[*].id, [0])
data "grafana_dashboards" "this" {
folder_ids = concat(data.grafana_folders.this.folders[*].id, [0])
}

data "grafana_dashboard" "from_dashboard_uids" {
count = length(data.grafana_dashboards.from_folder_ids.dashboards[*].uid)
data "grafana_dashboard" "this" {
count = length(data.grafana_dashboards.this.dashboards[*].uid)

uid = element(data.grafana_dashboards.from_folder_ids.dashboards[*].uid, count.index)
uid = element(data.grafana_dashboards.this.dashboards[*].uid, count.index)
}
6 changes: 3 additions & 3 deletions terraform/dashboard-export/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
output "grafana_folders_titles" {
description = "List of Grafana folders"
value = concat(data.grafana_folders.all.folders[*].title, ["General"])
value = concat(data.grafana_folders.this.folders[*].title, ["General"])
}

output "grafana_dashboards" {
description = "Complete dashboards models with corresponding titles"
value = {
title = data.grafana_dashboards.from_folder_ids.dashboards[*].title
config = data.grafana_dashboard.from_dashboard_uids[*].config_json
title = data.grafana_dashboards.this.dashboards[*].title
config = data.grafana_dashboard.this[*].config_json
}
}
3 changes: 3 additions & 0 deletions terraform/dashboard-export/variables.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
variable "grafana_auth" {
description = "Grafana authentication. Can be api key or basic auth."
type = string
sensitive = true
default = null
}

variable "grafana_url" {
description = "Grafana instance url"
type = string
default = null
}
2 changes: 2 additions & 0 deletions terraform/dashboard-export/versions.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
terraform {
required_version = ">= 1.5"

required_providers {
grafana = {
source = "grafana/grafana"
Expand Down
13 changes: 7 additions & 6 deletions terraform/dashboard-import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5 |
| <a name="requirement_grafana"></a> [grafana](#requirement\_grafana) | ~> 1.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_grafana"></a> [grafana](#provider\_grafana) | ~> 1.0 |
| <a name="provider_grafana"></a> [grafana](#provider\_grafana) | 1.41.0 |

## Modules

Expand All @@ -19,17 +20,17 @@ No modules.

| Name | Type |
|------|------|
| [grafana_dashboard.from_json](https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/dashboard) | resource |
| [grafana_dashboard.this](https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/dashboard) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_dashboard_files"></a> [dashboard\_files](#input\_dashboard\_files) | List of dashboard files to import | `list(string)` | n/a | yes |
| <a name="input_dashboard_files"></a> [dashboard\_files](#input\_dashboard\_files) | List of dashboard files to import | `list(string)` | `[]` | no |
| <a name="input_dashboard_overwrite"></a> [dashboard\_overwrite](#input\_dashboard\_overwrite) | Whether to overwrite existing dashboard with newer version | `bool` | `false` | no |
| <a name="input_dashboards_folder"></a> [dashboards\_folder](#input\_dashboards\_folder) | Source folder with dashboard files | `string` | n/a | yes |
| <a name="input_grafana_auth"></a> [grafana\_auth](#input\_grafana\_auth) | Grafana authentication. Can be api key or basic auth. | `string` | n/a | yes |
| <a name="input_grafana_url"></a> [grafana\_url](#input\_grafana\_url) | Grafana instance url | `string` | n/a | yes |
| <a name="input_dashboards_folder"></a> [dashboards\_folder](#input\_dashboards\_folder) | Source folder with dashboard files | `string` | `null` | no |
| <a name="input_grafana_auth"></a> [grafana\_auth](#input\_grafana\_auth) | Grafana authentication. Can be api key or basic auth. | `string` | `null` | no |
| <a name="input_grafana_url"></a> [grafana\_url](#input\_grafana\_url) | Grafana instance url | `string` | `null` | no |

## Outputs

Expand Down
10 changes: 10 additions & 0 deletions terraform/dashboard-import/checks.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
check "grafana_health_check" {
data "http" "grafana" {
url = var.grafana_url
}

assert {
condition = data.http.grafana.status_code == 200
error_message = "${data.http.grafana.url} returned an unhealthy status code"
}
}
7 changes: 4 additions & 3 deletions terraform/dashboard-import/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
provider "grafana" {
url = var.grafana_url
auth = var.grafana_auth
auth = var.grafana_auth
retries = 3
url = var.grafana_url
}

resource "grafana_dashboard" "from_json" {
resource "grafana_dashboard" "this" {
count = length(var.dashboard_files)

config_json = file("${var.dashboards_folder}${var.dashboard_files[count.index]}")
Expand Down
6 changes: 3 additions & 3 deletions terraform/dashboard-import/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
output "grafana_dashboards" {
description = "Imported dashboards information"
value = {
dashboard_id = grafana_dashboard.from_json[*].dashboard_id
url = grafana_dashboard.from_json[*].url
version = grafana_dashboard.from_json[*].version
dashboard_id = grafana_dashboard.this[*].dashboard_id
url = grafana_dashboard.this[*].url
version = grafana_dashboard.this[*].version
}
}
5 changes: 5 additions & 0 deletions terraform/dashboard-import/variables.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
variable "grafana_auth" {
description = "Grafana authentication. Can be api key or basic auth."
type = string
sensitive = true
default = null
}

variable "grafana_url" {
description = "Grafana instance url"
type = string
default = null
}

variable "dashboards_folder" {
description = "Source folder with dashboard files"
type = string
default = null
}

variable "dashboard_files" {
description = "List of dashboard files to import"
type = list(string)
default = []
}

variable "dashboard_overwrite" {
Expand Down
2 changes: 2 additions & 0 deletions terraform/dashboard-import/versions.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
terraform {
required_version = ">= 1.5"

required_providers {
grafana = {
source = "grafana/grafana"
Expand Down

0 comments on commit 0abaae9

Please sign in to comment.