| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
// Connect simply connects to Elasticsearch Service on AWS. |
| 6 |
// |
| 7 |
// Example |
| 8 |
// |
| 9 |
// aws-connect -url=https://search-xxxxx-yyyyy.eu-central-1.es.amazonaws.com |
| 10 |
// |
| 11 |
package main |
| 12 |
|
| 13 |
import ( |
| 14 |
"flag" |
| 15 |
"fmt" |
| 16 |
"log" |
| 17 |
|
| 18 |
"github.com/olivere/env" |
| 19 |
"github.com/smartystreets/go-aws-auth" |
| 20 |
elastic "gopkg.in/olivere/elastic.v5" |
| 21 |
"gopkg.in/olivere/elastic.v5/aws" |
| 22 |
) |
| 23 |
|
| 24 |
func main() { |
| 25 |
var ( |
| 26 |
accessKey = flag.String("access-key", env.String("", "AWS_ACCESS_KEY"), "Access Key ID") |
| 27 |
secretKey = flag.String("secret-key", env.String("", "AWS_SECRET_KEY"), "Secret access key") |
| 28 |
url = flag.String("url", "http://localhost:9200", "Elasticsearch URL") |
| 29 |
sniff = flag.Bool("sniff", false, "Enable or disable sniffing") |
| 30 |
) |
| 31 |
flag.Parse() |
| 32 |
log.SetFlags(0) |
| 33 |
|
| 34 |
if *url == "" { |
| 35 |
*url = "http://127.0.0.1:9200" |
| 36 |
} |
| 37 |
if *accessKey == "" { |
| 38 |
log.Fatal("missing -access-key or AWS_ACCESS_KEY environment variable") |
| 39 |
} |
| 40 |
if *secretKey == "" { |
| 41 |
log.Fatal("missing -secret-key or AWS_SECRET_KEY environment variable") |
| 42 |
} |
| 43 |
|
| 44 |
signingClient := aws.NewV4SigningClient(awsauth.Credentials{ |
| 45 |
AccessKeyID: *accessKey, |
| 46 |
SecretAccessKey: *secretKey, |
| 47 |
}) |
| 48 |
|
| 49 |
// Create an Elasticsearch client |
| 50 |
client, err := elastic.NewClient( |
| 51 |
elastic.SetURL(*url), |
| 52 |
elastic.SetSniff(*sniff), |
| 53 |
elastic.SetHttpClient(signingClient), |
| 54 |
) |
| 55 |
if err != nil { |
| 56 |
log.Fatal(err) |
| 57 |
} |
| 58 |
_ = client |
| 59 |
|
| 60 |
// Just a status message |
| 61 |
fmt.Println("Connection succeeded") |
| 62 |
} |