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

error creating contentview resource #165

Open vhsantos opened 1 week ago

vhsantos commented 1 week ago

Hi there,

Im trying to create a contentview in a foreman 3.10 and katello 4.12 (https://docs.theforeman.org/3.10/Quickstart/index-katello.html), using the terraform-provider-foreman=0.6.3 and theses codes (and much variants):

resource "foreman_katello_content_view" "example" {
  name          = "AlmaLinux 9 CV"
}

or this other:

resource "foreman_katello_content_view" "example" {
  auto_publish  = false
  component_ids = []
  composite     = false
  description   = "tests"
  label              = "test12"
  name               = "test12"
  organization_id    = 1
  repository_ids     = [6, ]
  solve_dependencies = false
}

but in all the cases, I get the same error:

$ tofu apply
OpenTofu will perform the following actions:

  # foreman_katello_content_view.example will be created
  + resource "foreman_katello_content_view" "example" {
      + __meta__           = (known after apply)
      + auto_publish       = false
      + component_ids      = []
      + composite          = false
      + description        = "tests"
      + filter             = (known after apply)
      + id                 = (known after apply)
      + label              = "test12"
      + name               = "test12"
      + organization_id    = 1
      + repository_ids     = [
          + 6,
        ]
      + solve_dependencies = false
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  OpenTofu will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

foreman_katello_content_view.example: Creating...
╷
│ Error: Plugin did not respond
│ 
│   with foreman_katello_content_view.example,
│   on content_view.tf line 21, in resource "foreman_katello_content_view" "example":
│   21: resource "foreman_katello_content_view" "example" {
│ 
│ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ApplyResourceChange call. The plugin logs may contain more details.
╵

Stack trace from the terraform-provider-foreman_v0.6.3 plugin:

panic: assignment to entry in nil map

goroutine 13 [running]:
github.com/terraform-coop/terraform-provider-foreman/foreman/api.(*Client).WrapJSONWithTaxonomy(0xc0003f9ad0, {0x0?, 0x0?}, {0xbf33a0?, 0xc0001aad80?})
    github.com/terraform-coop/terraform-provider-foreman/foreman/api/client.go:460 +0x87
github.com/terraform-coop/terraform-provider-foreman/foreman/api.(*Client).CreateKatelloContentViewFilters(0xc0003f9ad0, {0xe7c9f8, 0xc0004c2690}, 0xc0001aadc8?, 0xc0001aad80)
    github.com/terraform-coop/terraform-provider-foreman/foreman/api/katello_content_views.go:273 +0xa5
github.com/terraform-coop/terraform-provider-foreman/foreman/api.(*Client).CreateKatelloContentView(0xc0003f9ad0, {0xe7c9f8, 0xc0004c2690}, 0xc0001aab08)
    github.com/terraform-coop/terraform-provider-foreman/foreman/api/katello_content_views.go:257 +0x16b
github.com/terraform-coop/terraform-provider-foreman/foreman.resourceForemanKatelloContentViewCreate({0xe7c9f8, 0xc0004c2690}, 0xc00019f900, {0xd33300?, 0xc0003f9ad0})
    github.com/terraform-coop/terraform-provider-foreman/foreman/resource_foreman_katello_content_views.go:308 +0x69
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0xc0003ced20, {0xe7c950, 0xc00049cf60}, 0xc00019f900, {0xd33300, 0xc0003f9ad0})
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/resource.go:707 +0x10f
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc0003ced20, {0xe7c950, 0xc00049cf60}, 0xc0005185b0, 0xc00019f280, {0xd33300, 0xc0003f9ad0})
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/resource.go:837 +0xa5e
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc0003a24c8, {0xe7c950?, 0xc00049cea0?}, 0xc0003c0550)
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/grpc_provider.go:1021 +0xd5c
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc00027efa0, {0xe7c950?, 0xc00049c510?}, 0xc0004c2070)
    github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/tf5server/server.go:818 +0x56f
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0xd01b00, 0xc00027efa0}, {0xe7c950, 0xc00049c510}, 0xc0004c2000, 0x0)
    github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x1a6
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0002741e0, {0xe80f60, 0xc0003cc680}, 0xc0004fad80, 0xc00049c330, 0x13de060, 0x0)
    google.golang.org/grpc@v1.56.3/server.go:1335 +0xdb8
google.golang.org/grpc.(*Server).handleStream(0xc0002741e0, {0xe80f60, 0xc0003cc680}, 0xc0004fad80, 0x0)
    google.golang.org/grpc@v1.56.3/server.go:1712 +0x9da
google.golang.org/grpc.(*Server).serveStreams.func1.1()
    google.golang.org/grpc@v1.56.3/server.go:947 +0xbb
created by google.golang.org/grpc.(*Server).serveStreams.func1 in goroutine 7
    google.golang.org/grpc@v1.56.3/server.go:958 +0x136

Error: The terraform-provider-foreman_v0.6.3 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

on the logs of the TF foreman plugin I get this:

2024/06/19 12:38:51 [DEBUG] Server: [{URL:{Scheme:https Opaque: User: Host:foreman.mydomain.com Path: RawPath: OmitHost:false ForceQuery:false RawQuery: Fragment: RawFragment:}}], ClientConfig: [{TLSInsecureEnabled:true NegotiateAuthEnabled:false LocationID:2 OrganizationID:1}]
2024/06/19 12:38:51 [DEBUG] Rest Client configured
2024/06/19 12:38:51 [DEBUG] client.go#WrapJSONWithTaxonomy: item map[auto_publish:false component_ids:<nil> composite:false description: filtered:false id:0 label: location_id:2 name:AlmaLinux 9 CV4 organization_id:1 repository_ids:[6] solve_dependencies:false]
2024/06/19 12:38:51 [DEBUG] method: [POST], endpoint: [/katello/api/content_views]
2024/06/19 12:38:51 [DEBUG] reqURL: [https://foreman.mydomain.com/katello/api/content_views]
2024/06/19 12:38:52 [DEBUG] server response:{
  endpoint:   [https://foreman.mydomain.com/katello/api/content_views]
  method:     [POST]
  statusCode: [201]
  respBody:   [  {"content_host_count":0,"needs_publish":true,"composite":false,"component_ids":[],"duplicate_repositories_to_publish":[],"default":false,"version_count":0,"latest_version":null,"latest_version_id":null,"auto_publish":false,"solve_dependencies":false,"import_only":false,"generated_for":"none","related_cv_count":0,"related_composite_cvs":[],"filtered":false,"repository_ids":[6],"id":6,"name":"AlmaLinux 9 CV4","label":"AlmaLinux_9_CV4","description":"","organization_id":1,"organization":{"name":"TEST","label":"TEST","id":1},"created_at":"2024-06-19 10:38:52 UTC","updated_at":"2024-06-19 10:38:52 UTC","last_task":null,"latest_version_environments":[],"repositories":[{"id":6,"name":"AlmaLinux BaseOS","label":"AlmaLinux_BaseOS","content_type":"yum"}],"versions":[],"components":[],"content_view_components":[],"activation_keys":[],"hosts":[],"next_version":"1.0","last_published":null,"environments":[],"errors":null}
]
}

But, the content view is created on the foreman:


{
  "content_host_count": 0,
  "needs_publish": true,
  "composite": false,
  "component_ids": [],
  "duplicate_repositories_to_publish": [],
  "default": false,
  "version_count": 0,
  "latest_version": null,
  "latest_version_id": null,
  "auto_publish": false,
  "solve_dependencies": false,
  "import_only": false,
  "generated_for": "none",
  "related_cv_count": 0,
  "related_composite_cvs": [],
  "filtered": false,
  "repository_ids": [6],
  "id": 15,
  "name": "test12",
  "label": "test12",
  "description": "tests",
  "organization_id": 1,
  "organization": {
    "name": "TEST",
    "label": "TEST",
    "id": 1
  },
  "created_at": "2024-06-19 13:47:01 +0200",
  "updated_at": "2024-06-19 13:47:01 +0200",
  "last_task": null,
  "latest_version_environments": [],
  "repositories": [
    {
      "id": 6,
      "name": "AlmaLinux BaseOS",
      "label": "AlmaLinux_BaseOS",
      "content_type": "yum"
    }
  ],
  "versions": [],
  "components": [],
  "content_view_components": [],
  "activation_keys": [],
  "hosts": [],
  "next_version": "1.0",
  "last_published": null,
  "environments": [],
  "errors": null
}

So, looks like the plugin are having some issues trying to work with the foreman response !!! :-(

I have tried to use the PR https://github.com/terraform-coop/terraform-provider-foreman/pull/157 from @bitkeks (which looks good) but the result is the same.

Suggestions ??

bitkeks commented 1 week ago

panic: assignment to entry in nil map is caused by an error in the passed-in object into WrapJSONWithTaxonomy. I had this in my work on the #157 but it came up repeatedly, I'll have to debug it with your example data.

In this case, it should originate here: https://github.com/terraform-coop/terraform-provider-foreman/blob/27ca840eef52bcf8355fa458ac6d5325e8c04b16/foreman/api/katello_content_views.go#L273

bitkeks commented 6 days ago

This error does not appear on the branch of #157, so it is probably fixed with b8a52c9313b2d69da0db43e8a8934f35f95ca787. We'll have to see to merge it quickly and release a new provider version. May be we'll find the time to take another test with your example data from the main branch, which is 0.6.3.

vhsantos commented 6 days ago

Let me know if I can help with anything.