vault/api/sys_billing.go
Vault Automation c0e8de6ed9
VAULT-41572: hookup billing overview endpoint (#12328) (#12451)
* hookup the new path to the system backend

* add API client method for the new endpoint

* add test for the api method structure

* adjust the path implementation to capture all so-far added metrics

* add tests

* add go docs to the tests

* add more tests

* feedback: add go doc to test

* feedback: use require in tests

* fix the api: use parse secret method to properly parse response, add mapstructure definitions to api structs

* feedback: fix api method test by using mock api response

* Update vault/logical_system_use_case_billing.go



* Update vault/logical_system_use_case_billing.go



* feedback: refactor build month data method into methods that collect data separately

* feedback: make update_counts parameter a new user set field for the endpoint

* feedback: remove basic comments

* update logic around determining updated at field

* fix tests: add actual data and fix some assertions

* separate out ent only features from the neutral test file

* add a new test file to test ent only features

* call one update method to update all metrics

* add external tests for the endpoint

* add a changelog

* feedback: rename update_counts parameter to refresh_data

* feedback: fix determination of updated_at field

* feedback: convert created methods into core methods from system backend methods

* Update changelog/12328.txt



* feedback: create a new atomic tracker of last updated time for the metrics update and use that in the endpoint

* add unit tests to test updated_at

* always build metrics, even when the values are 0

* add test coverage to verify metrics still exist in the response with zero values even when there are no billing resources

* feedback: remove manual check of root namespace - rely on system backend to enforce root namespace restriction

* remove namespace test from oss test file

* properly accomodate new totp metric

* add pki cert and totp to endpoint response, add test coverage

* rename changelog file

* linters

* change changelog type to improvement, make the file CE and ENT

* test ent changelog

* fix some tests after the addition of totp and pki

* add test coverage for the new metrics in external and api tests

* make changelog CE file

---------

Co-authored-by: Amir Aslamov <amir.aslamov@hashicorp.com>
Co-authored-by: Violet Hynes <violet.hynes@hashicorp.com>
Co-authored-by: divyaac <divya.chandrasekaran@hashicorp.com>
2026-02-20 08:17:23 -05:00

70 lines
2.1 KiB
Go

// Copyright IBM Corp. 2016, 2025
// SPDX-License-Identifier: MPL-2.0
package api
import (
"context"
"errors"
"net/http"
"github.com/mitchellh/mapstructure"
)
// BillingOverview returns billing metrics for the current and previous month.
// If updateCounts is true, the current month's counts will be updated before returning.
// This is an expensive operation that holds locks and should be used sparingly.
func (c *Sys) BillingOverview(updateCounts bool) (*BillingOverviewResponse, error) {
return c.BillingOverviewWithContext(context.Background(), updateCounts)
}
// BillingOverviewWithContext returns billing metrics for the current and previous month.
func (c *Sys) BillingOverviewWithContext(ctx context.Context, updateCounts bool) (*BillingOverviewResponse, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodGet, "/v1/sys/billing/overview")
if updateCounts {
r.Params.Set("refresh_data", "true")
}
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, err
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}
var result BillingOverviewResponse
err = mapstructure.Decode(secret.Data, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// BillingOverviewResponse represents the response from the billing overview endpoint.
type BillingOverviewResponse struct {
Months []BillingMonth `json:"months" mapstructure:"months"`
}
// BillingMonth represents billing data for a single month.
type BillingMonth struct {
Month string `json:"month" mapstructure:"month"`
UpdatedAt string `json:"updated_at" mapstructure:"updated_at"`
UsageMetrics []UsageMetric `json:"usage_metrics" mapstructure:"usage_metrics"`
}
// UsageMetric represents a single usage metric with its data.
type UsageMetric struct {
MetricName string `json:"metric_name" mapstructure:"metric_name"`
MetricData map[string]interface{} `json:"metric_data" mapstructure:"metric_data"`
}