/
client.go
745 lines (653 loc) · 24.2 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package aztables
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
generated "github.com/Azure/azure-sdk-for-go/sdk/data/aztables/internal"
)
// A Client represents a client to the tables service affinitized to a specific table.
type Client struct {
client *generated.TableClient
service *ServiceClient
cred *SharedKeyCredential
name string
con *generated.Connection
}
// NewClient creates a Client struct in the context of the table specified in the serviceURL, authorizing requests with an Azure AD access token.
// The serviceURL param is expected to have the name of the table in a format similar to: "https://myAccountName.core.windows.net/<myTableName>".
func NewClient(serviceURL string, cred azcore.TokenCredential, options *ClientOptions) (*Client, error) {
if options == nil {
options = &ClientOptions{}
}
rawServiceURL, tableName, err := parseURL(serviceURL)
if err != nil {
return nil, err
}
s, err := NewServiceClient(rawServiceURL, cred, options)
if err != nil {
return nil, err
}
return s.NewClient(tableName), nil
}
// NewClientWithNoCredential creates a Client struct in the context of the table specified in the serviceURL.
// The serviceURL param is expected to have the name of the table in a format similar to: "https://myAccountName.core.windows.net/<myTableName>?<SAS token>".
func NewClientWithNoCredential(serviceURL string, options *ClientOptions) (*Client, error) {
if options == nil {
options = &ClientOptions{}
}
rawServiceURL, tableName, err := parseURL(serviceURL)
if err != nil {
return nil, err
}
s, err := NewServiceClientWithNoCredential(rawServiceURL, options)
if err != nil {
return nil, err
}
return s.NewClient(tableName), nil
}
// NewClientWithSharedKey creates a Client struct in the context of the table specified in the serviceURL, authorizing requests with a shared key.
// The serviceURL param is expected to have the name of the table in a format similar to: "https://myAccountName.core.windows.net/<myTableName>".
func NewClientWithSharedKey(serviceURL string, cred *SharedKeyCredential, options *ClientOptions) (*Client, error) {
if options == nil {
options = &ClientOptions{}
}
rawServiceURL, tableName, err := parseURL(serviceURL)
if err != nil {
return nil, err
}
s, err := NewServiceClientWithSharedKey(rawServiceURL, cred, options)
if err != nil {
return nil, err
}
return s.NewClient(tableName), nil
}
func parseURL(serviceURL string) (string, string, error) {
parsedUrl, err := url.Parse(serviceURL)
if err != nil {
return "", "", err
}
tableName := parsedUrl.Path[1:]
rawServiceURL := parsedUrl.Scheme + "://" + parsedUrl.Host
if parsedUrl.Scheme == "" {
rawServiceURL = parsedUrl.Host
}
if strings.Contains(tableName, "/") {
splits := strings.Split(parsedUrl.Path, "/")
tableName = splits[len(splits)-1]
rawServiceURL += strings.Join(splits[:len(splits)-1], "/")
}
sas := parsedUrl.Query()
if len(sas) > 0 {
rawServiceURL += "/?" + sas.Encode()
}
return rawServiceURL, tableName, nil
}
type CreateTableResponse struct {
RawResponse *http.Response
}
func createTableResponseFromGen(g *generated.TableClientCreateResponse) CreateTableResponse {
if g == nil {
return CreateTableResponse{}
}
return CreateTableResponse{
RawResponse: g.RawResponse,
}
}
// Create creates the table with the tableName specified when NewClient was called.
func (t *Client) Create(ctx context.Context, options *CreateTableOptions) (CreateTableResponse, error) {
if options == nil {
options = &CreateTableOptions{}
}
resp, err := t.client.Create(ctx, generated.Enum1Three0, generated.TableProperties{TableName: &t.name}, options.toGenerated(), &generated.QueryOptions{})
return createTableResponseFromGen(&resp), err
}
// Delete deletes the table with the tableName specified when NewClient was called.
func (t *Client) Delete(ctx context.Context, options *DeleteTableOptions) (DeleteTableResponse, error) {
return t.service.DeleteTable(ctx, t.name, options)
}
// ListEntitiesOptions contains a group of parameters for the Table.Query method.
type ListEntitiesOptions struct {
// OData filter expression.
Filter *string
// Select expression using OData notation. Limits the columns on each record to just those requested, e.g. "$select=PolicyAssignmentId, ResourceId".
Select *string
// Maximum number of records to return.
Top *int32
// The PartitionKey to start paging from
PartitionKey *string
// The RowKey to start paging from
RowKey *string
}
func (l *ListEntitiesOptions) toQueryOptions() *generated.QueryOptions {
if l == nil {
return &generated.QueryOptions{}
}
return &generated.QueryOptions{
Filter: l.Filter,
Format: generated.ODataMetadataFormatApplicationJSONODataMinimalmetadata.ToPtr(),
Select: l.Select,
Top: l.Top,
}
}
// ListEntitiesPage is the response envelope for operations that return a list of entities.
type ListEntitiesPage struct {
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
// ContinuationNextPartitionKey contains the information returned from the x-ms-continuation-NextPartitionKey header response.
ContinuationNextPartitionKey *string
// ContinuationNextRowKey contains the information returned from the x-ms-continuation-NextRowKey header response.
ContinuationNextRowKey *string
// The metadata response of the table.
ODataMetadata *string
// List of table entities.
Entities [][]byte
}
// ListEntitiesResponse - The properties for the table entity query response.
type ListEntitiesResponse struct {
// The metadata response of the table.
ODataMetadata *string
// List of table entities stored as byte slices.
Entities [][]byte
}
// transforms a generated query response into the ListEntitiesPaged
func newListEntitiesPage(resp *generated.TableClientQueryEntitiesResponse) (ListEntitiesPage, error) {
marshalledValue := make([][]byte, 0)
for _, e := range resp.TableEntityQueryResponse.Value {
m, err := json.Marshal(e)
if err != nil {
return ListEntitiesPage{}, err
}
marshalledValue = append(marshalledValue, m)
}
t := ListEntitiesResponse{
ODataMetadata: resp.TableEntityQueryResponse.ODataMetadata,
Entities: marshalledValue,
}
return ListEntitiesPage{
RawResponse: resp.RawResponse,
ContinuationNextPartitionKey: resp.XMSContinuationNextPartitionKey,
ContinuationNextRowKey: resp.XMSContinuationNextRowKey,
ODataMetadata: t.ODataMetadata,
Entities: t.Entities,
}, nil
}
// ListEntitiesPager is a Pager for Table entity query results.
//
// NextPage should be called first. It fetches the next available page of results from the service.
// If the fetched page contains results, the return value is true, else false.
// Results fetched from the service can be evaluated by calling PageResponse on this Pager.
// If the result is false, the value of Err() will indicate if an error occurred.
//
// PageResponse returns the results from the page most recently fetched from the service.
type ListEntitiesPager interface {
// PageResponse returns the current TableQueryResponseResponse.
PageResponse() ListEntitiesPage
// NextPage returns true if there is another page of data available, false if not
NextPage(context.Context) bool
// Err returns an error if there was an error on the last request
Err() error
// NextPagePartitionKey returns the PartitionKey for the current page
NextPagePartitionKey() *string
// NextPageRowKey returns the RowKey for the current page
NextPageRowKey() *string
}
type tableEntityQueryResponsePager struct {
tableClient *Client
current *ListEntitiesPage
tableQueryOptions *generated.TableClientQueryEntitiesOptions
listOptions *ListEntitiesOptions
err error
}
func (p *tableEntityQueryResponsePager) NextPagePartitionKey() *string {
return p.tableQueryOptions.NextPartitionKey
}
func (p *tableEntityQueryResponsePager) NextPageRowKey() *string {
return p.tableQueryOptions.NextRowKey
}
// NextPage fetches the next available page of results from the service.
// If the fetched page contains results, the return value is true, else false.
// Results fetched from the service can be evaluated by calling PageResponse on this Pager.
func (p *tableEntityQueryResponsePager) NextPage(ctx context.Context) bool {
if p.err != nil || (p.current != nil && p.current.ContinuationNextPartitionKey == nil && p.current.ContinuationNextRowKey == nil) {
return false
}
var resp generated.TableClientQueryEntitiesResponse
resp, p.err = p.tableClient.client.QueryEntities(
ctx,
generated.Enum1Three0,
p.tableClient.name,
p.tableQueryOptions,
p.listOptions.toQueryOptions(),
)
c, err := newListEntitiesPage(&resp)
if err != nil {
p.err = nil
}
p.current = &c
p.tableQueryOptions.NextPartitionKey = resp.XMSContinuationNextPartitionKey
p.tableQueryOptions.NextRowKey = resp.XMSContinuationNextRowKey
return p.err == nil && len(resp.TableEntityQueryResponse.Value) > 0
}
// PageResponse returns the results from the page most recently fetched from the service.
func (p *tableEntityQueryResponsePager) PageResponse() ListEntitiesPage {
return *p.current
}
// Err returns an error value if the most recent call to NextPage was not successful, else nil.
func (p *tableEntityQueryResponsePager) Err() error {
return p.err
}
// List queries the entities using the specified ListEntitiesOptions.
// listOptions can specify the following properties to affect the query results returned:
//
// Filter: An OData filter expression that limits results to those entities that satisfy the filter expression.
// For example, the following expression would return only entities with a PartitionKey of 'foo': "PartitionKey eq 'foo'"
//
// Select: A comma delimited list of entity property names that selects which set of entity properties to return in the result set.
// For example, the following value would return results containing only the PartitionKey and RowKey properties: "PartitionKey, RowKey"
//
// Top: The maximum number of entities that will be returned per page of results.
// Note: This value does not limit the total number of results if NextPage is called on the returned Pager until it returns false.
//
// List returns a Pager, which allows iteration through each page of results.
func (t *Client) List(listOptions *ListEntitiesOptions) ListEntitiesPager {
if listOptions == nil {
listOptions = &ListEntitiesOptions{}
}
return &tableEntityQueryResponsePager{
tableClient: t,
listOptions: listOptions,
tableQueryOptions: &generated.TableClientQueryEntitiesOptions{
NextPartitionKey: listOptions.PartitionKey,
NextRowKey: listOptions.RowKey,
},
}
}
// Options for Client.GetEntity method
type GetEntityOptions struct {
}
func (g *GetEntityOptions) toGenerated() (*generated.TableClientQueryEntityWithPartitionAndRowKeyOptions, *generated.QueryOptions) {
return &generated.TableClientQueryEntityWithPartitionAndRowKeyOptions{}, &generated.QueryOptions{Format: generated.ODataMetadataFormatApplicationJSONODataMinimalmetadata.ToPtr()}
}
// GetEntityResponse is the return type for a GetEntity operation. The individual entities are stored in the Value property
type GetEntityResponse struct {
// ETag contains the information returned from the ETag header response.
ETag azcore.ETag
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
// The properties of the table entity.
Value []byte
}
// newGetEntityResponse transforms a generated response to the GetEntityResponse type
func newGetEntityResponse(g generated.TableClientQueryEntityWithPartitionAndRowKeyResponse) (GetEntityResponse, error) {
marshalledValue, err := json.Marshal(g.Value)
if err != nil {
return GetEntityResponse{}, err
}
var ETag azcore.ETag
if g.ETag != nil {
ETag = azcore.ETag(*g.ETag)
}
return GetEntityResponse{
ETag: ETag,
RawResponse: g.RawResponse,
Value: marshalledValue,
}, nil
}
// GetEntity retrieves a specific entity from the service using the specified partitionKey and rowKey values. If no entity is available it returns an error
func (t *Client) GetEntity(ctx context.Context, partitionKey string, rowKey string, options *GetEntityOptions) (GetEntityResponse, error) {
if options == nil {
options = &GetEntityOptions{}
}
genOptions, queryOptions := options.toGenerated()
resp, err := t.client.QueryEntityWithPartitionAndRowKey(ctx, generated.Enum1Three0, t.name, partitionKey, rowKey, genOptions, queryOptions)
if err != nil {
return GetEntityResponse{}, err
}
return newGetEntityResponse(resp)
}
// Options for the Client.AddEntity operation
type AddEntityOptions struct {
// Specifies whether the response should include the inserted entity in the payload. Possible values are return-no-content and return-content.
ResponsePreference *ResponseFormat
}
type AddEntityResponse struct {
RawResponse *http.Response
ETag azcore.ETag
}
func addEntityResponseFromGenerated(g *generated.TableClientInsertEntityResponse) AddEntityResponse {
if g == nil {
return AddEntityResponse{}
}
var ETag azcore.ETag
if g.ETag != nil {
ETag = azcore.ETag(*g.ETag)
}
return AddEntityResponse{
RawResponse: g.RawResponse,
ETag: ETag,
}
}
// AddEntity adds an entity (described by a byte slice) to the table. This method returns an error if an entity with
// the same PartitionKey and RowKey already exists in the table. If the supplied entity does not contain both a PartitionKey
// and a RowKey an error will be returned.
func (t *Client) AddEntity(ctx context.Context, entity []byte, options *AddEntityOptions) (AddEntityResponse, error) {
var mapEntity map[string]interface{}
err := json.Unmarshal(entity, &mapEntity)
if err != nil {
return AddEntityResponse{}, err
}
resp, err := t.client.InsertEntity(ctx, generated.Enum1Three0, t.name, &generated.TableClientInsertEntityOptions{TableEntityProperties: mapEntity, ResponsePreference: generated.ResponseFormatReturnNoContent.ToPtr()}, nil)
if err != nil {
err = checkEntityForPkRk(&mapEntity, err)
return AddEntityResponse{}, err
}
return addEntityResponseFromGenerated(&resp), err
}
type DeleteEntityOptions struct {
IfMatch *azcore.ETag
}
func (d *DeleteEntityOptions) toGenerated() *generated.TableClientDeleteEntityOptions {
return &generated.TableClientDeleteEntityOptions{}
}
type DeleteEntityResponse struct {
RawResponse *http.Response
}
func deleteEntityResponseFromGenerated(g *generated.TableClientDeleteEntityResponse) DeleteEntityResponse {
if g == nil {
return DeleteEntityResponse{}
}
return DeleteEntityResponse{
RawResponse: g.RawResponse,
}
}
// DeleteEntity deletes the entity with the specified partitionKey and rowKey from the table.
func (t *Client) DeleteEntity(ctx context.Context, partitionKey string, rowKey string, options *DeleteEntityOptions) (DeleteEntityResponse, error) {
if options == nil {
options = &DeleteEntityOptions{}
}
if options.IfMatch == nil {
nilEtag := azcore.ETag("*")
options.IfMatch = &nilEtag
}
resp, err := t.client.DeleteEntity(ctx, generated.Enum1Three0, t.name, partitionKey, rowKey, string(*options.IfMatch), options.toGenerated(), &generated.QueryOptions{})
return deleteEntityResponseFromGenerated(&resp), err
}
type UpdateEntityOptions struct {
IfMatch *azcore.ETag
UpdateMode EntityUpdateMode
}
func (u *UpdateEntityOptions) toGeneratedMergeEntity(m map[string]interface{}) *generated.TableClientMergeEntityOptions {
if u == nil {
return &generated.TableClientMergeEntityOptions{}
}
return &generated.TableClientMergeEntityOptions{
IfMatch: to.StringPtr(string(*u.IfMatch)),
TableEntityProperties: m,
}
}
func (u *UpdateEntityOptions) toGeneratedUpdateEntity(m map[string]interface{}) *generated.TableClientUpdateEntityOptions {
if u == nil {
return &generated.TableClientUpdateEntityOptions{}
}
return &generated.TableClientUpdateEntityOptions{
IfMatch: to.StringPtr(string(*u.IfMatch)),
TableEntityProperties: m,
}
}
type UpdateEntityResponse struct {
RawResponse *http.Response
ETag azcore.ETag
}
func updateEntityResponseFromMergeGenerated(g *generated.TableClientMergeEntityResponse) UpdateEntityResponse {
if g == nil {
return UpdateEntityResponse{}
}
var ETag azcore.ETag
if g.ETag != nil {
ETag = azcore.ETag(*g.ETag)
}
return UpdateEntityResponse{
RawResponse: g.RawResponse,
ETag: ETag,
}
}
func updateEntityResponseFromUpdateGenerated(g *generated.TableClientUpdateEntityResponse) UpdateEntityResponse {
if g == nil {
return UpdateEntityResponse{}
}
var ETag azcore.ETag
if g.ETag != nil {
ETag = azcore.ETag(*g.ETag)
}
return UpdateEntityResponse{
RawResponse: g.RawResponse,
ETag: ETag,
}
}
// UpdateEntity updates the specified table entity if it exists.
// If updateMode is Replace, the entity will be replaced. This is the only way to remove properties from an existing entity.
// If updateMode is Merge, the property values present in the specified entity will be merged with the existing entity. Properties not specified in the merge will be unaffected.
// The specified etag value will be used for optimistic concurrency. If the etag does not match the value of the entity in the table, the operation will fail.
// The response type will be TableEntityMergeResponse if updateMode is Merge and TableEntityUpdateResponse if updateMode is Replace.
func (t *Client) UpdateEntity(ctx context.Context, entity []byte, options *UpdateEntityOptions) (UpdateEntityResponse, error) {
if options == nil {
options = &UpdateEntityOptions{
UpdateMode: MergeEntity,
}
}
if options.IfMatch == nil {
star := azcore.ETag("*")
options.IfMatch = &star
}
var mapEntity map[string]interface{}
err := json.Unmarshal(entity, &mapEntity)
if err != nil {
return UpdateEntityResponse{}, err
}
pk := mapEntity[partitionKey]
partKey := pk.(string)
rk := mapEntity[rowKey]
rowkey := rk.(string)
switch options.UpdateMode {
case MergeEntity:
resp, err := t.client.MergeEntity(
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
options.toGeneratedMergeEntity(mapEntity),
&generated.QueryOptions{},
)
return updateEntityResponseFromMergeGenerated(&resp), err
case ReplaceEntity:
resp, err := t.client.UpdateEntity(
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
options.toGeneratedUpdateEntity(mapEntity),
&generated.QueryOptions{},
)
return updateEntityResponseFromUpdateGenerated(&resp), err
}
if pk == "" || rk == "" {
return UpdateEntityResponse{}, errPartitionKeyRowKeyError
}
return UpdateEntityResponse{}, errInvalidUpdateMode
}
type InsertEntityOptions struct {
ETag azcore.ETag
UpdateMode EntityUpdateMode
}
type InsertEntityResponse struct {
RawResponse *http.Response
ETag azcore.ETag
}
func insertEntityFromGeneratedMerge(g *generated.TableClientMergeEntityResponse) InsertEntityResponse {
if g == nil {
return InsertEntityResponse{}
}
var ETag azcore.ETag
if g.ETag != nil {
ETag = azcore.ETag(*g.ETag)
}
return InsertEntityResponse{
RawResponse: g.RawResponse,
ETag: ETag,
}
}
func insertEntityFromGeneratedUpdate(g *generated.TableClientUpdateEntityResponse) InsertEntityResponse {
if g == nil {
return InsertEntityResponse{}
}
var ETag azcore.ETag
if g.ETag != nil {
ETag = azcore.ETag(*g.ETag)
}
return InsertEntityResponse{
RawResponse: g.RawResponse,
ETag: ETag,
}
}
// InsertEntity inserts an entity if it does not already exist in the table. If the entity does exist, the entity is
// replaced or merged as specified the updateMode parameter. If the entity exists and updateMode is Merge, the property
// values present in the specified entity will be merged with the existing entity rather than replaced.
// The response type will be TableEntityMergeResponse if updateMode is Merge and TableEntityUpdateResponse if updateMode is Replace.
func (t *Client) InsertEntity(ctx context.Context, entity []byte, options *InsertEntityOptions) (InsertEntityResponse, error) {
if options == nil {
options = &InsertEntityOptions{
UpdateMode: MergeEntity,
}
}
var mapEntity map[string]interface{}
err := json.Unmarshal(entity, &mapEntity)
if err != nil {
return InsertEntityResponse{}, err
}
pk := mapEntity[partitionKey]
partKey := pk.(string)
rk := mapEntity[rowKey]
rowkey := rk.(string)
switch options.UpdateMode {
case MergeEntity:
resp, err := t.client.MergeEntity(
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
&generated.TableClientMergeEntityOptions{TableEntityProperties: mapEntity},
&generated.QueryOptions{},
)
return insertEntityFromGeneratedMerge(&resp), err
case ReplaceEntity:
resp, err := t.client.UpdateEntity(
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
&generated.TableClientUpdateEntityOptions{TableEntityProperties: mapEntity},
&generated.QueryOptions{},
)
return insertEntityFromGeneratedUpdate(&resp), err
}
if pk == "" || rk == "" {
return InsertEntityResponse{}, errPartitionKeyRowKeyError
}
return InsertEntityResponse{}, errInvalidUpdateMode
}
type GetAccessPolicyOptions struct {
}
func (g *GetAccessPolicyOptions) toGenerated() *generated.TableClientGetAccessPolicyOptions {
return &generated.TableClientGetAccessPolicyOptions{}
}
type GetAccessPolicyResponse struct {
RawResponse *http.Response
SignedIdentifiers []*SignedIdentifier
}
func getAccessPolicyResponseFromGenerated(g *generated.TableClientGetAccessPolicyResponse) GetAccessPolicyResponse {
if g == nil {
return GetAccessPolicyResponse{}
}
var sis []*SignedIdentifier
for _, s := range g.SignedIdentifiers {
sis = append(sis, fromGeneratedSignedIdentifier(s))
}
return GetAccessPolicyResponse{
RawResponse: g.RawResponse,
SignedIdentifiers: sis,
}
}
// GetAccessPolicy retrieves details about any stored access policies specified on the table that may be used with the Shared Access Signature
func (t *Client) GetAccessPolicy(ctx context.Context, options *GetAccessPolicyOptions) (GetAccessPolicyResponse, error) {
resp, err := t.client.GetAccessPolicy(ctx, t.name, generated.Enum4ACL, options.toGenerated())
return getAccessPolicyResponseFromGenerated(&resp), err
}
type SetAccessPolicyOptions struct {
TableACL []*SignedIdentifier
}
type SetAccessPolicyResponse struct {
RawResponse *http.Response
}
func setAccessPolicyResponseFromGenerated(g *generated.TableClientSetAccessPolicyResponse) SetAccessPolicyResponse {
if g == nil {
return SetAccessPolicyResponse{}
}
return SetAccessPolicyResponse{
RawResponse: g.RawResponse,
}
}
func (s *SetAccessPolicyOptions) toGenerated() *generated.TableClientSetAccessPolicyOptions {
var sis []*generated.SignedIdentifier
for _, t := range s.TableACL {
sis = append(sis, toGeneratedSignedIdentifier(t))
}
return &generated.TableClientSetAccessPolicyOptions{
TableACL: sis,
}
}
// SetAccessPolicy sets stored access policies for the table that may be used with SharedAccessSignature
func (t *Client) SetAccessPolicy(ctx context.Context, options *SetAccessPolicyOptions) (SetAccessPolicyResponse, error) {
response, err := t.client.SetAccessPolicy(ctx, t.name, generated.Enum4ACL, options.toGenerated())
if len(options.TableACL) > 5 {
err = errTooManyAccessPoliciesError
}
return setAccessPolicyResponseFromGenerated(&response), err
}
// GetTableSASToken is a convenience method for generating a SAS token for a specific table.
// It can only be used by clients created by NewClientWithSharedKey().
func (t Client) GetTableSASToken(permissions SASPermissions, start time.Time, expiry time.Time) (string, error) {
if t.cred == nil {
return "", errors.New("SAS can only be signed with a SharedKeyCredential")
}
qps, err := SASSignatureValues{
TableName: t.name,
Permissions: permissions.String(),
StartTime: start,
ExpiryTime: expiry,
StartPartitionKey: permissions.StartPartitionKey,
StartRowKey: permissions.StartRowKey,
EndPartitionKey: permissions.EndPartitionKey,
EndRowKey: permissions.EndRowKey,
}.NewSASQueryParameters(t.cred)
if err != nil {
return "", err
}
serviceURL := t.con.Endpoint()
if !strings.Contains(serviceURL, "/") {
serviceURL += "/"
}
serviceURL += t.name + "?" + qps.Encode()
return serviceURL, nil
}