github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/runtimevar/gcpruntimeconfig/example_test.go (about) 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 gcpruntimeconfig_test 16 17 import ( 18 "context" 19 "log" 20 21 "gocloud.dev/gcp" 22 "gocloud.dev/runtimevar" 23 "gocloud.dev/runtimevar/gcpruntimeconfig" 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 Runtime Configurator service. 40 client, cleanup, err := gcpruntimeconfig.Dial(ctx, creds.TokenSource) 41 if err != nil { 42 log.Fatal(err) 43 } 44 defer cleanup() 45 46 // You can use the VariableKey helper to construct a Variable key from 47 // your project ID, config ID, and the variable name; alternatively, 48 // you can construct the full string yourself (e.g., 49 // "projects/gcp-project-id/configs/config-id/variables/variable-name"). 50 // See https://cloud.google.com/deployment-manager/runtime-configurator/ 51 // for more details. 52 // 53 // For this example, the GCP Cloud Runtime Configurator variable being 54 // referenced should have a JSON string that decodes into MyConfig. 55 variableKey := gcpruntimeconfig.VariableKey("gcp-project-id", "config-id", "variable-name") 56 57 // Construct a *runtimevar.Variable that watches the variable. 58 v, err := gcpruntimeconfig.OpenVariable(client, variableKey, runtimevar.StringDecoder, nil) 59 if err != nil { 60 log.Fatal(err) 61 } 62 defer v.Close() 63 } 64 65 func Example_openVariableFromURL() { 66 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 67 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/gcpruntimeconfig" 68 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 69 ctx := context.Background() 70 71 // runtimevar.OpenVariable creates a *runtimevar.Variable from a URL. 72 // The URL Host+Path are used as the GCP Runtime Configurator Variable key; 73 // see https://cloud.google.com/deployment-manager/runtime-configurator/ 74 // for more details. 75 76 v, err := runtimevar.OpenVariable(ctx, "gcpruntimeconfig://projects/myproject/configs/myconfigid/variables/myvar?decoder=string") 77 if err != nil { 78 log.Fatal(err) 79 } 80 defer v.Close() 81 }