Documentation
¶
Overview ¶
Package gcp provides Google Cloud helpers for mygoto.me service.
This file contains helpers for interacting with BigQuery. It includes functions to authenticate using the default service account, create datasets and tables when they do not exist and stream rows using the BigQuery streaming API. All operations log errors via the common logging helpers and streaming inserts are retried once when failures occur.
Package gcp provides Google Cloud helpers. This file defines helpers for retrieving results from Cloud Datastore queries.
Package gcp contains helpers backed by Google Cloud services. This file implements datastore-backed storage of authenticated users and their roles.
Index ¶
- Variables
- func B2S(b []byte) string
- func CreateDatasetIfNotExists(c context.Context, projectID, datasetID string) error
- func CreateTableInBigQuery(c context.Context, newTable *bigquery.Table) error
- func Debug(format string, v ...interface{})
- func DeleteMemCache(c context.Context, key string) (err error)
- func Error(format string, v ...interface{})
- func GetBQServiceAccountClient(c context.Context) (*bigquery.Service, error)
- func GetFirst(c context.Context, q *datastore.Query, dst interface{}) (*datastore.Key, error)
- func GetMemCache(c context.Context, key string) ([]byte, error)
- func GetMemCacheString(c context.Context, key string) string
- func GetObjMemCache(c context.Context, key string, v interface{}) error
- func GetUserRole(c context.Context, email string) (string, error)
- func Info(format string, v ...interface{})
- func SetMemCache(c context.Context, key string, item []byte, hours int32)
- func SetMemCacheString(c context.Context, key string, item string, hours int32)
- func SetObjMemCache(c context.Context, key string, v interface{}, hours int32) error
- func StreamDataInBigquery(c context.Context, projectId, datasetId, tableId string, ...) error
- func Version(c context.Context) string
- type User
Constants ¶
This section is empty.
Variables ¶
var VERSION string
VERSION stores the deployed application version retrieved from App Engine. It is set by the Version helper in appengine.go.
Functions ¶
func B2S ¶
b2s converts a byte slice possibly containing a null terminator into a string. It mirrors common.B2S but is duplicated here to avoid a package dependency.
func CreateDatasetIfNotExists ¶
CreateDatasetIfNotExists ensures the given dataset exists. It first attempts to retrieve the dataset and if it receives a 404 error, a new dataset is created. All errors are logged and returned to the caller.
func CreateTableInBigQuery ¶
CreateTableInBigQuery deletes any existing table with the same ID and then creates the provided table definition. Deletion errors are logged but do not stop table creation. The caller is expected to provide a table with a valid TableReference and Schema.
func Debug ¶
func Debug(format string, v ...interface{})
Debug writes a formatted debug message. This is a lightweight replacement for the helpers in the parent package to avoid an import cycle.
func DeleteMemCache ¶
DeleteMemCache removes a key from memcache. memcache.ErrCacheMiss is ignored so callers can treat missing keys as a no-op. Any other error is logged and returned.
func Error ¶
func Error(format string, v ...interface{})
Error writes a formatted error message prefixed with "ERROR:".
func GetBQServiceAccountClient ¶
GetBQServiceAccountClient returns an authenticated BigQuery service using the default service account credentials. Errors encountered while creating the client are logged and returned to the caller.
func GetFirst ¶
GetFirst executes the query and loads the first entity into dst.
Parameters:
c - context used for datastore calls. q - query to run. dst - pointer that will receive the entity data.
Returns the entity key if one was found. When the query yields no results, the key is nil and the error is datastore.ErrNoSuchEntity. Any other error encountered when fetching the first result is returned as-is. The datastore.Done error from t.Next is converted to datastore.ErrNoSuchEntity so callers can easily detect the no-results case.
func GetMemCache ¶
GetMemCache retrieves raw bytes from memcache. When the key is missing, memcache.ErrCacheMiss is returned with an empty slice. Other errors are logged and returned with an empty slice.
func GetMemCacheString ¶
GetMemCacheString returns the cached string for the provided key. An empty string is returned when the key is missing or an error occurs.
Example:
name := GetMemCacheString(ctx, "user-name")
func GetObjMemCache ¶
GetObjMemCache retrieves an object stored with SetObjMemCache. memcache.ErrCacheMiss is returned when the key is missing. Any other error is returned as-is.
func GetUserRole ¶
GetUserRole fetches and returns the role for the user with the given email. It returns datastore.ErrNoSuchEntity if the user is not found.
func Info ¶
func Info(format string, v ...interface{})
Info writes a formatted informational message.
func SetMemCache ¶
SetMemCache stores bytes in memcache for the specified number of hours. If the key already exists it is overwritten. Errors are logged.
func SetMemCacheString ¶
SetMemCacheString stores the provided string in memcache for the given hours. Existing values are overwritten.
Example:
SetMemCacheString(ctx, "user-name", "gopher", 2)
func SetObjMemCache ¶
SetObjMemCache stores an arbitrary object in memcache for the given duration. Do not store sensitive data here unless it is encrypted as the cache is accessible by other applications running in the same environment.
func StreamDataInBigquery ¶
func StreamDataInBigquery(c context.Context, projectId, datasetId, tableId string, req *bigquery.TableDataInsertAllRequest) error
StreamDataInBigquery inserts rows into a BigQuery table using the streaming API. If the first attempt fails, the function waits 10 seconds and retries once. Errors from each attempt are logged and the error from the second attempt is returned.
Types ¶
type User ¶
type User struct {
Email string // unique user identifier and datastore key
Role string // current user role, e.g. "admin" or "organizer"
Created time.Time // timestamp when the entity was first stored
}
User represents an authenticated user. User represents an authenticated user stored in the datastore.
func EnsureUserExists ¶
EnsureUserExists retrieves a user from the datastore by email. If the user does not exist a new entity is created with the provided role. If the user exists but the role differs, the stored role is updated. The resulting user entity is returned.