|
@@ -1,9 +1,10 @@
|
|
|
package sdk
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
"git.sxidc.com/service-supports/ds-sdk/client"
|
|
|
"git.sxidc.com/service-supports/ds-sdk/db_operations"
|
|
|
- "strings"
|
|
|
)
|
|
|
|
|
|
var sdkInstance *SDK
|
|
@@ -12,7 +13,7 @@ func GetInstance() *SDK {
|
|
|
return sdkInstance
|
|
|
}
|
|
|
|
|
|
-func InitInstance(token string, baseUrl string, namespace string, dataSource *DataSourceOption, opts ...Option) error {
|
|
|
+func InitInstance(token string, baseUrl string, namespace string, dataSource string, opts ...Option) error {
|
|
|
if sdkInstance != nil {
|
|
|
return nil
|
|
|
}
|
|
@@ -30,17 +31,7 @@ func InitInstance(token string, baseUrl string, namespace string, dataSource *Da
|
|
|
|
|
|
c := client.New(options.timeout)
|
|
|
|
|
|
- err := initNamespace(c, options)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- err = initDataSource(c, options)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- dbOperations, err := initDBOperations(options.dataSource.Spec)
|
|
|
+ dbOperations, err := initDBOperations(c, options)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -54,46 +45,34 @@ func InitInstance(token string, baseUrl string, namespace string, dataSource *Da
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func initNamespace(c *client.Client, options *Options) error {
|
|
|
- namespaceInfos, err := c.GetNamespaces(options.token, options.baseUrl, options.namespace, 1, 1)
|
|
|
+func initDBOperations(c *client.Client, options *Options) (db_operations.DBOperations, error) {
|
|
|
+ dataSourceInfo, err := c.GetDataSources(options.token, options.baseUrl, options.namespace, options.dataSource,
|
|
|
+ "database", 0, 0)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- if namespaceInfos != nil && len(namespaceInfos) != 0 {
|
|
|
- return nil
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- err = c.CreateNamespace(options.token, options.baseUrl, options.namespace)
|
|
|
- if err != nil && !strings.Contains(err.Error(), "已存在") {
|
|
|
- return err
|
|
|
+ if dataSourceInfo == nil || len(dataSourceInfo) == 0 {
|
|
|
+ return nil, errors.New("不存在类型为database的数据源" + options.dataSource)
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func initDataSource(c *client.Client, options *Options) error {
|
|
|
- dataSourceInfos, err := c.GetDataSources(options.token, options.baseUrl, options.namespace,
|
|
|
- options.dataSource.Name, options.dataSource.Type, 1, 1)
|
|
|
+ specStr, err := c.GetDataSourceSpec(options.token, options.baseUrl, options.namespace, options.dataSource)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- if dataSourceInfos != nil && len(dataSourceInfos) != 0 {
|
|
|
- return nil
|
|
|
+ specMap := make(map[string]any)
|
|
|
+ err = json.Unmarshal([]byte(specStr), &specMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- err = c.CreateDataSource(options.token, options.baseUrl, options.namespace,
|
|
|
- options.dataSource.Name, options.dataSource.Type, options.dataSource.Spec)
|
|
|
- if err != nil && !strings.Contains(err.Error(), "已存在") {
|
|
|
- return err
|
|
|
+ dbOperations, err := db_operations.NewOperationsFromMap(specMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func initDBOperations(dataSourceSpec map[string]any) (db_operations.DBOperations, error) {
|
|
|
- return db_operations.NewOperationsFromMap(dataSourceSpec)
|
|
|
+ return dbOperations, nil
|
|
|
}
|
|
|
|
|
|
func DestroyInstance() error {
|