예제 #1
0
func createClient(ctx context.Context, query Query, cfg *Config) (ocpb.OpenConfigClient, error) {
	switch {
	case ctx == nil:
		return nil, fmt.Errorf("ctx must not be nil")
	case cfg == nil:
		return nil, fmt.Errorf("cfg must not be nil")
	case query.Target == "":
		return nil, fmt.Errorf("query target must be specified")
	}
	if cfg.Display == nil {
		cfg.Display = defaultDisplay
	}
	log.Infof("Creating connection: %+v", query)
	conn, err := grpc.Dial(query.Target, query.DialOptions...)
	if err != nil {
		return nil, err
	}
	return ocpb.NewOpenConfigClient(conn), nil
}
예제 #2
0
// New creates a new gRPC client and connects it
func New(username, password, addr string, opts []grpc.DialOption) *Client {
	device := addr
	if !strings.ContainsRune(addr, ':') {
		addr += ":" + defaultPort
	}
	conn, err := grpc.Dial(addr, opts...)
	if err != nil {
		glog.Fatalf("Failed to dial: %s", err)
	}
	glog.Infof("Connected to %s", addr)
	client := openconfig.NewOpenConfigClient(conn)

	ctx := context.Background()
	if username != "" {
		ctx = metadata.NewContext(ctx, metadata.Pairs(
			"username", username,
			"password", password))
	}
	return &Client{
		client: client,
		device: device,
		ctx:    ctx,
	}
}