cost

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package cost defines structures that represent cloud resources and states in a cloud-agnostic, as well as tool-agnostic way. The hierarchy of structs is as follows:

- Component is the lowest level, it describes the cost of a single cloud entity (e.g. storage space or compute time).

- Resource is a collection of components and directly correlates to cloud resources (e.g. VM instance).

- State is a collection of resources that exist (or are planned to exist) at any given moment across one or multiple cloud providers.

- Plan is a difference between two states. It includes the prior (current) state and a planned state and it can be used to retrieve a list of ResourceDiff's.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrProductNotFound = fmt.Errorf("product not found")
	ErrPriceNotFound   = fmt.Errorf("price not found")
)

Errors that might be returned from NewState if either a product or a price are not found.

View Source
var HoursPerMonth = decimal.NewFromInt(730)

HoursPerMonth is an approximate number of hours in a month. It is calculated as 365 days in a year x 24 hours in a day / 12 months in year.

View Source
var Zero = Cost{}

Zero is Cost with zero value.

Functions

This section is empty.

Types

type Component

type Component struct {
	Quantity decimal.Decimal
	Unit     string
	Rate     Cost
	Details  []string

	Error error
}

Component describes the pricing of a single resource cost component. This includes Rate and Quantity and allows for final cost computation.

func (Component) Cost

func (c Component) Cost() Cost

Cost returns the cost of this component (Rate multiplied by Quantity).

type ComponentDiff

type ComponentDiff struct {
	Prior, Planned *Component
}

ComponentDiff is a difference between the Prior and Planned Component.

func (ComponentDiff) PlannedCost

func (cd ComponentDiff) PlannedCost() Cost

PlannedCost returns the full cost of the Planned Component or decimal.Zero if it doesn't exist.

func (ComponentDiff) PriorCost

func (cd ComponentDiff) PriorCost() Cost

PriorCost returns the full cost of the Prior Component or decimal.Zero if it doesn't exist.

func (ComponentDiff) Valid

func (cd ComponentDiff) Valid() bool

Valid returns true if there are no errors in both the Planned and Prior components.

type Cost

type Cost struct {
	// Decimal is price per month.
	decimal.Decimal
	// Currency of the cost.
	Currency string
}

Cost represents a monthly or hourly cost of a cloud resource or its component.

func NewHourly

func NewHourly(hourly decimal.Decimal, currency string) Cost

NewHourly returns a new Cost from price per hour with currency.

func NewMonthly

func NewMonthly(monthly decimal.Decimal, currency string) Cost

NewMonthly returns a new Cost from price per month with currency.

func (Cost) Add

func (c Cost) Add(c2 Cost) (Cost, error)

Add adds the values of two Cost structs. If the currency of both costs doesn't match, error is returned.

func (Cost) Hourly

func (c Cost) Hourly() decimal.Decimal

Hourly returns the cost per hour.

func (Cost) Monthly

func (c Cost) Monthly() decimal.Decimal

Monthly returns the cost per month.

func (Cost) MulDecimal

func (c Cost) MulDecimal(d decimal.Decimal) Cost

MulDecimal multiplies the Cost by the given decimal.Decimal.

type Plan

type Plan struct {
	Prior, Planned *State
}

Plan is the cost difference between two State instances. It is not tied to any specific cloud provider or IaC tool. Instead, it is a representation of the differences between two snapshots of cloud resources, with their associated costs. The Plan instance can be used to calculate the total cost difference of a plan, as well as cost differences of each resource (and their components) separately.

func NewPlan

func NewPlan(prior, planned *State) *Plan

NewPlan returns a new Plan from Prior and Planned State.

func (Plan) PlannedCost

func (p Plan) PlannedCost() (Cost, error)

PlannedCost returns the total cost of the Planned State or decimal.Zero if it isn't included in the plan.

func (Plan) PriorCost

func (p Plan) PriorCost() (Cost, error)

PriorCost returns the total cost of the Prior State or decimal.Zero if it isn't included in the plan.

func (Plan) ResourceDifferences

func (p Plan) ResourceDifferences() []ResourceDiff

ResourceDifferences merges the Prior and Planned State and returns a slice of differences between resources. The order of the elements in the slice is undefined and unstable.

func (Plan) SkippedAddresses

func (p Plan) SkippedAddresses() []string

SkippedAddresses returns the addresses of resources that were excluded from the estimation process. The order of the elements in the slice is undefined and unstable.

type Resource

type Resource struct {
	Provider   string
	Type       string
	Components map[string]Component
	Skipped    bool
}

Resource represents costs of a single cloud resource. Each Resource includes a Component map, keyed by the label.

func (Resource) Cost

func (re Resource) Cost() (Cost, error)

Cost returns the sum of costs of every Component of this Resource. Error is returned if there is a mismatch in Component currency.

type ResourceDiff

type ResourceDiff struct {
	Address        string
	Provider       string
	Type           string
	ComponentDiffs map[string]*ComponentDiff
}

ResourceDiff is the difference in costs between prior and planned Resource. It contains a ComponentDiff map, keyed by the label.

func (ResourceDiff) Errors

func (rd ResourceDiff) Errors() map[string]error

Errors returns a map of Component errors keyed by the Component label.

func (ResourceDiff) PlannedCost

func (rd ResourceDiff) PlannedCost() (Cost, error)

PlannedCost returns the sum of costs of every Component's PlannedCost. Error is returned if there is a mismatch between currencies of the Components.

func (ResourceDiff) PriorCost

func (rd ResourceDiff) PriorCost() (Cost, error)

PriorCost returns the sum of costs of every Component's PriorCost. Error is returned if there is a mismatch between currencies of the Components.

func (ResourceDiff) Valid

func (rd ResourceDiff) Valid() bool

Valid returns true if there are no errors in all of the ResourceDiff's components.

type State

type State struct {
	Resources map[string]Resource
}

State represents a collection of all the Resource costs (either prior or planned.) It is not tied to any specific cloud provider or IaC tool. Instead, it is a representation of a snapshot of cloud resources at a given point in time, with their associated costs.

func NewState

func NewState(ctx context.Context, backend backend.Backend, queries []query.Resource) (*State, error)

NewState returns a new State from a query.Resource slice by using the Backend to fetch the pricing data.

func (*State) Cost

func (s *State) Cost() (Cost, error)

Cost returns the sum of the costs of every Resource included in this State. Error is returned if there is a mismatch in resource currencies.

Jump to

Keyboard shortcuts

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