terraform-provider-github icon indicating copy to clipboard operation
terraform-provider-github copied to clipboard

This resource can only be used in the context of an organization, "" is a user

Open MaXeraph opened this issue 3 years ago • 10 comments

Terraform Version

  • Terraform version: 1.0.0
  • Github version: 4.11.0

Affected Resource(s)

  • github_membership

Terraform Configuration Files

resource "github_membership" "configuration" {
  username = var.username
  role     = var.role
}

variable "username" {}

variable "role" {
  default = "member"
}

provider "github" {
  owner = "foo"
}

Debug Output

Debug/Trace doesn't show any new output that could specify the error.

Panic Output

N/A

Expected Behavior

Should be able to output a plan to remove github_membership resource belonging to an organization.

Actual Behavior

Errors:

Error: This resource can only be used in the context of an organization, "" is a user.

  with module.users_foo["bar"].github_membership.configuration,
  on modules/user-memberships/main.tf line 1, in resource "github_membership" "configuration":
   1: resource "github_membership" "configuration" {

Steps to Reproduce

  1. terraform plan --refresh=false

Important Factoids

  • Errors and crashes when running plan on Jenkins. There were no changes on our Jenkins configuration, and we were able to run plan and apply with no errors prior to this config modification.
  • Locally running the same configuration with terraform plan --refresh=false successfully create a plan. However, it is run with my own Github token, supplied in the provider resource. Whereas Jenkins utilizes a global token (which has been working fine until now) in env GITHUB_TOKEN

References

Possibly related to https://github.com/integrations/terraform-provider-github/issues/501

MaXeraph avatar Jun 23 '21 22:06 MaXeraph

I was experiencing this behaviour when updating the remote (terraform cloud) state file from my CLI using terraform import github_team.core 1234567. I resolved the issue by locally setting the environment variable GITHUB_TOKEN to my personal access token (which has the same privileges as the token set on my terrraform cloud workspace) In my case the issue was that I was trying to access resources locally that I was not authenticated to do. see: https://registry.terraform.io/providers/integrations/github/latest/docs#authentication

martyca avatar Jun 24 '21 03:06 martyca

Hey, I have also come across this same problem. The issue happens for me when using modules, it seems like the provider owner value is not inherited by modules and GITHUB_OWNER must be set in the environment in order to fix.

edited to add: Like @martyca, I have also found that I must have GITHUB_TOKEN set even though I have provided the token to the github provider block.

roobert avatar Jun 28 '21 12:06 roobert

still happening in 4.13.0

@roobert workaround of environment variables still works, but it relies now on a deprecated functionality.

╷
│ Warning: "organization": [DEPRECATED] Use owner (or GITHUB_OWNER) instead of organization (or GITHUB_ORGANIZATION)
│ 
│   with provider["registry.terraform.io/hashicorp/github"],
│   on <empty> line 0:
│   (source code not available)
│ 

alvarolorentedev avatar Aug 12 '21 08:08 alvarolorentedev

Encountering the same in v4.17.0.

hazcod avatar Oct 18 '21 09:10 hazcod

Yes, as @hazcod said, with v4.17.0 I faced the same problem creating a new team

guillermotti avatar Oct 20 '21 13:10 guillermotti

Hi, same error with v4.18.0 too.

lcaille avatar Nov 18 '21 09:11 lcaille

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

hazcod avatar Dec 27 '21 14:12 hazcod

exporting GITHUB_OWNER does not solve the problem for me. v4.24.1

mwos-sl avatar May 06 '22 13:05 mwos-sl

Still needing to do export GITHUB_TOKEN="${TF_VAR_github_token}" to workaround.

hazcod avatar Jun 29 '22 05:06 hazcod

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

This solution works for me with integrations/github provider version 4.28.0 so long as the provider configuration block is empty. In other words:

provider "github" {
  token = "var.token"
  owner = "my-org"
}

will fail even with the GITHUB_TOKEN and GITHUB_OWNER environment variables set, but

provider "github" {}

will work if the environment variables are set.

kmdouglass avatar Aug 15 '22 14:08 kmdouglass

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

This solution works for me with integrations/github provider version 4.28.0 so long as the provider configuration block is empty. In other words:

provider "github" {
  token = "var.token"
  owner = "my-org"
}

will fail even with the GITHUB_TOKEN and GITHUB_OWNER environment variables set, but

provider "github" {}

will work if the environment variables are set.

+1 with v.5.7.0

alexlop29 avatar Oct 31 '22 14:10 alexlop29

it seems like the provider owner value is not inherited by modules and GITHUB_OWNER must be set in the environment in order to fix.

I wonder if that's a thing we can adjust in our code or a limitation of Terraform as a whole.

kfcampbell avatar Nov 10 '22 00:11 kfcampbell

Work For me, Provider configure:

variable "GITHUB_TOKEN" {
  description = "Github Token Admin"
  type        = string
}

variable "GITHUB_OWNER" {
  description = "Github Owner Organization"
  type        = string
}
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 4.0"
    }
  }
provider "github" {
  token     = var.GITHUB_TOKEN
  owner     = var.GITHUB_OWNER
}

export TF_VAR_GITHUB_TOKEN={personal_token} export TF_VAR_GITHUB_OWNER={org-name} export GITHUB_OWNER="${TF_VAR_GITHUB_OWNER}" export GITHUB_TOKEN="${TF_VAR_GITHUB_TOKEN}"

sergio91zulu avatar Nov 16 '22 15:11 sergio91zulu

I'm in the process of updating our TF code base from the 2.x version of github when I ran into this issue.

It looks like this issue starts in v3.0.0. I tested on v2.9.1 and it works without issue.

shaun-rutherford avatar Nov 29 '22 23:11 shaun-rutherford

Hmm...here's the diff between those two tags. There's some changes in github/config.go that could potentially be at fault.

kfcampbell avatar Nov 30 '22 23:11 kfcampbell

BEWARE: this error is very misleading. We thought the Go library needed because of https://github.com/integrations/terraform-provider-github/issues/1391

For us, it turns out our Personal Access Token just stopped working for some reason. We made a new token and everything started working again.

Seems like this has come up a lot: https://github.com/integrations/terraform-provider-github/search?q=in+the+context+of+an+organization+is+a+user&type=issues

Maybe this can be added to the README or something as a troubleshooting step?

thetimbecker avatar Feb 10 '23 22:02 thetimbecker

@thetimbecker agreed that usually the cause is an invalid PAT! Would you be interested in opening up a PR to update the docs accordingly?

kfcampbell avatar Feb 17 '23 23:02 kfcampbell

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

This solution works for me with integrations/github provider version 4.28.0 so long as the provider configuration block is empty. In other words:

provider "github" {
  token = "var.token"
  owner = "my-org"
}

will fail even with the GITHUB_TOKEN and GITHUB_OWNER environment variables set, but

provider "github" {}

will work if the environment variables are set.

+1 with v.5.7.0

Here's another confirmation 4 months later. This workaround still works in v5.18.0.

aiham avatar Feb 26 '23 16:02 aiham

@thetimbecker agreed that usually the cause is an invalid PAT! Would you be interested in opening up a PR to update the docs accordingly?

Clearly I never got around to this 😅

Unfortunately I can't take the time to do it well right now, especially since the issue it's fresh in my mind anymore.

thetimbecker avatar Aug 31 '23 14:08 thetimbecker

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

github-actions[bot] avatar May 28 '24 01:05 github-actions[bot]