cycloidio / terracognita

Reads from existing public and private cloud providers (reverse Terraform) and generates your infrastructure as code on Terraform configuration
MIT License
2.17k stars 163 forks source link

Import fails on vsphere when template/-s exist #364

Open fatspiderlinux opened 1 year ago

fatspiderlinux commented 1 year ago

General information: Terracognita goes fine through all the resources but when it comes to vsphere_virtual_machine and a template is found it exits with an error.

Describe the bug:

I am trying to map existing vsphere infra and when I run terracognita specifying all the resources but vsphere_virtual_machine it works perfectly fine. When I try to discover virtual machines as well it exits with an error.

Log message

All good:

# terracognita vsphere --insecure --password "xxx" --username "xxx" --vsphereserver "xxx" --tfstate resources.tfstate --hcl resources.hcl --soap-url https://xxx/sdk -i vsphere_compute_cluster,vsphere_resource_pool,vsphere_datacenter,vsphere_folder,vsphere_datastore_cluster
Starting Terracognita with version v0.8.3
Importing with filters:
        Tags:    [],
        Include: [vsphere_compute_cluster vsphere_resource_pool vsphere_datacenter vsphere_folder vsphere_datastore_cluster],
        Exclude: [],
        Targets: [],
Importing vsphere_compute_cluster [1/1] Done!
Importing vsphere_resource_pool [1/1] Done!
Importing vsphere_datacenter [1/1] Done!
Importing vsphere_folder [8/8] Done!
Writing HCL Done!
Writing TFState Done!

Borked:

ts=2023-03-24T17:54:18.706597953Z caller=import.go:105 func=provider.Import resource=vsphere_virtual_machine id=/VxRail-Datacenter/vm/template.rocky.9 total=26 current=3 msg="reading from TF"
Error: could not import from vsphere: could not import resource vsphere_virtual_machine with id /VxRail-Datacenter/vm/template.rocky.9: VM "/VxRail-Datacenter/vm/template.rocky.9" is a template and cannot be imported
fatspiderlinux commented 1 year ago

The above error exists for vsphere version 7.0.3, I just tried to import from older vsphere 6.0.0 and got the same issue:

ts=2023-03-24T18:15:59.200093938Z caller=import.go:105 func=provider.Import resource=vsphere_virtual_machine id=/Europe/vm/Templates/template-x-dbmxx_1 total=173 current=111 msg="reading from TF"
Error: could not import from vsphere: could not import resource vsphere_virtual_machine with id /Europe/vm/Templates/template-slvr-dbmxx_1: VM "/Europe/vm/Templates/template-x-dbmxx_1" is a template and cannot be imported
fatspiderlinux commented 1 year ago

Looks like this error comes from vsphere provider itself: https://github.com/hashicorp/terraform-provider-vsphere/blob/main/vsphere/resource_vsphere_virtual_machine.go#L1138

xescugc commented 1 year ago

Hi @fatspiderlinux

If the errors is directly from the Provider it means they intended this type to not be imported when that condition happens, at the end TerraCognita tries to use the provider defined import so there is not much we can do around that.

I've checked a bit deeper and there is no specific reason on the PR/Commit in which this was introduced.

Jeff-Nasseri-Azihub commented 4 months ago

it would be great if we had something to filter in Terracognita, maybe by using regex, we could only import vms which has not any "template" key on it's name, normally vsphere includes templates in the list of VM, by checking this API "/api/vcenter/vm" you can see it.

shacharSirotkin commented 2 months ago

Hi, It's been a while but.. Did you find any solution to avoid all templates?

fatspiderlinux commented 1 month ago

Hey, unfortunately not.