terraform-coop / terraform-provider-foreman

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

go get issues #16

Closed galindro closed 3 years ago

galindro commented 3 years ago

I'm trying to install the newest plugin version, but I'm getting this error:

cannot find package "github.com/hashicorp/hcl/v2" in any of:
    /usr/lib/go-1.15/src/github.com/hashicorp/hcl/v2 (from $GOROOT)
    /home/galindro/go/src/github.com/hashicorp/hcl/v2 (from $GOPATH)
cannot find package "github.com/hashicorp/hcl/v2/hclsyntax" in any of:
    /usr/lib/go-1.15/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOROOT)
    /home/galindro/go/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOPATH)

According to https://github.com/hashicorp/hcl/issues/371, the error occurs because HCL 2 can be automatically installed only in Go Modules mode.

How could I install this plugin in Go Modules mode? The instructions are still referring to legacy go get mode.

galindro commented 3 years ago

I could workaround the issue by exporting the following env var: GO111MODULE=on as mentioned here.

lhw commented 3 years ago

I think that should be the default with Go 1.13 and newer. But yes the go modules mode is required. All other dependency tools for go are basically deprecated.

galindro commented 3 years ago

Shouldn't this info be placed on install instructions? Not everyone is familiar with golang...

lhw commented 3 years ago

Those instructions are mostly for developing with the provider. Most users should go the official registry route and use

terraform {
  required_providers {
    foreman = {
      source = "HanseMerkur/foreman"
      version = "0.3.1"
    }
  }
}

provider "foreman" {
  # Configuration options
}

as described here https://registry.terraform.io/providers/HanseMerkur/foreman/latest That approach is way more stable and automatically selects the correct architecture etc. But yes the README needs some updates to point normal users in the right direction.

galindro commented 3 years ago

Oh nice. Didn't knew that it was published on terraform registry. I'll close this ticket.

galindro commented 3 years ago

@lhw the required_providers is a terraform 0.13 feature. For 0.12 and bellow, we still need to install the provider in an old fashion way. That's why it is better to add the env var tricky on the docs. There is no way to select the source on TF 0.12. https://www.terraform.io/docs/configuration/provider-requirements.html#v0-12-compatible-provider-requirements

galindro commented 3 years ago

Just realized that if you set that environment variable, it seems that the sources aren't updated locally. To workaround, I deleted the root local package folder (in my case ~/go/src/github.com/HanseMerkur/), unset the GO111MODULE env var, and I did a go get -u github.com:HanseMerkur/terraform-provider-foreman. Some errors will be displayed, but they can be ignored. Then, I could cd to the folder and could compile it.

But I think that the above workaround shall only work if you previously ran go get -u github.com:HanseMerkur/terraform-provider-foreman with the GO111MODULE=yes env var set. Dirty installation process, but seems to work.