entitlement

package
v1.0.0-beta.92 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEntitlementGrantOwnerAdapter

func NewEntitlementGrantOwnerAdapter(
	featureRepo productcatalog.FeatureRepo,
	entitlementRepo EntitlementRepo,
	usageResetRepo UsageResetRepo,
	meterRepo meter.Repository,
	logger *slog.Logger,
) credit.OwnerConnector

Types

type BalanceHistoryParams

type BalanceHistoryParams struct {
	From           time.Time
	To             time.Time
	WindowSize     WindowSize
	WindowTimeZone time.Location
}

type CreateEntitlementGrantInputs

type CreateEntitlementGrantInputs struct {
	credit.CreateGrantInput
}

type CreateEntitlementInputs

type CreateEntitlementInputs struct {
	Namespace        string
	FeatureID        string    `json:"featureId"`
	MeasureUsageFrom time.Time `json:"measureUsageFrom,omitempty"`
	SubjectKey       string    `json:"subjectKey"`
}

type Entitlement

type Entitlement struct {
	models.NamespacedModel
	models.ManagedModel
	ID               string    `json:"id,omitempty"`
	FeatureID        string    `json:"featureId,omitempty"`
	MeasureUsageFrom time.Time `json:"measureUsageFrom,omitempty"`
	SubjectKey       string    `json:"subjectKey,omitempty"`
}

type EntitlementAlreadyExistsError

type EntitlementAlreadyExistsError struct {
	EntitlementID string
	FeatureID     string
	SubjectKey    string
}

func (*EntitlementAlreadyExistsError) Error

type EntitlementBalance

type EntitlementBalance struct {
	EntitlementID string    `json:"entitlementId"`
	Balance       float64   `json:"balance"`
	UsageInPeriod float64   `json:"usageInPeriod"`
	Overage       float64   `json:"overage"`
	StartOfPeriod time.Time `json:"startOfPeriod"`
}

type EntitlementBalanceConnector

type EntitlementBalanceConnector interface {
	GetEntitlementBalance(ctx context.Context, entitlementID models.NamespacedID, at time.Time) (*EntitlementBalance, error)
	GetEntitlementBalanceHistory(ctx context.Context, entitlementID models.NamespacedID, params BalanceHistoryParams) ([]EntitlementBalanceHistoryWindow, credit.GrantBurnDownHistory, error)
	ResetEntitlementUsage(ctx context.Context, entitlementID models.NamespacedID, resetAt time.Time) (balanceAfterReset *EntitlementBalance, err error)

	// GetEntitlementGrantBalanceHistory(ctx context.Context, entitlementGrantID EntitlementGrantID, params BalanceHistoryParams) ([]EntitlementBalanceHistoryWindow, error)
	CreateGrant(ctx context.Context, entitlement models.NamespacedID, inputGrant CreateEntitlementGrantInputs) (EntitlementGrant, error)
	ListEntitlementGrants(ctx context.Context, entitlementID models.NamespacedID) ([]EntitlementGrant, error)
}

func NewEntitlementBalanceConnector

func NewEntitlementBalanceConnector(
	streamingConnector streaming.Connector,
	ownerConnector credit.OwnerConnector,
	balanceConnector credit.BalanceConnector,
	grantConnector credit.GrantConnector,
) EntitlementBalanceConnector

type EntitlementBalanceHistoryWindow

type EntitlementBalanceHistoryWindow struct {
	From           time.Time
	To             time.Time
	UsageInPeriod  float64
	BalanceAtStart float64
	OverageAtStart float64
}

type EntitlementConnector

type EntitlementConnector interface {
	// Entitlement Management
	CreateEntitlement(ctx context.Context, input CreateEntitlementInputs) (Entitlement, error)
	GetEntitlementsOfSubject(ctx context.Context, namespace string, subjectKey models.SubjectKey) ([]Entitlement, error)
	GetEntitlementValue(ctx context.Context, entitlementId models.NamespacedID, at time.Time) (EntitlementValue, error)
}

func NewEntitlementConnector

func NewEntitlementConnector(
	entitlementBalanceConnector EntitlementBalanceConnector,
	entitlementRepo EntitlementRepo,
	featureConnector productcatalog.FeatureConnector,
) EntitlementConnector

type EntitlementGrant

type EntitlementGrant struct {
	credit.Grant

	// "removing" fields
	OwnerID          string  `json:"-"`
	ResetMaxRollover float64 `json:"-"`

	// "adding" fields
	EntitlementID     string     `json:"entitlementId"`
	NextRecurrence    *time.Time `json:"nextRecurrence,omitempty"`
	MaxRolloverAmount float64    `json:"maxRolloverAmount"`
}

func GrantFromCreditGrant

func GrantFromCreditGrant(grant credit.Grant) (*EntitlementGrant, error)

type EntitlementNotFoundError

type EntitlementNotFoundError struct {
	EntitlementID models.NamespacedID
}

func (*EntitlementNotFoundError) Error

func (e *EntitlementNotFoundError) Error() string

type EntitlementRepo

type EntitlementRepo interface {
	// Entitlement Management
	GetEntitlementsOfSubject(ctx context.Context, namespace string, subjectKey models.SubjectKey) ([]Entitlement, error)
	CreateEntitlement(ctx context.Context, entitlement EntitlementRepoCreateEntitlementInputs) (*Entitlement, error)
	GetEntitlement(ctx context.Context, entitlementID models.NamespacedID) (*Entitlement, error)

	//FIXME: This is a terrbile hack
	LockEntitlementForTx(ctx context.Context, entitlementID models.NamespacedID) error

	entutils.TxCreator
	entutils.TxUser[EntitlementRepo]
}

type EntitlementRepoCreateEntitlementInputs

type EntitlementRepoCreateEntitlementInputs struct {
	Namespace        string
	FeatureID        string    `json:"featureId"`
	MeasureUsageFrom time.Time `json:"measureUsageFrom,omitempty"`
	SubjectKey       string    `json:"subjectKey"`
}

type EntitlementValue

type EntitlementValue struct {
	HasAccess bool    `json:"hasAccess"`
	Balance   float64 `json:"balance"`
	Usage     float64 `json:"usage"`
	Overage   float64 `json:"overage"`
}

type UsageResetNotFoundError

type UsageResetNotFoundError struct {
	EntitlementID models.NamespacedID
}

func (UsageResetNotFoundError) Error

func (e UsageResetNotFoundError) Error() string

type UsageResetRepo

type UsageResetRepo interface {
	Save(ctx context.Context, usageResetTime UsageResetTime) error
	GetLastAt(ctx context.Context, entitlementID models.NamespacedID, at time.Time) (*UsageResetTime, error)
	GetBetween(ctx context.Context, entitlementID models.NamespacedID, from time.Time, to time.Time) ([]UsageResetTime, error)

	entutils.TxCreator
	entutils.TxUser[UsageResetRepo]
}

type UsageResetTime

type UsageResetTime struct {
	models.NamespacedModel
	ResetTime     time.Time
	EntitlementID string
}

type WindowSize

type WindowSize string
const (
	// We don't support minute precision as that results in an extremely heavy calculation
	// WindowSizeMinute WindowSize = "MINUTE"
	WindowSizeHour WindowSize = "HOUR"
	WindowSizeDay  WindowSize = "DAY"
)

Jump to

Keyboard shortcuts

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