Debian Codegraph @

runtimevar/gcpsecretmanager/example_test.go

raw
1 // Copyright 2020 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 gcpsecretmanager_test
16
17 import (
18 "context"
19 "log"
20
21 "gocloud.dev/gcp"
22 "gocloud.dev/runtimevar"
23 "gocloud.dev/runtimevar/gcpsecretmanager"
24 )
25
26 func ExampleOpenVariable() {
27 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
28 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
29 ctx := context.Background()
30
31 // Your GCP credentials.
32 // See https://cloud.google.com/docs/authentication/production
33 // for more info on alternatives.
34 creds, err := gcp.DefaultCredentials(ctx)
35 if err != nil {
36 log.Fatal(err)
37 }
38
39 // Connect to the GCP Secret Manager service.
40 client, cleanup, err := gcpsecretmanager.Dial(ctx, creds.TokenSource)
41 if err != nil {
42 log.Fatal(err)
43 }
44 defer cleanup()
45
46 // You can use the SecretKey helper to construct a secret key from
47 // your project ID and the secret ID; alternatively,
48 // you can construct the full string yourself (e.g.,
49 // "projects/gcp-project-id/secrets/secret-id").
50 // gcpsecretmanager package will always use the latest secret value,
51 // so `/version/latest` postfix must NOT be added to the secret key.
52 // See https://cloud.google.com/secret-manager
53 // for more details.
54 //
55 // For this example, the GCP Secret Manager secret being
56 // referenced should have a JSON string that decodes into MyConfig.
57 variableKey := gcpsecretmanager.SecretKey("gcp-project-id", "secret-id")
58
59 // Construct a *runtimevar.Variable that watches the variable.
60 v, err := gcpsecretmanager.OpenVariable(client, variableKey, runtimevar.StringDecoder, nil)
61 if err != nil {
62 log.Fatal(err)
63 }
64 defer v.Close()
65 }
66
67 func Example_openVariableFromURL() {
68 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
69 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/gcpsecretmanager"
70 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
71 ctx := context.Background()
72
73 // runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
74 // The URL Host+Path are used as the GCP Secret Manager secret key;
75 // see https://cloud.google.com/secret-manager
76 // for more details.
77
78 v, err := runtimevar.OpenVariable(ctx, "gcpsecretmanager://projects/myproject/secrets/mysecret?decoder=string")
79 if err != nil {
80 log.Fatal(err)
81 }
82 defer v.Close()
83 }