usageconfiguration.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package ibm
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/IBM/platform-services-go-sdk/usagereportsv4"
  6. "github.com/opencost/opencost/core/pkg/opencost"
  7. "github.com/opencost/opencost/pkg/cloud"
  8. )
  9. // UsageConfiguration connects OpenCost to the IBM Cloud Usage Reports API
  10. // (account cost and usage / CUR-style billing data).
  11. type UsageConfiguration struct {
  12. AccountID string `json:"accountID"`
  13. Authorizer Authorizer `json:"authorizer"`
  14. }
  15. func (c *UsageConfiguration) Validate() error {
  16. if c.Authorizer == nil {
  17. return fmt.Errorf("UsageConfiguration: missing Authorizer")
  18. }
  19. if err := c.Authorizer.Validate(); err != nil {
  20. return fmt.Errorf("UsageConfiguration: %s", err)
  21. }
  22. if c.AccountID == "" {
  23. return fmt.Errorf("UsageConfiguration: missing accountID")
  24. }
  25. return nil
  26. }
  27. func (c *UsageConfiguration) Equals(config cloud.Config) bool {
  28. if config == nil {
  29. return false
  30. }
  31. that, ok := config.(*UsageConfiguration)
  32. if !ok {
  33. return false
  34. }
  35. if c.Authorizer != nil {
  36. if !c.Authorizer.Equals(that.Authorizer) {
  37. return false
  38. }
  39. } else if that.Authorizer != nil {
  40. return false
  41. }
  42. return c.AccountID == that.AccountID
  43. }
  44. func (c *UsageConfiguration) Sanitize() cloud.Config {
  45. var auth Authorizer
  46. if c.Authorizer != nil {
  47. auth = c.Authorizer.Sanitize().(Authorizer)
  48. }
  49. return &UsageConfiguration{
  50. AccountID: c.AccountID,
  51. Authorizer: auth,
  52. }
  53. }
  54. // Key identifies the integration for storage paths. Account IDs are normalized
  55. // to bare hex so "a/<hex>" cannot create an extra path segment.
  56. func (c *UsageConfiguration) Key() string {
  57. return normalizeAccountID(c.AccountID)
  58. }
  59. func (c *UsageConfiguration) Provider() string {
  60. return opencost.IBMProvider
  61. }
  62. func (c *UsageConfiguration) GetUsageReportsClient() (*usagereportsv4.UsageReportsV4, error) {
  63. if c.Authorizer == nil {
  64. return nil, fmt.Errorf("missing authorizer")
  65. }
  66. authenticator, err := c.Authorizer.CreateAuthenticator()
  67. if err != nil {
  68. return nil, fmt.Errorf("creating IAM authenticator: %w", err)
  69. }
  70. client, err := usagereportsv4.NewUsageReportsV4(&usagereportsv4.UsageReportsV4Options{
  71. Authenticator: authenticator,
  72. })
  73. if err != nil {
  74. return nil, fmt.Errorf("creating usage reports client: %w", err)
  75. }
  76. return client, nil
  77. }
  78. func (c *UsageConfiguration) UnmarshalJSON(b []byte) error {
  79. var f any
  80. if err := json.Unmarshal(b, &f); err != nil {
  81. return err
  82. }
  83. fmap, ok := f.(map[string]any)
  84. if !ok {
  85. return fmt.Errorf("UsageConfiguration: UnmarshalJSON: expected object")
  86. }
  87. accountID, err := cloud.GetInterfaceValue[string](fmap, "accountID")
  88. if err != nil {
  89. return fmt.Errorf("UsageConfiguration: UnmarshalJSON: %w", err)
  90. }
  91. c.AccountID = accountID
  92. authAny, ok := fmap["authorizer"]
  93. if !ok {
  94. return fmt.Errorf("UsageConfiguration: UnmarshalJSON: missing authorizer")
  95. }
  96. authorizer, err := cloud.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  97. if err != nil {
  98. return fmt.Errorf("UsageConfiguration: UnmarshalJSON: %w", err)
  99. }
  100. c.Authorizer = authorizer
  101. return nil
  102. }