Skip to content

Commit 693d3d1

Browse files
authored
Merge pull request #624 from weichou1229/issue-623
feat: Add secret route path and HTTP client
2 parents 6fe3687 + 2bee6ef commit 693d3d1

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed

v2/clients/http/common.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (C) 2020 IOTech Ltd
2+
// Copyright (C) 2020-2021 IOTech Ltd
33
//
44
// SPDX-License-Identifier: Apache-2.0
55

@@ -61,3 +61,11 @@ func (cc *commonClient) Version(ctx context.Context) (common.VersionResponse, er
6161
}
6262
return vr, nil
6363
}
64+
65+
func (cc *commonClient) AddSecret(ctx context.Context, request common.SecretRequest) (res common.BaseResponse, err errors.EdgeX) {
66+
err = utils.PostRequestWithRawData(ctx, &res, cc.baseUrl+v2.ApiSecretRoute, request)
67+
if err != nil {
68+
return res, errors.NewCommonEdgeXWrapper(err)
69+
}
70+
return res, nil
71+
}

v2/clients/http/common_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (C) 2020 IOTech Ltd
2+
// Copyright (C) 2020-2021 IOTech Ltd
33
//
44
// SPDX-License-Identifier: Apache-2.0
55

@@ -65,6 +65,24 @@ func TestVersion(t *testing.T) {
6565
require.Equal(t, expected, response)
6666
}
6767

68+
func TestAddSecret(t *testing.T) {
69+
expected := common.BaseResponse{}
70+
req := common.NewSecretRequest(
71+
"testPath",
72+
[]common.SecretDataKeyValue{
73+
{Key: "username", Value: "tester"},
74+
{Key: "password", Value: "123"},
75+
},
76+
)
77+
ts := newTestServer(http.MethodPost, v2.ApiSecretRoute, expected)
78+
defer ts.Close()
79+
80+
client := NewCommonClient(ts.URL)
81+
res, err := client.AddSecret(context.Background(), req)
82+
require.NoError(t, err)
83+
require.IsType(t, expected, res)
84+
}
85+
6886
func newTestServer(httpMethod string, apiRoute string, expectedResponse interface{}) *httptest.Server {
6987
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7088
if r.Method != httpMethod {

v2/clients/interfaces/common.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (C) 2020 IOTech Ltd
2+
// Copyright (C) 2020-2021 IOTech Ltd
33
//
44
// SPDX-License-Identifier: Apache-2.0
55

@@ -21,4 +21,6 @@ type CommonClient interface {
2121
Ping(ctx context.Context) (common.PingResponse, errors.EdgeX)
2222
// Version obtains version information from the target service.
2323
Version(ctx context.Context) (common.VersionResponse, errors.EdgeX)
24+
// AddSecret adds EdgeX Service exclusive secret to the Secret Store
25+
AddSecret(ctx context.Context, request common.SecretRequest) (common.BaseResponse, errors.EdgeX)
2426
}

v2/clients/interfaces/mocks/CommonClient.go

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const (
9494
ApiMetricsRoute = ApiBase + "/metrics"
9595
ApiPingRoute = ApiBase + "/ping"
9696
ApiVersionRoute = ApiBase + "/version"
97+
ApiSecretRoute = ApiBase + "/secret"
9798

9899
ApiDeviceCallbackRoute = ApiBase + "/callback/device"
99100
ApiDeviceCallbackNameRoute = ApiBase + "/callback/device/name/{name}"

v2/dtos/common/secret.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ func (sr *SecretRequest) UnmarshalJSON(b []byte) error {
6464
}
6565
return nil
6666
}
67+
68+
func NewSecretRequest(path string, secretData []SecretDataKeyValue) SecretRequest {
69+
return SecretRequest{
70+
BaseRequest: NewBaseRequest(),
71+
Path: path,
72+
SecretData: secretData,
73+
}
74+
}

0 commit comments

Comments
 (0)