Merge pull request #28720 from jlhawn/add_secret_update_method

Add SecretUpdate method to client
This commit is contained in:
Vincent Demeester 2017-01-13 21:28:43 +01:00 committed by GitHub
commit bd9361b810
5 changed files with 105 additions and 0 deletions

View file

@ -7770,3 +7770,36 @@ paths:
type: "string" type: "string"
description: "ID of the secret" description: "ID of the secret"
tags: ["Secret"] tags: ["Secret"]
/secrets/{id}/update:
post:
summary: "Update a Secret"
operationId: "SecretUpdate"
responses:
200:
description: "no error"
404:
description: "no such secret"
schema:
$ref: "#/definitions/ErrorResponse"
500:
description: "server error"
schema:
$ref: "#/definitions/ErrorResponse"
parameters:
- name: "id"
in: "path"
description: "The ID of the secret"
type: "string"
required: true
- name: "body"
in: "body"
schema:
$ref: "#/definitions/SecretSpec"
description: "The spec of the secret to update. Currently, only the Labels field can be updated. All other fields must remain unchanged from the [SecretInspect endpoint](#operation/SecretInspect) response values."
- name: "version"
in: "query"
description: "The version number of the secret object being updated. This is required to avoid conflicting writes."
type: "integer"
format: "int64"
required: true
tags: ["Secret"]

View file

@ -401,6 +401,9 @@ func (s secretAPIClientMock) SecretRemove(ctx context.Context, id string) error
func (s secretAPIClientMock) SecretInspectWithRaw(ctx context.Context, name string) (swarm.Secret, []byte, error) { func (s secretAPIClientMock) SecretInspectWithRaw(ctx context.Context, name string) (swarm.Secret, []byte, error) {
return swarm.Secret{}, []byte{}, nil return swarm.Secret{}, []byte{}, nil
} }
func (s secretAPIClientMock) SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error {
return nil
}
// TestUpdateSecretUpdateInPlace tests the ability to update the "target" of an secret with "docker service update" // TestUpdateSecretUpdateInPlace tests the ability to update the "target" of an secret with "docker service update"
// by combining "--secret-rm" and "--secret-add" for the same secret. // by combining "--secret-rm" and "--secret-add" for the same secret.

View file

@ -166,4 +166,5 @@ type SecretAPIClient interface {
SecretCreate(ctx context.Context, secret swarm.SecretSpec) (types.SecretCreateResponse, error) SecretCreate(ctx context.Context, secret swarm.SecretSpec) (types.SecretCreateResponse, error)
SecretRemove(ctx context.Context, id string) error SecretRemove(ctx context.Context, id string) error
SecretInspectWithRaw(ctx context.Context, name string) (swarm.Secret, []byte, error) SecretInspectWithRaw(ctx context.Context, name string) (swarm.Secret, []byte, error)
SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error
} }

19
client/secret_update.go Normal file
View file

@ -0,0 +1,19 @@
package client
import (
"net/url"
"strconv"
"github.com/docker/docker/api/types/swarm"
"golang.org/x/net/context"
)
// SecretUpdate updates a Secret. Currently, the only part of a secret spec
// which can be updated is Labels.
func (cli *Client) SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error {
query := url.Values{}
query.Set("version", strconv.FormatUint(version.Index, 10))
resp, err := cli.post(ctx, "/secrets/"+id+"/update", query, secret, nil)
ensureReaderClosed(resp)
return err
}

View file

@ -0,0 +1,49 @@
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
"github.com/docker/docker/api/types/swarm"
)
func TestSecretUpdateError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.SecretUpdate(context.Background(), "secret_id", swarm.Version{}, swarm.SecretSpec{})
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestSecretUpdate(t *testing.T) {
expectedURL := "/secrets/secret_id/update"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != "POST" {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
}),
}
err := client.SecretUpdate(context.Background(), "secret_id", swarm.Version{}, swarm.SecretSpec{})
if err != nil {
t.Fatal(err)
}
}