citrix / terraform-provider-citrix

Terraform Provider for Citrix
https://registry.terraform.io/providers/citrix/citrix/latest
Apache License 2.0
45 stars 5 forks source link

Import citrix_daas_machine_catalog (Resource) not working #33

Closed dimi4ik closed 8 months ago

dimi4ik commented 9 months ago

Terraform Core Version

he semantic version of Terraform Core used when experiencing the bug. If multiple versions have been tested, a comma separated list.

Terraform v1.5.6 on linux_amd64

provider registry.terraform.io/citrix/citrix v0.3.6 provider registry.terraform.io/hashicorp/azurerm v3.87.0 provider registry.terraform.io/hashicorp/random v3.6.0

Description

I want to import a manually created machine catalog using Terraform and I get an error:

error: Error updating Machine Catalog │ │ with citrix_daas_machine_catalog.mc, │ on 06_machine_catalog_import_case.tf line 2, in resource "citrix_daas_machine_catalog" "mc": │ 2: resource "citrix_daas_machine_catalog" "mc" { │ │ Failed to resolve service offering on Azure, error: could not find resource

for me it is very interesting that terraform plan runs without errors, but terraform apply throws an error

If you need further logs/data from me, please send me a PM

Affected Resource(s) Please list the affected resource(s) and/or data source(s). ==============================
citrix_daas_machine_catalog

Expected Behavior What should have happened?

Import machine catalog with Terraform

Relevant Error/Panic Output Snippet If applicable, provide a relevant snippet from the error or panic output. This will be rendered as shell, so there is no need to add a

error: Error updating Machine Catalog │ │ with citrix_daas_machine_catalog.mc, │ on 06_machine_catalog_import_case.tf line 2, in resource "citrix_daas_machine_catalog" "mc": │ 2: resource "citrix_daas_machine_catalog" "mc" { │ │ Failed to resolve service offering on Azure, error: could not find resource

Terraform Configuration Files ===========================

here is the code, I want to import the machine catalog with terraform: the code was extracted from the terraform state file and written to a tf file

# citrix_daas_machine_catalog.mc:
resource "citrix_daas_machine_catalog" "mc" {
    allocation_type     = "Random"
    is_power_managed    = true
    is_remote_pc        = false
    name                = "001-P-HSD-NON-xxx-00111"
    provisioning_scheme = {
        availability_zones             = ""
        machine_account_creation_rules = {
            domain             = "m001.abxcloud.ch"
            domain_ou          = "OU=HSD,OU=xxx,OU=_Management,DC=m001,DC=firma,DC=ch"
            naming_scheme      = "vm0010500##"
            naming_scheme_type = "Numeric"
        }
        machine_config                 = {
            hypervisor               = "xxxxx"
            hypervisor_resource_pool = "xxxx"
            machine_profile          = "xxx001050000"
            master_image             = "xxx001050000_OsDisk_1_xxxx"
            resource_group           = "rg-ctx-prod-ch-xxx"
                  service_account          = "user"
      service_account_password = var.service_account_password
        }
        number_of_total_machines       = 1
        storage_type                   = "StandardSSD_LRS"
        use_managed_disks              = true
    }
    provisioning_type   = "MCS"
    session_support     = "MultiSession"
    zone                = "1e129f43-1efc-486c-a61b-0461125aa0a4"
}
zhuolun-citrix commented 8 months ago

Hi @dimi4ik ,

There are a couple of things that might have gone wrong in your process:

  1. You mentioned that you created a catalog manually and tried to import it into Terraform. However, you were using terraform apply instead of terraform import. Since the catalog already exist, you will eventually get an error complaining about catalog name conflict.
  2. When creating an Azure MCS catalog, you will have to specify the provisioning_scheme.machine_config.service_offering field in your terraform, as suggested in the documentation: https://registry.terraform.io/providers/citrix/citrix/latest/docs/resources/daas_machine_catalog#service_offering. However, we understand that the existing documentation is quite cumbersome to understand as there are a lot of fields shared across multiple hypervisors that have different semantic meaning according to the context. There is an upcoming release to address this problem and hopefully make the user experience smoother.

Please let me know if you are able to import your catalog once you try the above solution.

Thank you.

dimi4ik commented 8 months ago

Hello @zhuolun-citrix 👍 and thank you for your reply,

perhaps I did not write down correctly what I did and would like to explain it again in more detail:


resource "citrix_daas_machine_catalog" "mc" {

}
citrix_daas_machine_catalog.mc: Import prepared!
  Prepared citrix_daas_machine_catalog for import
citrix_daas_machine_catalog.mc: Refreshing state... [id=xxxxxx-406d-b1a3-xxxxxxxx]

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.
│ Error: Incorrect attribute value type
│ 
│   on 06_machine_catalog_import.tf line 7, in resource "citrix_daas_machine_catalog" "mc":
│    provisioning_scheme = {
│         availability_zones = ""
│         machine_account_creation_rules = {
│          domain             = "xxxx.de"
│          domain_ou          = "OU=HSD,DC=muster,DC=com"
│          naming_scheme      = "xxx0010500##"
│          naming_scheme_type = "Numeric"
│        }
│        machine_config = {
│          hypervisor               = "xxxxxx-4710-4c58-add8-xxxxxx"
│          hypervisor_resource_pool = "xxxxxx-a2e4a6e-5a7a-4429-b950-xxxxxxx"
│          machine_profile          = "xxx001050000"
│          master_image             = "xxx001050000_OsDisk_1_xxxxxxxxxxxxxxxx"
│          resource_group           = "rg-ctx-prod-xx-m001"
│        }
│        number_of_total_machines = 1
│        storage_type             = "StandardSSD_LRS"
│        use_managed_disks        = true
│      }
│ 
│ Inappropriate value for attribute "provisioning_scheme": attribute "machine_config": attributes "service_account" and "service_account_password" are required.
machine_config = {
│          hypervisor               = "xxxxxx-4710-4c58-add8-xxxxxx"
│          hypervisor_resource_pool = "xxxxxx-a2e4a6e-5a7a-4429-b950-xxxxxxx"
│          machine_profile          = "xxx001050000"
│          master_image             = "xxx001050000_OsDisk_1_xxxxxxxxxxxxxxxx"
│          resource_group           = "rg-ctx-prod-xx-m001"
  NEW      service_account          = "azureuser"
  NEW      service_account_password = "xxxxx"

**Plan: 0 to add, 2 to change, 0 to destroy**.
│ Error: Error updating Resource Pool xxxxxxxxxxxxxxx
│ 
│   with citrix_daas_azure_hypervisor_resource_pool.resource_pool,
│   on 05_hypervisor.tf line 12, in resource "citrix_daas_azure_hypervisor_resource_pool" "resource_pool":
│   12: resource "citrix_daas_azure_hypervisor_resource_pool" "resource_pool" {
│ 
│ TransactionId: e1dd07fc-eef9-4e72-9994-006079cd3999
│ JobId: 13f61dd1-4b21-496d-9642-6b79560b7471
│ Error message: Cannot rename Hosting Unit. An object with the same name already exists.
╵
╷
│ Error: Error updating Machine Catalog
│ 
│   with citrix_daas_machine_catalog.mc,
│   on 06_machine_catalog_import.tf line 1, in resource "citrix_daas_machine_catalog" "mc":
│    1: resource "citrix_daas_machine_catalog" "mc" {
│ 
│ Failed to resolve service offering  on Azure, error: could not find resource

If I want to retrieve the information from the terraform state with the command

terraform state rm citrix_daas_machine_catalog.mc

and create a new TF file with a different name for the machine catalog, I think the new MC should be created. Therefore, I think the error lies with Terraform Import.

I actually want to create a new MC catalog with Terraform, and unfortunately I cannot. The examples in the repository are not usable for me, because I don't have a VHDX file on the Azure Storage account. Instead, I have a master MCS VM.

Can you please either review Terraform Import or give me an example of how I can create a MC catalog?

Just FYI, I was able to do this with the old provider version. 💯

Thanks Dima

zhuolun-citrix commented 8 months ago

Hi @dimi4ik ,

There are some nuances that lies within terraform provider framework design and terraform in general about import. What terraform import does is a best effort attempt to first read the resource from remote, and then map it to the schema model. Unfortunately due to the special use cases, there will be certain fields that could not be imported.

I would strongly advice not using the result of terraform import + terraform show as is to get the terraform from an existing resource. You can of course use the result as an example, but you will also need to inspect terraform and cross reference the documentation to make sure that all fields mandatory for your use case (which is Azure MCS in this case) are presented.

We will have a new release soon that introduces some breaking change to improve the schema validation and documentation of our provider. I believe that the change will also help smoothen the experience you get with terraform import + terraform show for reverse engineering a TF for the existing env. In fact, we also have this automated on our side, but yet to be released. Please stay tuned to the future release.

zhuolun-citrix commented 8 months ago

From the error message your shared in the comment, I saw that there were two issues:

  1. Terraform not able to create the hypervisor resource pool because it already exists, which I assume was created manually. In that case you will have to import it to terraform state, or change the name of the one you are creating with terraform to avoid name clash.
  2. Service offering for the machine catalog not being resolved. That was because you didn't have service_offering specified in your TF, while service_offering is a mandatory field for Azure MCS.

From the following comment:

I actually want to create a new MC catalog with Terraform, and unfortunately I cannot. The examples in the repository are not usable for me, because I don't have a VHDX file on the Azure Storage account. Instead, I have a master MCS VM.

I assume that your were trying to use a VM's managed disk / snapshot as the image of the machine catalog. In that case, you don't have to specify storage_account or container. Simply specify the resource_group that contains the VM snapshot / managed disk, and the name of the snapshot / managed disk in master_image as suggested in the doc: https://registry.terraform.io/providers/citrix/citrix/latest/docs/resources/daas_machine_catalog#master_image

So your machine_config will be:

machine_config = { hypervisor = "xxxxxx-4710-4c58-add8-xxxxxx" hypervisor_resource_pool = "xxxxxx-a2e4a6e-5a7a-4429-b950-xxxxxxx" service_offering = "Standard_D2_v2" master_image = "xxx001050000_OsDisk_1_xxxxxxxxxxxxxxxx" resource_group = "rg-ctx-prod-xx-m001" }

dimi4ik commented 8 months ago

@zhuolun-citrix Thank you for your comment. 👍 As you described, I tried to create an MC, but unfortunately without success. Now I am waiting for the new version of Terraform, then I will try again and test it. 🥇

AlanCitrix commented 7 months ago

In fact, we also have this automated on our side, but yet to be released. Please stay tuned to the future release.

It's released now! This script can import your existing resources and create the TF files: https://github.com/citrix/terraform-provider-citrix/tree/main/scripts/onboarding-helper