From 426f54099145841677bfa327dd6a02ecfc08a032 Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Wed, 18 Jan 2023 23:24:35 +0000 Subject: [PATCH] Adding example in user guide Signed-off-by: Vacha Shah --- USER_GUIDE.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 727e0ebd4..bbc599b94 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -1,6 +1,9 @@ - [User Guide](#user-guide) - - [Example](#example) - - [How to use IAMs as authentication method](#how-to-use-iams-as-authentication-method) + - [Example](#example) + - [How to use IAMs as authentication method](#how-to-use-iams-as-authentication-method) + - [AWS SDK V1](#aws-sdk-v1) + - [AWS SDK V2](#aws-sdk-v2) + - [AWS SDK V2 with OpenSearch Serverless](#aws-sdk-v2-with-opensearch-serverless) # User Guide @@ -283,3 +286,76 @@ func main() { } ``` + +#### AWS SDK V2 with OpenSearch Serverless + +AWS SDK V2 for Go can be used for Amazon OpenSearch Serverless. To create a client, please see the example below: + +```go +package main + +import ( + "context" + "log" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + opensearch "github.com/opensearch-project/opensearch-go/v2" + opensearchapi "github.com/opensearch-project/opensearch-go/v2/opensearchapi" + requestsigner "github.com/opensearch-project/opensearch-go/v2/signer/awsv2" +) + +const endpoint = "" // serverless collection endpoint + +func main() { + ctx := context.Background() + + cfg, err := config.LoadDefaultConfig(ctx) + if err != nil { + log.Fatal(err) // Do not log.fatal in a production ready app. + } + + // Create an AWS request Signer and load AWS configuration using default config folder or env vars. + // See https://docs.aws.amazon.com/opensearch-service/latest/developerguide/request-signing.html#request-signing-go + signer, err := requestsigner.NewSignerWithService(awsCfg, "aoss") // "aoss" for Amazon OpenSearch Serverless + if err != nil { + log.Fatal(err) // Do not log.fatal in a production ready app. + } + + // Create an opensearch client and use the request-signer + client, err := opensearch.NewClient(opensearch.Config{ + Addresses: []string{endpoint}, + Signer: signer, + }) + if err != nil { + log.Fatal("client creation err", err) + } + + IndexName = "go-test-index" + + // Create an index with non-default settings. + createIndex := opensearchapi.IndicesCreateRequest{ + Index: IndexName, + } + createIndexResponse, err := createIndex.Do(context.Background(), client) + if err != nil { + log.Println("Error ", err.Error()) + log.Println("failed to create index ", err) + log.Fatal("create response body read err", err) + } + log.Println(createIndexResponse) + + // Delete previously created index. + deleteIndex := opensearchapi.IndicesDeleteRequest{ + Index: []string{IndexName}, + } + + deleteIndexResponse, err := deleteIndex.Do(context.Background(), client) + if err != nil { + log.Println("failed to delete index ", err) + log.Fatal("delete index response body read err", err) + } + log.Println("deleting index", deleteIndexResponse) +} + +```