terraform-coop / terraform-provider-foreman

Terraform provider for Foreman
https://registry.terraform.io/providers/terraform-coop/foreman
Mozilla Public License 2.0
33 stars 31 forks source link

Issue with reading foreman_host resource parameters #91

Closed togoschi closed 1 year ago

togoschi commented 1 year ago

Latest provider version is no longer able to read host parameters which are already set in foreman. The resulting error when running tf apply is as follows (tf plan isn't affected)

Error: Invalid index
│ 
│   on provision.tf line 212, in data "template_file" "geo_setup":
│  212:     cs_master_ip = "${foreman_host.vxccph03.parameters["cluster_ipaddress"]}"
│     ├────────────────
│     │ foreman_host.vxccph03.parameters is empty map of string
│ 
│ The given key does not identify an element in this collection value.
╵

But the missing key is available in API

$ curl -s -k https://servername/api/v2/hosts/808 | jq '.parameters?'
[
...
  {
    "priority": 70,
     ...
    "id": 4382,
    "name": "cluster_ipaddress",
    "value": "10.6.0.3"
  }
]

Terraform TRACE log shows

2022-07-20T14:05:26.006+0200 [TRACE] provider.terraform-provider-foreman_v0.5.3: Called downstream: tf_rpc=ApplyResourceChange tf_req_id=1270a616-94fb-a021-a637-4bc01de7a868 @module=sdk.helper_schema tf_provider_addr=provider tf_resource_type=fore
man_host @caller=github.com/hashicorp/terraform-plugin-sdk/v2@v2.16.0/helper/schema/resource.go:848 timestamp=2022-07-20T14:05:26.006+0200
2022-07-20T14:05:26.007+0200 [TRACE] provider.terraform-provider-foreman_v0.5.3: Called downstream: tf_rpc=ApplyResourceChange @caller=github.com/hashicorp/terraform-plugin-go@v0.9.0/tfprotov5/tf5server/server.go:817 tf_proto_version=5.2 tf_provid
er_addr=provider tf_req_id=1270a616-94fb-a021-a637-4bc01de7a868 @module=sdk.proto tf_resource_type=foreman_host timestamp=2022-07-20T14:05:26.007+0200
2022-07-20T14:05:26.007+0200 [TRACE] provider.terraform-provider-foreman_v0.5.3: Served request: @caller=github.com/hashicorp/terraform-plugin-go@v0.9.0/tfprotov5/tf5server/server.go:824 tf_proto_version=5.2 tf_provider_addr=provider tf_rpc=ApplyR
esourceChange @module=sdk.proto tf_req_id=1270a616-94fb-a021-a637-4bc01de7a868 tf_resource_type=foreman_host timestamp=2022-07-20T14:05:26.007+0200
2022-07-20T14:05:26.008+0200 [WARN]  Provider "provider[\"registry.terraform.io/hansemerkur/foreman\"]" produced an unexpected new value for foreman_host.vxccph03, but we are tolerating it because it is using the legacy plugin SDK.
    The following problems may be the cause of any confusing errors from downstream operations:
      - .parameters: element "bus_id_cluster" has vanished
      - .parameters: element "bus_id_infra" has vanished
      - .parameters: element "cluster_ipaddress" has vanished
      - .interfaces_attributes: block count changed from 2 to 0
2022-07-20T14:05:26.008+0200 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState to workingState for foreman_host.vxccph03
2022-07-20T14:05:26.008+0200 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState: writing state object for foreman_host.vxccph03
2022-07-20T14:05:26.008+0200 [TRACE] evalApplyProvisioners: foreman_host.vxccph03 is not freshly-created, so no provisioning is required
2022-07-20T14:05:26.009+0200 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState to workingState for foreman_host.vxccph03
2022-07-20T14:05:26.009+0200 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState: writing state object for foreman_host.vxccph03
foreman_host.vxccph03: Modifications complete after 2s [id=808]
2022-07-20T14:05:26.010+0200 [TRACE] vertex "foreman_host.vxccph03": visit complete
togoschi commented 1 year ago

Verified the issue with a simple foreman host and an example parameter for debugging (debugstr)

resource "foreman_host" "proposal" {
  name = var.hostname
  method = "build"
...
  parameters = {
    debugstr = "test string for debug purposes"
  }
}

data "template_file" "preinstall" {
  template = file("${path.module}/templates/preinstall.erb")

  vars = {
    hostgroup = var.group
    var_debug  = "${foreman_host.proposal.parameters["debugstr"]}"
  }
}

resource "foreman_provisioningtemplate" "preinstall" {
  name     = "${var.group} preinstall"
  template = data.template_file.preinstall.rendered
  snippet  = true
}

The (vsphere virtual) machine was successfully deployed with foreman provider 0.4.5.

After updating the foreman provider to 0.5.3 (and replacing method with managed and build attribute) tf apply throws error (tf plan still works)

Error: Invalid index
│ 
│   on provision.tf line 76, in data "template_file" "preinstall":
│   76:     var_debug  = "${foreman_host.proposal.parameters["debugstr"]}"
│     ├────────────────
│     │ foreman_host.proposal.parameters is empty map of string
│ 
│ The given key does not identify an element in this collection value.

The output of tf apply with terraform TRACE log shows that the parameter is "vanished" although the foreman host parameter is already set

2022-08-18T15:31:00.755+0200 [WARN]  Provider "provider[\"registry.terraform.io/hansemerkur/foreman\"]" produced an unexpected new value for foreman_host.proposal, but we are tolerating it because it is using the legacy plugin SDK.
    The following problems may be the cause of any confusing errors from downstream operations:
      - .parameters: element "debugstr" has vanished

$ curl -s -k -u xxx:xxx https://foremanhost/api/v2/hosts/1094 | jq '.parameters?'
[
  {
    "priority": 70,
    "created_at": "2022-08-18T13:14:26.368Z",
    "updated_at": "2022-08-18T13:14:26.368Z",
    "id": 6042,
    "name": "debugstr",
    "value": "test string for debug purposes"
  }
]
togoschi commented 1 year ago

While running tf apply i've also created a foreman provider log output

provider "foreman" {
  provider_loglevel = "DEBUG"
  provider_logfile = "terraform-provider-foreman.log"
...
}
lhw commented 1 year ago

Closed by #95