| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package pricing
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "sync"
- "github.com/opencost/opencost/core/pkg/storage"
- )
- type StoragePricingStore struct {
- store storage.Storage
- path string
- mu sync.RWMutex
- }
- func NewStoragePricingStore(ctx context.Context, store storage.Storage, path string) (*StoragePricingStore, error) {
- if store == nil {
- return nil, errors.New("nil storage")
- }
- if path == "" {
- return nil, errors.New("empty path")
- }
- sps := &StoragePricingStore{
- store: store,
- path: path,
- }
- exists, err := store.Exists(path)
- if err != nil {
- return nil, fmt.Errorf("checking pricing path %q: %w", path, err)
- }
- if !exists {
- if err := sps.SetPricingSet(ctx, &PricingSet{}); err != nil {
- return nil, fmt.Errorf("initializing empty pricing set at %q: %w", path, err)
- }
- }
- return sps, nil
- }
- func (sps *StoragePricingStore) GetPricingSet(ctx context.Context) (*PricingSet, error) {
- sps.mu.RLock()
- defer sps.mu.RUnlock()
- data, err := sps.store.Read(sps.path)
- if err != nil {
- return nil, fmt.Errorf("reading path '%s': %w", sps.path, err)
- }
- var pricing PricingSet
- err = json.Unmarshal(data, &pricing)
- if err != nil {
- return nil, fmt.Errorf("decoding pricing: %w", err)
- }
- return &pricing, nil
- }
- func (sps *StoragePricingStore) SetPricingSet(ctx context.Context, pricing *PricingSet) error {
- sps.mu.Lock()
- defer sps.mu.Unlock()
- if pricing == nil {
- return errors.New("nil pricing")
- }
- data, err := json.Marshal(pricing)
- if err != nil {
- return fmt.Errorf("encoding pricing: %w", err)
- }
- err = sps.store.Write(sps.path, data)
- if err != nil {
- return fmt.Errorf("writing pricing: %w", err)
- }
- return nil
- }
|