Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: AzureRM implementation #63

Merged
merged 6 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Google simple implementation with support for `compute_instances`
([Issue #2](https://github.com/cycloidio/terracost/issues/2))
- AzureRM simple implementation with support for `virtual_machine`(just linux) and `linux_virtual_machine`
([Issue #3](https://github.com/cycloidio/terracost/issues/3))

### Changed

Expand Down
17 changes: 17 additions & 0 deletions azurerm/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package azurerm

import "github.com/cycloidio/terracost/price"

// IngestionFilter allows control over what pricing data is ingested. Given a price.WithProduct the function returns
// true if the record should be ingested, false if it should be skipped.
type IngestionFilter func(pp *price.WithProduct) bool

// DefaultFilter ingests all the records without filtering.
func DefaultFilter(_ *price.WithProduct) bool {
return true
}

// MinimalFilter only ingests the supported records, skipping those that would never be used.
func MinimalFilter(pp *price.WithProduct) bool {
return pp.Price.Attributes["type"] == "Consumption" && pp.Product.Attributes["priority"] == "regular"
}
172 changes: 172 additions & 0 deletions azurerm/ingester.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package azurerm

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/cycloidio/terracost/price"
"github.com/cycloidio/terracost/product"
"github.com/shopspring/decimal"
)

// ProviderName is the provider that this package implements
const ProviderName = "azurerm"

var (
// The list of all services is https://azure.microsoft.com/en-us/services/, the left side is
// the Family and the main content is the Services
services = map[string]struct{}{
VirtualMachines.String(): struct{}{},
}

// ErrNotSupportedService reports that the service is not supported
ErrNotSupportedService = errors.New("not supported service")
)

// Ingester is the entity that will manage the ingestion process from AzureRM
type Ingester struct {
service string
region string

client *http.Client

ingestionFilter IngestionFilter
endpoint string
endpointURL *url.URL

err error
}

// NewIngester returns a new Ingester for AzureRM for the specified service and region (ex: francecentral) with the
// given options
func NewIngester(ctx context.Context, service, region string, opts ...Option) (*Ingester, error) {
if _, ok := services[service]; !ok {
return nil, ErrNotSupportedService
}
ing := &Ingester{
client: http.DefaultClient,
region: region,
service: service,
endpoint: "https://prices.azure.com/",
ingestionFilter: DefaultFilter,
}

for _, opt := range opts {
opt(ing)
}

u, err := url.Parse(ing.endpoint)
if err != nil {
return nil, err
}
ing.endpointURL = u

return ing, nil
}

// Ingest will initialize the process of ingesting and it'll push the price.WithProduct found
// to the returned channel
func (ing *Ingester) Ingest(ctx context.Context, chSize int) <-chan *price.WithProduct {
results := make(chan *price.WithProduct, chSize)
go func() {
defer close(results)

for rp := range ing.fetchPrices(ctx) {
priority := "regular"
if strings.HasSuffix(rp.MeterName, " Spot") {
priority = "spot"
} else if strings.HasSuffix(rp.MeterName, " Low Priority") {
priority = "low"
}
prod := &product.Product{
Provider: ProviderName,
SKU: rp.SkuID,
Service: rp.ServiceName,
Family: rp.ServiceFamily,
Location: rp.ArmRegionName,
Attributes: map[string]string{
"arm_sku_name": rp.ArmSkuName,
"product_name": rp.ProductName,
"sku_name": rp.SkuName,
"priority": priority,
},
}
pwp := &price.WithProduct{
Price: price.Price{
Unit: rp.UnitOfMeasure,
Value: decimal.NewFromFloat(rp.UnitPrice),
Currency: rp.CurrencyCode,
Attributes: map[string]string{
"type": rp.Type,
},
},
Product: prod,
}
if ing.ingestionFilter(pwp) {
results <- pwp
}
}
}()

return results
}

func (ing *Ingester) fetchPrices(ctx context.Context) <-chan retailPrice {
results := make(chan retailPrice, 100)

go func() {
defer close(results)
// Docs: https://docs.microsoft.com/en-us/rest/api/cost-management/retail-prices/azure-retail-prices
f := url.PathEscape(fmt.Sprintf("serviceName eq '%s' and armRegionName eq '%s'", ing.service, ing.region))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s?$filter=%s", ing.buildPricesURL(), f), nil)
if err != nil {
ing.err = err
return
}

for req != nil {
res, err := ing.client.Do(req)
if err != nil {
ing.err = err
return
}

var rps retailPricesResponse
err = json.NewDecoder(res.Body).Decode(&rps)
if err != nil {
ing.err = err
return
}
res.Body.Close()

for _, rp := range rps.Items {
results <- rp
}

if rps.NextPageLink == "" {
req = nil
} else {
req, _ = http.NewRequestWithContext(ctx, http.MethodGet, rps.NextPageLink, nil)
}
}
}()

return results
}

// buildPricesURL will build the prices url from the endpoint defined
// and the path to the api endpoint
func (ing *Ingester) buildPricesURL() string {
path, _ := url.Parse("./api/retail/prices")
return ing.endpointURL.ResolveReference(path).String()
}

// Err returns any error that might have happened during the ingestion.
func (ing *Ingester) Err() error {
return ing.err
}
51 changes: 51 additions & 0 deletions azurerm/ingester_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package azurerm_test

import (
"context"
"testing"

"github.com/cycloidio/terracost/azurerm"
"github.com/cycloidio/terracost/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIngest(t *testing.T) {
var (
ctx = context.Background()
region = "francecentral"
)

ts := testutil.StartAzureServer(t)
defer ts.Close()
t.Run("SuccessIngestAll", func(t *testing.T) {

i, err := azurerm.NewIngester(ctx, azurerm.VirtualMachines.String(), region, azurerm.WithEndpoint(ts.URL))
require.NoError(t, err)

var count int
for _ = range i.Ingest(ctx, 10) {
count++
}

require.NoError(t, i.Err())
assert.Equal(t, 4436, count)
})
t.Run("SuccessMinimal", func(t *testing.T) {

i, err := azurerm.NewIngester(ctx, azurerm.VirtualMachines.String(), region, azurerm.WithIngestionFilter(azurerm.MinimalFilter), azurerm.WithEndpoint(ts.URL))
require.NoError(t, err)

var count int
for _ = range i.Ingest(ctx, 10) {
count++
}

require.NoError(t, i.Err())
assert.Equal(t, 840, count)
})
t.Run("ErrNotSupportedService", func(t *testing.T) {
_, err := azurerm.NewIngester(ctx, "invalid service", region, azurerm.WithIngestionFilter(azurerm.MinimalFilter), azurerm.WithEndpoint(ts.URL))
assert.EqualError(t, err, azurerm.ErrNotSupportedService.Error())
})
}
18 changes: 18 additions & 0 deletions azurerm/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package azurerm

// Option is used to configure the Ingester.
type Option func(ing *Ingester)

// WithIngestionFilter sets a custom IngestionFilter to control which pricing data records should be ingested.
func WithIngestionFilter(filter IngestionFilter) Option {
return func(ing *Ingester) {
ing.ingestionFilter = filter
}
}

// WithEndpoint sets a custom endpoint to user for the api calls
func WithEndpoint(endpoint string) Option {
return func(ing *Ingester) {
ing.endpoint = endpoint
}
}
35 changes: 35 additions & 0 deletions azurerm/retail_prices_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azurerm

type retailPricesResponse struct {
BillingCurrency string `json:"BillingCurrency"`
CustomerEntityID string `json:"CustomerEntityId"`
CustomerEntityType string `json:"CustomerEntityType"`
Items []retailPrice `json:"Items"`
NextPageLink string `json:"NextPageLink"`
Count int `json:"Count"`
}

type retailPrice struct {
CurrencyCode string `json:"currencyCode"`
TierMinimumUnits float64 `json:"tierMinimumUnits"`
RetailPrice float64 `json:"retailPrice"`
UnitPrice float64 `json:"unitPrice"`
ArmRegionName string `json:"armRegionName"`
Location string `json:"location"`
EffectiveStartDate string `json:"effectiveStartDate"`
MeterID string `json:"meterId"`
MeterName string `json:"meterName"`
ProductID string `json:"productId"`
SkuID string `json:"skuId"`
ProductName string `json:"productName"`
SkuName string `json:"skuName"`
ServiceName string `json:"serviceName"`
ServiceID string `json:"serviceId"`
ServiceFamily string `json:"serviceFamily"`
UnitOfMeasure string `json:"unitOfMeasure"`
Type string `json:"type"`
IsPrimaryMeterRegion bool `json:"isPrimaryMeterRegion"`
ArmSkuName string `json:"armSkuName"`
EffectiveEndDate string `json:"effectiveEndDate,omitempty"`
ReservationTerm string `json:"reservationTerm,omitempty"`
}
11 changes: 11 additions & 0 deletions azurerm/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package azurerm

//go:generate enumer -type=Service -output=service_string.go -linecomment=true

// Service is the type defining the services
type Service uint8

// List of all the supported services
const (
VirtualMachines Service = iota // Virtual Machines
)
74 changes: 74 additions & 0 deletions azurerm/service_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading