Skip to content

Commit 0045f0f

Browse files
author
Gal Topper
authored
Fix TSDB-4: handle tenant correctly in CLI. (#48)
1 parent 35b6dff commit 0045f0f

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

pkg/tsdbctl/tsdbctl.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func (rc *RootCommandeer) CreateMarkdown(path string) error {
9393
}
9494

9595
func (rc *RootCommandeer) initialize() error {
96-
9796
cfg, err := config.LoadConfig(rc.cfgFilePath)
9897
if err != nil {
9998
// if we couldn't load the file and its not the default
@@ -103,11 +102,14 @@ func (rc *RootCommandeer) initialize() error {
103102
cfg = &config.V3ioConfig{} // initialize struct, will try and set it from individual flags
104103
config.InitDefaults(cfg)
105104
}
105+
rc.populateConfig(cfg)
106+
return nil
107+
}
106108

109+
func (rc *RootCommandeer) populateConfig(cfg *config.V3ioConfig) error {
107110
if rc.v3ioPath != "" {
108-
109111
// read username and password
110-
if i := strings.Index(rc.v3ioPath, "@"); i > 0 {
112+
if i := strings.LastIndex(rc.v3ioPath, "@"); i > 0 {
111113
cfg.Username = rc.v3ioPath[0:i]
112114
rc.v3ioPath = rc.v3ioPath[i+1:]
113115
if userpass := strings.Split(cfg.Username, ":"); len(userpass) > 1 {
@@ -123,19 +125,15 @@ func (rc *RootCommandeer) initialize() error {
123125
cfg.V3ioUrl = rc.v3ioPath[0:slash]
124126
cfg.Container = rc.v3ioPath[slash+1:]
125127
}
126-
127128
if rc.dbPath != "" {
128129
cfg.Path = rc.dbPath
129130
}
130-
131131
if cfg.V3ioUrl == "" || cfg.Container == "" || cfg.Path == "" {
132-
return fmt.Errorf("User must provide V3IO URL, container name, and table path via the config file or flags")
132+
return fmt.Errorf("user must provide V3IO URL, container name, and table path via the config file or flags")
133133
}
134-
135134
if rc.verbose != "" {
136135
cfg.Verbose = rc.verbose
137136
}
138-
139137
rc.v3iocfg = cfg
140138
return nil
141139
}

pkg/tsdbctl/tsdbctl_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2018 Iguazio Systems Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License") with
5+
an addition restriction as set forth herein. You may not use this
6+
file except in compliance with the License. You may obtain a copy of
7+
the License at http://www.apache.org/licenses/LICENSE-2.0.
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
implied. See the License for the specific language governing
13+
permissions and limitations under the License.
14+
15+
In addition, you may not use the software for any purposes that are
16+
illegal under applicable law, and the grant of the foregoing license
17+
under the Apache 2.0 license is conditioned upon your compliance with
18+
such restriction.
19+
*/
20+
21+
package tsdbctl
22+
23+
import (
24+
"github.com/stretchr/testify/suite"
25+
"github.com/v3io/v3io-tsdb/pkg/config"
26+
"testing"
27+
)
28+
29+
type testSuite struct {
30+
suite.Suite
31+
}
32+
33+
func (suite *testSuite) TestPopulateConfigWithTenant() {
34+
rc := RootCommandeer{v3ioPath: "Vel@Odar:p455w0rd@localhost:80123/123"}
35+
cfg := &config.V3ioConfig{
36+
Path: "/x/y/z",
37+
}
38+
39+
err := rc.populateConfig(cfg)
40+
41+
expectedRc := RootCommandeer{
42+
v3iocfg: cfg,
43+
v3ioPath: "localhost:80123/123",
44+
}
45+
expectedCfg := &config.V3ioConfig{
46+
V3ioUrl: "localhost:80123",
47+
Container: "123",
48+
Path: "/x/y/z",
49+
Username: "Vel@Odar",
50+
Password: "p455w0rd",
51+
}
52+
53+
suite.Require().Nil(err)
54+
suite.Require().Equal(expectedCfg, rc.v3iocfg)
55+
suite.Require().Equal(expectedRc, rc)
56+
}
57+
58+
func TestBuilderSuite(t *testing.T) {
59+
suite.Run(t, new(testSuite))
60+
}

0 commit comments

Comments
 (0)