|
|
@@ -7,13 +7,15 @@ import (
|
|
|
|
|
|
var sdkInstance *SDK
|
|
|
|
|
|
-func InitInstance(token string, baseUrl string, namespace string, opts ...Option) error {
|
|
|
- sdk, err := New(token, baseUrl, namespace, opts...)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+func InitInstance(token string, baseUrl string, namespace string, dataSource *DataSource, opts ...Option) error {
|
|
|
+ if sdkInstance == nil {
|
|
|
+ sdk, err := newSDK(token, baseUrl, namespace, dataSource, opts...)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- sdkInstance = sdk
|
|
|
+ sdkInstance = sdk
|
|
|
+ }
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
@@ -27,11 +29,12 @@ type SDK struct {
|
|
|
client *client.Client
|
|
|
}
|
|
|
|
|
|
-func New(token string, baseUrl string, namespace string, opts ...Option) (*SDK, error) {
|
|
|
+func newSDK(token string, baseUrl string, namespace string, dataSource *DataSource, opts ...Option) (*SDK, error) {
|
|
|
options := &Options{
|
|
|
- token: token,
|
|
|
- baseUrl: baseUrl,
|
|
|
- namespace: namespace,
|
|
|
+ token: token,
|
|
|
+ baseUrl: baseUrl,
|
|
|
+ namespace: namespace,
|
|
|
+ dataSource: dataSource,
|
|
|
}
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
@@ -52,19 +55,17 @@ func New(token string, baseUrl string, namespace string, opts ...Option) (*SDK,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- for _, dataSourceOption := range options.dataSourceMap {
|
|
|
- dataSourceInfos, err := c.GetDataSources(token, baseUrl, namespace,
|
|
|
- dataSourceOption.Name, dataSourceOption.Type, 1, 1)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+ dataSourceInfos, err := c.GetDataSources(token, baseUrl, namespace,
|
|
|
+ options.dataSource.Name, options.dataSource.Type, 1, 1)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- if dataSourceInfos == nil || len(dataSourceInfos) == 0 {
|
|
|
- err := c.CreateDataSource(token, baseUrl, namespace,
|
|
|
- dataSourceOption.Name, dataSourceOption.Type, dataSourceOption.Spec)
|
|
|
- if err != nil && !strings.Contains(err.Error(), "已存在") {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+ if dataSourceInfos == nil || len(dataSourceInfos) == 0 {
|
|
|
+ err := c.CreateDataSource(token, baseUrl, namespace,
|
|
|
+ options.dataSource.Name, options.dataSource.Type, options.dataSource.Spec)
|
|
|
+ if err != nil && !strings.Contains(err.Error(), "已存在") {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
}
|
|
|
|