github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/runtimevar/awsparamstore/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 awsparamstore_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	awsv2cfg "github.com/aws/aws-sdk-go-v2/config"
    22  	ssmv2 "github.com/aws/aws-sdk-go-v2/service/ssm"
    23  	"github.com/aws/aws-sdk-go/aws/session"
    24  	"gocloud.dev/runtimevar"
    25  	"gocloud.dev/runtimevar/awsparamstore"
    26  )
    27  
    28  func ExampleOpenVariable() {
    29  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    30  
    31  	// Establish an AWS session.
    32  	// See https://docs.aws.amazon.com/sdk-for-go/api/aws/session/ for more info.
    33  	sess, err := session.NewSession(nil)
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  
    38  	// Construct a *runtimevar.Variable that watches the variable.
    39  	v, err := awsparamstore.OpenVariable(sess, "cfg-variable-name", runtimevar.StringDecoder, nil)
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  	defer v.Close()
    44  }
    45  
    46  func ExampleOpenVariableV2() {
    47  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    48  
    49  	// Establish a AWS V2 Config.
    50  	// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.
    51  	ctx := context.Background()
    52  	cfg, err := awsv2cfg.LoadDefaultConfig(ctx)
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	// Construct a *runtimevar.Variable that watches the variable.
    58  	clientV2 := ssmv2.NewFromConfig(cfg)
    59  	v, err := awsparamstore.OpenVariableV2(clientV2, "cfg-variable-name", runtimevar.StringDecoder, nil)
    60  	if err != nil {
    61  		log.Fatal(err)
    62  	}
    63  	defer v.Close()
    64  }
    65  
    66  func Example_openVariableFromURL() {
    67  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    68  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/awsparamstore"
    69  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    70  	ctx := context.Background()
    71  
    72  	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
    73  	v, err := runtimevar.OpenVariable(ctx, "awsparamstore://myvar?region=us-west-1&decoder=string")
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  	defer v.Close()
    78  
    79  	// Use "awssdk=v1" or "v2" to force a specific AWS SDK version.
    80  	vUsingV2, err := runtimevar.OpenVariable(ctx, "awsparamstore://myvar?region=us-west-1&decoder=string&awssdk=v2")
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	defer vUsingV2.Close()
    85  }