shareddiscovery

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

Test Lint

Shared Discovery

A library for shared code in the Discovery Service.

API Documentation

Contributing

To contribute, you will need:

  • go >= 1.19
  • gopages for generating documentation

Create a feature branch from main and make your changes updates. Be sure to follow documentation standards when writing public functions and types and provide examples where applicable.

Make sure to run make docgen to update the documentation files.

Make tasks
  • make test runs unit tests
  • make mockgen builds mocks needed for testing

Once you feel good about your change and get it merged to main, you'll want to create a new release. Please follow semantic versioning as you release this library.

Installation

Since this is a private repository, you'll need a new environment variable called GOPRIVATE which you'll need to set to this repo. Put this in your .bashrc or .zshrc file:

export GOPRIVATE="github.com/pgdevelopers"

Now you should be able to run go get github.com/pgdevelopers/shareddiscovery to install this package in your project.

Usage

See the docs for complete API documentation.

Basic usage

  import (
    "github.com/pgdevelopers/shareddiscovery"
    "github.com/aws/aws-sdk-go/aws/session"
	  "github.com/aws/aws-sdk-go/service/dynamodb"
  )

  var discovery shareddiscovery.IFace

  func main() {
    query := QueryInput{Workspace: "tableName"}
    discovery.GetConfig(context.Background(), "someApiToken", query)
  }

  func init() {
    // setup AWS Session
    session := session.New()

    // setup DynamoDB
    dynamo := dynamodb.New(session)

    // setup shareddiscovery now
    discovery = shareddiscovery.New(dynamo)
  }
Testing

Provided is an interface that can be used with gomock to generate a mock for testing with. See the discovery service for more examples of testing with this library.

Documentation

Overview

SharedDiscovery is a shared library that supports getting data from the discovery dynamodb tables based on several criteria, including:

  • APIToken
  • Country
  • Workspace (tablename)

Admin Capabilities

This library also supports the idea of admin calls that can get any token needed. This is done using HMAC. For out purposes, the private key is stored in Secrets Manager under the name of the brand/environment.

Example

Typical usage is to setup a variable with the interface type and initialize that variable in your modules init function using the New() function provided

var shareddiscovery IFace

// setup AWS Session
session, _ := session.NewSession()

// setup DynamoDB
dynamo := dynamodb.New(session)

// define a QueryInput
query := QueryInput{Workspace: "tableName"}

// setup shareddiscovery now
shareddiscovery = New(dynamo)

// call functions
_, err := shareddiscovery.GetConfig(context.Background(), "someApiToken", query)
if err != nil {
	// deal with the error
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var LambdaTimer = 9 * time.Second

Functions

This section is empty.

Types

type IFace

type IFace interface {
	GetValidation(ctx context.Context, query QueryInput) (bool, error)
	GetConfig(ctx context.Context, apiToken string, query QueryInput) (map[string]interface{}, error)
	AdminGetAPIToken(ctx context.Context, secretKey string, query QueryInput) (string, error)
}

IFace describes what is required for building a SharedDiscovery implementation.

type QueryInput

type QueryInput struct {
	AppName     string
	Brand       string
	Country     string
	Environment string
	Signature   string
	QueryString map[string]string
	Workspace   string
}

QueryInput defines the values used to query dynamo with.

type Result added in v0.0.3

type Result struct {
	String string
	Map    map[string]interface{}
	Bool   bool
	Error  error
}

type SharedDiscovery

type SharedDiscovery struct {
	IFace
	DynamodbSvc dynamodbiface.DynamoDBAPI
}

SharedDiscovery is a custom service object for interacting with the global config

func New

New is a constructor that takes a preconfigured dynamodbiface and returns an implementation of SharedDiscoveryIFace Use this in your init function after creating your aws session and initializing dynamo.

func (SharedDiscovery) AdminGetAPIToken

func (service SharedDiscovery) AdminGetAPIToken(ctx context.Context, secretKey string, query QueryInput) (string, error)
Example

For making an admin call, a secretKey is required and the query string is needed to regenerate the signature correctly. The callers signature should also be passed via in to verify.

var shareddiscovery IFace

// setup AWS Session
session, _ := session.NewSession()

// setup DynamoDB
dynamo := dynamodb.New(session)

// define a QueryInput
query := QueryInput{
	Workspace:   "discovery_app",
	Signature:   "e52af791ec66085081f993a42d2a02a4b1dc08ad7b9f030dac5a0c20d5a0c68c",
	Brand:       "oralb",
	Environment: "qa",
	Country:     "US",
	QueryString: map[string]string{
		"brand":       "oralb",
		"environment": "qa",
		"countryCode": "US",
	},
}

// setup shareddiscovery now
shareddiscovery = New(dynamo)

// call functions
_, err := shareddiscovery.AdminGetAPIToken(context.Background(), "secretKey", query)
if err != nil {
	// deal with the error
}
Output:

func (SharedDiscovery) GetConfig

func (service SharedDiscovery) GetConfig(ctx context.Context, apiToken string, query QueryInput) (map[string]interface{}, error)
Example (WithCountry)

Passing in a Country will get data from the table filtered by the apiToken and Country. This is useful when the apiToken isn't unique per row, but an apiToken can have multiple countries.

session, _ := session.NewSession()
shared := New(dynamodb.New(session))
query := QueryInput{Workspace: "tableName", Country: "US"}
_, err := shared.GetConfig(context.Background(), "apitoken", query)
if err != nil {
	// deal with the error
}
Output:

Example (WithoutCountry)

Passing in only the workspace as a query is best used when the apiToken is unique for every row. In our case, this is the Firmware table

session, _ := session.NewSession()
shared := New(dynamodb.New(session))
query := QueryInput{Workspace: "tableName"}
_, err := shared.GetConfig(context.Background(), "apitoken", query)
if err != nil {
	// deal with the error
}
Output:

func (SharedDiscovery) GetValidation added in v0.0.2

func (service SharedDiscovery) GetValidation(ctx context.Context, query QueryInput) (bool, error)

Directories

Path Synopsis
mocks
mock_dynamodbiface
Package mock_dynamodbiface is a generated GoMock package.
Package mock_dynamodbiface is a generated GoMock package.

Jump to

Keyboard shortcuts

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