Skip to content

Commit 2a0fb85

Browse files
committed
cluster: moved cmd.UriOpts to connect
Part of #TNTP-1561
1 parent 5e7b6a9 commit 2a0fb85

16 files changed

+1016
-576
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Improvement in the work of templates.
3030
### Changed
3131

3232
- `tt connect`: allow to disconnect with `Ctrl+C` or `Ctrl+\` if script execution hung.
33+
- Moved `cluster.cmd.UriOpts` to `connect.UriOpts`.
3334

3435
### Fixed
3536

cli/cluster/cmd/common.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ type connectOpts struct {
144144
}
145145

146146
// connectTarantool establishes a connection to Tarantool.
147-
func connectTarantool(uriOpts UriOpts, connOpts connectOpts) (tarantool.Connector, error) {
147+
func connectTarantool(uriOpts connect.UriOpts, connOpts connectOpts) (tarantool.Connector, error) {
148148
if uriOpts.Username == "" && uriOpts.Password == "" {
149149
uriOpts.Username = connOpts.Username
150150
uriOpts.Password = connOpts.Password
@@ -175,7 +175,7 @@ func connectTarantool(uriOpts UriOpts, connOpts connectOpts) (tarantool.Connecto
175175
}
176176

177177
// connectEtcd establishes a connection to etcd.
178-
func connectEtcd(uriOpts UriOpts, connOpts connectOpts) (*clientv3.Client, error) {
178+
func connectEtcd(uriOpts connect.UriOpts, connOpts connectOpts) (*clientv3.Client, error) {
179179
etcdOpts := MakeEtcdOptsFromUriOpts(uriOpts)
180180
if etcdOpts.Username == "" && etcdOpts.Password == "" {
181181
etcdOpts.Username = connOpts.Username
@@ -196,7 +196,7 @@ func connectEtcd(uriOpts UriOpts, connOpts connectOpts) (*clientv3.Client, error
196196
}
197197

198198
// doOnStorage determines a storage based on the opts.
199-
func doOnStorage(connOpts connectOpts, opts UriOpts,
199+
func doOnStorage(connOpts connectOpts, opts connect.UriOpts,
200200
tarantoolFunc func(tarantool.Connector) error, etcdFunc func(*clientv3.Client) error) error {
201201
etcdcli, errEtcd := connectEtcd(opts, connOpts)
202202
if errEtcd == nil {
@@ -217,8 +217,8 @@ func createPublisherAndCollector(
217217
publishers libcluster.DataPublisherFactory,
218218
collectors libcluster.CollectorFactory,
219219
connOpts connectOpts,
220-
opts UriOpts) (libcluster.DataPublisher, libcluster.Collector, func(), error) {
221-
prefix, key, timeout := opts.Prefix, opts.Key, opts.Timeout
220+
opts connect.UriOpts) (libcluster.DataPublisher, libcluster.Collector, func(), error) {
221+
prefix, key, timeout := opts.Prefix, opts.Params["key"], opts.Timeout
222222

223223
var (
224224
publisher libcluster.DataPublisher

cli/cluster/cmd/failover.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"net/url"
76
"time"
87

98
"github.com/apex/log"
109
"github.com/google/uuid"
1110
libcluster "github.com/tarantool/tt/lib/cluster"
11+
"github.com/tarantool/tt/lib/connect"
1212
"go.etcd.io/etcd/api/v3/mvccpb"
1313
clientv3 "go.etcd.io/etcd/client/v3"
1414
"gopkg.in/yaml.v2"
@@ -63,7 +63,7 @@ type SwitchStatusCtx struct {
6363
TaskID string
6464
}
6565

66-
func makeEtcdOpts(uriOpts UriOpts) libcluster.EtcdOpts {
66+
func makeEtcdOpts(uriOpts connect.UriOpts) libcluster.EtcdOpts {
6767
opts := libcluster.EtcdOpts{
6868
Endpoints: []string{uriOpts.Endpoint},
6969
Username: uriOpts.Username,
@@ -80,10 +80,10 @@ func makeEtcdOpts(uriOpts UriOpts) libcluster.EtcdOpts {
8080
}
8181

8282
// Switch master instance.
83-
func Switch(uri *url.URL, switchCtx SwitchCtx) error {
84-
uriOpts, err := ParseUriOpts(uri)
83+
func Switch(url string, switchCtx SwitchCtx) error {
84+
uriOpts, err := connect.CreateUriOpts(url)
8585
if err != nil {
86-
return fmt.Errorf("invalid URL %q: %w", uri, err)
86+
return fmt.Errorf("invalid URL %q: %w", url, err)
8787
}
8888

8989
opts := makeEtcdOpts(uriOpts)
@@ -166,16 +166,16 @@ func Switch(uri *url.URL, switchCtx SwitchCtx) error {
166166
fmt.Printf("%s\n%s %s %s\n",
167167
"To check the switching status, run:",
168168
"tt cluster failover switch-status",
169-
uri, uuid)
169+
url, uuid)
170170

171171
return nil
172172
}
173173

174174
// SwitchStatus shows master switching status.
175-
func SwitchStatus(uri *url.URL, switchCtx SwitchStatusCtx) error {
176-
uriOpts, err := ParseUriOpts(uri)
175+
func SwitchStatus(url string, switchCtx SwitchStatusCtx) error {
176+
uriOpts, err := connect.CreateUriOpts(url)
177177
if err != nil {
178-
return fmt.Errorf("invalid URL %q: %w", uri, err)
178+
return fmt.Errorf("invalid URL %q: %w", url, err)
179179
}
180180

181181
opts := makeEtcdOpts(uriOpts)

cli/cluster/cmd/publish.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package cmd
22

33
import (
44
"fmt"
5-
"net/url"
65

76
libcluster "github.com/tarantool/tt/lib/cluster"
7+
"github.com/tarantool/tt/lib/connect"
88
)
99

1010
// PublishCtx contains information abould cluster publish command execution
@@ -32,13 +32,8 @@ type PublishCtx struct {
3232
}
3333

3434
// PublishUri publishes a configuration to URI.
35-
func PublishUri(publishCtx PublishCtx, uri *url.URL) error {
36-
uriOpts, err := ParseUriOpts(uri)
37-
if err != nil {
38-
return fmt.Errorf("invalid URL %q: %w", uri, err)
39-
}
40-
41-
instance := uriOpts.Instance
35+
func PublishUri(publishCtx PublishCtx, opts connect.UriOpts) error {
36+
instance := opts.Params["name"]
4237
if err := publishCtxValidateConfig(publishCtx, instance); err != nil {
4338
return err
4439
}
@@ -50,7 +45,7 @@ func PublishUri(publishCtx PublishCtx, uri *url.URL) error {
5045
publisher, collector, cancel, err := createPublisherAndCollector(
5146
publishCtx.Publishers,
5247
publishCtx.Collectors,
53-
connOpts, uriOpts)
48+
connOpts, opts)
5449
if err != nil {
5550
return err
5651
}

cli/cluster/cmd/replicaset.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package cmd
22

33
import (
44
"fmt"
5-
"net/url"
65
"time"
76

87
"github.com/apex/log"
98
"github.com/manifoldco/promptui"
109
"github.com/tarantool/go-tarantool/v2"
1110
"github.com/tarantool/tt/cli/replicaset"
1211
libcluster "github.com/tarantool/tt/lib/cluster"
12+
"github.com/tarantool/tt/lib/connect"
1313
clientv3 "go.etcd.io/etcd/client/v3"
1414
)
1515

@@ -107,9 +107,9 @@ func pickPatchKey(keys []string, force bool, pathMsg string) (int, error) {
107107
func createDataCollectorAndKeyPublisher(
108108
collectors libcluster.DataCollectorFactory,
109109
publishers libcluster.DataPublisherFactory,
110-
opts UriOpts, connOpts connectOpts) (
110+
opts connect.UriOpts, connOpts connectOpts) (
111111
libcluster.DataCollector, replicaset.DataPublisher, func(), error) {
112-
prefix, key, timeout := opts.Prefix, opts.Key, opts.Timeout
112+
prefix, key, timeout := opts.Prefix, opts.Params["key"], opts.Timeout
113113
var (
114114
collector libcluster.DataCollector
115115
publisher replicaset.DataPublisher
@@ -153,10 +153,10 @@ func createDataCollectorAndKeyPublisher(
153153
}
154154

155155
// Promote promotes an instance by patching the cluster config.
156-
func Promote(uri *url.URL, ctx PromoteCtx) error {
157-
opts, err := ParseUriOpts(uri)
156+
func Promote(url string, ctx PromoteCtx) error {
157+
opts, err := connect.CreateUriOpts(url)
158158
if err != nil {
159-
return fmt.Errorf("invalid URL %q: %w", uri, err)
159+
return fmt.Errorf("invalid URL %q: %w", url, err)
160160
}
161161
connOpts := connectOpts{
162162
Username: ctx.Username,
@@ -200,10 +200,10 @@ type DemoteCtx struct {
200200
}
201201

202202
// Demote demotes an instance by patching the cluster config.
203-
func Demote(uri *url.URL, ctx DemoteCtx) error {
204-
opts, err := ParseUriOpts(uri)
203+
func Demote(url string, ctx DemoteCtx) error {
204+
opts, err := connect.CreateUriOpts(url)
205205
if err != nil {
206-
return fmt.Errorf("invalid URL %q: %w", uri, err)
206+
return fmt.Errorf("invalid URL %q: %w", url, err)
207207
}
208208
connOpts := connectOpts{
209209
Username: ctx.Username,
@@ -247,10 +247,10 @@ type ExpelCtx struct {
247247
}
248248

249249
// Expel expels an instance by patching the cluster config.
250-
func Expel(uri *url.URL, ctx ExpelCtx) error {
251-
opts, err := ParseUriOpts(uri)
250+
func Expel(url string, ctx ExpelCtx) error {
251+
opts, err := connect.CreateUriOpts(url)
252252
if err != nil {
253-
return fmt.Errorf("invalid URL %q: %w", uri, err)
253+
return fmt.Errorf("invalid URL %q: %w", url, err)
254254
}
255255
connOpts := connectOpts{
256256
Username: ctx.Username,
@@ -301,10 +301,10 @@ type RolesChangeCtx struct {
301301
}
302302

303303
// ChangeRole adds/removes a role by patching the cluster config.
304-
func ChangeRole(uri *url.URL, ctx RolesChangeCtx, action replicaset.RolesChangerAction) error {
305-
opts, err := ParseUriOpts(uri)
304+
func ChangeRole(url string, ctx RolesChangeCtx, action replicaset.RolesChangerAction) error {
305+
opts, err := connect.CreateUriOpts(url)
306306
if err != nil {
307-
return fmt.Errorf("invalid URL %q: %w", uri, err)
307+
return fmt.Errorf("invalid URL %q: %w", url, err)
308308
}
309309
connOpts := connectOpts{
310310
Username: ctx.Username,

cli/cluster/cmd/show.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package cmd
22

33
import (
44
"fmt"
5-
"net/url"
65

76
"github.com/tarantool/tt/cli/cluster"
87
libcluster "github.com/tarantool/tt/lib/cluster"
8+
"github.com/tarantool/tt/lib/connect"
99
)
1010

1111
// ShowCtx contains information about cluster show command execution context.
@@ -22,20 +22,15 @@ type ShowCtx struct {
2222
}
2323

2424
// ShowUri shows a configuration from URI.
25-
func ShowUri(showCtx ShowCtx, uri *url.URL) error {
26-
uriOpts, err := ParseUriOpts(uri)
27-
if err != nil {
28-
return fmt.Errorf("invalid URL %q: %w", uri, err)
29-
}
30-
25+
func ShowUri(showCtx ShowCtx, opts connect.UriOpts) error {
3126
connOpts := connectOpts{
3227
Username: showCtx.Username,
3328
Password: showCtx.Password,
3429
}
3530
_, collector, cancel, err := createPublisherAndCollector(
3631
nil,
3732
showCtx.Collectors,
38-
connOpts, uriOpts)
33+
connOpts, opts)
3934
if err != nil {
4035
return err
4136
}
@@ -46,9 +41,11 @@ func ShowUri(showCtx ShowCtx, uri *url.URL) error {
4641
return fmt.Errorf("failed to collect a configuration: %w", err)
4742
}
4843

49-
instance := uriOpts.Instance
44+
instance := opts.Params["name"]
5045
if showCtx.Validate {
51-
err = validateRawConfig(config, instance)
46+
if err = validateRawConfig(config, instance); err != nil {
47+
return err
48+
}
5249
}
5350

5451
return printRawClusterConfig(config, instance, showCtx.Validate)

0 commit comments

Comments
 (0)