-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.go
43 lines (37 loc) · 1.52 KB
/
setup.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
package timber
import (
"context"
"errors"
"net/http"
"github.com/evergreen-ci/aviation/services"
"google.golang.org/grpc"
)
// DialCedarOptions describes the options for the DialCedar function. The base
// address defaults to `cedar.mongodb.com` and the RPC port to 7070. If a base
// address is provided the RPC port must also be provided. Username and
// either password or API key must always be provided. This aliases the same
// type in aviation.
type DialCedarOptions services.DialCedarOptions
// DialCedar is a convenience function for creating a RPC client connection
// with cedar via gRPC. This wraps the same function in aviation.
func DialCedar(ctx context.Context, client *http.Client, opts DialCedarOptions) (*grpc.ClientConn, error) {
serviceOpts := services.DialCedarOptions(opts)
return services.DialCedar(ctx, client, &serviceOpts)
}
// ConnectionOptions contains the options needed to create a gRPC connection
// with cedar.
type ConnectionOptions struct {
DialOpts DialCedarOptions
Client http.Client
}
func (opts ConnectionOptions) Validate() error {
if (opts.DialOpts.BaseAddress == "" && opts.DialOpts.RPCPort != "") ||
(opts.DialOpts.BaseAddress != "" && opts.DialOpts.RPCPort == "") {
return errors.New("must provide both base address and rpc port or neither")
}
hasAuth := opts.DialOpts.Username != "" && (opts.DialOpts.APIKey != "")
if !hasAuth && opts.DialOpts.BaseAddress == "" {
return errors.New("must specify username and api key, or address and port for an insecure connection")
}
return nil
}