| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package awsdynamodb |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"net/url" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 25 |
dyn "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 26 |
gcaws "gocloud.dev/aws" |
| 27 |
"gocloud.dev/docstore" |
| 28 |
) |
| 29 |
|
| 30 |
func init() { |
| 31 |
docstore.DefaultURLMux().RegisterCollection(Scheme, new(lazySessionOpener)) |
| 32 |
} |
| 33 |
|
| 34 |
type lazySessionOpener struct { |
| 35 |
init sync.Once |
| 36 |
opener *URLOpener |
| 37 |
err error |
| 38 |
} |
| 39 |
|
| 40 |
func (o *lazySessionOpener) OpenCollectionURL(ctx context.Context, u *url.URL) (*docstore.Collection, error) { |
| 41 |
o.init.Do(func() { |
| 42 |
|
| 43 |
o.opener = &URLOpener{} |
| 44 |
}) |
| 45 |
if o.err != nil { |
| 46 |
return nil, fmt.Errorf("open collection %s: %v", u, o.err) |
| 47 |
} |
| 48 |
return o.opener.OpenCollectionURL(ctx, u) |
| 49 |
} |
| 50 |
|
| 51 |
// Scheme is the URL scheme dynamodb registers its URLOpener under on |
| 52 |
// docstore.DefaultMux. |
| 53 |
const Scheme = "dynamodb" |
| 54 |
|
| 55 |
// URLOpener opens dynamodb URLs like |
| 56 |
// "dynamodb://mytable?partition_key=partkey&sort_key=sortkey". |
| 57 |
// |
| 58 |
// The URL Host is used as the table name. See |
| 59 |
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html |
| 60 |
// for more details. |
| 61 |
// |
| 62 |
// The following query parameters are supported: |
| 63 |
// |
| 64 |
// - partition_key (required): the path to the partition key of a table or an index. |
| 65 |
// - sort_key: the path to the sort key of a table or an index. |
| 66 |
// - allow_scans: if "true", allow table scans to be used for queries |
| 67 |
// - consistent_read: if "true", a strongly consistent read is used whenever possible. |
| 68 |
// |
| 69 |
// See https://godoc.org/gocloud.dev/aws#V2ConfigFromURLParams for supported query |
| 70 |
// parameters for overriding the aws.Session from the URL. |
| 71 |
type URLOpener struct { |
| 72 |
} |
| 73 |
|
| 74 |
// OpenCollectionURL opens the collection at the URL's path. See the package doc for more details. |
| 75 |
func (o *URLOpener) OpenCollectionURL(_ context.Context, u *url.URL) (*docstore.Collection, error) { |
| 76 |
db, tableName, partitionKey, sortKey, opts, err := o.processURL(u) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
return OpenCollection(db, tableName, partitionKey, sortKey, opts) |
| 81 |
} |
| 82 |
|
| 83 |
func (o *URLOpener) processURL(u *url.URL) (db *dyn.Client, tableName, partitionKey, sortKey string, opts *Options, err error) { |
| 84 |
q := u.Query() |
| 85 |
|
| 86 |
partitionKey = q.Get("partition_key") |
| 87 |
if partitionKey == "" { |
| 88 |
return nil, "", "", "", nil, fmt.Errorf("open collection %s: partition_key is required to open a table", u) |
| 89 |
} |
| 90 |
q.Del("partition_key") |
| 91 |
sortKey = q.Get("sort_key") |
| 92 |
q.Del("sort_key") |
| 93 |
opts = &Options{ |
| 94 |
AllowScans: q.Get("allow_scans") == "true", |
| 95 |
RevisionField: q.Get("revision_field"), |
| 96 |
ConsistentRead: q.Get("consistent_read") == "true", |
| 97 |
} |
| 98 |
q.Del("allow_scans") |
| 99 |
q.Del("revision_field") |
| 100 |
q.Del("consistent_read") |
| 101 |
|
| 102 |
tableName = u.Host |
| 103 |
if tableName == "" { |
| 104 |
return nil, "", "", "", nil, fmt.Errorf("open collection %s: URL's host cannot be empty (the table name)", u) |
| 105 |
} |
| 106 |
if u.Path != "" { |
| 107 |
return nil, "", "", "", nil, fmt.Errorf("open collection %s: URL path must be empty, only the host is needed", u) |
| 108 |
} |
| 109 |
|
| 110 |
cfg, err := gcaws.V2ConfigFromURLParams(context.Background(), q) |
| 111 |
if err != nil { |
| 112 |
return nil, "", "", "", nil, fmt.Errorf("open collection %s: %v", u, err) |
| 113 |
} |
| 114 |
db, err = Dial(cfg) |
| 115 |
if err != nil { |
| 116 |
return nil, "", "", "", nil, fmt.Errorf("open collection %s: %v", u, err) |
| 117 |
} |
| 118 |
return db, tableName, partitionKey, sortKey, opts, nil |
| 119 |
} |
| 120 |
|
| 121 |
// Dial gets an AWS DynamoDB service client. |
| 122 |
func Dial(p aws.Config) (*dyn.Client, error) { |
| 123 |
if p.Credentials == nil { |
| 124 |
return nil, errors.New("getting Dynamo service: no AWS session provided") |
| 125 |
} |
| 126 |
return dyn.NewFromConfig(p), nil |
| 127 |
} |