Remove dependency from OSS to enterprise packages by moving Spanner functions to xorm. (#102692)
* Remove dependency from OSS to enterprise packages by moving Spanner functions to xorm.
This commit is contained in:
@@ -10,9 +10,10 @@ import (
|
||||
|
||||
_ "github.com/googleapis/go-sql-spanner"
|
||||
spannerdriver "github.com/googleapis/go-sql-spanner"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"xorm.io/core"
|
||||
|
||||
spannerext "github.com/grafana/grafana/pkg/extensions/spanner"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -381,7 +382,7 @@ func (s *spanner) CreateSequenceGenerator(db *sql.DB) (SequenceGenerator, error)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if spannerext.UsePlainText(connectorConfig) {
|
||||
if UsePlainText(connectorConfig) {
|
||||
// Plain-text means we're either using spannertest or Spanner emulator.
|
||||
// Switch to fake in-memory sequence number generator in that case.
|
||||
//
|
||||
@@ -393,3 +394,32 @@ func (s *spanner) CreateSequenceGenerator(db *sql.DB) (SequenceGenerator, error)
|
||||
|
||||
return newSequenceGenerator(db), nil
|
||||
}
|
||||
|
||||
func UsePlainText(connectorConfig spannerdriver.ConnectorConfig) bool {
|
||||
if strval, ok := connectorConfig.Params["useplaintext"]; ok {
|
||||
if val, err := strconv.ParseBool(strval); err == nil {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// SpannerConnectorConfigToClientOptions is adapted from https://github.com/googleapis/go-sql-spanner/blob/main/driver.go#L341-L477, from version 1.11.1.
|
||||
func SpannerConnectorConfigToClientOptions(connectorConfig spannerdriver.ConnectorConfig) []option.ClientOption {
|
||||
var opts []option.ClientOption
|
||||
if connectorConfig.Host != "" {
|
||||
opts = append(opts, option.WithEndpoint(connectorConfig.Host))
|
||||
}
|
||||
if strval, ok := connectorConfig.Params["credentials"]; ok {
|
||||
opts = append(opts, option.WithCredentialsFile(strval))
|
||||
}
|
||||
if strval, ok := connectorConfig.Params["credentialsjson"]; ok {
|
||||
opts = append(opts, option.WithCredentialsJSON([]byte(strval)))
|
||||
}
|
||||
if UsePlainText(connectorConfig) {
|
||||
opts = append(opts,
|
||||
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
|
||||
option.WithoutAuthentication())
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user