Skip to content

Instantly share code, notes, and snippets.

@quantumsheep
Created May 17, 2023 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantumsheep/c78d4b9f2f54d47cfa4374bfdc84b770 to your computer and use it in GitHub Desktop.
Save quantumsheep/c78d4b9f2f54d47cfa4374bfdc84b770 to your computer and use it in GitHub Desktop.
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