Skip to content

Commit

Permalink
Merge pull request #385 from chroju/add_registry_module_list
Browse files Browse the repository at this point in the history
Add List Registry Modules Endpoint
  • Loading branch information
sebasslash committed Jun 8, 2022
2 parents f920ad3 + 36b9398 commit 5a2a37b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion helper_test.go
Expand Up @@ -674,7 +674,7 @@ func createRegistryModule(t *testing.T, client *Client, org *Organization) (*Reg
ctx := context.Background()

options := RegistryModuleCreateOptions{
Name: String("name"),
Name: String(randomString(t)),
Provider: String("provider"),
}
rm, err := client.RegistryModules.Create(ctx, org.Name, options)
Expand Down
15 changes: 15 additions & 0 deletions mocks/registry_module_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions registry_module.go
Expand Up @@ -14,6 +14,9 @@ var _ RegistryModules = (*registryModules)(nil)
//
// TFE API docs: https://www.terraform.io/docs/cloud/api/modules.html
type RegistryModules interface {
// List all the registory modules within an organization
List(ctx context.Context, organization string, options *RegistryModuleListOptions) (*RegistryModuleList, error)

// Create a registry module without a VCS repo
Create(ctx context.Context, organization string, options RegistryModuleCreateOptions) (*RegistryModule, error)

Expand Down Expand Up @@ -81,6 +84,12 @@ type RegistryModuleID struct {
Provider string
}

// RegistryModuleList represents a list of registry modules.
type RegistryModuleList struct {
*Pagination
Items []*RegistryModule
}

// RegistryModule represents a registry module
type RegistryModule struct {
ID string `jsonapi:"primary,registry-modules"`
Expand Down Expand Up @@ -125,6 +134,11 @@ type RegistryModuleVersionStatuses struct {
Error string `jsonapi:"attr,error"`
}

// RegistryModuleListOptions represents the options for listing registry modules.
type RegistryModuleListOptions struct {
ListOptions
}

// RegistryModuleCreateOptions is used when creating a registry module without a VCS repo
type RegistryModuleCreateOptions struct {
// Type is a public field utilized by JSON:API to
Expand Down Expand Up @@ -167,6 +181,27 @@ type RegistryModuleVCSRepoOptions struct {
DisplayIdentifier *string `json:"display-identifier"` // Required
}

// List all the registory modules within an organization.
func (s *registryModules) List(ctx context.Context, organization string, options *RegistryModuleListOptions) (*RegistryModuleList, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}

u := fmt.Sprintf("organizations/%s/registry-modules", url.QueryEscape(organization))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}

ml := &RegistryModuleList{}
err = s.client.do(ctx, req, ml)
if err != nil {
return nil, err
}

return ml, nil
}

// Upload uploads Terraform configuration files for the provided registry module version. It
// requires a path to the configuration files on disk, which will be packaged by
// hashicorp/go-slug before being uploaded.
Expand Down
45 changes: 45 additions & 0 deletions registry_module_integration_test.go
Expand Up @@ -17,6 +17,51 @@ import (
"github.com/stretchr/testify/require"
)

func TestRegistryModulesList(t *testing.T) {
client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()

registryModuleTest1, registryModuleTest1Cleanup := createRegistryModule(t, client, orgTest)
defer registryModuleTest1Cleanup()
registryModuleTest2, registryModuleTest2Cleanup := createRegistryModule(t, client, orgTest)
defer registryModuleTest2Cleanup()

t.Run("with no list options", func(t *testing.T) {
modl, err := client.RegistryModules.List(ctx, orgTest.Name, &RegistryModuleListOptions{})
require.NoError(t, err)
assert.Contains(t, modl.Items, registryModuleTest1)
assert.Contains(t, modl.Items, registryModuleTest2)
assert.Equal(t, 1, modl.CurrentPage)
assert.Equal(t, 2, modl.TotalCount)
})

t.Run("with list options", func(t *testing.T) {
modl, err := client.RegistryModules.List(ctx, orgTest.Name, &RegistryModuleListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
// Out of range page number, so the items should be empty
assert.Empty(t, modl.Items)
assert.Equal(t, 999, modl.CurrentPage)

modl, err = client.RegistryModules.List(ctx, orgTest.Name, &RegistryModuleListOptions{
ListOptions: ListOptions{
PageNumber: 1,
PageSize: 100,
},
})
require.NoError(t, err)
assert.NotEmpty(t, modl.Items)
assert.Equal(t, 1, modl.CurrentPage)
})
}

func TestRegistryModulesCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down

0 comments on commit 5a2a37b

Please sign in to comment.