| 25 | } |
| 26 | |
| 27 | func New(dbType DBType) (interface{}, error) { |
| 28 | // use the "database/sql" package for connection management. |
| 29 | // this allows us to connect to the target database (postgres, mysql, etc) using |
| 30 | // the common dialect provided by the "database/sql" package. |
| 31 | connProducer := &connutil.SQLConnectionProducer{} |
| 32 | connProducer.Type = dbType.String() |
| 33 | |
| 34 | var delegateVaultPlugin dbplugin.Database |
| 35 | var connectorCleanUpFunc func() error |
| 36 | var secretValuesMaskingFunc func() map[string]string |
| 37 | var err error |
| 38 | |
| 39 | // determine the target cloudsql db instance type and delegate database operations to it |
| 40 | if dbType == Postgres { |
| 41 | delegateVaultPlugin, connectorCleanUpFunc, secretValuesMaskingFunc, err = newPostgresDatabase(dbType, connProducer) |
| 42 | if err != nil { |
| 43 | return nil, errors.Wrap(err, "unable to initialize connector for 'postgres' database instance") |
| 44 | } |
| 45 | } else { |
| 46 | // no other types are supported yet |
| 47 | return nil, errors.Errorf("unsupported target cloudsql database instance type: %s", dbType) |
| 48 | } |
| 49 | |
| 50 | // initialize the database plugin |
| 51 | cloudsqlDB := &CloudSQL{ |
| 52 | dbType: dbType, |
| 53 | SQLConnectionProducer: connProducer, |
| 54 | delegateVaultPlugin: delegateVaultPlugin, |
| 55 | connectorCleanup: connectorCleanUpFunc, |
| 56 | } |
| 57 | |
| 58 | // Wrap the plugin with middleware to sanitize errors |
| 59 | wrappedDB := dbplugin.NewDatabaseErrorSanitizerMiddleware(cloudsqlDB, secretValuesMaskingFunc) |
| 60 | return wrappedDB, nil |
| 61 | } |
| 62 | |
| 63 | // Initialize the database plugin. This is the equivalent of a constructor for the |
| 64 | // database object itself. |