terraform-routeros / terraform-provider-routeros

Terraform Provider for Mikrotik RouterOS
Mozilla Public License 2.0
205 stars 56 forks source link

Unable to import routeros_interface_ethernet #375

Closed bennysp closed 7 months ago

bennysp commented 8 months ago

Describe the bug

I have the below setup in terraform:

resource "routeros_interface_ethernet" "combo1" {
  factory_name = "combo1"
  name = "combo1"
}

resource "routeros_interface_ethernet" "ether4" {
  factory_name = "ether4"
  name = "ether4"
}

I have this on the print out in RouterOS:

[admin@mtrouter] > :put [/interface/ethernet get [print show-ids]]
Flags: R - RUNNING; S - SLAVE
Columns: NAME, MTU, MAC-ADDRESS, ARP
*     NAME           MTU  MAC-ADDRESS        ARP    
*2 RS combo1        1500  CC:2D:E0:CA:4E:C5  enabled
*3 RS ether1        1500  CC:2D:E0:CA:4E:C6  enabled
*4  S ether2        1500  CC:2D:E0:CA:4E:C7  enabled
*5 RS ether3        1500  CC:2D:E0:CA:4E:C8  enabled
*6 RS ether4        1500  CC:2D:E0:CA:4E:C9  enabled
*7 RS ether5        1500  CC:2D:E0:CA:4E:CA  enabled
*8 RS ether6        1500  CC:2D:E0:CA:4E:CB  enabled
*9  S ether7        1500  CC:2D:E0:CA:4E:CC  enabled
*1 R  sfp-sfpplus1  1500  CC:2D:E0:CA:4E:C4  enabled

When I go to run tofu import routeros_interface_ethernet.ether4 "*6" and tofu import routeros_interface_ethernet.combo1 "*2", I get this error:

data.vault_generic_secret.mikrotik: Reading...
data.vault_generic_secret.mikrotik: Read complete after 0s [id=secret/mikrotik/creds]
routeros_interface_ethernet.ether4: Importing from ID "*6"...
routeros_interface_ethernet.ether4: Import prepared!
  Prepared routeros_interface_ethernet for import
routeros_interface_ethernet.ether4: Refreshing state... [id=*6]
data.routeros_firewall.fw: Reading...
data.routeros_firewall.fw: Read complete after 1s [id=terraform-20240315230300530800000002]
╷
│ Error: unable to find interface
│ 
│ 
╵

To Reproduce Please see above.

Expected behavior Expected the import to complete

Stack Trace NA

Additional context I am using RouerOS 7.13 and latest routeros tf provider 1.34,0. I am using OpenTofu, but this happens with Terraform too.

durandguru commented 8 months ago

Some resources can not be imported. I think ethernet interfaces are also that type. In my project I could overwrite the settings on the router with terraform without importing.

vaerh commented 7 months ago

@jlpedrosa Please tell me, what was the reason to use the filter by interface name and not by its Id? Import does not work:

routeros_interface_ethernet.ether5: Importing from ID "*5"...
routeros_interface_ethernet.ether5: Import prepared!
  Prepared routeros_interface_ethernet for import
routeros_interface_ethernet.ether5: Refreshing state... [id=*5]
[DEBUG] provider.terraform-provider-routeros_v1.32.4: GET request URL:  https://172.16.3.1/rest/interface/ethernet?default-name=: tf_req_id=30811585-bf86-da11-52ab-0589d0821337 tf_rpc=Configure 
[DEBUG] provider.terraform-provider-routeros_v1.32.4: response body: []: @module=routeros tf_provider_addr=terraform-routeros/routeros tf_req_id=30811585-bf86-da11-52ab-0589d0821337 tf_rpc=Configure @
bennysp commented 7 months ago

Per the documentation it is importable @durandguru. The documentation shows using ID: https://registry.terraform.io/providers/terraform-routeros/routeros/1.34.0#import

(I will probably do that @durandguru too, but I wanted to report this for awareness too.)

vaerh commented 7 months ago

A little patience. There is a small nuance in the implementation of the resource, because of which the import does not work.

jlpedrosa commented 7 months ago

@vaerh are you refering to: "findInterfaceByDefaultName" ?

That is how many of the scripts for Mikrotik work (in their samples), as names can be reset/updated, but the underlying factory-name is the same, we use that field to retrieve the actual ID.

filter := buildReadFilter(map[string]interface{}{"default-name": d.Get("factory_name")})
jlpedrosa commented 7 months ago

The interface still should be importable by ID, I'm not sure why it's not working, I'm trying to troubleshoot it.

jlpedrosa commented 7 months ago

PR in Draft. It works, but I'd like to add an integration test for import so we don't hit this again in the future. https://github.com/terraform-routeros/terraform-provider-routeros/pull/379

vaerh commented 7 months ago

Yes, I was referring to that exact function. The problem is that we can't access to factory_name when importing. It seems to me that for interfaces, the Id should not change during the whole functioning time. I could have fixed this yesterday, but I didn't want to break anything. I'll have a look at the PR on my lab tonight.

jlpedrosa commented 7 months ago

The PR is ready for review, forgot to update here. Basically if we have the ID already (import scenario) we don't do the lookup

vaerh commented 7 months ago

:tada: This issue has been resolved in version 1.37.1 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: