|
|
@@ -0,0 +1,121 @@
|
|
|
+package ds_sdk
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "git.sxidc.com/go-tools/utils/fileutils"
|
|
|
+ "git.sxidc.com/go-tools/utils/strutils"
|
|
|
+ "git.sxidc.com/go-tools/utils/yaml/yaml_loader"
|
|
|
+ "git.sxidc.com/service-supports/ds-sdk/client"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+var sdkInstance *SDK
|
|
|
+
|
|
|
+func GetInstance() *SDK {
|
|
|
+ return sdkInstance
|
|
|
+}
|
|
|
+
|
|
|
+func InitInstance(token string, baseUrl string, namespace string, dataSource *DataSourceOption, opts ...Option) error {
|
|
|
+ if sdkInstance != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ options := &Options{
|
|
|
+ token: token,
|
|
|
+ baseUrl: baseUrl,
|
|
|
+ namespace: namespace,
|
|
|
+ dataSource: dataSource,
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, opt := range opts {
|
|
|
+ opt(options)
|
|
|
+ }
|
|
|
+
|
|
|
+ c := client.New(options.timeout)
|
|
|
+
|
|
|
+ err := initNamespace(c, options)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = initDataSource(c, options)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = initSqlResources(c, options)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ sdkInstance = &SDK{
|
|
|
+ options: options,
|
|
|
+ client: c,
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func initNamespace(c *client.Client, options *Options) error {
|
|
|
+ namespaceInfos, err := c.GetNamespaces(options.token, options.baseUrl, options.namespace, 1, 1)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if namespaceInfos != nil && len(namespaceInfos) != 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ err = c.CreateNamespace(options.token, options.baseUrl, options.namespace)
|
|
|
+ if err != nil && !strings.Contains(err.Error(), "已存在") {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if dataSourceInfos != nil && len(dataSourceInfos) != 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func initSqlResources(c *client.Client, options *Options) error {
|
|
|
+ if strutils.IsStringEmpty(options.sqlResourcesDir) {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if !fileutils.PathExists(options.sqlResourcesDir) {
|
|
|
+ return errors.New("指定的SQL资源目录不存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ files, err := fileutils.GetDirFiles(options.sqlResourcesDir)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, file := range files {
|
|
|
+ err := yaml_loader.LoadYamlMapFile(file, func(objectMap map[string]any) error {
|
|
|
+
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|