-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
73 lines (58 loc) · 1.72 KB
/
aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
awsopensearch "github.com/aws/aws-sdk-go-v2/service/opensearch"
"github.com/opensearch-project/opensearch-go/v2"
"github.com/opensearch-project/opensearch-go/v2/signer/awsv2"
)
var awsConfig *aws.Config
func awsInit() {
if awsConfig == nil {
config, err := awsconfig.LoadDefaultConfig(context.TODO())
if err != nil {
FatalError(fmt.Errorf("failed to load AWS config: %w", err))
}
awsConfig = &config
}
}
func resolveAWSDomainEndpoint(domainName string) string {
awsInit()
aosClient := awsopensearch.NewFromConfig(*awsConfig)
domain, err := aosClient.DescribeDomain(context.TODO(), &awsopensearch.DescribeDomainInput{
DomainName: &domainName,
})
if err != nil {
FatalError(fmt.Errorf("failed to describe OpenSearch domain: %w", err))
}
var endpoint string
switch {
case domain.DomainStatus.EndpointV2 != nil:
endpoint = *domain.DomainStatus.EndpointV2
case domain.DomainStatus.Endpoint != nil:
endpoint = *domain.DomainStatus.Endpoint
case domain.DomainStatus.Endpoints != nil:
switch {
case domain.DomainStatus.Endpoints["vpcv2"] != "":
endpoint = domain.DomainStatus.Endpoints["vpcv2"]
case domain.DomainStatus.Endpoints["vpc"] != "":
endpoint = domain.DomainStatus.Endpoints["vpc"]
}
}
if endpoint == "" {
FatalError(fmt.Errorf("no endpoints found for OpenSearch domain: %s", domainName))
}
return "https://" + endpoint
}
func buildAWSAuthClientConfig() opensearch.Config {
awsInit()
signer, err := awsv2.NewSigner(*awsConfig)
if err != nil {
FatalError(fmt.Errorf("failed to create AWS V4 signer: %w", err))
}
return opensearch.Config{
Signer: signer,
}
}