Debian Codegraph @

secrets/gcpkms/example_test.go

raw
1 // Copyright 2018 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 gcpkms_test
16
17 import (
18 "context"
19 "log"
20
21 "gocloud.dev/secrets"
22 "gocloud.dev/secrets/gcpkms"
23 )
24
25 func ExampleOpenKeeper() {
26 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
27 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
28 ctx := context.Background()
29
30 // Get a client to use with the KMS API.
31 client, done, err := gcpkms.Dial(ctx, nil)
32 if err != nil {
33 log.Fatal(err)
34 }
35 // Close the connection when done.
36 defer done()
37
38 // You can also use gcpkms.KeyResourceID to construct this string.
39 const keyID = "projects/MYPROJECT/" +
40 "locations/MYLOCATION/" +
41 "keyRings/MYKEYRING/" +
42 "cryptoKeys/MYKEY"
43
44 // Construct a *secrets.Keeper.
45 keeper := gcpkms.OpenKeeper(client, keyID, nil)
46 defer keeper.Close()
47 }
48
49 func Example_openFromURL() {
50 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
51 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/secrets/gcpkms"
52 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
53 ctx := context.Background()
54
55 keeper, err := secrets.OpenKeeper(ctx,
56 "gcpkms://projects/MYPROJECT/"+
57 "locations/MYLOCATION/"+
58 "keyRings/MYKEYRING/"+
59 "cryptoKeys/MYKEY")
60 if err != nil {
61 log.Fatal(err)
62 }
63 defer keeper.Close()
64 }