persistentvolume.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package pricing
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/model/shared"
  8. )
  9. type PersistentVolumePricing struct {
  10. Properties PersistentVolumePricingProperties `json:"properties" yaml:"properties"`
  11. Prices Prices `json:"prices" yaml:"prices"`
  12. }
  13. func (vp *PersistentVolumePricing) String() string {
  14. return vp.Properties.String() + "|" + vp.Prices.canonical()
  15. }
  16. type PersistentVolumePricingProperties struct {
  17. Provider shared.Provider `json:"provider,omitempty" yaml:"provider,omitempty"`
  18. Region string `json:"region,omitempty" yaml:"region,omitempty"`
  19. VolumeType VolumeType `json:"volumeType,omitempty" yaml:"volumeType,omitempty"`
  20. Cluster string `json:"cluster,omitempty" yaml:"cluster,omitempty"`
  21. ProviderID string `json:"providerID,omitempty" yaml:"providerID,omitempty"`
  22. Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
  23. Start *time.Time `json:"start,omitempty" yaml:"start,omitempty"`
  24. End *time.Time `json:"end,omitempty" yaml:"end,omitempty"`
  25. }
  26. func (vp *PersistentVolumePricingProperties) String() string {
  27. return fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s",
  28. vp.Provider,
  29. vp.Region,
  30. vp.VolumeType,
  31. vp.Cluster,
  32. vp.ProviderID,
  33. vp.labelsKey(),
  34. vp.timeKey(),
  35. )
  36. }
  37. func (vp *PersistentVolumePricingProperties) labelsKey() string {
  38. if len(vp.Labels) == 0 {
  39. return ""
  40. }
  41. keys := make([]string, 0, len(vp.Labels))
  42. for k := range vp.Labels {
  43. keys = append(keys, k)
  44. }
  45. sort.Strings(keys)
  46. var b strings.Builder
  47. for i, k := range keys {
  48. if i > 0 {
  49. b.WriteByte(':')
  50. }
  51. b.WriteString(k)
  52. b.WriteByte('=')
  53. b.WriteString(vp.Labels[k])
  54. }
  55. return b.String()
  56. }
  57. func (vp *PersistentVolumePricingProperties) timeKey() string {
  58. s := "nil"
  59. e := "nil"
  60. if vp.Start != nil {
  61. s = vp.Start.UTC().Format(time.RFC3339Nano)
  62. }
  63. if vp.End != nil {
  64. e = vp.End.UTC().Format(time.RFC3339Nano)
  65. }
  66. return fmt.Sprintf("%s:%s", s, e)
  67. }