Created
May 17, 2023 14:13
-
-
Save quantumsheep/c78d4b9f2f54d47cfa4374bfdc84b770 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "scaleway_project_id" { | |
type = string | |
description = "Scaleway project ID" | |
} | |
variable "scaleway_secret_key" { | |
type = string | |
sensitive = true | |
} | |
variable "vpc_name" { | |
type = string | |
description = "VPC name" | |
} | |
data "http" "get_vpc" { | |
url = "https://api.scaleway.com/vpc/v2/regions/fr-par/private-networks?project_id=${var.scaleway_project_id}&name=${var.vpc_name}" | |
method = "GET" | |
request_headers = { | |
"X-Auth-Token" = var.scaleway_secret_key | |
"Content-Type" = "application/json" | |
} | |
} | |
data "http" "create_vpc" { | |
count = one(jsondecode(data.http.get_vpc.response_body).private_networks.*.id) == null ? 1 : 0 | |
url = "https://api.scaleway.com/vpc/v2/regions/fr-par/private-networks" | |
method = "POST" | |
request_body = jsonencode({ | |
name = var.vpc_name | |
project_id = var.scaleway_project_id | |
tags = ["test", "dev"] | |
}) | |
request_headers = { | |
"X-Auth-Token" = var.scaleway_secret_key | |
"Content-Type" = "application/json" | |
} | |
lifecycle { | |
postcondition { | |
condition = contains([200, 201], self.status_code) | |
error_message = "Status code invalid" | |
} | |
} | |
} | |
data "http" "get_created_vpc" { | |
count = one(jsondecode(data.http.get_vpc.response_body).private_networks.*.id) == null ? 1 : 0 | |
url = "https://api.scaleway.com/vpc/v2/regions/fr-par/private-networks/${jsondecode(data.http.create_vpc.0.response_body).vpc_id}" | |
method = "GET" | |
request_headers = { | |
"X-Auth-Token" = var.scaleway_secret_key | |
"Content-Type" = "application/json" | |
} | |
} | |
resource "null_resource" "vpc_id" { | |
triggers = { | |
value = one(jsondecode(data.http.get_vpc.response_body).private_networks.*.id) == null ? jsondecode(data.http.create_vpc.0.response_body).vpc_id : jsondecode(data.http.get_vpc.response_body).private_networks.0.id | |
} | |
} | |
output "vpc_id" { | |
value = null_resource.vpc_id.triggers.value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment