Skip to content

Commit

Permalink
[TF-9605] Add Private Registry Module Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hashimoon committed Nov 8, 2023
1 parent 0050f18 commit 1585890
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FEATURES:
* `d/tfe_variable_set`: Add `priority` attribute, by @Netra2104 [1075](https://github.com/hashicorp/terraform-provider-tfe/pull/1075)
* `r/tfe_workspace`: Add `auto_apply_run_trigger` attribute, by @nfagerlund [1123](https://github.com/hashicorp/terraform-provider-tfe/pull/1123)
* `d/tfe_workspace`: Add `auto_apply_run_trigger` attribute, by @nfagerlund [1123](https://github.com/hashicorp/terraform-provider-tfe/pull/1123)
* `d/tfe_registry_module`: Add `vcs_repo.tags` and `vcs_repo.branch` attributes to allow configuration of `publishing_mechanism`. Add `test_config` to support running tests on `branch`-based registry modules., by @hashimoon [1096](https://github.com/hashicorp/terraform-provider-tfe/pull/1096)

BUG FIXES:

Expand Down
78 changes: 77 additions & 1 deletion internal/provider/resource_tfe_registry_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func resourceTFERegistryModule() *schema.Resource {
Computed: true,
ForceNew: true,
},
"publishing_mechanism": {
Type: schema.TypeString,
Computed: true,
},
"vcs_repo": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -78,6 +82,14 @@ func resourceTFERegistryModule() *schema.Resource {
ConflictsWith: []string{"vcs_repo.0.oauth_token_id"},
AtLeastOneOf: []string{"vcs_repo.0.oauth_token_id", "vcs_repo.0.github_app_installation_id"},
},
"branch": {
Type: schema.TypeString,
Optional: true,
},
"tags": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
Expand All @@ -104,6 +116,18 @@ func resourceTFERegistryModule() *schema.Resource {
true,
),
},
"test_config": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tests_enabled": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
},
}
}
Expand All @@ -113,6 +137,11 @@ func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d *
// Create module with VCS repo configuration block.
options := tfe.RegistryModuleCreateWithVCSConnectionOptions{}
vcsRepo := v.([]interface{})[0].(map[string]interface{})
var testConfig map[string]interface{}

if tc, ok := d.GetOk("test_config"); ok {
testConfig = tc.([]interface{})[0].(map[string]interface{})
}

orgName, err := config.schemaOrDefaultOrganization(d)
if err != nil {
Expand All @@ -125,11 +154,20 @@ func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d *
DisplayIdentifier: tfe.String(vcsRepo["display_identifier"].(string)),
OrganizationName: tfe.String(orgName),
}
if vcsRepo["branch"].(string) != "" {
options.VCSRepo.Branch = tfe.String(vcsRepo["branch"].(string))
}

if vcsRepo["oauth_token_id"] != nil && vcsRepo["oauth_token_id"].(string) != "" {
options.VCSRepo.OAuthTokenID = tfe.String(vcsRepo["oauth_token_id"].(string))
}

if testsEnabled, ok := testConfig["tests_enabled"].(bool); ok {
options.TestConfig = &tfe.RegistryModuleTestConfigOptions{
TestsEnabled: tfe.Bool(testsEnabled),
}
}

log.Printf("[DEBUG] Create registry module from repository %s", *options.VCSRepo.Identifier)
registryModule, err := config.Client.RegistryModules.CreateWithVCSConnection(ctx, options)
if err != nil {
Expand Down Expand Up @@ -174,7 +212,6 @@ func resourceTFERegistryModuleCreateWithoutVCS(meta interface{}, d *schema.Resou

func resourceTFERegistryModuleCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)

var registryModule *tfe.RegistryModule
var err error

Expand Down Expand Up @@ -242,6 +279,31 @@ func resourceTFERegistryModuleUpdate(d *schema.ResourceData, meta interface{}) e
RegistryName: tfe.RegistryName(d.Get("registry_name").(string)),
}

if v, ok := d.GetOk("vcs_repo"); ok { //nolint:nestif
vcsRepo := v.([]interface{})[0].(map[string]interface{})
options.VCSRepo = &tfe.RegistryModuleVCSRepoUpdateOptions{}

if vcsRepo["tags"] != nil {
if tags, ok := vcsRepo["tags"].(bool); ok {
options.VCSRepo.Tags = tfe.Bool(tags)
}
}
if vcsRepo["branch"] != nil {
if branch, ok := vcsRepo["branch"].(string); ok {
options.VCSRepo.Branch = tfe.String(branch)
}
}
}

if v, ok := d.GetOk("test_config"); ok {
testConfig := v.([]interface{})[0].(map[string]interface{})
options.TestConfig = &tfe.RegistryModuleTestConfigOptions{}

if testsEnabled, ok := testConfig["tests_enabled"].(bool); ok {
options.TestConfig.TestsEnabled = tfe.Bool(testsEnabled)
}
}

err = resource.Retry(time.Duration(5)*time.Minute, func() *resource.RetryError {
registryModule, err = config.Client.RegistryModules.Update(ctx, rmID, options)
if err != nil {
Expand Down Expand Up @@ -291,6 +353,7 @@ func resourceTFERegistryModuleRead(d *schema.ResourceData, meta interface{}) err
d.Set("namespace", registryModule.Namespace)
d.Set("registry_name", registryModule.RegistryName)
d.Set("no_code", registryModule.NoCode)
d.Set("publishing_mechanism", registryModule.PublishingMechanism)

// Set VCS repo options.
var vcsRepo []interface{}
Expand All @@ -300,12 +363,25 @@ func resourceTFERegistryModuleRead(d *schema.ResourceData, meta interface{}) err
"oauth_token_id": registryModule.VCSRepo.OAuthTokenID,
"github_app_installation_id": registryModule.VCSRepo.GHAInstallationID,
"display_identifier": registryModule.VCSRepo.DisplayIdentifier,
"branch": registryModule.VCSRepo.Branch,
"tags": registryModule.VCSRepo.Tags,
}
vcsRepo = append(vcsRepo, vcsConfig)

d.Set("vcs_repo", vcsRepo)
}

var testConfig []interface{}
if registryModule.TestConfig != nil {
testConfigValues := map[string]interface{}{
"tests_enabled": registryModule.TestConfig.TestsEnabled,
}

testConfig = append(testConfig, testConfigValues)

d.Set("test_config", testConfig)
}

return nil
}

Expand Down
Loading

0 comments on commit 1585890

Please sign in to comment.