datastoredb

package
Version: v1.49.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2021 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package datastoredb provides an implementation of github.com/alexandre-normand/slackscot/store's StringStorer interface backed by the Google Cloud Datastore.

Requirements for the Google Cloud Datastore integration:

- A valid project id with datastore mode enabled
- Google Cloud Credentials (typically in the form of a json file
  with credentials from https://console.cloud.google.com/apis/credentials/serviceaccountkey)

Note: for deployments using credentials rotation, the current solution supports this use-case with a naive lazy recreation of the client on error. In order for fresh credentials to be effective when an authentication error happens, the credential client options must reflect the fresh credentials. One example of this is

option.WithCredentialsFile(filename)

Since that option points to a filename, the fresh credentials at that file location would be refreshed on client recreation.

Example code:

import (
	"github.com/alexandre-normand/slackscot/store/datastoredb"
	"google.golang.org/api/option"
)

func main() {
	// The first argument is going to be this instance's namespace so the plugin name is a good candidate.
	// The second argument is the gcloud project id which is what you'll have created with your gcloud service account
	// The third argument are client options which are most useful for providing credentials either in the form of a pre-parsed json file or
	// most commonly, the path to a json credentials file
	karmaStorer, err := datastoredb.New(plugins.KarmaPluginName, "youppi", option.WithCredentialsFile(*gcloudCredentialsFile))
	if err != nil {
		log.Fatalf("Opening [%s] db failed: %s", plugins.KarmaPluginName, err.Error())
	}
	defer karmaStorer.Close()

	// Do something with the database
	karma := plugins.NewKarma(karmaStorer)}

	// Run your instance
	...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewdatastorerWithTelemetry added in v1.44.0

func NewdatastorerWithTelemetry(base datastorer, name string, meter metric.Meter) datastorerWithTelemetry

NewdatastorerWithTelemetry returns an instance of the datastorer decorated with open telemetry timing and count metrics

Types

type DatastoreDB

type DatastoreDB struct {
	// contains filtered or unexported fields
}

DatastoreDB implements the slackscot StringStorer interface. It maps the given name (usually a plugin name) to the datastore entity Kind to isolate data between different plugins

func New

func New(name string, gcloudProjectID string, gcloudClientOpts ...option.ClientOption) (dsdb *DatastoreDB, err error)

New returns a new instance of DatastoreDB for the given name (which maps to the datastore entity "Kind" and can be thought of as the namespace). This function also requires a gcloudProjectID as well as at least one option to provide gcloud client credentials. Note that in order to support a deployment where credentials can get updated, the gcloudClientOpts should use something like option.WithCredentialsFile with the credentials file being updated on disk so that when reconnecting on a failure, the updated credentials are visible through the same gcloud client options

func NewWithTelemetry added in v1.44.0

func NewWithTelemetry(appName string, meter metric.Meter, kindName string, gcloudProjectID string, gcloudClientOpts ...option.ClientOption) (dsdb *DatastoreDB, err error)

NewWithTelemetry returns a new instance of DatastoreDB similar to New but with added opentelemetry instrumentation. Note that even if no metrics exporter is configured, it is safe to use telemetry and should result with NOOP operations with a small overhead.

For the given name (which maps to the datastore entity "Kind" and can be thought of as the namespace). This function also requires a gcloudProjectID as well as at least one option to provide gcloud client credentials. Note that in order to support a deployment where credentials can get updated, the gcloudClientOpts should use something like option.WithCredentialsFile with the credentials file being updated on disk so that when reconnecting on a failure, the updated credentials are visible through the same gcloud client options

func (*DatastoreDB) DeleteSiloString

func (dsdb *DatastoreDB) DeleteSiloString(silo string, key string) (err error)

DeleteSiloString deletes the entry for the given key in the given silo. If the entry is not found an error is returned

func (*DatastoreDB) DeleteString

func (dsdb *DatastoreDB) DeleteString(key string) (err error)

DeleteString deletes the entry for the given key. If the entry is not found an error is returned

func (*DatastoreDB) GetSiloString

func (dsdb *DatastoreDB) GetSiloString(silo string, key string) (value string, err error)

GetSiloString returns the value associated to a given key within the silo provided. If the value is not found or an error occurred, the zero-value string is returned along with the error

func (*DatastoreDB) GetString

func (dsdb *DatastoreDB) GetString(key string) (value string, err error)

GetString returns the value associated to a given key. If the value is not found or an error occurred, the zero-value string is returned along with the error

func (*DatastoreDB) GlobalScan

func (dsdb *DatastoreDB) GlobalScan() (entries map[string]map[string]string, err error)

GlobalScan returns all key/values for all silos keyed by silo name

func (*DatastoreDB) PutSiloString

func (dsdb *DatastoreDB) PutSiloString(silo string, key string, value string) (err error)

PutSiloString stores the key/value to the database in the given silo

func (*DatastoreDB) PutString

func (dsdb *DatastoreDB) PutString(key string, value string) (err error)

PutString stores the key/value to the database

func (*DatastoreDB) Scan

func (dsdb *DatastoreDB) Scan() (entries map[string]string, err error)

Scan returns all key/values from the database

func (*DatastoreDB) ScanSilo

func (dsdb *DatastoreDB) ScanSilo(silo string) (entries map[string]string, err error)

ScanSilo returns all key/values from the database in the given silo

type EntryValue

type EntryValue struct {
	Value string `datastore:",noindex"`
}

EntryValue represents an entity/entry value mapped to a datastore key

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL