From eb44ad35ce4d5004840a3cb746d8a0d6f47c9459 Mon Sep 17 00:00:00 2001 From: Marques Johansson Date: Fri, 6 Sep 2024 16:28:36 -0400 Subject: [PATCH] fix: deploy to existing projects in selected metro Signed-off-by: Marques Johansson --- README.md | 11 ++++++++--- data.tf | 7 ++++--- main.tf | 9 +++++---- outputs.tf | 12 ++++++++++++ terraform.tfvars.example | 3 +++ variables.tf | 26 ++++++++++++++++++-------- 6 files changed, 50 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 191efc1..d5b3214 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,15 @@ The user needs to provide two environment variables: `METAL_AUTH_TOKEN` API token to access your Equinix account -`TF_VAR_api_key` API token to access your Equinix account. +`TF_VAR_api_key` API token to access your Equinix account (needed when Spot Market is enabled) -`TF_VAR_project_name` Terraform variable to identify project in your Equinix account. +`TF_VAR_project_name` or `TF_VAR_project_id` Terraform variable to identify project in your Equinix account. Optionally the user can also provide: `TF_VAR_metal_create_project` Terraform variable to create a project of name `TF_VAR_project_name` if it does not exist. -You can overwrite any values in variables.tf by using .tfvars files or [other means](https://www.terraform.io/language/values/variables#assigning-values-to-root-module-variables) +You can overwrite any values in `variables.tf` by using `.tfvars` files or [other means](https://www.terraform.io/language/values/variables#assigning-values-to-root-module-variables) By default the module will create a 3 node Harvester cluster. @@ -22,6 +22,11 @@ The Harvester console can be accessed using an Elastic IP created by the sample. A random token and password will be generated for your example. +```sh +terraform output -raw harvester_admin_password +terraform output -raw harvester_api_token +``` + If you provide a Rancher API URL and keys, your Harvester environment can be managed by Rancher and a kubeconfig file will be saved locally. ### Using `terraform.tfvars.example` to Override Variable Values diff --git a/data.tf b/data.tf index da87df7..1c0a6f7 100644 --- a/data.tf +++ b/data.tf @@ -1,10 +1,11 @@ data "equinix_metal_project" "project" { - name = var.metal_create_project ? equinix_metal_project.new_project[0].name : var.project_name + name = var.metal_create_project ? equinix_metal_project.new_project[0].name : (var.project_name == "" ? null : var.project_name) + project_id = var.metal_create_project ? null : (var.project_id == "" ? null : var.project_id) } data "equinix_metal_ip_block_ranges" "address_block" { project_id = local.project_id - metro = var.use_cheapest_metro ? local.cheapest_metro_price.metro : var.metro + metro = (var.spot_instance && var.use_cheapest_metro) ? local.cheapest_metro_price.metro : var.metro } @@ -30,7 +31,7 @@ data "equinix_metal_device" "join_devices" { } data "http" "prices" { - count = var.use_cheapest_metro ? 1 : 0 + count = var.spot_instance && var.use_cheapest_metro ? 1 : 0 url = "https://api.equinix.com/metal/v1/market/spot/prices/metros" method = "GET" request_headers = { diff --git a/main.tf b/main.tf index a0591e0..46e47e7 100644 --- a/main.tf +++ b/main.tf @@ -1,6 +1,6 @@ locals { project_id = var.metal_create_project ? equinix_metal_project.new_project[0].id : data.equinix_metal_project.project.project_id - metro = var.use_cheapest_metro ? local.cheapest_metro_price.metro : lower(var.metro) + metro = (var.spot_instance && var.use_cheapest_metro) ? local.cheapest_metro_price.metro : lower(var.metro) } // IP attachment to be added to seed node, and this is subsequently assigned as Harvester vip @@ -16,13 +16,14 @@ resource "random_password" "token" { } resource "equinix_metal_project" "new_project" { - count = var.metal_create_project ? 1 : 0 - name = var.project_name + count = var.metal_create_project ? 1 : 0 + name = var.project_name + organization_id = var.organization_id == "" ? null : var.organization_id } locals { machine_size = var.plan - pricing_data = var.use_cheapest_metro ? try(jsondecode(data.http.prices[0].response_body), null) : null + pricing_data = (var.spot_instance && var.use_cheapest_metro) ? try(jsondecode(data.http.prices[0].response_body), null) : null least_bid_price_metro = can(local.pricing_data) && can(local.pricing_data.spot_market_prices) ? flatten([for metro, machines in local.pricing_data.spot_market_prices : [ for machine, details in machines : { metro = metro diff --git a/outputs.tf b/outputs.tf index f3de273..3d24fb0 100644 --- a/outputs.tf +++ b/outputs.tf @@ -12,3 +12,15 @@ output "out_of_band_hostnames" { value = concat([data.equinix_metal_device.seed_device.sos_hostname], data.equinix_metal_device.join_devices.*.sos_hostname) description = "Out of band hostnames for SSH access to the console" } + +output "harvester_admin_password" { + value = random_password.password.result + description = "The password for the Harvester 'admin' user" + sensitive = true +} + +output "harvester_api_token" { + value = random_password.token.result + description = "The token for the Harvester API" + sensitive = true +} \ No newline at end of file diff --git a/terraform.tfvars.example b/terraform.tfvars.example index e215fc1..9606b78 100644 --- a/terraform.tfvars.example +++ b/terraform.tfvars.example @@ -2,6 +2,9 @@ harvester_version = "v1.3.1" hostname_prefix = "test-harvester" node_count = "3" project_name = "Harvester Labs" +# metal_create_project = false +# project_id = "uuid" +# organization_id = "uuid" plan = "m3.small.x86" #max_bid_price = "0.17" metro = "SV" diff --git a/variables.tf b/variables.tf index f397d07..8ff5bee 100644 --- a/variables.tf +++ b/variables.tf @@ -1,6 +1,6 @@ variable "harvester_version" { default = "v1.3.1" - description = "Harvester version to be installed" + description = "Harvester version to be installed (Must be a valid version tag from https://github.com/rancherlabs/terraform-harvester-equinix/tree/main/ipxe)" } variable "node_count" { @@ -10,7 +10,17 @@ variable "node_count" { variable "project_name" { default = "" - description = "Name of the Equinix metal project" + description = "Name of the Equinix Metal project to deploy into, when not looking up by project_id" +} + +variable "organization_id" { + description = "Equinix Metal organization ID to create or find a project in" + default = "" +} + +variable "project_id" { + description = "Equinix Metal project ID to deploy into, if not creating a new project or looking up by name" + default = "" } variable "metal_create_project" { @@ -21,22 +31,22 @@ variable "metal_create_project" { variable "plan" { default = "c3.small.x86" - description = "Size of the servers to be deployed on Equinix metal" + description = "Size of the servers to be deployed on Equinix metal (https://deploy.equinix.com/developers/docs/metal/hardware/standard-servers/)" } variable "billing_cycle" { default = "hourly" - description = "Equinix metal billing/invoice generation schedule" + description = "Equinix metal billing/invoice generation schedule (hourly/daily/monthly/yearly)" } variable "metro" { default = "SG" - description = "Equinix metal data center location. Examples: SG,SV,AM,MA,Ny,LA,etc." + description = "Equinix metal data center location (https://deploy.equinix.com/developers/docs/metal/locations/metros/). Examples: SG,SV,AM,MA,Ny,LA,etc." } variable "ipxe_script" { default = "https://raw.githubusercontent.com/rancherlabs/terraform-harvester-equinix/main/ipxe/ipxe-" - description = "URL for booting the servers with IPXE" + description = "URL to the iPXE script to use for booting the server (harvester_version will be appended to this without the 'v' prefix)" } variable "hostname_prefix" { @@ -46,7 +56,7 @@ variable "hostname_prefix" { variable "spot_instance" { default = false - description = "Set to true to use spot instance instead of on demand. Also set you max bid price if true." + description = "Set to true to use spot instance instead of on demand. Also set your max bid price if true." } variable "max_bid_price" { @@ -92,5 +102,5 @@ variable "api_key" { variable "use_cheapest_metro" { type = bool default = true - description = "A boolean variable to control cheapest metro selection" + description = "Equinix Metal authentication token. Required when using Spot Instances for HTTP pricing lookups. METAL_AUTH_TOKEN should always be set as an environment variable." }