bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/metabase/database.go (about)

     1  package metabase
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"bitbucket.org/Aishee/synsec/pkg/csconfig"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type Database struct {
    14  	DBUrl   string
    15  	Model   *Model
    16  	Config  *csconfig.DatabaseCfg
    17  	Client  *APIClient
    18  	Details *Details
    19  	// in case mysql host is 127.0.0.1 the ip address of mysql/pgsql host will be the docker gateway since metabase run in a container
    20  }
    21  
    22  type Details struct {
    23  	Db                string      `json:"db"`
    24  	Host              string      `json:"host"`
    25  	Port              int         `json:"port"`
    26  	Dbname            string      `json:"dbname"`
    27  	User              string      `json:"user"`
    28  	Password          string      `json:"password"`
    29  	Ssl               bool        `json:"ssl"`
    30  	AdditionalOptions interface{} `json:"additional-options"`
    31  	TunnelEnabled     bool        `json:"tunnel-enabled"`
    32  }
    33  
    34  type Model struct {
    35  	Engine         string                 `json:"engine"`
    36  	Name           string                 `json:"name"`
    37  	Details        *Details               `json:"details"`
    38  	AutoRunQueries bool                   `json:"auto_run_queries"`
    39  	IsFullSync     bool                   `json:"is_full_sync"`
    40  	IsOnDemand     bool                   `json:"is_on_demand"`
    41  	Schedules      map[string]interface{} `json:"schedules"`
    42  }
    43  
    44  func NewDatabase(config *csconfig.DatabaseCfg, client *APIClient, remoteDBAddr string) (*Database, error) {
    45  	var details *Details
    46  
    47  	database := Database{}
    48  
    49  	switch config.Type {
    50  	case "mysql":
    51  		return nil, fmt.Errorf("database '%s' is not supported yet", config.Type)
    52  	case "sqlite":
    53  		database.DBUrl = metabaseSQLiteDBURL
    54  		localFolder := filepath.Dir(config.DbPath)
    55  		// replace /var/lib/synsec/data/ with /metabase-data/
    56  		dbPath := strings.Replace(config.DbPath, localFolder, containerSharedFolder, 1)
    57  		details = &Details{
    58  			Db: dbPath,
    59  		}
    60  	case "postgresql", "postgres", "pgsql":
    61  		return nil, fmt.Errorf("database '%s' is not supported yet", config.Type)
    62  	default:
    63  		return nil, fmt.Errorf("database '%s' not supported", config.Type)
    64  	}
    65  	database.Details = details
    66  	database.Client = client
    67  	database.Config = config
    68  
    69  	return &database, nil
    70  }
    71  
    72  func (d *Database) Update() error {
    73  	success, errormsg, err := d.Client.Do("GET", routes[databaseEndpoint], nil)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	if errormsg != nil {
    78  		return fmt.Errorf("update sqlite db http error: %+v", errormsg)
    79  	}
    80  
    81  	data, err := json.Marshal(success)
    82  	if err != nil {
    83  		return errors.Wrap(err, "update sqlite db response (marshal)")
    84  	}
    85  
    86  	model := Model{}
    87  
    88  	if err := json.Unmarshal(data, &model); err != nil {
    89  		return errors.Wrap(err, "update sqlite db response (unmarshal)")
    90  	}
    91  	model.Details = d.Details
    92  	success, errormsg, err = d.Client.Do("PUT", routes[databaseEndpoint], model)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	if errormsg != nil {
    97  		return fmt.Errorf("update sqlite db http error: %+v", errormsg)
    98  	}
    99  
   100  	return nil
   101  }