Skip to content

Commit

Permalink
Merge pull request #935 from hashicorp/sams/fix-registry-module-vcs-c…
Browse files Browse the repository at this point in the history
…reate

allow creation of registry module via Github app
  • Loading branch information
dsa0x committed Jun 28, 2023
2 parents f757261 + 18fbfc2 commit 519f459
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
15 changes: 12 additions & 3 deletions tfe/resource_tfe_registry_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 10 additions & 2 deletions tfe/resource_tfe_registry_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
24 changes: 23 additions & 1 deletion website/docs/r/registry_module.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
}
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
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 519f459

Please sign in to comment.