Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for adding Public Modules to registry #383

Closed
lucymhdavies opened this issue Oct 19, 2021 · 6 comments
Closed

Support for adding Public Modules to registry #383

lucymhdavies opened this issue Oct 19, 2021 · 6 comments

Comments

@lucymhdavies
Copy link
Contributor

Use-cases

Looking at https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/registry_module it appears to only support adding Terraform modules backed by VCS repos.

Reviewing the code for the resource, this does appear to be the case:
https://github.com/hashicorp/terraform-provider-tfe/blob/main/tfe/resource_tfe_registry_module.go

Am I missing something, or is this not yet supported?

Attempted Solutions

Basic resource definition, without vcs component:

resource "tfe_registry_module" "vpc" {
  organization    = var.tfe_org
  module_provider = "aws"
  name            = "vpc"
}

which, unsurprisingly, does not work

$ terraform apply
Releasing state lock. This may take a few moments...
╷
│ Error: Insufficient vcs_repo blocks
│
│   on main.tf line 21, in resource "tfe_registry_module" "vpc":
│   21: resource "tfe_registry_module" "vpc" {
│
│ At least 1 "vcs_repo" blocks are required.

and various attempts at a Terraform import:

$ terraform import tfe_registry_module.vpc fancycorp/vpc/aws/mod-18F8qxmUGyMNF6y5

$ terraform import tfe_registry_module.vpc fancycorp/registry-modules/public/terraform-aws-modules/vpc/aws/mod-18F8qxmUGyMNF6y5

In my case, the module has been created through the API as defined here:
https://www.terraform.io/docs/cloud/api/modules.html#sample-payload-public-module-

$ curl -s  --header "Authorization: Bearer ${tfc_token}"   --header "Content-Type: application/vnd.api+json"   --request POST   --data @payload.json   https://app.terraform.io/api/v2/organizations/fancycorp/registry-modules | jq .
{
  "data": {
    "id": "mod-c4o6bFLGGzegj43r",
    "type": "registry-modules",
    "attributes": {
      "name": "vpc",
      "namespace": "terraform-aws-modules",
      "provider": "aws",
      "status": "setup_complete",
      "version-statuses": [],
      "created-at": "2021-10-19T12:39:23.689Z",
      "updated-at": "2021-10-19T12:39:23.698Z",
      "registry-name": "public",
      "permissions": {
        "can-delete": true,
        "can-resync": true,
        "can-retry": true
      }
    },
    "relationships": {
      "organization": {
        "data": {
          "id": "fancycorp",
          "type": "organizations"
        }
      }
    },
    "links": {
      "self": "/api/v2/organizations/fancycorp/registry-modules/public/terraform-aws-modules/vpc/aws"
    }
  }
}

Proposal

Expand upon the existing tfe_registry_module resource to allow sourcing from other places, such as mirroring from the public module registry.

@lucymhdavies
Copy link
Contributor Author

lucymhdavies commented Oct 19, 2021

As a short-term workaround, I'm using the null provider:

# Public VPC Module
# Not yet supported by the provider
# https://github.com/hashicorp/terraform-provider-tfe/issues/383
resource "null_resource" "vpc-module" {
  provisioner "local-exec" {
    command     = <<EOF
		curl -s --header "Authorization: Bearer ${local.tfc_token}" \
			--header "Content-Type: application/vnd.api+json" \
			--request POST \
			--data '{
				"data": {
					"type": "registry-modules",
					"attributes": {
						"name": "vpc",
						"namespace": "terraform-aws-modules",
						"provider": "aws",
						"registry-name": "public"
					}
				}
			}' \
			https://app.terraform.io/api/v2/organizations/fancycorp/registry-modules
EOF
    interpreter = ["bash", "-c"]
  }

  # TODO: Destroy
  # https://www.terraform.io/docs/language/resources/provisioners/syntax.html#destroy-time-provisioners
}

For my use-case, this is good enough, but would be good to see it officially supported by the provider.

@brandonc
Copy link
Collaborator

@lucymhdavies Thanks for raising this issue! This seems reasonable and has been possible to do through the API (as you've shown) since May.

@devopsrick
Copy link

Any ETA on this feature? The latest Terraform Enterprise version supports this now so we would love to be able to programmatically add modules and providers from the public registry.

@brandonc
Copy link
Collaborator

brandonc commented Jun 24, 2022

We had put this feature into a backlog but have recently begun working on it.

@lucymhdavies
Copy link
Contributor Author

I can confirm, the example from
https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/registry_module

resource "tfe_registry_module" "test-public-registry-module" {
  organization    = tfe_organization.test-organization.name
  namespace       = "terraform-aws-modules"
  module_provider = "aws"
  name            = "vpc"
  registry_name   = "public"
}

works as expected

@lucymhdavies
Copy link
Contributor Author

Looks like this was added in #546, so thanks @Uk1288 and @brandonc for making this happen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants