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-06-23 18:55:40 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
2019-02-03 02:56:32 +00:00
|
|
|
"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-06-23 18:55:40 +00:00
|
|
|
cmp *mcmp.Component
|
2018-07-18 23:14:18 +00:00
|
|
|
Project string
|
|
|
|
CredFile string
|
|
|
|
}
|
|
|
|
|
2019-06-23 18:55:40 +00:00
|
|
|
type gceOpts struct {
|
|
|
|
defaultProject string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GCEOption is a value which adjusts the behavior of InstGCE.
|
|
|
|
type GCEOption func(*gceOpts)
|
|
|
|
|
|
|
|
// GCEDefaultProject sets the given string to be the default project of the GCE
|
|
|
|
// instance. The default project will still be configurable via mcfg regardless
|
|
|
|
// of what this is set to.
|
|
|
|
func GCEDefaultProject(defaultProject string) GCEOption {
|
|
|
|
return func(opts *gceOpts) {
|
|
|
|
opts.defaultProject = defaultProject
|
2019-02-03 02:56:32 +00:00
|
|
|
}
|
2019-06-23 18:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InstGCE instantiates a GCE which will be initialized when the Init event is
|
|
|
|
// triggered on the given Component. defaultProject is used as the default value
|
|
|
|
// for the mcfg parameter this function creates.
|
|
|
|
func InstGCE(cmp *mcmp.Component, options ...GCEOption) *GCE {
|
|
|
|
var opts gceOpts
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
gce := GCE{cmp: cmp.Child("gce")}
|
|
|
|
credFile := mcfg.String(gce.cmp, "cred-file",
|
|
|
|
mcfg.ParamUsage("Path to GCE credientials JSON file, if any"))
|
|
|
|
project := mcfg.String(gce.cmp, "project",
|
|
|
|
mcfg.ParamDefaultOrRequired(opts.defaultProject),
|
|
|
|
mcfg.ParamUsage("Name of GCE project to use"))
|
2019-02-03 02:56:32 +00:00
|
|
|
|
2019-06-23 18:55:40 +00:00
|
|
|
mrun.InitHook(gce.cmp, func(ctx 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-06-23 18:55:40 +00:00
|
|
|
gce.cmp.Annotate("project", gce.Project)
|
|
|
|
mlog.From(gce.cmp).Info("GCE config initialized", ctx)
|
2018-07-18 23:14:18 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2019-06-23 18:55:40 +00:00
|
|
|
return &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 {
|
2019-06-23 18:55:40 +00:00
|
|
|
return gce.cmp.Context()
|
2018-07-18 23:14:18 +00:00
|
|
|
}
|