tenantid

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2022 License: MIT Imports: 8 Imported by: 3

README

Tenant ID translator client

This package contains a client library for the tenant translator service. The tenant translator service provides operations for conversion between different tenant identifiers.

Installation

go get github.com/RedHatInsights/tenant-utils/pkg/tenantid

Client initialization

Initialize the translator with default settings:

translator := tenantid.NewTranslator("https://apicast.svc.cluster.local:8892")

Note that in Red Hat ConsoleDot environments the TENANT_TRANSLATOR_HOST and TENANT_TRANSLATOR_PORT parameters are configured. These should be used to initialize the tenant translator service client, i.e. "http://${TENANT_TRANSLATOR_HOST}:${TENANT_TRANSLATOR_PORT}"

Additional configuration options can be provided to customize the tenant translator service client.

Custom timeout

A custom timeout value can be specified with:

translator := tenantid.NewTranslator(
	"https://apicast.svc.cluster.local:8892",
	tenantid.WithTimeout(15*time.Second),
)
Collecting metrics

Collection of metrics can be enabled with:

translator := tenantid.NewTranslator(
	"https://apicast.svc.cluster.local:8892",
	tenantid.WithMetrics(),
)

This will cause a new Prometheus histogram named tenant_translator_request_duration_seconds to be registered with the default registry. The histogram uses the default bucket configuration and labels are used to distinguish operations and result (status code).

Alternatively, the metrics can be registered with a given registry:

translator := tenantid.NewTranslator(
	"https://apicast.svc.cluster.local:8892",
	tenantid.WithMetricsWithCustomRegisterer(registry),
)
Custom HTTP client

An entirely custom HTTP client can be provided also:

translator := tenantid.NewTranslator(
	"https://apicast.svc.cluster.local:8892",
	tenantid.WithDoer(customClient),
)

Using the client

Translate org_id to account number:

orgID, err := translator.OrgIDToEAN(context.Background(), "901578")

Note that the returned account number may be nil in case when the given tenant does not define an account number.

Translate account number to org_id:

account, err := translator.EANToOrgID(context.Background(), "5318290")

Batch version of these operations are also provided. See API documentation for more details.

Using the mock implementation

In addition to the HTTP client, a mock implementation is provided. This implementation is useful for test execution, local development, etc.

mappings := map[string]*string{
	"5318290":  stringRef("901578"),
	"654321":   nil,
}

translator := tenantid.NewTranslatorMockWithMapping(mappings)

Documentation

Overview

Package tenantid is a client library for the Tenant Translator service. The service provides translation between different tenant identifiers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchTranslator

type BatchTranslator interface {

	// Converts a slice of EANs (EBS account number) to org_ids
	EANsToOrgIDs(ctx context.Context, eans []string) (results []TranslationResult, err error)

	// Converts a slice of org_ids to EANs (EBS account number)
	OrgIDsToEANs(ctx context.Context, orgIDs []string) (results []TranslationResult, err error)
}

Provides translation between tenant identifiers.

type HttpRequestDoer

type HttpRequestDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

abstraction of http.Client

type TenantNotFoundError

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

Indicates that no tenant matches the provided identifier

func (*TenantNotFoundError) Error

func (e *TenantNotFoundError) Error() string

type TranslationResult

type TranslationResult struct {
	OrgID string
	EAN   *string
	Err   error
}

Holds the result of tenant identifier translation

type Translator

type Translator interface {
	BatchTranslator

	// Converts an EAN (EBS account number) to org_id.
	// Returns TenantNotFoundError (second return value) if the EAN is not known.
	EANToOrgID(ctx context.Context, ean string) (orgId string, err error)

	// Converts an org_id to EAN (EBS account number).
	// Returns nil if the org_id belongs to an anemic tenant or the org_id is not known.
	OrgIDToEAN(ctx context.Context, orgId string) (ean *string, err error)
}

Provides translation between tenant identifiers. Namely, it converts an org_id to EAN (EBS account number) and vice versa. Both single-operation and batch variants are provided

func NewTranslator

func NewTranslator(serviceHost string, options ...TranslatorOption) Translator

NewTranslator returns a new translator instance configured with the provided options

func NewTranslatorMock

func NewTranslatorMock() Translator

NewTranslatorMock returns a mock implementation of translator with a predefined mapping.

func NewTranslatorMockWithMapping

func NewTranslatorMockWithMapping(mapping map[string]*string) Translator

NewTranslatorMockWithMapping returns a mock implementation of translator that operates on the given mapping.

type TranslatorOption

type TranslatorOption interface {

	// Options may need to be applied in certain order (e.g. timeout-setting should be applied before an option that wraps the doer)
	// Priority lets us have complete ordering of all options
	// Options are applied in ascending priority (highest priority options go last)
	Priority() int
	// contains filtered or unexported methods
}

func WithDoer

func WithDoer(doer HttpRequestDoer) TranslatorOption

WithDoer allow a custom http.Client to be provided.

func WithDoerWrapper

func WithDoerWrapper(fn func(HttpRequestDoer) HttpRequestDoer) TranslatorOption

WithDoerWrapper allow for the default http.Client to be wrapped by a custom decorator.

func WithMetrics

func WithMetrics() TranslatorOption

WithMetrics registers a new histogram measuring latency with the default prometheus Registerer

func WithMetricsWithCustomRegisterer

func WithMetricsWithCustomRegisterer(registerer prometheus.Registerer) TranslatorOption

WithMetricsWithCustomRegisterer registers a new histogram measuring latency with the provided prometheus Registerer

func WithTimeout

func WithTimeout(timeout time.Duration) TranslatorOption

WithTimeout allows a custom timeout value to be defined. The default value of 10 seconds is used otherwise.

Jump to

Keyboard shortcuts

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