TRITON-2500

triton terraform provider should support import

Status:
Resolved
Created:
2025-08-18T11:07:43.751-0400
Updated:
2025-08-28T11:08:39.911-0400

Description

Now that we have an updated provider with SDKv2, we should implement import to make the provider more useful for managing existing resources.

Comments (2)

Jhonas Wernery commented on 2025-08-25T13:14:30.167-0400:

Testing notes:

❯ git status
On branch TRITON-2500
Your branch is up to date with 'origin/TRITON-2500'.

❯ eval "$(triton env)"

❯ make testacc
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v  -timeout 120m
?   	github.com/TritonDataCenter/terraform-provider-triton	[no test files]
=== RUN   TestAccTritonAccount
--- PASS: TestAccTritonAccount (6.31s)
=== RUN   TestAccTritonDataCenter
--- PASS: TestAccTritonDataCenter (5.50s)
=== RUN   TestAccTritonFabricNetwork_MissingArguments
--- PASS: TestAccTritonFabricNetwork_MissingArguments (0.11s)
=== RUN   TestAccTritonFabricNetwork_BadArguments
--- PASS: TestAccTritonFabricNetwork_BadArguments (0.11s)
=== RUN   TestAccTritonFabricNetwork_NotFound
--- PASS: TestAccTritonFabricNetwork_NotFound (7.53s)
=== RUN   TestAccTritonFabricNetwork_Basic
--- PASS: TestAccTritonFabricNetwork_Basic (23.02s)
=== RUN   TestAccTritonFabricVLAN_MissingArguments
--- PASS: TestAccTritonFabricVLAN_MissingArguments (0.34s)
=== RUN   TestAccTritonFabricVLAN_BadArguments
--- PASS: TestAccTritonFabricVLAN_BadArguments (0.12s)
=== RUN   TestAccTritonFabricVLAN_Basic
--- PASS: TestAccTritonFabricVLAN_Basic (12.36s)
=== RUN   TestAccTritonFabricVLAN_WildCard
--- PASS: TestAccTritonFabricVLAN_WildCard (13.64s)
=== RUN   TestAccTritonFabricVLAN_Filters
--- PASS: TestAccTritonFabricVLAN_Filters (13.29s)
=== RUN   TestAccTritonFabricVLAN_NotFound
--- PASS: TestAccTritonFabricVLAN_NotFound (1.00s)
=== RUN   TestAccTritonFabricVLAN_FiltersNotFound
--- PASS: TestAccTritonFabricVLAN_FiltersNotFound (8.76s)
=== RUN   TestAccTritonFabricVLAN_MultipleFound
--- PASS: TestAccTritonFabricVLAN_MultipleFound (8.37s)
=== RUN   TestAccTritonImage_multipleResults
--- PASS: TestAccTritonImage_multipleResults (0.95s)
=== RUN   TestAccTritonImage_noResults
--- PASS: TestAccTritonImage_noResults (0.93s)
=== RUN   TestAccTritonImage_mostRecent
--- PASS: TestAccTritonImage_mostRecent (5.00s)
=== RUN   TestAccTritonImage_nameVersionAndMostRecent
--- PASS: TestAccTritonImage_nameVersionAndMostRecent (4.96s)
=== RUN   TestAccTritonNetwork_Basic
--- PASS: TestAccTritonNetwork_Basic (15.05s)
=== RUN   TestAccTritonNetwork_NotFound
--- PASS: TestAccTritonNetwork_NotFound (1.36s)
=== RUN   TestAccTritonPackage_basic
--- PASS: TestAccTritonPackage_basic (5.56s)
=== RUN   TestAccTritonDataVolume_basic
--- PASS: TestAccTritonDataVolume_basic (83.87s)
=== RUN   TestAccTritonDataVolume_noResults
--- PASS: TestAccTritonDataVolume_noResults (0.93s)
=== RUN   TestAccTritonDataVolume_BadSize
--- PASS: TestAccTritonDataVolume_BadSize (0.12s)
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestFabricMigrateState
--- PASS: TestFabricMigrateState (0.00s)
=== RUN   TestAccTritonFabric_basic
--- PASS: TestAccTritonFabric_basic (25.59s)
=== RUN   TestAccTritonFirewallRule_basic
--- PASS: TestAccTritonFirewallRule_basic (8.88s)
=== RUN   TestAccTritonFirewallRule_update
--- PASS: TestAccTritonFirewallRule_update (14.64s)
=== RUN   TestAccTritonFirewallRule_enable
--- PASS: TestAccTritonFirewallRule_enable (13.46s)
=== RUN   TestAccTritonFirewallRule_heredoc
--- PASS: TestAccTritonFirewallRule_heredoc (6.78s)
=== RUN   TestAccTritonKey_basic
--- PASS: TestAccTritonKey_basic (23.84s)
=== RUN   TestAccTritonKey_noKeyName
--- PASS: TestAccTritonKey_noKeyName (21.62s)
=== RUN   TestAccTritonMachine_basic
--- PASS: TestAccTritonMachine_basic (114.91s)
=== RUN   TestAccTritonMachine_affinity
--- PASS: TestAccTritonMachine_affinity (125.31s)
=== RUN   TestAccTritonMachine_dns
--- PASS: TestAccTritonMachine_dns (109.47s)
=== RUN   TestAccTritonMachine_nic
--- PASS: TestAccTritonMachine_nic (112.26s)
=== RUN   TestAccTritonMachine_addNIC
--- PASS: TestAccTritonMachine_addNIC (342.91s)
=== RUN   TestAccTritonMachine_firewall
--- PASS: TestAccTritonMachine_firewall (118.32s)
=== RUN   TestAccTritonMachine_metadata
--- PASS: TestAccTritonMachine_metadata (229.12s)
=== RUN   TestAccTritonMachine_cns
--- PASS: TestAccTritonMachine_cns (164.27s)
=== RUN   TestAccTritonMachine_locality
--- PASS: TestAccTritonMachine_locality (194.08s)
=== RUN   TestAccTritonMachine_deletionProtection
--- PASS: TestAccTritonMachine_deletionProtection (136.08s)
=== RUN   TestAccTritonMachine_volume
--- PASS: TestAccTritonMachine_volume (170.15s)
=== RUN   TestAccTritonSnapshot_basic
--- PASS: TestAccTritonSnapshot_basic (128.75s)
=== RUN   TestAccTritonVLAN_basic
--- PASS: TestAccTritonVLAN_basic (8.72s)
=== RUN   TestAccTritonVLAN_update
--- PASS: TestAccTritonVLAN_update (13.46s)
=== RUN   TestAccTritonVolume_basic
--- PASS: TestAccTritonVolume_basic (84.14s)
=== RUN   TestAccTritonVolume_singleNetwork
--- PASS: TestAccTritonVolume_singleNetwork (88.96s)
=== RUN   TestWildcardMatch
--- PASS: TestWildcardMatch (0.00s)
=== RUN   TestValidateVLANIdentifier
--- PASS: TestValidateVLANIdentifier (0.00s)
PASS
ok  	github.com/TritonDataCenter/terraform-provider-triton/triton	2474.911s

Jhonas Wernery commented on 2025-08-27T07:07:15.115-0400:

Additional testing notes, this time trying to import a manually created instance, snapshot, vlan and more.

First, we configure terraform to use the development provider for triton from our filesystem instead of trying to download it from registry.terraform.io:

❯ cat ~/.terraformrc
provider_installation {

  # Use /home/developer/tmp/terraform-null as an overridden package directory
  # for the hashicorp/null provider. This disables the version and checksum
  # verifications for this provider and forces Terraform to look for the
  # null provider plugin in the given directory.
  dev_overrides {
    "tritondatacenter/triton" = "/home/teutat3s/.local/share/go/bin"
  }

  # For all other providers, install them directly from their origin provider
  # registries as normal. If you omit this, Terraform will _only_ use
  # the dev_overrides block, and so no other providers will be available.
  direct {}
}

Next, we build and install (copy) the provider binary to the above directory:

❯ make install
==> Checking that code complies with gofmt requirements...
go build -v ./...
github.com/TritonDataCenter/terraform-provider-triton
go install -v ./...

Now we need to create a terraform configuration file to reference in our import statements.

❯ cat triton.tf
terraform {
  required_providers {
    triton = {
      source = "tritondatacenter/triton"
    }
  }
}

provider "triton" {
  # Configuration options
}

resource "triton_fabric" "test-import" {
  vlan_id            = 1782
  name               = "acctest-1445757892313670223-network"
  description        = "test network"
  subnet             = "10.51.0.0/24"
  provision_start_ip = "10.51.0.10"
  provision_end_ip   = "10.51.0.250"
  gateway            = "10.51.0.1"
  resolvers          = ["8.8.8.8", "8.8.4.4"]

  internet_nat = true
}
resource "triton_firewall_rule" "test-import" {
  description = ""
  rule        = "FROM any TO vm a204840c-f2df-49ca-a835-47f29ce2daef ALLOW tcp (PORT 80 AND PORT 443)"
  enabled     = false
}
resource "triton_machine" "test-import" {
  name    = "acctest-1445757892313670223-instance"
  package = "g1.nano"
  image   = "2f1dc911-6401-4fa4-8e9d-67ea2e39c271"
  networks = [
    "9723e3d2-c91b-4545-8882-78cfafb116af"
  ]

  cns {
    services = ["test"]
  }

  tags = {
    test = "Test"
  }
}
resource "triton_key" "test-import" {
  name = "YubiKey-5-NFC"
  key  = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFro/k4Mgqyh8yV/7Zwjc0dv60ZM7bROBU9JNd99P/4co6fxPt1pJiU/pEz2Dax/HODxgcO+jFZfvPEuLMCeAl0= YubiKey #10593996 PIV Slot 9a"
}
resource "triton_snapshot" "test-import" {
  name       = "20250827T095828Z"
  machine_id = triton_machine.test-import.id
}
resource "triton_vlan" "test-import" {
  vlan_id     = 1782
  name        = "acctest-1445757892313670223-vlan"
  description = "test vlan"
}

Now we’re ready to start importing resources:

❯ terraform import triton_key.test-import "YubiKey-5-NFC"
triton_key.test-import: Importing from ID "YubiKey-5-NFC"...
triton_key.test-import: Import prepared!
  Prepared triton_key for import
triton_key.test-import: Refreshing state... [id=YubiKey-5-NFC]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

[...]

❯ terraform import triton_snapshot.test-import 6fe0b6e9-3c1a-437a-8ab2-4e234ec5c627.20250827T095828Z
triton_snapshot.test-import: Importing from ID "6fe0b6e9-3c1a-437a-8ab2-4e234ec5c627.20250827T095828Z"...
triton_snapshot.test-import: Import prepared!
  Prepared triton_snapshot for import
triton_snapshot.test-import: Refreshing state... [id=20250827T095828Z]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

After importing all resources using their identifiers, terraform plan output shows no changes, as expected:

❯ terraform plan -out test-import.plan
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - tritondatacenter/triton in /home/teutat3s/.local/share/go/bin
│
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
triton_key.test-import: Refreshing state... [id=YubiKey-5-NFC]
triton_firewall_rule.test-import: Refreshing state... [id=acaeb30a-560d-47c3-a260-e1c3fcc919c5]
triton_vlan.test-import: Refreshing state... [id=1782]
triton_fabric.test-import: Refreshing state... [id=f5746f6b-077a-4316-b3dd-0b9dbca75231]
triton_machine.test-import: Refreshing state... [id=6fe0b6e9-3c1a-437a-8ab2-4e234ec5c627]
triton_snapshot.test-import: Refreshing state... [id=20250827T095828Z]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Related Links