integrations / terraform-provider-github

Terraform GitHub provider
https://www.terraform.io/docs/providers/github/
MIT License
890 stars 733 forks source link

Provider config not inherited #846

Open figadore opened 3 years ago

figadore commented 3 years ago

I believe this is a bug based on what I read about provider config inheritance for modules, but it could also be that I just don't understand terraform very well yet

Terraform Version

1.0.1

Affected Resource(s)

Terraform Configuration Files

main.tf

...
provider "github" {
  owner = var.github_org
  app_auth {
    id              = var.gh_automations_app_id
    installation_id = var.gh_automations_installation_id
    pem_file        = var.gh_automations_private_key
  }
}

resource "github_repository" "this" {
  name        = "repo-name"
  description = "Something"
  visibility  = "private"
}

module "submodule" {
  source = "./submodule"
  repo_name = github_repository.this.name
  env = "sandbox"
}
...

submodule/main.tf

resource "github_repository_environment" "this" {
  repository  = var.repo_name #e.g. `repo-name`
  environment = var.env
}

resource "github_actions_environment_secret" "folder_terraformer_key" {
  repository      = var.repo_name
  environment     = github_repository_environment.this.environment
  secret_name     = "GCP_KEY" #tfsec:ignore:GEN003
  plaintext_value = <val>
}

Expected Behavior

API calls to https://api.github.com/repos/<owner>/<repo-name>

Actual Behavior

API calls to https://api.github.com/repos//<repo-name>

No owner is passed in when constructing API urls

Error: GET https://api.github.com/repos//repo-name: 404 Not Found []

or

Error: PUT https://api.github.com/repos//repo-name/environments/sandbox: 404 Not Found []

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. set owner in provider config in root module
  2. provision a repo in the root module
  3. attempt to provision an environment for that repo in a submodule
knopka commented 3 years ago

Faced with same bug, child module doesn't inherit provider configuration from parent module.

Terraform v1.0.0
+ provider registry.terraform.io/hashicorp/github v4.13.0
devopsrick commented 3 years ago

As a workaround, you can use the 'explicit' passing of providers to modules. This is quite cumbersome of course and not a good long-term solution.

module "example" { providers = { github = "github" }

... }

jodok commented 3 years ago

As a workaround, you can use the 'explicit' passing of providers to modules. This is quite cumbersome of course and not a good long-term solution.

if you use the module multiple times, it's enough to pass it explicitly once - the other occurrences seem to pick it up as well.

mwos-sl commented 2 years ago

As a workaround, you can use the 'explicit' passing of providers to modules. This is quite cumbersome of course and not a good long-term solution.

Doesn't work for me :( I tried: A)

data "github_team" "dev-excellence-team" {
  provider = github
  slug = "dev-excellence-team"
}

Still getting 404.

B) What you suggested:

data "github_team" "dev-excellence-team" {
  providers = {
    github = "github"
  }
  slug = "dev-excellence-team"
}

and getting:

│ An argument named "providers" is not expected here.
devopsrick commented 2 years ago

As a workaround, you can use the 'explicit' passing of providers to modules. This is quite cumbersome of course and not a good long-term solution.

Doesn't work for me :( I tried: A)

data "github_team" "dev-excellence-team" {
  provider = github
  slug = "dev-excellence-team"
}

Still getting 404.

B) What you suggested:

data "github_team" "dev-excellence-team" {
  providers = {
    github = "github"
  }
  slug = "dev-excellence-team"
}

and getting:

│ An argument named "providers" is not expected here.

Sorry that workaround is for modules, not specific resources/data resources.

thomascrowley commented 1 year ago

I am still seeing this with Terraform v1.3.2 and + provider registry.terraform.io/hashicorp/github v5.5.0. The work around did work, but I had to remember to include a required providers block inside my module.

As a workaround, you can use the 'explicit' passing of providers to modules. This is quite cumbersome of course and not a good long-term solution.

module "example" { providers = { github = "github" } ... }

kfcampbell commented 1 year ago

This issue may have some overlap with #1231.

github-actions[bot] commented 1 year ago

👋 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!

reedloden commented 1 year ago

Not stale :-)