Using the Terraform provider

This is a terraform plugin for controlling Metalcloud resources.

Using the Provider

A terraform main.tf template file, for an infrastructure with a single server would look something like this:

terraform {
  required_providers {
    metalcloud = {
      source = "metalsoft-io/metalcloud"
    }
  }
}

provider "metalcloud" {
  endpoint = var.endpoint
  user_email = var.user_email
  api_key = var.api_key
}

data "metalcloud_site" "dc" {
  label = "${var.site}"
}

data "metalcloud_fabric" "wan" {
  site_id = data.metalcloud_site.dc.site_id
  label = "wan-fabric"
}

data "metalcloud_logical_network_profile" "np01" {
  label = "np-01"
  fabric_id = data.metalcloud_fabric.wan.fabric_id
}

data "metalcloud_server_type" "srv1" {
  label = "M.16.64.2"
}

data "metalcloud_os_template" "os1" {
  label = "ubuntu-22-04"
}

data "metalcloud_infrastructure" "infra" {
  site_id = data.metalcloud_site.dc.site_id
  label = "my-infra01"

  create_if_missing = true
}

resource "metalcloud_logical_network" "net1" {
  infrastructure_id = data.metalcloud_infrastructure.infra.infrastructure_id
  logical_network_profile_id = data.metalcloud_logical_network_profile.np01.logical_network_profile_id

  name = "net01"
  label = "net01"
}

resource "metalcloud_server_instance_group" "inst01" {
  infrastructure_id = data.metalcloud_infrastructure.infra.infrastructure_id

  name = "inst01"
  label = "inst01"

  instance_count = 1
  server_type_id = data.metalcloud_server_type.srv1.server_type_id
  os_template_id = data.metalcloud_os_template.os1.os_template_id

  network_connections = [
    {
      logical_network_id = metalcloud_logical_network.net1.logical_network_id
      tagged = true
      access_mode = "l2"
      mtu = 1500
    }
  ]

  custom_variables = [
    {
      name = "key1"
      value = "test1"
    },
    {
      name = "key2"
      value = "test2"
    }
  ]

  depends_on = [
    metalcloud_logical_network.net1,
  ]
}

# Use this resource to effect deploys of the above resources.
resource "metalcloud_infrastructure_deployer" "infrastructure_deployer" {
  infrastructure_id = data.metalcloud_infrastructure.infra.infrastructure_id

  # Set this to false to actually trigger deploys.
  prevent_deploy = true

  # These options will make terraform apply operation will wait for the deploy to finish (when prevent_deploy is false)
  # instead of exiting while the deploy is ongoing
  await_deploy_finish = false

  # This option disables a safety check that metalsoft performs to prevent accidental data loss
  # It is required when testing delete operations
  allow_data_loss = true

  # IMPORTANT. This is important to ensure that deploys happen after everything else. If you need to add or remove resources dynamically
  # use either count or for_each in the resources or move everything that is dynamic into a module and make this depend on the module
  depends_on = [
      metalcloud_server_instance_group.inst01,
  ]
}

variable "endpoint" {
  default =""
}

variable "api_key" {
  default = ""
}

variable "logging" {
  default="false"
}

variable "site" {
  default=""
}

Initialize the provider with your API key and the api endpoint either by using environment variables or -var.

export TF_VAR_api_key="<yourkey>"
export TF_VAR_user_email="[email protected]"
export TF_VAR_endpoint="https://api.env.metalsoft.io/metal-cloud"
export TF_VAR_site="uk-reading"

Initialize the provider:

terraform init

To deploy this infrastructure export the following variables (or use -var):

The plan phase:

terraform plan

The apply phase:

terraform apply

To delete the infrastrucure:

terraform destroy