aws

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package aws provides AWS Marketplace integration for Zerfoo Cloud, including metering, subscription lifecycle management, entitlement verification, and token-based billing.

Index

Constants

View Source
const DimensionTokens = "tokens_1m"

DimensionTokens is the metering dimension name for token-based billing.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchMeterUsageInput

type BatchMeterUsageInput struct {
	ProductCode  string        `json:"productCode"`
	UsageRecords []UsageRecord `json:"usageRecords"`
}

BatchMeterUsageInput is the input for BatchMeterUsage.

type BatchMeterUsageOutput

type BatchMeterUsageOutput struct {
	Results            []UsageRecordResult `json:"results"`
	UnprocessedRecords []UsageRecord       `json:"unprocessedRecords"`
}

BatchMeterUsageOutput is the output of BatchMeterUsage.

type Entitlement

type Entitlement struct {
	CustomerIdentifier string    `json:"customerIdentifier"`
	ProductCode        string    `json:"productCode"`
	Dimension          string    `json:"dimension"`
	Value              int       `json:"value"`
	ExpiresAt          time.Time `json:"expiresAt"`
}

Entitlement represents a customer's entitlement to use the product.

type EntitlementChecker

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

EntitlementChecker verifies customer entitlements with caching.

func NewEntitlementChecker

func NewEntitlementChecker(store EntitlementStore, cacheTTL time.Duration) *EntitlementChecker

NewEntitlementChecker creates an EntitlementChecker with the given store and cache TTL.

func (*EntitlementChecker) GrantEntitlement

func (c *EntitlementChecker) GrantEntitlement(ctx context.Context, ent Entitlement) error

GrantEntitlement stores an entitlement for a customer.

func (*EntitlementChecker) Invalidate

func (c *EntitlementChecker) Invalidate(customerIdentifier, dimension string)

Invalidate removes a cached entitlement entry, forcing the next check to query the store.

func (*EntitlementChecker) IsEntitled

func (c *EntitlementChecker) IsEntitled(ctx context.Context, customerIdentifier, dimension string) (bool, error)

IsEntitled checks whether a customer is entitled to use a given dimension. Results are cached for the configured TTL.

func (*EntitlementChecker) RevokeEntitlement

func (c *EntitlementChecker) RevokeEntitlement(ctx context.Context, customerIdentifier, dimension string) error

RevokeEntitlement removes an entitlement and invalidates the cache.

type EntitlementStore

type EntitlementStore interface {
	Put(ctx context.Context, ent Entitlement) error
	Get(ctx context.Context, customerIdentifier, dimension string) (*Entitlement, error)
	List(ctx context.Context, customerIdentifier string) ([]Entitlement, error)
	Delete(ctx context.Context, customerIdentifier, dimension string) error
}

EntitlementStore persists entitlement records.

type MemoryEntitlementStore

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

MemoryEntitlementStore is an in-memory EntitlementStore for testing.

func NewMemoryEntitlementStore

func NewMemoryEntitlementStore() *MemoryEntitlementStore

NewMemoryEntitlementStore creates a new in-memory entitlement store.

func (*MemoryEntitlementStore) Delete

func (s *MemoryEntitlementStore) Delete(_ context.Context, customerIdentifier, dimension string) error

Delete removes an entitlement.

func (*MemoryEntitlementStore) Get

func (s *MemoryEntitlementStore) Get(_ context.Context, customerIdentifier, dimension string) (*Entitlement, error)

Get retrieves an entitlement.

func (*MemoryEntitlementStore) List

func (s *MemoryEntitlementStore) List(_ context.Context, customerIdentifier string) ([]Entitlement, error)

List returns all entitlements for a customer.

func (*MemoryEntitlementStore) Put

Put stores an entitlement.

type MemorySubscriptionStore

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

MemorySubscriptionStore is an in-memory SubscriptionStore for testing.

func NewMemorySubscriptionStore

func NewMemorySubscriptionStore() *MemorySubscriptionStore

NewMemorySubscriptionStore creates a new in-memory subscription store.

func (*MemorySubscriptionStore) Delete

func (s *MemorySubscriptionStore) Delete(_ context.Context, customerIdentifier string) error

Delete removes a subscription by customer identifier.

func (*MemorySubscriptionStore) Get

func (s *MemorySubscriptionStore) Get(_ context.Context, customerIdentifier string) (*Subscription, error)

Get retrieves a subscription by customer identifier.

func (*MemorySubscriptionStore) List

List returns all subscriptions with the given status.

func (*MemorySubscriptionStore) Put

Put stores a subscription.

type MeterUsageInput

type MeterUsageInput struct {
	ProductCode      string            `json:"productCode"`
	Dimension        string            `json:"dimension"`
	Quantity         int               `json:"quantity"`
	Timestamp        time.Time         `json:"timestamp"`
	UsageAllocations []UsageAllocation `json:"usageAllocations,omitempty"`
}

MeterUsageInput is the input for MeterUsage.

type MeterUsageOutput

type MeterUsageOutput struct {
	MeteringRecordID string `json:"meteringRecordId"`
}

MeterUsageOutput is the output of MeterUsage.

type MeteringAPI

type MeteringAPI interface {
	// ResolveCustomer resolves a registration token to a customer identifier
	// and product code. Called when a customer subscribes via AWS Marketplace.
	ResolveCustomer(ctx context.Context, registrationToken string) (*ResolveCustomerOutput, error)

	// BatchMeterUsage submits a batch of usage records for metered billing.
	BatchMeterUsage(ctx context.Context, input *BatchMeterUsageInput) (*BatchMeterUsageOutput, error)

	// MeterUsage submits a single usage record for metered billing.
	MeterUsage(ctx context.Context, input *MeterUsageInput) (*MeterUsageOutput, error)
}

MeteringAPI abstracts AWS Marketplace Metering Service API calls.

type MeteringClient

type MeteringClient struct {
	// Endpoint is the AWS Marketplace Metering Service endpoint URL.
	Endpoint string
	// Signer signs HTTP requests with AWS credentials.
	Signer RequestSigner
	// HTTPClient is the HTTP client used for API calls.
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

MeteringClient implements MeteringAPI by calling the AWS Marketplace Metering Service REST endpoints. It signs requests using the provided Signer interface.

func NewMeteringClient

func NewMeteringClient(endpoint string, signer RequestSigner) *MeteringClient

NewMeteringClient creates a new MeteringClient with the given endpoint and signer.

func (*MeteringClient) BatchMeterUsage

func (c *MeteringClient) BatchMeterUsage(ctx context.Context, input *BatchMeterUsageInput) (*BatchMeterUsageOutput, error)

BatchMeterUsage submits a batch of usage records.

func (*MeteringClient) MeterUsage

func (c *MeteringClient) MeterUsage(ctx context.Context, input *MeterUsageInput) (*MeterUsageOutput, error)

MeterUsage submits a single usage record.

func (*MeteringClient) ResolveCustomer

func (c *MeteringClient) ResolveCustomer(ctx context.Context, registrationToken string) (*ResolveCustomerOutput, error)

ResolveCustomer resolves a registration token to a customer identifier.

type RequestSigner

type RequestSigner interface {
	Sign(req *http.Request) error
}

RequestSigner signs an HTTP request with AWS credentials.

type ResolveCustomerOutput

type ResolveCustomerOutput struct {
	CustomerIdentifier string `json:"customerIdentifier"`
	ProductCode        string `json:"productCode"`
}

ResolveCustomerOutput contains the result of resolving a registration token.

type Subscription

type Subscription struct {
	CustomerIdentifier string             `json:"customerIdentifier"`
	ProductCode        string             `json:"productCode"`
	Status             SubscriptionStatus `json:"status"`
	SubscribedAt       time.Time          `json:"subscribedAt"`
	UnsubscribedAt     *time.Time         `json:"unsubscribedAt,omitempty"`
}

Subscription represents an AWS Marketplace SaaS subscription.

type SubscriptionManager

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

SubscriptionManager handles the SaaS subscription lifecycle.

func NewSubscriptionManager

func NewSubscriptionManager(store SubscriptionStore, metering MeteringAPI) *SubscriptionManager

NewSubscriptionManager creates a SubscriptionManager backed by the given store and metering API client.

func (*SubscriptionManager) IsActive

func (m *SubscriptionManager) IsActive(ctx context.Context, customerIdentifier string) (bool, error)

IsActive checks whether a customer has an active subscription.

func (*SubscriptionManager) Subscribe

func (m *SubscriptionManager) Subscribe(ctx context.Context, registrationToken string) (*Subscription, error)

Subscribe processes a new subscription by resolving the registration token and storing the subscription record.

func (*SubscriptionManager) Unsubscribe

func (m *SubscriptionManager) Unsubscribe(ctx context.Context, customerIdentifier string) error

Unsubscribe marks a subscription as unsubscribed.

type SubscriptionStatus

type SubscriptionStatus string

SubscriptionStatus represents the lifecycle state of a SaaS subscription.

const (
	SubscriptionActive       SubscriptionStatus = "active"
	SubscriptionPending      SubscriptionStatus = "pending"
	SubscriptionUnsubscribed SubscriptionStatus = "unsubscribed"
	SubscriptionExpired      SubscriptionStatus = "expired"
)

type SubscriptionStore

type SubscriptionStore interface {
	// Put stores or updates a subscription.
	Put(ctx context.Context, sub Subscription) error
	// Get retrieves a subscription by customer identifier.
	Get(ctx context.Context, customerIdentifier string) (*Subscription, error)
	// List returns all subscriptions with the given status.
	List(ctx context.Context, status SubscriptionStatus) ([]Subscription, error)
	// Delete removes a subscription by customer identifier.
	Delete(ctx context.Context, customerIdentifier string) error
}

SubscriptionStore persists subscription records.

type TokenBillingTracker

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

TokenBillingTracker accumulates token usage per customer and flushes metering records to AWS Marketplace in batches.

func NewTokenBillingTracker

func NewTokenBillingTracker(productCode string, metering MeteringAPI) *TokenBillingTracker

NewTokenBillingTracker creates a new TokenBillingTracker for the given product.

func (*TokenBillingTracker) Flush

Flush sends accumulated usage to AWS Marketplace via BatchMeterUsage and resets the accumulators. Usage is billed per 1M tokens.

func (*TokenBillingTracker) RecordUsage

func (t *TokenBillingTracker) RecordUsage(customerIdentifier string, inputTokens, outputTokens int64)

RecordUsage adds token usage for a customer.

func (*TokenBillingTracker) Snapshot

func (t *TokenBillingTracker) Snapshot() []TokenUsageRecord

Snapshot returns the current accumulated usage per customer without resetting the accumulators.

type TokenUsageRecord

type TokenUsageRecord struct {
	CustomerIdentifier string    `json:"customerIdentifier"`
	InputTokens        int64     `json:"inputTokens"`
	OutputTokens       int64     `json:"outputTokens"`
	TotalTokens        int64     `json:"totalTokens"`
	Timestamp          time.Time `json:"timestamp"`
}

TokenUsageRecord tracks token usage for a customer within a billing period.

type UsageAllocation

type UsageAllocation struct {
	AllocatedUsageQuantity int               `json:"allocatedUsageQuantity"`
	Tags                   map[string]string `json:"tags,omitempty"`
}

UsageAllocation allows tagging usage with allocation metadata.

type UsageRecord

type UsageRecord struct {
	CustomerIdentifier string    `json:"customerIdentifier"`
	Dimension          string    `json:"dimension"`
	Quantity           int       `json:"quantity"`
	Timestamp          time.Time `json:"timestamp"`
}

UsageRecord represents a single metering record for AWS Marketplace.

type UsageRecordResult

type UsageRecordResult struct {
	UsageRecord    UsageRecord `json:"usageRecord"`
	MeteringStatus string      `json:"meteringStatus"`
}

UsageRecordResult contains the result of processing a single usage record.

Jump to

Keyboard shortcuts

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