Documentation
¶
Overview ¶
Package commitmentopts discovers which (term, payment) commitment tuples each AWS commitment-capable service actually sells, persists the result, and exposes it to the API layer so the frontend can hide impossible combinations instead of hardcoding them.
The package never blocks a save on missing data: if no probe has been persisted yet (or the probe failed), Service.Validate falls back to permissive-true and the frontend's hardcoded rules still gate the UI.
Index ¶
- Variables
- func ValidateCombo(opts Options, provider, service string, term int, payment string) bool
- type AccountLister
- type AzureCredentialFn
- type AzureCurrencyFn
- type AzureSPProber
- type AzureSPValidateAPI
- type BuildConfigFn
- type Combo
- type EC2DescribeOfferings
- type EC2Prober
- type ElastiCacheDescribeOfferings
- type ElastiCacheProber
- type MemoryDBDescribeOfferings
- type MemoryDBProber
- type OpenSearchDescribeOfferings
- type OpenSearchProber
- type Options
- type PostgresStore
- func (s *PostgresStore) Get(ctx context.Context) (Options, bool, error)
- func (s *PostgresStore) HasData(ctx context.Context, provider string) (bool, error)
- func (s *PostgresStore) ProbedAt(ctx context.Context, provider string) (time.Time, bool, error)
- func (s *PostgresStore) Replace(ctx context.Context, provider string, combos []Combo, sourceAccountID string) error
- func (s *PostgresStore) Save(ctx context.Context, provider string, combos []Combo, sourceAccountID string) error
- type Prober
- type RDSDescribeOfferings
- type RDSProber
- type RedshiftDescribeOfferings
- type RedshiftProber
- type SavingsPlansDescribeOfferings
- type SavingsPlansProber
- type Service
- func (s *Service) CachedOptions(ctx context.Context, provider string) (byService map[string][]Combo, ok bool, err error)
- func (s *Service) FreshAzureOptions(ctx context.Context) (byService map[string][]Combo, ok bool, err error)
- func (s *Service) Get(ctx context.Context) (Options, error)
- func (s *Service) RefreshAzure(ctx context.Context) error
- func (s *Service) Validate(ctx context.Context, provider, service string, term int, payment string) (bool, error)
- func (s *Service) WithAzure(credential AzureCredentialFn, currency AzureCurrencyFn, ...) *Service
- type Store
Constants ¶
This section is empty.
Variables ¶
var ErrNoData = errors.New("commitment options unavailable")
ErrNoData signals "we don't have cached commitment options and can't produce them right now" (no AWS account connected, or a probe failed). Callers MUST NOT treat this as a hard error — the API handler maps it to `{"status":"unavailable"}` and Validate maps it to permissive-true.
Functions ¶
func ValidateCombo ¶
ValidateCombo reports whether (provider, service, term, payment) is a legal combination according to opts, a snapshot returned by Get. It is pure: no database or network access, so it is safe to call inside a transaction.
It is permissive where opts has nothing to say: a provider with no combos, or a service the probe returned no combos for, is treated as valid.
Types ¶
type AccountLister ¶
type AccountLister interface {
ListCloudAccounts(ctx context.Context, filter config.CloudAccountFilter) ([]config.CloudAccount, error)
}
AccountLister is the narrow subset of config.StoreInterface that Service needs. Defining it here (rather than depending on the full interface) keeps tests small and avoids a circular import when the API layer wires up the real store.
type AzureCredentialFn ¶
type AzureCredentialFn func(ctx context.Context, account *config.CloudAccount) (azcore.TokenCredential, error)
AzureCredentialFn resolves the token credential for an Azure account.
type AzureCurrencyFn ¶
AzureCurrencyFn returns the billing currency (3-letter ISO 4217 code) of an Azure account. It must fail rather than guess: Azure validates a purchase in the billing account's currency.
type AzureSPProber ¶
type AzureSPProber struct {
// NewClient builds an AzureSPValidateAPI from a credential. Override
// in tests to return a mock.
NewClient func(cred azcore.TokenCredential) (AzureSPValidateAPI, error)
}
AzureSPProber probes the Azure Billing Benefits ValidatePurchase endpoint for each candidate (term, payment) combination and returns those the API accepts as live Combos.
Design notes:
- Azure has no "list offerings" catalog endpoint comparable to AWS Describe*Offerings. ValidatePurchase is the closest live signal that a given (term, billingPlan) configuration is currently accepted.
- The probe validates against the caller's real subscription with the minimum non-zero hourly commitment; ValidatePurchase never creates a purchase, so no billing impact occurs.
- A 4xx answer (other than 401, 403, 404, 408 and 429) for a 5-year combo means Azure does not sell it: the combo is dropped and the probe continues, but a probe that ends with no accepted combo at all is an error. Any other error (a 4xx on a 1- or 3-year combo, auth, throttling, 5xx, network, canceled context) is a probe failure and is bubbled up so the Service can apply its all-or-nothing policy.
func DefaultAzureProbers ¶
func DefaultAzureProbers() []*AzureSPProber
DefaultAzureProbers returns one prober instance for the Azure Savings Plans service. The Service wires these up by default for Azure probe runs.
func (*AzureSPProber) ProbeAzure ¶
func (p *AzureSPProber) ProbeAzure(ctx context.Context, cred azcore.TokenCredential, subscriptionID, currencyCode string) ([]Combo, error)
ProbeAzure probes the Azure ValidatePurchase endpoint for each candidate (term, billingPlan) combo and returns Combos for those the API accepts.
subscriptionID is the Azure subscription the probe is scoped to; it must be a real one (an enabled Azure account's), never a placeholder, so an empty value fails closed before any client is built.
currencyCode is the billing currency of the account the probe runs against (a 3-letter ISO 4217 code such as "USD" or "EUR"). Azure evaluates a ValidatePurchase request against the billing account's currency, so a probe sent in any other currency yields a verdict about a commitment the account could never buy. The caller must resolve the account's real currency; there is no USD default. An empty or malformed code fails closed before any client is built or any request is sent.
The method signature differs from the AWS Prober interface because Azure authentication uses azcore.TokenCredential rather than aws.Config. Callers use this method directly; Service.probeAndPersistAzure wires it up.
func (*AzureSPProber) Service ¶
func (p *AzureSPProber) Service() string
Service returns "savingsplans".
type AzureSPValidateAPI ¶
type AzureSPValidateAPI interface {
ValidatePurchase(
ctx context.Context,
body armbillingbenefits.SavingsPlanPurchaseValidateRequest,
options *armbillingbenefits.RPClientValidatePurchaseOptions,
) (armbillingbenefits.RPClientValidatePurchaseResponse, error)
}
AzureSPValidateAPI is the minimal Azure Billing Benefits surface the probe needs. It matches the ValidatePurchase method on *armbillingbenefits.RPClient so tests can substitute a mock without importing the concrete SDK client.
NOTE: providers/azure/services/savingsplans.RPValidateAPI defines the same one-method interface. The duplication is intentional: importing the provider package from internal/commitmentopts would create a circular dependency. The interface is tiny enough that duplicating it here is the cleanest option.
type BuildConfigFn ¶
BuildConfigFn resolves an aws.Config for a given CloudAccount. The real wiring uses credentials.ResolveAWSCredentialProvider; tests substitute a stub that returns a zero aws.Config.
type Combo ¶
Combo is one (provider, service, term, payment) tuple harvested from the reserved-offerings APIs. TermYears is 1 or 3; Payment is one of "all-upfront", "partial-upfront", "no-upfront". Other durations and legacy utilization-style payment options are dropped during normalization.
type EC2DescribeOfferings ¶
type EC2DescribeOfferings interface {
DescribeReservedInstancesOfferings(ctx context.Context, params *ec2.DescribeReservedInstancesOfferingsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeReservedInstancesOfferingsOutput, error)
}
EC2DescribeOfferings is the minimal EC2 surface we use.
type EC2Prober ¶
type EC2Prober struct {
NewClient func(cfg aws.Config) EC2DescribeOfferings
}
EC2Prober probes ec2:DescribeReservedInstancesOfferings.
type ElastiCacheDescribeOfferings ¶
type ElastiCacheDescribeOfferings interface {
DescribeReservedCacheNodesOfferings(ctx context.Context, params *elasticache.DescribeReservedCacheNodesOfferingsInput, optFns ...func(*elasticache.Options)) (*elasticache.DescribeReservedCacheNodesOfferingsOutput, error)
}
ElastiCacheDescribeOfferings is the minimal ElastiCache surface we use.
type ElastiCacheProber ¶
type ElastiCacheProber struct {
NewClient func(cfg aws.Config) ElastiCacheDescribeOfferings
}
ElastiCacheProber probes elasticache:DescribeReservedCacheNodesOfferings.
func (*ElastiCacheProber) Service ¶
func (p *ElastiCacheProber) Service() string
Service returns "elasticache".
type MemoryDBDescribeOfferings ¶
type MemoryDBDescribeOfferings interface {
DescribeReservedNodesOfferings(ctx context.Context, params *memorydb.DescribeReservedNodesOfferingsInput, optFns ...func(*memorydb.Options)) (*memorydb.DescribeReservedNodesOfferingsOutput, error)
}
MemoryDBDescribeOfferings is the minimal MemoryDB surface we use.
type MemoryDBProber ¶
type MemoryDBProber struct {
NewClient func(cfg aws.Config) MemoryDBDescribeOfferings
}
MemoryDBProber probes memorydb:DescribeReservedNodesOfferings.
func (*MemoryDBProber) Service ¶
func (p *MemoryDBProber) Service() string
Service returns "memorydb".
type OpenSearchDescribeOfferings ¶
type OpenSearchDescribeOfferings interface {
DescribeReservedInstanceOfferings(ctx context.Context, params *opensearch.DescribeReservedInstanceOfferingsInput, optFns ...func(*opensearch.Options)) (*opensearch.DescribeReservedInstanceOfferingsOutput, error)
}
OpenSearchDescribeOfferings is the minimal OpenSearch surface we use. The OpenSearch API has no per-instance-type filter on this endpoint, so the probe filters client-side after fetching.
type OpenSearchProber ¶
type OpenSearchProber struct {
NewClient func(cfg aws.Config) OpenSearchDescribeOfferings
}
OpenSearchProber probes opensearch:DescribeReservedInstanceOfferings.
func (*OpenSearchProber) Service ¶
func (p *OpenSearchProber) Service() string
Service returns "opensearch".
type Options ¶
Options groups valid combos by provider and service. Shape:
Options["aws"]["rds"] = []Combo{...}
Only AWS is ever populated today — Azure and GCP stay hardcoded in the frontend because their commitment APIs don't have an equivalent probe.
type PostgresStore ¶
type PostgresStore struct {
// contains filtered or unexported fields
}
PostgresStore is the Postgres-backed commitmentopts.Store implementation.
func NewPostgresStore ¶
func NewPostgresStore(db *database.Connection) *PostgresStore
NewPostgresStore returns a store backed by the given connection. The connection must already have the 000176 migration applied.
func (*PostgresStore) Get ¶
Get returns the persisted combos grouped by provider and service, limited to providers that have a probe run row, plus a boolean that is true iff any run row exists. The combos map may be empty even when the boolean is true — that legitimately means "we probed and nothing matched our normalizer".
func (*PostgresStore) ProbedAt ¶
ProbedAt returns provider's probe run time; the bool is false without a row.
func (*PostgresStore) Replace ¶
func (s *PostgresStore) Replace(ctx context.Context, provider string, combos []Combo, sourceAccountID string) error
Replace overwrites provider's run row (new probed_at and source) and swaps its combos for the given ones in one transaction, so a reader never sees the provider with no combos and a failed write keeps the previous snapshot.
func (*PostgresStore) Save ¶
func (s *PostgresStore) Save(ctx context.Context, provider string, combos []Combo, sourceAccountID string) error
Save persists provider's probe run row and every combo transactionally. If the provider's run row already exists (a second process raced us to persist), the first row's probed_at and source win, but this call's combos are still added next to the existing ones (ON CONFLICT DO NOTHING). Replace is the path that overwrites the row and swaps the combos. Other providers' rows and combos are never touched.
type Prober ¶
type Prober interface {
// Service returns the canonical service name used as the Options key
// and persisted in commitment_options_combos.service.
Service() string
// Probe issues paginated Describe*Offerings calls against a canonical
// small instance type, normalizes the results, and returns unique
// Combos. Errors bubble up — the orchestrating Service treats ANY
// probe failure as "don't persist" (all-or-nothing).
Probe(ctx context.Context, cfg aws.Config) ([]Combo, error)
}
Prober probes a single AWS service's reserved-offerings API and returns every unique (term, payment) tuple it exposes. Implementations live in probe.go, one per service.
func DefaultProbers ¶
func DefaultProbers() []Prober
DefaultProbers returns one prober instance per commitment-capable service. The Service wires these up by default; tests override via a custom slice.
type RDSDescribeOfferings ¶
type RDSDescribeOfferings interface {
DescribeReservedDBInstancesOfferings(ctx context.Context, params *rds.DescribeReservedDBInstancesOfferingsInput, optFns ...func(*rds.Options)) (*rds.DescribeReservedDBInstancesOfferingsOutput, error)
}
RDSDescribeOfferings is the minimal RDS surface the probe needs. It matches the method signature on the generated client so tests can substitute a mock without dragging in the full RDSAPI interface.
type RDSProber ¶
type RDSProber struct {
// NewClient builds a client from the probe's aws.Config. Override in
// tests to return a mock.
NewClient func(cfg aws.Config) RDSDescribeOfferings
}
RDSProber probes rds:DescribeReservedDBInstancesOfferings.
type RedshiftDescribeOfferings ¶
type RedshiftDescribeOfferings interface {
DescribeReservedNodeOfferings(ctx context.Context, params *redshift.DescribeReservedNodeOfferingsInput, optFns ...func(*redshift.Options)) (*redshift.DescribeReservedNodeOfferingsOutput, error)
}
RedshiftDescribeOfferings is the minimal Redshift surface we use. The API has no NodeType filter on DescribeReservedNodeOfferings so the probe filters client-side.
type RedshiftProber ¶
type RedshiftProber struct {
NewClient func(cfg aws.Config) RedshiftDescribeOfferings
}
RedshiftProber probes redshift:DescribeReservedNodeOfferings.
func (*RedshiftProber) Service ¶
func (p *RedshiftProber) Service() string
Service returns "redshift".
type SavingsPlansDescribeOfferings ¶
type SavingsPlansDescribeOfferings interface {
DescribeSavingsPlansOfferings(ctx context.Context, params *savingsplans.DescribeSavingsPlansOfferingsInput, optFns ...func(*savingsplans.Options)) (*savingsplans.DescribeSavingsPlansOfferingsOutput, error)
}
SavingsPlansDescribeOfferings is the minimal Savings Plans surface the probe needs. It matches the relevant method on the generated client so tests can substitute a mock without dragging in the full SavingsPlansAPI interface.
type SavingsPlansProber ¶
type SavingsPlansProber struct {
// NewClient builds a client from the probe's aws.Config. Override in
// tests to return a mock.
NewClient func(cfg aws.Config) SavingsPlansDescribeOfferings
}
SavingsPlansProber probes savingsplans:DescribeSavingsPlansOfferings once per plan type (Compute, EC2Instance, SageMaker, Database) and emits per-product service keys. The probe runs all four plan types in sequence and returns all combos; Service.probeAndPersist fans them out concurrently alongside the RI probers. Any plan type that returns zero offerings for a given account is silently skipped — the frontend falls back to its hardcoded rules for that key.
func (*SavingsPlansProber) Probe ¶
Probe calls DescribeSavingsPlansOfferings for each of the four plan types and returns combos keyed with the per-product service slug. Empty results for a plan type are silently dropped so the caller never stores a zero-combo entry that would incorrectly suppress the fallback.
func (*SavingsPlansProber) Service ¶
func (p *SavingsPlansProber) Service() string
Service returns "savings-plans" — a sentinel used only for error wrapping. Each plan type emits its own per-product service key via Probe; the umbrella name is never stored.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service orchestrates probe-and-cache of AWS commitment options.
Concurrency model:
- Get is DB-first. The DB lookup is lock-free.
- On a cache miss, Get acquires mu, re-checks the DB under the lock (to absorb a concurrent writer), then probes. The lock is per- process ; the provider PK on commitment_options_probe_runs handles cross-process races.
- Probes run concurrently under errgroup. Any probe error aborts the whole attempt and NOTHING is persisted — we never want a partial snapshot to set the "cache warm" sentinel.
func New ¶
func New(store Store, accounts AccountLister, buildConfig BuildConfigFn, probers []Prober) *Service
New returns a Service wired to the given dependencies. Pass DefaultProbers() for the real probe set; tests inject stubs.
func (*Service) CachedOptions ¶
func (s *Service) CachedOptions(ctx context.Context, provider string) (byService map[string][]Combo, ok bool, err error)
CachedOptions returns provider's stored combos grouped by service, reading the store only: it never lists accounts, resolves credentials or probes, so it is safe on a request path. ok is false when provider has no probe run row. Options are global to the deployment (the store has no tenant key).
func (*Service) FreshAzureOptions ¶
func (s *Service) FreshAzureOptions(ctx context.Context) (byService map[string][]Combo, ok bool, err error)
FreshAzureOptions returns the stored Azure combos by service only while the row is younger than azureOptionsTTL, reading the store alone. ok is false for an absent or stale row, and without WithAzure, so callers treat the provider as unprobed instead of gating on data that may have gone out of date.
func (*Service) Get ¶
Get returns cached Options if they exist, otherwise probes. On any failure (no AWS account connected, probe error) it returns ErrNoData without persisting; callers map that to the unavailable status.
func (*Service) RefreshAzure ¶
RefreshAzure probes Azure and persists the azure probe run row unless a fresh one (younger than azureOptionsTTL) exists already; a stale row is replaced only by a successful probe. It is a no-op without WithAzure or without an enabled Azure account. It is deliberately not part of Get, so AWS requests never wait on Azure. The scheduler calls it, best effort, after a collection run.
The run is bounded by azureRefreshTimeout. A failure is remembered for azureFailureTTL and returned again, without any Azure call, until it expires; a failure caused by the caller canceling its own context is not. A failure persists nothing and never touches the AWS row.
func (*Service) Validate ¶
func (s *Service) Validate(ctx context.Context, provider, service string, term int, payment string) (bool, error)
Validate reports whether (provider, service, term, payment) is a legal combination according to the cached probe data.
Fallback behavior: if no probe data exists (the server has never successfully probed) Validate returns true so saves aren't blocked when we can't verify. The frontend's hardcoded rules are the user-facing gate; this check is belt-and-braces.
Validate reads the store through its own pool connections and can probe AWS on a cold cache, so it must not run while the caller holds a pooled transaction. Such callers fetch the Options with Get first and check them with ValidateCombo inside the transaction.
func (*Service) WithAzure ¶
func (s *Service) WithAzure(credential AzureCredentialFn, currency AzureCurrencyFn, probers []*AzureSPProber) *Service
WithAzure enables the Azure probe. Call it once, before the Service is shared between goroutines. It only makes RefreshAzure available: Get never touches Azure.
type Store ¶
type Store interface {
// Get returns the cached Options of every provider with a probe run
// row, and a boolean indicating whether any probe run row exists. The boolean is the authoritative "cache warm"
// signal — an empty combos slice plus a present run row means "we
// probed, AWS returned nothing matching our normalizer", and callers
// should NOT re-probe.
Get(ctx context.Context) (Options, bool, error)
// Save writes provider's probe run row and combos atomically. If the
// provider's run row already exists (concurrent writer won the race),
// the row is kept rather than raising an error. Other providers' rows
// and combos are left untouched.
Save(ctx context.Context, provider string, combos []Combo, sourceAccountID string) error
// HasData reports whether a probe run row exists for provider. Used as
// the per-provider "cache warm" check before acquiring the prober lock.
HasData(ctx context.Context, provider string) (bool, error)
// ProbedAt returns when provider's probe run row was written, and false
// when no row exists.
ProbedAt(ctx context.Context, provider string) (time.Time, bool, error)
// Replace atomically swaps provider's probe run row and combos for the
// given ones, stamping probed_at with the current time. Unlike Save it
// overwrites an existing row. Other providers are left untouched.
Replace(ctx context.Context, provider string, combos []Combo, sourceAccountID string) error
}
Store persists probe results. Implementations must treat the combos table as idempotent (ON CONFLICT DO NOTHING) so concurrent writers don't trip each other.