github.com/thiagoyeds/go-cloud@v0.26.0/runtimevar/awssecretsmanager/example_test.go (about)

     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 awssecretsmanager_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	awsv2cfg "github.com/aws/aws-sdk-go-v2/config"
    22  	secretsmanagerv2 "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
    23  	"github.com/aws/aws-sdk-go/aws/session"
    24  	"gocloud.dev/runtimevar"
    25  	"gocloud.dev/runtimevar/awssecretsmanager"
    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  	// `secret-variable-name` must be a friendly name of the secret, NOT the Amazon Resource Name (ARN).
    40  	v, err := awssecretsmanager.OpenVariable(sess, "secret-variable-name", runtimevar.StringDecoder, nil)
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  	defer v.Close()
    45  }
    46  
    47  func ExampleOpenVariableV2() {
    48  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    49  
    50  	// Establish a AWS V2 Config.
    51  	// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.
    52  	ctx := context.Background()
    53  	cfg, err := awsv2cfg.LoadDefaultConfig(ctx)
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  
    58  	// Construct a *runtimevar.Variable that watches the variable.
    59  	// `secret-variable-name` must be a friendly name of the secret, NOT the Amazon Resource Name (ARN).
    60  	clientV2 := secretsmanagerv2.NewFromConfig(cfg)
    61  	v, err := awssecretsmanager.OpenVariableV2(clientV2, "secret-variable-name", runtimevar.StringDecoder, nil)
    62  	if err != nil {
    63  		log.Fatal(err)
    64  	}
    65  	defer v.Close()
    66  }
    67  
    68  func Example_openVariableFromURL() {
    69  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    70  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/awssecretsmanager"
    71  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    72  	ctx := context.Background()
    73  
    74  	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
    75  	// `secret-variable-name` must be a friendly name of the secret, NOT the Amazon Resource Name (ARN).
    76  	v, err := runtimevar.OpenVariable(ctx, "awssecretsmanager://secret-variable-name?region=us-east-2&decoder=string")
    77  	if err != nil {
    78  		log.Fatal(err)
    79  	}
    80  	defer v.Close()
    81  
    82  	// Use "awssdk=v1" or "v2" to force a specific AWS SDK version.
    83  	vUsingV2, err := runtimevar.OpenVariable(ctx, "awssecretsmanager://secret-variable-name?region=us-east-2&decoder=string&awssdk=v2")
    84  	if err != nil {
    85  		log.Fatal(err)
    86  	}
    87  	defer vUsingV2.Close()
    88  }