Skip to content

Commit

Permalink
Merge pull request edgexfoundry#624 from weichou1229/issue-623
Browse files Browse the repository at this point in the history
feat: Add secret route path and HTTP client
  • Loading branch information
cloudxxx8 authored May 26, 2021
2 parents 6fe3687 + 2bee6ef commit 693d3d1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
10 changes: 9 additions & 1 deletion v2/clients/http/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -61,3 +61,11 @@ func (cc *commonClient) Version(ctx context.Context) (common.VersionResponse, er
}
return vr, nil
}

func (cc *commonClient) AddSecret(ctx context.Context, request common.SecretRequest) (res common.BaseResponse, err errors.EdgeX) {
err = utils.PostRequestWithRawData(ctx, &res, cc.baseUrl+v2.ApiSecretRoute, request)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
20 changes: 19 additions & 1 deletion v2/clients/http/common_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -65,6 +65,24 @@ func TestVersion(t *testing.T) {
require.Equal(t, expected, response)
}

func TestAddSecret(t *testing.T) {
expected := common.BaseResponse{}
req := common.NewSecretRequest(
"testPath",
[]common.SecretDataKeyValue{
{Key: "username", Value: "tester"},
{Key: "password", Value: "123"},
},
)
ts := newTestServer(http.MethodPost, v2.ApiSecretRoute, expected)
defer ts.Close()

client := NewCommonClient(ts.URL)
res, err := client.AddSecret(context.Background(), req)
require.NoError(t, err)
require.IsType(t, expected, res)
}

func newTestServer(httpMethod string, apiRoute string, expectedResponse interface{}) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != httpMethod {
Expand Down
4 changes: 3 additions & 1 deletion v2/clients/interfaces/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -21,4 +21,6 @@ type CommonClient interface {
Ping(ctx context.Context) (common.PingResponse, errors.EdgeX)
// Version obtains version information from the target service.
Version(ctx context.Context) (common.VersionResponse, errors.EdgeX)
// AddSecret adds EdgeX Service exclusive secret to the Secret Store
AddSecret(ctx context.Context, request common.SecretRequest) (common.BaseResponse, errors.EdgeX)
}
25 changes: 24 additions & 1 deletion v2/clients/interfaces/mocks/CommonClient.go

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

1 change: 1 addition & 0 deletions v2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const (
ApiMetricsRoute = ApiBase + "/metrics"
ApiPingRoute = ApiBase + "/ping"
ApiVersionRoute = ApiBase + "/version"
ApiSecretRoute = ApiBase + "/secret"

ApiDeviceCallbackRoute = ApiBase + "/callback/device"
ApiDeviceCallbackNameRoute = ApiBase + "/callback/device/name/{name}"
Expand Down
8 changes: 8 additions & 0 deletions v2/dtos/common/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ func (sr *SecretRequest) UnmarshalJSON(b []byte) error {
}
return nil
}

func NewSecretRequest(path string, secretData []SecretDataKeyValue) SecretRequest {
return SecretRequest{
BaseRequest: NewBaseRequest(),
Path: path,
SecretData: secretData,
}
}

0 comments on commit 693d3d1

Please sign in to comment.