-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconnector.go
67 lines (58 loc) · 1.85 KB
/
connector.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
package main
import (
"context"
"log"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
type connector struct {
grpcConnexion *grpc.ClientConn
dgraphClient *dgo.Dgraph
context *context.Context
awsSession *session.Session
awsRegion string
awsAccountName string
awsAccountID string
ressources map[string]map[string]string
waitGroup sync.WaitGroup
stats statistics
}
type statistics struct {
NumberOfNodes int
}
func newConnector(profile, region, dgraph *string) *connector {
// Create AWS session (credentials from ~/.aws/config)
awsSession := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable, //enable use of ~/.aws/config
AssumeRoleTokenProvider: stscreds.StdinTokenProvider, //ask for MFA if needed
Profile: *profile,
Config: aws.Config{Region: aws.String(*region)},
}))
resultSts, err := sts.New(awsSession).GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
log.Fatal(err.Error())
}
connexion, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err.Error())
}
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(connexion))
ctx := context.Background()
return &connector{
dgraphClient: dgraphClient,
grpcConnexion: connexion,
context: &ctx,
awsSession: awsSession,
awsRegion: *region,
awsAccountName: *profile,
awsAccountID: *resultSts.Account,
ressources: map[string]map[string]string{},
waitGroup: sync.WaitGroup{},
}
}