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 Oct 24, 2023
1 parent 34d9585 commit 9f6fc4a
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## UNRELEASED

Features
* `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)

## v0.49.2 (October 4, 2023)

BUG FIXES:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/hashicorp/go-slug v0.12.2
github.com/hashicorp/go-tfe v1.34.0
github.com/hashicorp/go-tfe v1.37.0
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
Expand Down Expand Up @@ -76,7 +76,7 @@ require (
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sync v0.4.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/hashicorp/go-slug v0.12.2 h1:Gb6nxnV5GI1UVa3aLJGUj66J8AOZFnjIoYalNCp2
github.com/hashicorp/go-slug v0.12.2/go.mod h1:JZVtycnZZbiJ4oxpJ/zfhyfBD8XxT4f0uOSyjNLCqFY=
github.com/hashicorp/go-tfe v1.34.0 h1:33xcVQ8sbkdPVhubpVH208+S72l6l5bFEi14363yOIE=
github.com/hashicorp/go-tfe v1.34.0/go.mod h1:xA+N8r81ldvS7PC/DVbL+FqGsgfrcu1Ak4+6His+51s=
github.com/hashicorp/go-tfe v1.37.0 h1:AHCl8+errpmyteWENIez3seeSAqpHJv52ysF53mjQjE=
github.com/hashicorp/go-tfe v1.37.0/go.mod h1:awOuTZ4K9F1EJsKBIoxonJlb7Axn3PIb8YeBLtm/G/0=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down Expand Up @@ -186,6 +188,8 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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 9f6fc4a

Please sign in to comment.