diff --git a/tfe/resource_tfe_registry_module.go b/tfe/resource_tfe_registry_module.go index 6cc71d806..54a23559e 100644 --- a/tfe/resource_tfe_registry_module.go +++ b/tfe/resource_tfe_registry_module.go @@ -108,17 +108,26 @@ 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{}) + 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 +179,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" diff --git a/website/docs/r/registry_module.html.markdown b/website/docs/r/registry_module.html.markdown index 821b10b6b..f2d45a48b 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 = tfe_organization.test-organization.name + 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.