From 02e9acaa70fcb21c8245ec77f8dbae56816f9282 Mon Sep 17 00:00:00 2001 From: Samsondeen Dare Date: Mon, 19 Jun 2023 21:12:11 +0200 Subject: [PATCH 1/4] allow creation of registry module via Github app --- tfe/resource_tfe_registry_module.go | 17 ++++++++++++++--- tfe/resource_tfe_registry_module_test.go | 12 ++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tfe/resource_tfe_registry_module.go b/tfe/resource_tfe_registry_module.go index 6cc71d806..5b9b6ae08 100644 --- a/tfe/resource_tfe_registry_module.go +++ b/tfe/resource_tfe_registry_module.go @@ -108,17 +108,28 @@ func resourceTFERegistryModule() *schema.Resource { } } -func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}) (*tfe.RegistryModule, error) { +func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d *schema.ResourceData) (*tfe.RegistryModule, error) { config := meta.(ConfiguredClient) // Create module with VCS repo configuration block. options := tfe.RegistryModuleCreateWithVCSConnectionOptions{} vcsRepo := v.([]interface{})[0].(map[string]interface{}) + // Creating a registry module from a VCS repo through a Github app requires the organization name, + // but creating via OAuth token does not. + orgName, err := config.schemaOrDefaultOrganization(d) + if err != nil { + log.Printf("[WARN] Error getting organization name: %s", err) + } + options.VCSRepo = &tfe.RegistryModuleVCSRepoOptions{ Identifier: tfe.String(vcsRepo["identifier"].(string)), - OAuthTokenID: tfe.String(vcsRepo["oauth_token_id"].(string)), GHAInstallationID: tfe.String(vcsRepo["github_app_installation_id"].(string)), DisplayIdentifier: tfe.String(vcsRepo["display_identifier"].(string)), + OrganizationName: tfe.String(orgName), + } + + if vcsRepo["oauth_token_id"] != nil && vcsRepo["oauth_token_id"].(string) != "" { + options.VCSRepo.OAuthTokenID = tfe.String(vcsRepo["oauth_token_id"].(string)) } log.Printf("[DEBUG] Create registry module from repository %s", *options.VCSRepo.Identifier) @@ -170,7 +181,7 @@ func resourceTFERegistryModuleCreate(d *schema.ResourceData, meta interface{}) e var err error if v, ok := d.GetOk("vcs_repo"); ok { - registryModule, err = resourceTFERegistryModuleCreateWithVCS(v, meta) + registryModule, err = resourceTFERegistryModuleCreateWithVCS(v, meta, d) } else { registryModule, err = resourceTFERegistryModuleCreateWithoutVCS(meta, d) } diff --git a/tfe/resource_tfe_registry_module_test.go b/tfe/resource_tfe_registry_module_test.go index c31b21490..4d2593f8b 100644 --- a/tfe/resource_tfe_registry_module_test.go +++ b/tfe/resource_tfe_registry_module_test.go @@ -600,8 +600,15 @@ func testAccCheckTFERegistryModuleVCSAttributes(registryModule *tfe.RegistryModu return fmt.Errorf("Bad VCS repo identifier: %v", registryModule.VCSRepo.Identifier) } - if registryModule.VCSRepo.OAuthTokenID == "" { - return fmt.Errorf("Bad VCS repo oauth token id: %v", registryModule.VCSRepo) + switch registryModule.VCSRepo.ServiceProvider { + case "github_app": + if registryModule.VCSRepo.GHAInstallationID == "" { + return fmt.Errorf("Bad VCS repo github app installation id: %v", registryModule.VCSRepo) + } + default: + if registryModule.VCSRepo.OAuthTokenID == "" { + return fmt.Errorf("Bad VCS repo oauth token id: %v", registryModule.VCSRepo) + } } return nil @@ -727,6 +734,7 @@ resource "tfe_organization" "foobar" { } resource "tfe_registry_module" "foobar" { + organization = tfe_organization.foobar.id vcs_repo { display_identifier = "%s" identifier = "%s" From a2a88d981a528d76f570bda3892ae2b146c0606e Mon Sep 17 00:00:00 2001 From: Samsondeen Dare Date: Mon, 19 Jun 2023 21:55:20 +0200 Subject: [PATCH 2/4] add docs --- website/docs/r/registry_module.html.markdown | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/website/docs/r/registry_module.html.markdown b/website/docs/r/registry_module.html.markdown index 821b10b6b..c55d0e199 100644 --- a/website/docs/r/registry_module.html.markdown +++ b/website/docs/r/registry_module.html.markdown @@ -36,6 +36,28 @@ resource "tfe_registry_module" "test-registry-module" { } ``` +Create private registry module with GitHub App: + +```hcl +resource "tfe_organization" "test-organization" { + name = "my-org-name" + email = "admin@company.com" +} + +data "tfe_github_app_installation" "gha_installation" { + name = "YOUR_GH_NAME" +} + +resource "tfe_registry_module" "petstore" { + organization_name = tfe_organization.foobar2 + vcs_repo { + display_identifier = "GH_NAME/REPO_NAME" + identifier = "GH_NAME/REPO_NAME" + github_app_installation_id = data.tfe_github_app_installation.gha_installation.id + } +} +``` + Create private registry module without VCS: ```hcl @@ -99,7 +121,7 @@ The following arguments are supported: new resource if changed. One of `vcs_repo` or `module_provider` is required. * `module_provider` - (Optional) Specifies the Terraform provider that this module is used for. For example, "aws" * `name` - (Optional) The name of registry module. It must be set if `module_provider` is used. -* `organization` - (Optional) The name of the organization associated with the registry module. It must be set if `module_provider` is used. +* `organization` - (Optional) The name of the organization associated with the registry module. It must be set if `module_provider` is used, or if `vcs_repo` is used via a GitHub App. * `namespace` - (Optional) The namespace of a public registry module. It can be used if `module_provider` is set and `registry_name` is public. * `registry_name` - (Optional) Whether the registry module is private or public. It can be used if `module_provider` is set. From 71a0799804c448d928a006678effaea7fe39336f Mon Sep 17 00:00:00 2001 From: Samsondeen Dare Date: Tue, 20 Jun 2023 16:15:45 +0200 Subject: [PATCH 3/4] correct a comment --- tfe/resource_tfe_registry_module.go | 2 -- website/docs/r/registry_module.html.markdown | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/tfe/resource_tfe_registry_module.go b/tfe/resource_tfe_registry_module.go index 5b9b6ae08..54a23559e 100644 --- a/tfe/resource_tfe_registry_module.go +++ b/tfe/resource_tfe_registry_module.go @@ -114,8 +114,6 @@ func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d * options := tfe.RegistryModuleCreateWithVCSConnectionOptions{} vcsRepo := v.([]interface{})[0].(map[string]interface{}) - // Creating a registry module from a VCS repo through a Github app requires the organization name, - // but creating via OAuth token does not. orgName, err := config.schemaOrDefaultOrganization(d) if err != nil { log.Printf("[WARN] Error getting organization name: %s", err) diff --git a/website/docs/r/registry_module.html.markdown b/website/docs/r/registry_module.html.markdown index c55d0e199..789f1f97a 100644 --- a/website/docs/r/registry_module.html.markdown +++ b/website/docs/r/registry_module.html.markdown @@ -49,7 +49,7 @@ data "tfe_github_app_installation" "gha_installation" { } resource "tfe_registry_module" "petstore" { - organization_name = tfe_organization.foobar2 + organization_name = tfe_organization.test-organization.name vcs_repo { display_identifier = "GH_NAME/REPO_NAME" identifier = "GH_NAME/REPO_NAME" From 18fbfc28192d7f8c82fcea75719c4457f7901f2b Mon Sep 17 00:00:00 2001 From: Samsondeen <40821565+dsa0x@users.noreply.github.com> Date: Wed, 28 Jun 2023 08:53:27 +0200 Subject: [PATCH 4/4] Update website/docs/r/registry_module.html.markdown Co-authored-by: Nick Fagerlund --- website/docs/r/registry_module.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/registry_module.html.markdown b/website/docs/r/registry_module.html.markdown index 789f1f97a..f2d45a48b 100644 --- a/website/docs/r/registry_module.html.markdown +++ b/website/docs/r/registry_module.html.markdown @@ -49,7 +49,7 @@ data "tfe_github_app_installation" "gha_installation" { } resource "tfe_registry_module" "petstore" { - organization_name = tfe_organization.test-organization.name + organization = tfe_organization.test-organization.name vcs_repo { display_identifier = "GH_NAME/REPO_NAME" identifier = "GH_NAME/REPO_NAME"