megaport / terraform-provider-megaport

Terraform Provider for Megaport Network
Mozilla Public License 2.0
30 stars 16 forks source link

Terarfrom plan fails on Megaport Staging Environment after resource cleanup #73

Closed Kostoprav19 closed 3 months ago

Kostoprav19 commented 5 months ago

Hello!

I am trying to create CD pipeline that will test Terraform code in Megaport Staging environment on Merge request. Megaport Staging environment is overwritten every 24 hours and synchronized with the production environment. So any changes that were deployed and saved in Terraform state are deleted. https://docs.megaport.com/troubleshooting/staging/

code that I am running

resource "megaport_port" "ipxgrx" {
  product_name           = "${var.name_prefix}-Port"
  port_speed             = var.ipxgrx_port_speed
  location_id            = data.megaport_location.this.id
  contract_term_months   = var.ipxgrx_port_contract_term_months
  marketplace_visibility = var.marketplace_visibility
  cost_centre            = var.cost_centre
}

My expectation is that when Terraform plan detects that resources are gone it will not fail but will try to recreate them (as Terraform is declarative infrastructure management tool).

But instead I get error while running terraform plan

Error: Error Reading port
│ 
│   with module.megaport-lbo.megaport_port.ipxgrx,
│   on .terraform/modules/megaport-lbo/main.tf line 1, in resource "megaport_port" "ipxgrx":
│    1: resource "megaport_port" "ipxgrx" {
│ 
│ Could not read port with ID d2c085e1-5a57-4f20-8f20-94dd055b108d: GET https://api-staging.megaport.com/v2/product/d2c085e1-5a57-4f20-8f20-94dd055b108d: 400 (trace_id "637184185824fe32920e62a17e9c11e6") Could not find a service with UID = d2c085e1-5a57-4f20-8f20-94dd055b108d
mega-alex commented 5 months ago

This is expected behavior with how terraform handles state. You could try terraform apply -refresh-only, see the documentation here.

Kostoprav19 commented 4 months ago

@mega-alex unfortunately it does not work in this case. Here is my example in Staging Portal where all resources where removed.

terraform apply -var-file test-config.tfvars -refresh-only -auto-approve
data.aws_secretsmanager_secret.megaport: Reading...
data.aws_secretsmanager_secret.megaport: Read complete after 0s [id=arn:aws:secretsmanager:eu-central-1:893677514652:secret:megaport-dev-EMVzzv]
data.aws_secretsmanager_secret_version.megaport: Reading...
data.aws_secretsmanager_secret_version.megaport: Read complete after 0s [id=arn:aws:secretsmanager:eu-central-1:893677514652:secret:megaport-dev-EMVzzv|AWSCURRENT]
data.megaport_location.this: Reading...
data.megaport_location.this: Read complete after 9s [name=Interxion FRA6]
megaport_port.ipxgrx: Refreshing state...
data.megaport_partner.aws_port: Reading...
data.megaport_partner.internet: Reading...
megaport_mve.fortigate: Refreshing state...
data.megaport_partner.internet: Read complete after 6s
data.megaport_partner.aws_port: Still reading... [10s elapsed]
╷
│ Warning: Value for undeclared variable
│ 
│ The root module does not declare a variable named "tags" but a value was found in file "test-config.tfvars". If you meant to use this value, add a "variable" block to the configuration.
│ 
│ To silence these warnings, use TF_VAR_... environment variables to provide certain "global" settings to all configurations in your organization. To reduce the verbosity of these warnings, use the -compact-warnings option.
╵
╷
│ Error: Error Reading port
│ 
│   with megaport_port.ipxgrx,
│   on main.tf line 2, in resource "megaport_port" "ipxgrx":
│    2: resource "megaport_port" "ipxgrx" {
│ 
│ Could not read port with ID 8eabde19-929f-436b-b9cc-7f36eb8fbd2c: GET https://api-staging.megaport.com/v2/product/8eabde19-929f-436b-b9cc-7f36eb8fbd2c: 400 (trace_id "f02587d7cde8ab088cb4cd24f0187d0b") Could not find a service with UID = 8eabde19-929f-436b-b9cc-7f36eb8fbd2c 
╵
╷
│ Error: Error Reading MVE
│ 
│   with megaport_mve.fortigate,
│   on main.tf line 12, in resource "megaport_mve" "fortigate":
│   12: resource "megaport_mve" "fortigate" {
│ 
│ Could not read MVE with ID a59bbe09-a36c-4b08-b654-e7dbb4fc13e7: GET https://api-staging.megaport.com/v2/product/a59bbe09-a36c-4b08-b654-e7dbb4fc13e7: 400 (trace_id "10bea7d13d9ef1e98fbf5110e684f0b7") Could not find a service with UID = a59bbe09-a36c-4b08-b654-e7dbb4fc13e7 
mega-alex commented 4 months ago

The PR waiting on an API change to go in, but after that this should work and recreate resources that are missing in the staging environment.