Skip to content

Commit 8bd669d

Browse files
authored
Merge pull request #804 from hashicorp/hashimoon/terraform-test-validations
[TF-9605] Add validation when configuring Registry Module Publishing
2 parents a245d58 + dde8355 commit 8bd669d

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

errors.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ var (
222222

223223
ErrRequiredAgentPoolID = errors.New("'agent' execution mode requires an agent pool ID to be specified")
224224

225-
ErrRequiredAgentMode = errors.New("specifying an agent pool ID requires 'agent' execution mode")
226-
ErrRequiredBranchWhenTestsEnabled = errors.New("VCS branch is required when enabling tests")
227-
ErrRequiredCategory = errors.New("category is required")
225+
ErrRequiredAgentMode = errors.New("specifying an agent pool ID requires 'agent' execution mode")
226+
ErrRequiredBranchWhenTestsEnabled = errors.New("VCS branch is required when enabling tests")
227+
ErrBranchMustBeEmptyWhenTagsEnabled = errors.New("VCS branch must be empty to enable tags")
228+
ErrRequiredCategory = errors.New("category is required")
228229

229230
ErrRequiredDestinationType = errors.New("destination type is required")
230231

registry_module.go

+13
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ type RegistryModuleVCSRepoOptions struct {
289289
//
290290
// **Note: This field is still in BETA and subject to change.**
291291
Branch *string `json:"branch,omitempty"`
292+
Tags *bool `json:"tags,omitempty"`
292293
}
293294

294295
type RegistryModuleVCSRepoUpdateOptions struct {
@@ -426,6 +427,12 @@ func (r *registryModules) Update(ctx context.Context, moduleID RegistryModuleID,
426427
log.Println("[WARN] Support for using the NoCode field is deprecated as of release 1.22.0 and may be removed in a future version. The preferred way to update a no-code module is with the registryNoCodeModules.Update method.")
427428
}
428429

430+
if options.VCSRepo != nil {
431+
if options.VCSRepo.Tags != nil && *options.VCSRepo.Tags && validString(options.VCSRepo.Branch) {
432+
return nil, ErrBranchMustBeEmptyWhenTagsEnabled
433+
}
434+
}
435+
429436
org := url.QueryEscape(moduleID.Organization)
430437
registryName := url.QueryEscape(string(moduleID.RegistryName))
431438
namespace := url.QueryEscape(moduleID.Namespace)
@@ -738,6 +745,12 @@ func (o RegistryModuleCreateWithVCSConnectionOptions) valid() error {
738745
}
739746
}
740747

748+
if o.VCSRepo.Tags != nil && *o.VCSRepo.Tags {
749+
if validString(o.VCSRepo.Branch) {
750+
return ErrBranchMustBeEmptyWhenTagsEnabled
751+
}
752+
}
753+
741754
return o.VCSRepo.valid()
742755
}
743756

registry_module_integration_test.go

+57-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestRegistryModulesCreate(t *testing.T) {
160160
assert.Equal(t, *options.Provider, rm.Provider)
161161
assert.Equal(t, options.RegistryName, rm.RegistryName)
162162
assert.Equal(t, orgTest.Name, rm.Namespace)
163-
assert.Equal(t, options.NoCode, rm.NoCode)
163+
assert.Equal(t, options.NoCode, Bool(rm.NoCode))
164164

165165
assertRegistryModuleAttributes(t, rm)
166166
})
@@ -363,6 +363,43 @@ func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {
363363
assert.False(t, rm.NoCode)
364364
})
365365

366+
t.Run("prevents setting the branch when using tag based publishing", func(t *testing.T) {
367+
options := RegistryModuleUpdateOptions{
368+
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
369+
Branch: String("main"),
370+
Tags: Bool(true),
371+
},
372+
}
373+
374+
_, err = client.RegistryModules.Update(ctx, RegistryModuleID{
375+
Organization: orgTest.Name,
376+
Name: rm.Name,
377+
Provider: rm.Provider,
378+
Namespace: rm.Namespace,
379+
RegistryName: rm.RegistryName,
380+
}, options)
381+
382+
assert.Error(t, err)
383+
assert.EqualError(t, err, ErrBranchMustBeEmptyWhenTagsEnabled.Error())
384+
385+
options = RegistryModuleUpdateOptions{
386+
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
387+
Branch: String(""),
388+
Tags: Bool(true),
389+
},
390+
}
391+
392+
rm, err = client.RegistryModules.Update(ctx, RegistryModuleID{
393+
Organization: orgTest.Name,
394+
Name: rm.Name,
395+
Provider: rm.Provider,
396+
Namespace: rm.Namespace,
397+
RegistryName: rm.RegistryName,
398+
}, options)
399+
400+
assert.NoError(t, err)
401+
})
402+
366403
t.Run("toggle between git tag-based and branch-based publishing", func(t *testing.T) {
367404
assert.Equal(t, rm.PublishingMechanism, PublishingMechanismTag)
368405

@@ -385,7 +422,8 @@ func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {
385422

386423
options = RegistryModuleUpdateOptions{
387424
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
388-
Tags: Bool(true),
425+
Branch: String(""),
426+
Tags: Bool(true),
389427
},
390428
}
391429
rm, err = client.RegistryModules.Update(ctx, RegistryModuleID{
@@ -634,7 +672,7 @@ func TestRegistryModulesShowVersion(t *testing.T) {
634672
rmvRead, errRead := client.RegistryModules.ReadVersion(ctx, registryModuleIDTest, *invalidVersion)
635673

636674
require.Error(t, errRead)
637-
assert.Equal(t, ErrResourceNotFound, err)
675+
assert.Equal(t, ErrResourceNotFound, errRead)
638676
assert.Empty(t, rmvRead)
639677
})
640678
}
@@ -815,6 +853,22 @@ func TestRegistryModulesCreateWithVCSConnection(t *testing.T) {
815853
assert.Nil(t, rm)
816854
assert.Equal(t, err, ErrRequiredDisplayIdentifier)
817855
})
856+
857+
t.Run("when tags are enabled and a branch is provided", func(t *testing.T) {
858+
options := RegistryModuleCreateWithVCSConnectionOptions{
859+
VCSRepo: &RegistryModuleVCSRepoOptions{
860+
Identifier: String(githubIdentifier),
861+
OAuthTokenID: String(oauthTokenTest.ID),
862+
DisplayIdentifier: String(githubIdentifier),
863+
Tags: Bool(true),
864+
Branch: String("main"),
865+
},
866+
}
867+
868+
rm, err := client.RegistryModules.CreateWithVCSConnection(ctx, options)
869+
assert.Nil(t, rm)
870+
assert.Equal(t, err, ErrBranchMustBeEmptyWhenTagsEnabled)
871+
})
818872
})
819873

820874
t.Run("without options", func(t *testing.T) {

0 commit comments

Comments
 (0)