Skip to content

Commit

Permalink
feat: support new OpenGemini client with config
Browse files Browse the repository at this point in the history
Signed-off-by: PennyYoon <[email protected]>
  • Loading branch information
Chenxulin97 committed Nov 16, 2023
1 parent 2a41447 commit 7c0a7f7
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
51 changes: 51 additions & 0 deletions opengemini/client.go
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
package opengemini

Check failure on line 1 in opengemini/client.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/openGemini/opengemini-client-go/opengemini [github.com/openGemini/opengemini-client-go/opengemini.test]

import (
"crypto/tls"
)

var _, _ = NewClient(&Config{})

type Client interface {
WriteBatchPoints(points BatchPoints)

Check failure on line 10 in opengemini/client.go

View workflow job for this annotation

GitHub Actions / lint

undefined: BatchPoints

Check failure on line 10 in opengemini/client.go

View workflow job for this annotation

GitHub Actions / build

undefined: BatchPoints
WritePoint(point Point)

Check failure on line 11 in opengemini/client.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Point

Check failure on line 11 in opengemini/client.go

View workflow job for this annotation

GitHub Actions / build

undefined: Point
WriteLineProtocol(data, database, retentionPolicy, precision string)
Query()
Client()
}

type Config struct {
AddressList []*Address
AuthConfig *AuthConfig
BatchConfig *BatchConfig
GzipEnabled bool
TlsConfig *tls.Config
}

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 {
BatchEnabled bool
BatchInterval int
BatchSize int
}

func NewClient(config *Config) (Client, error) {
return newClient(config)
}
60 changes: 60 additions & 0 deletions opengemini/client_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package opengemini

import "errors"

type client struct {
config *Config
}

func newClient(c *Config) (Client, error) {
if len(c.AddressList) == 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 {
if len(c.AuthConfig.UserName) == 0 {
return nil, errors.New("invalid auth config cause empty username")
}
if len(c.AuthConfig.Password) == 0 {
return nil, errors.New("invalid auth config cause empty password")
}
}
if c.BatchConfig.BatchEnabled {
if c.BatchConfig.BatchInterval <= 0 {
return nil, errors.New("batch interval must be more than 0 , if batch operate enable")
}
if c.BatchConfig.BatchSize <= 0 {
return nil, errors.New("batch size must be more than 0 , if batch operate enable")
}
}
client := &client{
config: c,
}
return client, nil
}

func (c *client) WriteBatchPoints(points BatchPoints) {

Check failure on line 38 in opengemini/client_impl.go

View workflow job for this annotation

GitHub Actions / lint

undefined: BatchPoints

Check failure on line 38 in opengemini/client_impl.go

View workflow job for this annotation

GitHub Actions / build

undefined: BatchPoints
//TODO implement me
panic("implement me")
}
func (c *client) WritePoint(point Point) {

Check failure on line 42 in opengemini/client_impl.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Point

Check failure on line 42 in opengemini/client_impl.go

View workflow job for this annotation

GitHub Actions / build

undefined: Point
//TODO implement me
panic("implement me")
}

func (c *client) WriteLineProtocol(data, database, retentionPolicy, precision string) {
//TODO implement me
panic("implement me")
}

func (c *client) Query() {
//TODO implement me
panic("implement me")
}

func (c *client) Client() {
//TODO implement me
panic("implement me")
}
93 changes: 93 additions & 0 deletions opengemini/client_test.go
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}},

Check failure on line 22 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / lint

unknown field Addresses in struct literal of type Config

Check failure on line 22 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field Addresses in struct literal of type Config
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}},

Check failure on line 32 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / lint

unknown field Addresses in struct literal of type Config

Check failure on line 32 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field Addresses in struct literal of type Config
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}},

Check failure on line 42 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / lint

unknown field Addresses in struct literal of type Config

Check failure on line 42 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field Addresses in struct literal of type Config
AuthConfig: &AuthConfig{
AuthType: Password,
UserName: "TestUser",
Password: "TestPassword",
},
BatchConfig: &BatchConfig{
BatchEnable: true,

Check failure on line 49 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / lint

unknown field BatchEnable in struct literal of type BatchConfig

Check failure on line 49 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field BatchEnable in struct literal of type BatchConfig
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}},

Check failure on line 59 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / lint

unknown field Addresses in struct literal of type Config

Check failure on line 59 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field Addresses in struct literal of type Config
AuthConfig: &AuthConfig{
AuthType: Password,
UserName: "TestUser",
Password: "TestPassword",
},
BatchConfig: &BatchConfig{
BatchEnable: true,

Check failure on line 66 in opengemini/client_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field BatchEnable in struct literal of type BatchConfig
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)
}
}

}

0 comments on commit 7c0a7f7

Please sign in to comment.