Skip to content

Commit

Permalink
registry/docker-credentials: add support for the read/write request p…
Browse files Browse the repository at this point in the history
…arameter (#275)

Co-authored-by: adamwg <[email protected]>
  • Loading branch information
kamaln7 and adamwg authored Nov 13, 2019
1 parent 4e7bcaa commit d3c0748
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
14 changes: 10 additions & 4 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RegistryService interface {
Create(context.Context, *RegistryCreateRequest) (*Registry, *Response, error)
Get(context.Context) (*Registry, *Response, error)
Delete(context.Context) (*Response, error)
DockerCredentials(context.Context) (*DockerCredentials, *Response, error)
DockerCredentials(context.Context, *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error)
}

var _ RegistryService = &RegistryServiceOp{}
Expand All @@ -33,6 +33,12 @@ type RegistryCreateRequest struct {
Name string `json:"name,omitempty"`
}

// RegistryDockerCredentialsRequest represents a request to retrieve docker
// credentials for a registry.
type RegistryDockerCredentialsRequest struct {
ReadWrite bool `json:"read_write"`
}

// Registry represents a registry.
type Registry struct {
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -91,9 +97,9 @@ type DockerCredentials struct {
DockerConfigJSON []byte
}

// DockerCredentials retrieves a Docker config file with the registry's credentials.
func (svc *RegistryServiceOp) DockerCredentials(ctx context.Context) (*DockerCredentials, *Response, error) {
path := fmt.Sprintf("%s/%s", registryPath, "docker-credentials")
// DockerCredentials retrieves a Docker config file containing the registry's credentials.
func (svc *RegistryServiceOp) DockerCredentials(ctx context.Context, request *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error) {
path := fmt.Sprintf("%s/%s?read_write=%t", registryPath, "docker-credentials", request.ReadWrite)

req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
Expand Down
45 changes: 33 additions & 12 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,38 @@ func TestRegistry_Delete(t *testing.T) {
}

func TestRegistry_DockerCredentials(t *testing.T) {
setup()
defer teardown()

want := []byte("this is a valid docker config json")

mux.HandleFunc("/v2/registry/docker-credentials", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.Write(want)
})
got, _, err := client.Registry.DockerCredentials(ctx)
returnedConfig := "this could be a docker config"
tests := []struct {
name string
params *RegistryDockerCredentialsRequest
expectedReadWrite string
}{
{
name: "read-only (default)",
params: &RegistryDockerCredentialsRequest{},
expectedReadWrite: "false",
},
{
name: "read/write",
params: &RegistryDockerCredentialsRequest{ReadWrite: true},
expectedReadWrite: "true",
},
}

require.NoError(t, err)
require.Equal(t, want, got.DockerConfigJSON)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/registry/docker-credentials", func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, test.expectedReadWrite, r.URL.Query().Get("read_write"))
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, returnedConfig)
})

got, _, err := client.Registry.DockerCredentials(ctx, test.params)
require.NoError(t, err)
require.Equal(t, []byte(returnedConfig), got.DockerConfigJSON)
})
}
}

0 comments on commit d3c0748

Please sign in to comment.