-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from k1LoW/separate-datasource-go
Separate datasource/datasource.go
- Loading branch information
Showing
4 changed files
with
177 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package datasource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"github.com/k1LoW/tbls/drivers/dynamo" | ||
"github.com/k1LoW/tbls/schema" | ||
) | ||
|
||
// AnalizeDynamodb analyze `dynamodb://` | ||
func AnalyzeDynamodb(urlstr string) (*schema.Schema, error) { | ||
s := &schema.Schema{} | ||
u, err := url.Parse(urlstr) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
values := u.Query() | ||
err = setEnvAWSCredentials(values) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
region := u.Host | ||
|
||
sess := session.Must(session.NewSessionWithOptions(session.Options{ | ||
SharedConfigState: session.SharedConfigEnable, | ||
})) | ||
|
||
config := aws.NewConfig().WithRegion(region) | ||
if os.Getenv("AWS_ENDPOINT_URL") != "" { | ||
config = config.WithEndpoint(os.Getenv("AWS_ENDPOINT_URL")) | ||
} | ||
|
||
client := dynamodb.New(sess, config) | ||
ctx := context.Background() | ||
|
||
driver, err := dynamo.New(ctx, client) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
s.Name = fmt.Sprintf("Amazon DynamoDB (%s)", region) | ||
err = driver.Analyze(s) | ||
if err != nil { | ||
return s, err | ||
} | ||
return s, nil | ||
} | ||
|
||
func setEnvAWSCredentials(values url.Values) error { | ||
for k := range values { | ||
if strings.HasPrefix(k, "aws_") { | ||
return os.Setenv(strings.ToUpper(k), values.Get(k)) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package datasource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"strings" | ||
|
||
"cloud.google.com/go/bigquery" | ||
cloudspanner "cloud.google.com/go/spanner" | ||
"github.com/k1LoW/tbls/drivers/bq" | ||
"github.com/k1LoW/tbls/drivers/spanner" | ||
"github.com/k1LoW/tbls/schema" | ||
) | ||
|
||
// AnalyzeBigquery analyze `bq://` | ||
func AnalyzeBigquery(urlstr string) (*schema.Schema, error) { | ||
s := &schema.Schema{} | ||
ctx := context.Background() | ||
client, projectID, datasetID, err := NewBigqueryClient(ctx, urlstr) | ||
if err != nil { | ||
return s, err | ||
} | ||
defer client.Close() | ||
|
||
s.Name = fmt.Sprintf("%s:%s", projectID, datasetID) | ||
driver, err := bq.New(ctx, client, datasetID) | ||
if err != nil { | ||
return s, err | ||
} | ||
err = driver.Analyze(s) | ||
if err != nil { | ||
return s, err | ||
} | ||
return s, nil | ||
} | ||
|
||
// NewBigqueryClient returns new bigquery.Client | ||
func NewBigqueryClient(ctx context.Context, urlstr string) (*bigquery.Client, string, string, error) { | ||
u, err := url.Parse(urlstr) | ||
if err != nil { | ||
return nil, "", "", err | ||
} | ||
values := u.Query() | ||
err = setEnvGoogleApplicationCredentials(values) | ||
if err != nil { | ||
return nil, "", "", err | ||
} | ||
|
||
splitted := strings.Split(u.Path, "/") | ||
|
||
projectID := u.Host | ||
datasetID := splitted[1] | ||
|
||
client, err := bigquery.NewClient(ctx, projectID) | ||
return client, projectID, datasetID, err | ||
} | ||
|
||
// AnalyzeSpanner analyze `spanner://` | ||
func AnalyzeSpanner(urlstr string) (*schema.Schema, error) { | ||
s := &schema.Schema{} | ||
u, err := url.Parse(urlstr) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
values := u.Query() | ||
err = setEnvGoogleApplicationCredentials(values) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
splitted := strings.Split(u.Path, "/") | ||
projectID := u.Host | ||
instanceID := splitted[1] | ||
databaseID := splitted[2] | ||
|
||
db := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectID, instanceID, databaseID) | ||
ctx := context.Background() | ||
client, err := cloudspanner.NewClient(ctx, db) | ||
if err != nil { | ||
return s, err | ||
} | ||
defer client.Close() | ||
s.Name = db | ||
|
||
driver, err := spanner.New(ctx, client) | ||
if err != nil { | ||
return s, err | ||
} | ||
err = driver.Analyze(s) | ||
if err != nil { | ||
return s, err | ||
} | ||
return s, nil | ||
} | ||
|
||
func setEnvGoogleApplicationCredentials(values url.Values) error { | ||
keys := []string{ | ||
"google_application_credentials", | ||
"credentials", | ||
"creds", | ||
} | ||
for _, k := range keys { | ||
if values.Get(k) != "" { | ||
return os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", values.Get(k)) | ||
} | ||
} | ||
return nil | ||
} |