| 1 |
package fsutils |
| 2 |
|
| 3 |
import ( |
| 4 |
"log" |
| 5 |
"os" |
| 6 |
|
| 7 |
"google.golang.org/grpc" |
| 8 |
"google.golang.org/grpc/credentials" |
| 9 |
"google.golang.org/grpc/credentials/oauth" |
| 10 |
|
| 11 |
admin "go-genproto/googleapis/firestore/admin/v1beta1" |
| 12 |
firestore "go-genproto/googleapis/firestore/v1beta1" |
| 13 |
) |
| 14 |
|
| 15 |
// MakeFSClient ... Create a new Firestore Client using JWT credentials |
| 16 |
func MakeFSClient() (firestore.FirestoreClient, grpc.ClientConn) { |
| 17 |
|
| 18 |
keyFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") |
| 19 |
jwtCreds, err := oauth.NewServiceAccountFromFile(keyFile, "https://www.googleapis.com/auth/datastore") |
| 20 |
address := "firestore.googleapis.com:443" |
| 21 |
|
| 22 |
if err != nil { |
| 23 |
log.Fatalf("Failed to create JWT credentials: %v", err) |
| 24 |
} |
| 25 |
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(jwtCreds)) |
| 26 |
if err != nil { |
| 27 |
log.Fatalf("did not connect: %v", err) |
| 28 |
} |
| 29 |
|
| 30 |
client := firestore.NewFirestoreClient(conn) |
| 31 |
|
| 32 |
return client, *conn |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
// MakeFSAdminClient ... Create a new Firestore Admin Client using JWT credentials |
| 37 |
func MakeFSAdminClient() (admin.FirestoreAdminClient, grpc.ClientConn) { |
| 38 |
|
| 39 |
keyFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") |
| 40 |
jwtCreds, err := oauth.NewServiceAccountFromFile(keyFile, "https://www.googleapis.com/auth/datastore") |
| 41 |
address := "firestore.googleapis.com:443" |
| 42 |
|
| 43 |
if err != nil { |
| 44 |
log.Fatalf("Failed to create JWT credentials: %v", err) |
| 45 |
} |
| 46 |
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(jwtCreds)) |
| 47 |
if err != nil { |
| 48 |
log.Fatalf("did not connect: %v", err) |
| 49 |
} |
| 50 |
|
| 51 |
client := admin.NewFirestoreAdminClient(conn) |
| 52 |
|
| 53 |
return client, *conn |
| 54 |
|
| 55 |
} |