Skip to content

Commit f8fc52d

Browse files
Move etcd/tarantool connection to URIOpts-based in lib/cluster
1 parent eaab93e commit f8fc52d

File tree

4 files changed

+12
-89
lines changed

4 files changed

+12
-89
lines changed

cli/cluster/cmd/common.go

+4-81
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package cmd
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
7-
"os"
86

97
clientv3 "go.etcd.io/etcd/client/v3"
108

119
"github.com/tarantool/go-tarantool/v2"
1210
"github.com/tarantool/tt/cli/cluster"
1311
libcluster "github.com/tarantool/tt/lib/cluster"
14-
"github.com/tarantool/tt/lib/connect"
12+
libconnect "github.com/tarantool/tt/lib/connect"
1513
)
1614

1715
// printRawClusterConfig prints a raw cluster configuration or an instance
@@ -137,87 +135,12 @@ func printConfig(config *libcluster.Config) {
137135
fmt.Print(config.String())
138136
}
139137

140-
// connectOpts is additional connect options specified by a user.
141-
type connectOpts struct {
142-
Username string
143-
Password string
144-
}
145-
146-
// connectTarantool establishes a connection to Tarantool.
147-
func connectTarantool(uriOpts connect.UriOpts, connOpts connectOpts) (tarantool.Connector, error) {
148-
if uriOpts.Username == "" && uriOpts.Password == "" {
149-
uriOpts.Username = connOpts.Username
150-
uriOpts.Password = connOpts.Password
151-
if uriOpts.Username == "" {
152-
uriOpts.Username = os.Getenv(connect.TarantoolUsernameEnv)
153-
}
154-
if uriOpts.Password == "" {
155-
uriOpts.Password = os.Getenv(connect.TarantoolPasswordEnv)
156-
}
157-
}
158-
159-
dialer, connectorOpts, err := MakeConnectOptsFromUriOpts(uriOpts)
160-
if err != nil {
161-
return nil, err
162-
}
163-
164-
ctx := context.Background()
165-
if connectorOpts.Timeout > 0 {
166-
var cancel context.CancelFunc
167-
ctx, cancel = context.WithTimeout(ctx, connectorOpts.Timeout)
168-
defer cancel()
169-
}
170-
conn, err := tarantool.Connect(ctx, dialer, connectorOpts)
171-
if err != nil {
172-
return nil, fmt.Errorf("failed to connect to tarantool: %w", err)
173-
}
174-
return conn, nil
175-
}
176-
177-
// connectEtcd establishes a connection to etcd.
178-
func connectEtcd(uriOpts connect.UriOpts, connOpts connectOpts) (*clientv3.Client, error) {
179-
etcdOpts := MakeEtcdOptsFromUriOpts(uriOpts)
180-
if etcdOpts.Username == "" && etcdOpts.Password == "" {
181-
etcdOpts.Username = connOpts.Username
182-
etcdOpts.Password = connOpts.Password
183-
if etcdOpts.Username == "" {
184-
etcdOpts.Username = os.Getenv(connect.EtcdUsernameEnv)
185-
}
186-
if etcdOpts.Password == "" {
187-
etcdOpts.Password = os.Getenv(connect.EtcdPasswordEnv)
188-
}
189-
}
190-
191-
etcdcli, err := libcluster.ConnectEtcd(etcdOpts)
192-
if err != nil {
193-
return nil, fmt.Errorf("failed to connect to etcd: %w", err)
194-
}
195-
return etcdcli, nil
196-
}
197-
198-
// doOnStorage determines a storage based on the opts.
199-
func doOnStorage(connOpts connectOpts, opts connect.UriOpts,
200-
tarantoolFunc func(tarantool.Connector) error, etcdFunc func(*clientv3.Client) error) error {
201-
etcdcli, errEtcd := connectEtcd(opts, connOpts)
202-
if errEtcd == nil {
203-
return etcdFunc(etcdcli)
204-
}
205-
206-
conn, errTarantool := connectTarantool(opts, connOpts)
207-
if errTarantool == nil {
208-
return tarantoolFunc(conn)
209-
}
210-
211-
return fmt.Errorf("failed to establish a connection to tarantool or etcd: %w, %w",
212-
errTarantool, errEtcd)
213-
}
214-
215138
// createPublisherAndCollector creates a new data publisher and collector based on UriOpts.
216139
func createPublisherAndCollector(
217140
publishers libcluster.DataPublisherFactory,
218141
collectors libcluster.CollectorFactory,
219-
connOpts connectOpts,
220-
opts connect.UriOpts) (libcluster.DataPublisher, libcluster.Collector, func(), error) {
142+
connOpts libcluster.ConnectOpts,
143+
opts libconnect.UriOpts) (libcluster.DataPublisher, libcluster.Collector, func(), error) {
221144
prefix, key, timeout := opts.Prefix, opts.Params["key"], opts.Timeout
222145

223146
var (
@@ -265,7 +188,7 @@ func createPublisherAndCollector(
265188
return nil
266189
}
267190

268-
if err := doOnStorage(connOpts, opts, tarantoolFunc, etcdFunc); err != nil {
191+
if err := libcluster.DoOnStorage(connOpts, opts, tarantoolFunc, etcdFunc); err != nil {
269192
return nil, nil, nil, err
270193
}
271194

cli/cluster/cmd/publish.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func PublishUri(publishCtx PublishCtx, opts connect.UriOpts) error {
3838
return err
3939
}
4040

41-
connOpts := connectOpts{
41+
connOpts := libcluster.ConnectOpts{
4242
Username: publishCtx.Username,
4343
Password: publishCtx.Password,
4444
}

cli/cluster/cmd/replicaset.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func pickPatchKey(keys []string, force bool, pathMsg string) (int, error) {
107107
func createDataCollectorAndKeyPublisher(
108108
collectors libcluster.DataCollectorFactory,
109109
publishers libcluster.DataPublisherFactory,
110-
opts connect.UriOpts, connOpts connectOpts) (
110+
opts connect.UriOpts, connOpts libcluster.ConnectOpts) (
111111
libcluster.DataCollector, replicaset.DataPublisher, func(), error) {
112112
prefix, key, timeout := opts.Prefix, opts.Params["key"], opts.Timeout
113113
var (
@@ -145,7 +145,7 @@ func createDataCollectorAndKeyPublisher(
145145
return nil
146146
}
147147

148-
if err := doOnStorage(connOpts, opts, tarantoolFunc, etcdFunc); err != nil {
148+
if err := libcluster.DoOnStorage(connOpts, opts, tarantoolFunc, etcdFunc); err != nil {
149149
return nil, nil, nil, err
150150
}
151151

@@ -158,7 +158,7 @@ func Promote(url string, ctx PromoteCtx) error {
158158
if err != nil {
159159
return fmt.Errorf("invalid URL %q: %w", url, err)
160160
}
161-
connOpts := connectOpts{
161+
connOpts := libcluster.ConnectOpts{
162162
Username: ctx.Username,
163163
Password: ctx.Password,
164164
}
@@ -205,7 +205,7 @@ func Demote(url string, ctx DemoteCtx) error {
205205
if err != nil {
206206
return fmt.Errorf("invalid URL %q: %w", url, err)
207207
}
208-
connOpts := connectOpts{
208+
connOpts := libcluster.ConnectOpts{
209209
Username: ctx.Username,
210210
Password: ctx.Password,
211211
}
@@ -252,7 +252,7 @@ func Expel(url string, ctx ExpelCtx) error {
252252
if err != nil {
253253
return fmt.Errorf("invalid URL %q: %w", url, err)
254254
}
255-
connOpts := connectOpts{
255+
connOpts := libcluster.ConnectOpts{
256256
Username: ctx.Username,
257257
Password: ctx.Password,
258258
}
@@ -306,7 +306,7 @@ func ChangeRole(url string, ctx RolesChangeCtx, action replicaset.RolesChangerAc
306306
if err != nil {
307307
return fmt.Errorf("invalid URL %q: %w", url, err)
308308
}
309-
connOpts := connectOpts{
309+
connOpts := libcluster.ConnectOpts{
310310
Username: ctx.Username,
311311
Password: ctx.Password,
312312
}

cli/cluster/cmd/show.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ShowCtx struct {
2323

2424
// ShowUri shows a configuration from URI.
2525
func ShowUri(showCtx ShowCtx, opts connect.UriOpts) error {
26-
connOpts := connectOpts{
26+
connOpts := libcluster.ConnectOpts{
2727
Username: showCtx.Username,
2828
Password: showCtx.Password,
2929
}

0 commit comments

Comments
 (0)