Skip to content

Commit fc80091

Browse files
authored
Merge pull request #988 from hashicorp/sams/TFECO-6399-read-by-external-id
Enable alternative reading of a registry module by its ID
2 parents 9359d0a + 7649437 commit fc80091

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Add support for enabling Stacks on an organization by @brandonc [#987](https://github.com/hashicorp/go-tfe/pull/987)
66
* Add support for filtering by key/value tags by @brandonc [#987](https://github.com/hashicorp/go-tfe/pull/987)
7+
* Add support for reading a registry module by its unique identifier by @dsa0x
8+
[#988](https://github.com/hashicorp/go-tfe/pull/988)
79

810
# v1.68.0
911

registry_module.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ const (
109109

110110
// RegistryModuleID represents the set of IDs that identify a RegistryModule
111111
// Use NewPublicRegistryModuleID or NewPrivateRegistryModuleID to build one
112+
112113
type RegistryModuleID struct {
114+
// The unique ID of the module. If given, the other fields are ignored.
115+
ID string
113116
// The organization the module belongs to, see RegistryModule.Organization.Name
114117
Organization string
115118
// The name of the module, see RegistryModule.Name
@@ -523,24 +526,29 @@ func (r *registryModules) Read(ctx context.Context, moduleID RegistryModuleID) (
523526
return nil, err
524527
}
525528

526-
if moduleID.RegistryName == "" {
527-
log.Println("[WARN] Support for using the RegistryModuleID without RegistryName is deprecated as of release 1.5.0 and may be removed in a future version. The preferred method is to include the RegistryName in RegistryModuleID.")
528-
moduleID.RegistryName = PrivateRegistry
529-
}
529+
var u string
530+
if moduleID.ID == "" {
531+
if moduleID.RegistryName == "" {
532+
log.Println("[WARN] Support for using the RegistryModuleID without RegistryName is deprecated as of release 1.5.0 and may be removed in a future version. The preferred method is to include the RegistryName in RegistryModuleID.")
533+
moduleID.RegistryName = PrivateRegistry
534+
}
530535

531-
if moduleID.RegistryName == PrivateRegistry && strings.TrimSpace(moduleID.Namespace) == "" {
532-
log.Println("[WARN] Support for using the RegistryModuleID without Namespace is deprecated as of release 1.5.0 and may be removed in a future version. The preferred method is to include the Namespace in RegistryModuleID.")
533-
moduleID.Namespace = moduleID.Organization
534-
}
536+
if moduleID.RegistryName == PrivateRegistry && strings.TrimSpace(moduleID.Namespace) == "" {
537+
log.Println("[WARN] Support for using the RegistryModuleID without Namespace is deprecated as of release 1.5.0 and may be removed in a future version. The preferred method is to include the Namespace in RegistryModuleID.")
538+
moduleID.Namespace = moduleID.Organization
539+
}
535540

536-
u := fmt.Sprintf(
537-
"organizations/%s/registry-modules/%s/%s/%s/%s",
538-
url.PathEscape(moduleID.Organization),
539-
url.PathEscape(string(moduleID.RegistryName)),
540-
url.PathEscape(moduleID.Namespace),
541-
url.PathEscape(moduleID.Name),
542-
url.PathEscape(moduleID.Provider),
543-
)
541+
u = fmt.Sprintf(
542+
"organizations/%s/registry-modules/%s/%s/%s/%s",
543+
url.PathEscape(moduleID.Organization),
544+
url.PathEscape(string(moduleID.RegistryName)),
545+
url.PathEscape(moduleID.Namespace),
546+
url.PathEscape(moduleID.Name),
547+
url.PathEscape(moduleID.Provider),
548+
)
549+
} else {
550+
u = fmt.Sprintf("registry-modules/%s", url.PathEscape(moduleID.ID))
551+
}
544552

545553
req, err := r.client.NewRequest("GET", u, nil)
546554
if err != nil {
@@ -690,6 +698,10 @@ func (r *registryModules) DeleteVersion(ctx context.Context, moduleID RegistryMo
690698
}
691699

692700
func (o RegistryModuleID) valid() error {
701+
if validString(&o.ID) && validStringID(&o.ID) {
702+
return nil
703+
}
704+
693705
if !validStringID(&o.Organization) {
694706
return ErrInvalidOrg
695707
}

registry_module_integration_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,27 @@ func TestRegistryModulesRead(t *testing.T) {
11931193
})
11941194
})
11951195

1196+
t.Run("with a unique ID field for private module", func(t *testing.T) {
1197+
rm, err := client.RegistryModules.Read(ctx, RegistryModuleID{
1198+
ID: registryModuleTest.ID,
1199+
})
1200+
require.NoError(t, err)
1201+
require.NotEmpty(t, rm)
1202+
assert.Equal(t, registryModuleTest.ID, rm.ID)
1203+
1204+
t.Run("permissions are properly decoded", func(t *testing.T) {
1205+
require.NotEmpty(t, rm.Permissions)
1206+
assert.True(t, rm.Permissions.CanDelete)
1207+
assert.True(t, rm.Permissions.CanResync)
1208+
assert.True(t, rm.Permissions.CanRetry)
1209+
})
1210+
1211+
t.Run("timestamps are properly decoded", func(t *testing.T) {
1212+
assert.NotEmpty(t, rm.CreatedAt)
1213+
assert.NotEmpty(t, rm.UpdatedAt)
1214+
})
1215+
})
1216+
11961217
t.Run("without a name", func(t *testing.T) {
11971218
rm, err := client.RegistryModules.Read(ctx, RegistryModuleID{
11981219
Organization: orgTest.Name,

0 commit comments

Comments
 (0)