Skip to content

Commit b972d70

Browse files
authored
add support for etag and bulk secret modifications (#135)
1 parent ee170a2 commit b972d70

File tree

17 files changed

+1485
-731
lines changed

17 files changed

+1485
-731
lines changed

client/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Client interface {
5858
GetSecret(ctx context.Context, storeName, key string, meta map[string]string) (data map[string]string, err error)
5959

6060
// GetBulkSecret retrieves all preconfigured secrets for this application.
61-
GetBulkSecret(ctx context.Context, storeName string, meta map[string]string) (data map[string]string, err error)
61+
GetBulkSecret(ctx context.Context, storeName string, meta map[string]string) (data map[string]map[string]string, err error)
6262

6363
// SaveState saves the raw data into store using default state options.
6464
SaveState(ctx context.Context, storeName, key string, data []byte) error
@@ -79,7 +79,7 @@ type Client interface {
7979
DeleteState(ctx context.Context, storeName, key string) error
8080

8181
// DeleteStateWithETag deletes content from store using provided state options and etag.
82-
DeleteStateWithETag(ctx context.Context, storeName, key, etag string, meta map[string]string, opts *StateOptions) error
82+
DeleteStateWithETag(ctx context.Context, storeName, key string, etag *ETag, meta map[string]string, opts *StateOptions) error
8383

8484
// ExecuteStateTransaction provides way to execute multiple operations on a specified store.
8585
ExecuteStateTransaction(ctx context.Context, storeName string, meta map[string]string, ops []*StateOperation) error

client/client_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,12 @@ func (s *testDaprServer) GetSecret(ctx context.Context, req *pb.GetSecretRequest
191191
}
192192

193193
func (s *testDaprServer) GetBulkSecret(ctx context.Context, req *pb.GetBulkSecretRequest) (*pb.GetBulkSecretResponse, error) {
194-
d := make(map[string]string)
195-
d["test"] = "value"
194+
d := make(map[string]*pb.SecretResponse)
195+
d["test"] = &pb.SecretResponse{
196+
Secrets: map[string]string{
197+
"test": "value",
198+
},
199+
}
196200
return &pb.GetBulkSecretResponse{
197201
Data: d,
198202
}, nil

client/secret.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *GRPCClient) GetSecret(ctx context.Context, storeName, key string, meta
3535
}
3636

3737
// GetBulkSecret retrieves all preconfigured secrets for this application.
38-
func (c *GRPCClient) GetBulkSecret(ctx context.Context, storeName string, meta map[string]string) (data map[string]string, err error) {
38+
func (c *GRPCClient) GetBulkSecret(ctx context.Context, storeName string, meta map[string]string) (data map[string]map[string]string, err error) {
3939
if storeName == "" {
4040
return nil, errors.New("nil storeName")
4141
}
@@ -51,7 +51,15 @@ func (c *GRPCClient) GetBulkSecret(ctx context.Context, storeName string, meta m
5151
}
5252

5353
if resp != nil {
54-
data = resp.GetData()
54+
data = map[string]map[string]string{}
55+
56+
for secretName, secretResponse := range resp.Data {
57+
data[secretName] = map[string]string{}
58+
59+
for k, v := range secretResponse.Secrets {
60+
data[secretName][k] = v
61+
}
62+
}
5563
}
5664

5765
return

client/state.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,37 @@ type BulkStateItem struct {
120120
type SetStateItem struct {
121121
Key string
122122
Value []byte
123-
Etag string
123+
Etag *ETag
124124
Metadata map[string]string
125125
Options *StateOptions
126126
}
127127

128+
// ETag represents an versioned record information
129+
type ETag struct {
130+
Value string
131+
}
132+
128133
// StateOptions represents the state store persistence policy.
129134
type StateOptions struct {
130135
Concurrency StateConcurrency
131136
Consistency StateConsistency
132137
}
133138

134139
func toProtoSaveStateItem(si *SetStateItem) (item *v1.StateItem) {
135-
return &v1.StateItem{
136-
Etag: si.Etag,
140+
s := &v1.StateItem{
137141
Key: si.Key,
138142
Metadata: si.Metadata,
139143
Value: si.Value,
140144
Options: toProtoStateOptions(si.Options),
141145
}
146+
147+
if si.Etag != nil {
148+
s.Etag = &v1.Etag{
149+
Value: si.Etag.Value,
150+
}
151+
}
152+
153+
return s
142154
}
143155

144156
func toProtoStateOptions(so *StateOptions) (opts *v1.StateOptions) {
@@ -288,32 +300,37 @@ func (c *GRPCClient) GetStateWithConsistency(ctx context.Context, storeName, key
288300
}
289301

290302
return &StateItem{
291-
Etag: result.Etag,
292-
Key: key,
293-
Value: result.Data,
303+
Etag: result.Etag,
304+
Key: key,
305+
Value: result.Data,
294306
Metadata: result.Metadata,
295307
}, nil
296308
}
297309

298310
// DeleteState deletes content from store using default state options.
299311
func (c *GRPCClient) DeleteState(ctx context.Context, storeName, key string) error {
300-
return c.DeleteStateWithETag(ctx, storeName, key, "", nil, nil)
312+
return c.DeleteStateWithETag(ctx, storeName, key, nil, nil, nil)
301313
}
302314

303315
// DeleteStateWithETag deletes content from store using provided state options and etag.
304-
func (c *GRPCClient) DeleteStateWithETag(ctx context.Context, storeName, key, etag string, meta map[string]string, opts *StateOptions) error {
316+
func (c *GRPCClient) DeleteStateWithETag(ctx context.Context, storeName, key string, etag *ETag, meta map[string]string, opts *StateOptions) error {
305317
if err := hasRequiredStateArgs(storeName, key); err != nil {
306318
return errors.Wrap(err, "missing required arguments")
307319
}
308320

309321
req := &pb.DeleteStateRequest{
310322
StoreName: storeName,
311323
Key: key,
312-
Etag: etag,
313324
Options: toProtoStateOptions(opts),
314325
Metadata: meta,
315326
}
316327

328+
if etag != nil {
329+
req.Etag = &v1.Etag{
330+
Value: etag.Value,
331+
}
332+
}
333+
317334
_, err := c.protoClient.DeleteState(c.withAuthToken(ctx), req)
318335
if err != nil {
319336
return errors.Wrap(err, "error deleting state")

client/state_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ func TestSaveState(t *testing.T) {
6868

6969
t.Run("save data with version", func(t *testing.T) {
7070
item := &SetStateItem{
71-
Etag: "1",
71+
Etag: &ETag{
72+
Value: "1",
73+
},
7274
Key: key,
7375
Value: []byte(data),
7476
}
@@ -94,7 +96,7 @@ func TestDeleteState(t *testing.T) {
9496
assert.Nil(t, err)
9597
})
9698
t.Run("delete not exist data with etag and meta", func(t *testing.T) {
97-
err := testClient.DeleteStateWithETag(ctx, store, key, "100", map[string]string{"meta1": "value1"},
99+
err := testClient.DeleteStateWithETag(ctx, store, key, &ETag{Value: "100"}, map[string]string{"meta1": "value1"},
98100
&StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual})
99101
assert.Nil(t, err)
100102
})
@@ -127,9 +129,11 @@ func TestDeleteState(t *testing.T) {
127129

128130
t.Run("save data again with etag, meta", func(t *testing.T) {
129131
err := testClient.SaveBulkState(ctx, store, &SetStateItem{
130-
Key: key,
131-
Value: []byte(data),
132-
Etag: "100",
132+
Key: key,
133+
Value: []byte(data),
134+
Etag: &ETag{
135+
Value: "1",
136+
},
133137
Metadata: map[string]string{"meta1": "value1"},
134138
Options: &StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual},
135139
})
@@ -145,7 +149,7 @@ func TestDeleteState(t *testing.T) {
145149
})
146150

147151
t.Run("delete exist data with etag and meta", func(t *testing.T) {
148-
err := testClient.DeleteStateWithETag(ctx, store, key, "100", map[string]string{"meta1": "value1"},
152+
err := testClient.DeleteStateWithETag(ctx, store, key, &ETag{Value: "100"}, map[string]string{"meta1": "value1"},
149153
&StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual})
150154
assert.Nil(t, err)
151155
})
@@ -195,8 +199,10 @@ func TestStateTransactions(t *testing.T) {
195199
op := &StateOperation{
196200
Type: StateOperationTypeUpsert,
197201
Item: &SetStateItem{
198-
Key: item.Key,
199-
Etag: item.Etag,
202+
Key: item.Key,
203+
Etag: &ETag{
204+
Value: item.Etag,
205+
},
200206
Value: item.Value,
201207
},
202208
}

0 commit comments

Comments
 (0)