-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support new OpenGemini client with config
Signed-off-by: PennyYoon <[email protected]>
- Loading branch information
1 parent
2a41447
commit 7cf9b25
Showing
2 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,71 @@ | ||
package opengemini | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
) | ||
|
||
type Options = func(client *Client) error | ||
|
||
type Config struct { | ||
Addresses []*Address | ||
AuthConfig *AuthConfig | ||
BatchConfig *BatchConfig | ||
GzipEnable bool | ||
TlsConfig *tls.Config | ||
} | ||
|
||
type Client struct { | ||
addresses []*Address | ||
authConfig *AuthConfig | ||
batchConfig *BatchConfig | ||
gzipEnable bool | ||
tlsConfig *tls.Config | ||
} | ||
|
||
func NewClient(c *Config) (*Client, error) { | ||
if len(c.Addresses) == 0 { | ||
return nil, errors.New("must have at least one address") | ||
} | ||
if c.AuthConfig.AuthType == Token && len(c.AuthConfig.Token) == 0 { | ||
return nil, errors.New("invalid auth config cause empty token") | ||
} | ||
if c.AuthConfig.AuthType == Password && (len(c.AuthConfig.UserName) == 0 || len(c.AuthConfig.Password) == 0) { | ||
return nil, errors.New("invalid auth config cause empty username or password") | ||
} | ||
if c.BatchConfig.BatchEnable && (c.BatchConfig.BatchInterval <= 0 || c.BatchConfig.BatchSize <= 0) { | ||
return nil, errors.New("batch interval and batch size must be more than 0 ,if batch operate enable") | ||
} | ||
return &Client{ | ||
addresses: c.Addresses, | ||
authConfig: c.AuthConfig, | ||
batchConfig: c.BatchConfig, | ||
gzipEnable: c.GzipEnable, | ||
tlsConfig: c.TlsConfig, | ||
}, nil | ||
} | ||
|
||
type Address struct { | ||
Host string | ||
Port int | ||
} | ||
|
||
type AuthType int | ||
|
||
const ( | ||
Password AuthType = iota | ||
Token | ||
) | ||
|
||
type AuthConfig struct { | ||
AuthType AuthType | ||
UserName string | ||
Password string | ||
Token string | ||
} | ||
|
||
type BatchConfig struct { | ||
BatchEnable bool | ||
BatchInterval int | ||
BatchSize int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package opengemini | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
tests := map[string]struct { | ||
config *Config | ||
client *Client | ||
err error | ||
}{ | ||
"EmptyAddress": { | ||
config: &Config{}, | ||
client: nil, | ||
err: errors.New("must have at least one address"), | ||
}, | ||
"EmptyToken": { | ||
config: &Config{ | ||
Addresses: []*Address{{Host: "127.0.0.1", Port: 8086}}, | ||
AuthConfig: &AuthConfig{ | ||
AuthType: Token, | ||
}, | ||
}, | ||
client: nil, | ||
err: errors.New("invalid auth config cause empty token"), | ||
}, | ||
"EmptyPassword": { | ||
config: &Config{ | ||
Addresses: []*Address{{Host: "127.0.0.1", Port: 8086}}, | ||
AuthConfig: &AuthConfig{ | ||
AuthType: Password, | ||
}, | ||
}, | ||
client: nil, | ||
err: errors.New("invalid auth config cause empty username or password"), | ||
}, | ||
"InvalidBatchConfig": { | ||
config: &Config{ | ||
Addresses: []*Address{{Host: "127.0.0.1", Port: 8086}}, | ||
AuthConfig: &AuthConfig{ | ||
AuthType: Password, | ||
UserName: "TestUser", | ||
Password: "TestPassword", | ||
}, | ||
BatchConfig: &BatchConfig{ | ||
BatchEnable: true, | ||
BatchSize: 10, | ||
BatchInterval: -1, | ||
}, | ||
}, | ||
client: nil, | ||
err: errors.New("batch interval and batch size must be more than 0 ,if batch operate enable"), | ||
}, | ||
"NewClientSuccess": { | ||
config: &Config{ | ||
Addresses: []*Address{{Host: "127.0.0.1", Port: 8086}}, | ||
AuthConfig: &AuthConfig{ | ||
AuthType: Password, | ||
UserName: "TestUser", | ||
Password: "TestPassword", | ||
}, | ||
BatchConfig: &BatchConfig{ | ||
BatchEnable: true, | ||
BatchSize: 10, | ||
BatchInterval: 1, | ||
}, | ||
}, | ||
client: &Client{ | ||
addresses: []*Address{{Host: "127.0.0.1", Port: 8086}}, | ||
authConfig: &AuthConfig{ | ||
AuthType: Password, | ||
UserName: "TestUser", | ||
Password: "TestPassword", | ||
}, | ||
batchConfig: &BatchConfig{ | ||
BatchEnable: true, | ||
BatchSize: 10, | ||
BatchInterval: 1, | ||
}, | ||
}, | ||
}, | ||
} | ||
for caseName, tt := range tests { | ||
c, err := NewClient(tt.config) | ||
if !reflect.DeepEqual(c, tt.client) || !reflect.DeepEqual(tt.err, err) { | ||
t.Logf("case @%s not pass test", caseName) | ||
} | ||
} | ||
|
||
} |