handler

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package provider defines provider-agnostic interfaces for cloud cost estimation. AWS, GCP, Azure handlers all implement these interfaces.

Index

Constants

View Source
const HoursPerMonth = 730

HoursPerMonth is the average number of hours in a month for cost calculations.

Variables

This section is empty.

Functions

func FixedMonthlyCost

func FixedMonthlyCost(monthly float64) (hourly, monthly2 float64)

FixedMonthlyCost returns hourly and monthly costs from a fixed monthly rate.

func GetBoolAttr

func GetBoolAttr(attrs map[string]any, key string) bool

GetBoolAttr extracts a bool attribute from a resource attributes map.

func GetFloatAttr

func GetFloatAttr(attrs map[string]any, key string) float64

GetFloatAttr extracts a float64 attribute from a resource attributes map.

func GetIntAttr

func GetIntAttr(attrs map[string]any, key string) int

GetIntAttr extracts an int attribute from a resource attributes map.

func GetStringAttr

func GetStringAttr(attrs map[string]any, key string) string

GetStringAttr extracts a string attribute from a resource attributes map.

func HourlyCost

func HourlyCost(pricePerHour float64) (hourly, monthly float64)

HourlyCost returns hourly and monthly costs from an hourly rate.

func LogUnsupported

func LogUnsupported(resourceType, address string)

LogUnsupported logs unsupported resource types at debug level.

func ScaledHourlyCost

func ScaledHourlyCost(pricePerHour float64, count int) (hourly, monthly float64)

ScaledHourlyCost returns hourly and monthly costs for multiple units at an hourly rate.

Types

type CompoundHandler

type CompoundHandler interface {
	SubResources(attrs map[string]any) []SubResource
}

CompoundHandler is implemented by handlers that produce additional sub-resource costs. The estimator dispatches each SubResource to the appropriate handler.

type CostCategory

type CostCategory int

CostCategory classifies how a handler calculates costs.

const (
	// CostCategoryStandard requires pricing API lookup.
	CostCategoryStandard CostCategory = iota
	// CostCategoryFixed uses hardcoded costs (no API call needed).
	CostCategoryFixed
	// CostCategoryUsageBased is usage-based pricing (returns $0 for fixed estimates).
	CostCategoryUsageBased
)

type Describer added in v0.9.0

type Describer interface {
	// Describe returns human-readable resource details.
	// price may be nil for Fixed/UsageBased handlers or before API lookup.
	Describe(price *pricing.Price, attrs map[string]any) map[string]string
}

Describer is implemented by handlers that expose human-readable resource details.

type LookupBuilder added in v0.9.0

type LookupBuilder interface {
	// BuildLookup creates a PriceLookup from terraform resource attributes.
	BuildLookup(region string, attrs map[string]any) (*pricing.PriceLookup, error)
}

LookupBuilder is implemented by handlers that require a pricing lookup.

type Registry

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

Registry maps terraform resource types to cost estimation handlers. Provider-agnostic: AWS, GCP, Azure handlers all register here.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new empty resource registry.

func (*Registry) IsSupported

func (r *Registry) IsSupported(resourceType ResourceType) bool

IsSupported checks if a resource type is supported.

func (*Registry) Register

func (r *Registry) Register(providerID string, resourceType ResourceType, handler ResourceHandler)

Register adds a handler for a resource type.

func (*Registry) ResolveHandler added in v0.9.0

func (r *Registry) ResolveHandler(providerID string, resourceType ResourceType) (ResourceHandler, bool)

ResolveHandler returns a handler scoped to a provider and resource type.

func (*Registry) SupportedTypes

func (r *Registry) SupportedTypes() []string

SupportedTypes returns all supported resource types.

type ResourceHandler

type ResourceHandler interface {
	// Category returns how this handler calculates costs.
	Category() CostCategory
	// CalculateCost calculates monthly cost from price, index, and resource attributes.
	// For Fixed/UsageBased handlers, price and index may be nil.
	CalculateCost(price *pricing.Price, index *pricing.PriceIndex, region string, attrs map[string]any) (hourly, monthly float64)
}

ResourceHandler extracts pricing information from terraform resource attributes. Implemented by each cloud provider's resource handlers.

type ResourceType added in v0.9.0

type ResourceType string

ResourceType is a provider-neutral Terraform resource identifier.

func (ResourceType) String added in v0.9.0

func (r ResourceType) String() string

String returns the raw Terraform resource type value.

type RuntimeDeps added in v0.9.0

type RuntimeDeps[T any] struct {
	Runtime *T
}

RuntimeDeps stores an optional provider runtime dependency for a handler. Zero value is valid and can be paired with a fallback in tests.

func (RuntimeDeps[T]) RuntimeOr added in v0.9.0

func (d RuntimeDeps[T]) RuntimeOr(fallback *T) *T

RuntimeOr returns the injected runtime or the provided fallback when unset.

type SubResource

type SubResource struct {
	Suffix string         // Address suffix, e.g., "/root_volume"
	Type   ResourceType   // Resource type for handler lookup, e.g., "aws_ebs_volume"
	Attrs  map[string]any // Translated attributes for the sub-resource handler
}

SubResource represents a virtual sub-resource synthesized from a parent resource's inline attributes (e.g., root_block_device inside aws_instance → aws_ebs_volume).

Jump to

Keyboard shortcuts

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