2018-02-15 22:47:18 +00:00
|
|
|
// Package mdb contains a number of database wrappers for databases I commonly
|
|
|
|
// use
|
|
|
|
package mdb
|
2018-07-18 23:14:18 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
|
|
|
|
2018-07-18 23:14:18 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
2019-02-03 02:56:32 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
2018-07-18 23:14:18 +00:00
|
|
|
"google.golang.org/api/option"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GCE wraps configuration parameters commonly used for interacting with GCE
|
|
|
|
// services.
|
|
|
|
type GCE struct {
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx context.Context
|
2018-07-18 23:14:18 +00:00
|
|
|
Project string
|
|
|
|
CredFile string
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// WithGCE returns a GCE instance which will be initialized and configured when
|
|
|
|
// the start event is triggered on the returned Context (see mrun.Start).
|
2019-02-05 20:18:17 +00:00
|
|
|
// defaultProject is used as the default value for the mcfg parameter this
|
|
|
|
// function creates.
|
2019-02-09 19:08:30 +00:00
|
|
|
func WithGCE(parent context.Context, defaultProject string) (context.Context, *GCE) {
|
2019-02-05 20:18:17 +00:00
|
|
|
ctx := mctx.NewChild(parent, "gce")
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx, credFile := mcfg.WithString(ctx, "cred-file", "", "Path to GCE credientials JSON file, if any")
|
2019-02-03 02:56:32 +00:00
|
|
|
|
|
|
|
var project *string
|
|
|
|
const projectUsage = "Name of GCE project to use"
|
|
|
|
if defaultProject == "" {
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx, project = mcfg.WithRequiredString(ctx, "project", projectUsage)
|
2019-02-03 02:56:32 +00:00
|
|
|
} else {
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx, project = mcfg.WithString(ctx, "project", defaultProject, projectUsage)
|
2019-02-03 02:56:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 23:14:18 +00:00
|
|
|
var gce GCE
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx = mrun.WithStartHook(ctx, func(context.Context) error {
|
2019-02-03 02:56:32 +00:00
|
|
|
gce.Project = *project
|
2018-07-18 23:14:18 +00:00
|
|
|
gce.CredFile = *credFile
|
2019-02-09 19:08:30 +00:00
|
|
|
gce.ctx = mctx.Annotate(ctx, "project", gce.Project)
|
2018-07-18 23:14:18 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-02-09 19:08:30 +00:00
|
|
|
|
|
|
|
gce.ctx = ctx
|
2019-02-05 20:18:17 +00:00
|
|
|
return mctx.WithChild(parent, ctx), &gce
|
2018-07-18 23:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientOptions generates and returns the ClientOption instances which can be
|
|
|
|
// passed into most GCE client drivers.
|
|
|
|
func (gce *GCE) ClientOptions() []option.ClientOption {
|
|
|
|
var opts []option.ClientOption
|
|
|
|
if gce.CredFile != "" {
|
|
|
|
opts = append(opts, option.WithCredentialsFile(gce.CredFile))
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// Context returns the annotated Context from this instance's initialization.
|
|
|
|
func (gce *GCE) Context() context.Context {
|
|
|
|
return gce.ctx
|
2018-07-18 23:14:18 +00:00
|
|
|
}
|