boaconfiguration.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package alibaba
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/core/pkg/opencost"
  5. "github.com/opencost/opencost/core/pkg/util/json"
  6. "github.com/opencost/opencost/pkg/cloud"
  7. )
  8. // BOAConfiguration is the BSS open API configuration for Alibaba's Billing information
  9. type BOAConfiguration struct {
  10. Account string `json:"account"`
  11. Region string `json:"region"`
  12. Authorizer Authorizer `json:"authorizer"`
  13. }
  14. func (bc *BOAConfiguration) Validate() error {
  15. // Validate Authorizer
  16. if bc.Authorizer == nil {
  17. return fmt.Errorf("BOAConfiguration: missing authorizer")
  18. }
  19. err := bc.Authorizer.Validate()
  20. if err != nil {
  21. return err
  22. }
  23. // Validate base properties
  24. if bc.Region == "" {
  25. return fmt.Errorf("BOAConfiguration: missing region")
  26. }
  27. if bc.Account == "" {
  28. return fmt.Errorf("BOAConfiguration: missing account")
  29. }
  30. return nil
  31. }
  32. func (bc *BOAConfiguration) Equals(config cloud.Config) bool {
  33. if config == nil {
  34. return false
  35. }
  36. thatConfig, ok := config.(*BOAConfiguration)
  37. if !ok {
  38. return false
  39. }
  40. if bc.Authorizer != nil {
  41. if !bc.Authorizer.Equals(thatConfig.Authorizer) {
  42. return false
  43. }
  44. } else {
  45. if thatConfig.Authorizer != nil {
  46. return false
  47. }
  48. }
  49. if bc.Account != thatConfig.Account {
  50. return false
  51. }
  52. if bc.Region != thatConfig.Region {
  53. return false
  54. }
  55. return true
  56. }
  57. func (bc *BOAConfiguration) Sanitize() cloud.Config {
  58. return &BOAConfiguration{
  59. Account: bc.Account,
  60. Region: bc.Region,
  61. Authorizer: bc.Authorizer.Sanitize().(Authorizer),
  62. }
  63. }
  64. func (bc *BOAConfiguration) Key() string {
  65. return fmt.Sprintf("%s/%s", bc.Account, bc.Region)
  66. }
  67. func (bc *BOAConfiguration) Provider() string {
  68. return opencost.AlibabaProvider
  69. }
  70. func (bc *BOAConfiguration) UnmarshalJSON(b []byte) error {
  71. var f interface{}
  72. err := json.Unmarshal(b, &f)
  73. if err != nil {
  74. return err
  75. }
  76. fmap := f.(map[string]interface{})
  77. account, err := cloud.GetInterfaceValue[string](fmap, "account")
  78. if err != nil {
  79. return fmt.Errorf("BOAConfiguration: UnmarshalJSON: %s", err.Error())
  80. }
  81. bc.Account = account
  82. region, err := cloud.GetInterfaceValue[string](fmap, "region")
  83. if err != nil {
  84. return fmt.Errorf("BOAConfiguration: UnmarshalJSON: %s", err.Error())
  85. }
  86. bc.Region = region
  87. authAny, ok := fmap["authorizer"]
  88. if !ok {
  89. return fmt.Errorf("BOAConfiguration: UnmarshalJSON: missing authorizer")
  90. }
  91. authorizer, err := cloud.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  92. if err != nil {
  93. return fmt.Errorf("BOAConfiguration: UnmarshalJSON: %s", err.Error())
  94. }
  95. bc.Authorizer = authorizer
  96. return nil
  97. }
  98. func ConvertAlibabaInfoToConfig(acc AlibabaInfo) cloud.KeyedConfig {
  99. if acc.IsEmpty() {
  100. return nil
  101. }
  102. var configurer Authorizer
  103. configurer = &AccessKey{
  104. AccessKeyID: acc.AlibabaServiceKeyName,
  105. AccessKeySecret: acc.AlibabaServiceKeySecret,
  106. }
  107. return &BOAConfiguration{
  108. Account: acc.AlibabaAccountID,
  109. Region: acc.AlibabaClusterRegion,
  110. Authorizer: configurer,
  111. }
  112. }