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

allow creation of registry module via Github app #935

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "admin@company.com"
}

data "tfe_github_app_installation" "gha_installation" {
name = "YOUR_GH_NAME"
}

resource "tfe_registry_module" "petstore" {
organization_name = tfe_organization.test-organization.name
dsa0x marked this conversation as resolved.
Show resolved Hide resolved
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