boaquerier.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package alibaba
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  6. "github.com/aliyun/alibaba-cloud-sdk-go/services/bssopenapi"
  7. "github.com/opencost/opencost/core/pkg/log"
  8. "github.com/opencost/opencost/core/pkg/opencost"
  9. "github.com/opencost/opencost/pkg/cloud"
  10. )
  11. const (
  12. boaIsNode = "i-" // isNode if prefix of instance_id is i-
  13. boaIsDisk = "d-" // isDisk if prefix is disk is d-
  14. boaIsNetwork = "piece" //usage unit of network resource in Alibaba is Piece
  15. )
  16. type BoaQuerier struct {
  17. BOAConfiguration
  18. ConnectionStatus cloud.ConnectionStatus
  19. }
  20. func (bq *BoaQuerier) GetStatus() cloud.ConnectionStatus {
  21. // initialize status if it has not done so; this can happen if the integration is inactive
  22. if bq.ConnectionStatus.String() == "" {
  23. bq.ConnectionStatus = cloud.InitialStatus
  24. }
  25. return bq.ConnectionStatus
  26. }
  27. func (bq *BoaQuerier) Equals(config cloud.Config) bool {
  28. thatConfig, ok := config.(*BoaQuerier)
  29. if !ok {
  30. return false
  31. }
  32. return bq.BOAConfiguration.Equals(&thatConfig.BOAConfiguration)
  33. }
  34. // QueryInstanceBill performs the request to the BSS client and get the response for the current page number
  35. func (bq *BoaQuerier) QueryInstanceBill(client *bssopenapi.Client, isBillingItem bool, invocationScheme, granularity, billingCycle, billingDate string, pageNum int) (*bssopenapi.QueryInstanceBillResponse, error) {
  36. log.Debugf("QueryInstanceBill: query for BSS Open API for billing date: %s with pageNum: %d ", billingDate, pageNum)
  37. request := bssopenapi.CreateQueryInstanceBillRequest()
  38. request.Scheme = invocationScheme
  39. request.BillingCycle = billingCycle
  40. request.IsBillingItem = requests.NewBoolean(true)
  41. request.Granularity = granularity
  42. request.BillingDate = billingDate
  43. request.PageNum = requests.NewInteger(pageNum)
  44. response, err := client.QueryInstanceBill(request)
  45. if err != nil {
  46. return nil, fmt.Errorf("QueryInstanceBill: Failed to hit the BSS Open API with error for page num %d: %v", pageNum, err)
  47. }
  48. log.Debugf("QueryInstanceBill: Total Number of total items for billing Date: %s pageNum: %d is %d", billingDate, pageNum, response.Data.TotalCount)
  49. return response, nil
  50. }
  51. // QueryBoaPaginated Calls the API in a paginated fashion. There's no paramter in API that can distinguish if it hasMorePages
  52. // hence the logic of processedItem <= TotalItem.
  53. func (bq *BoaQuerier) QueryBoaPaginated(client *bssopenapi.Client, isBillingItem bool, invocationScheme, granularity, billingCycle, billingDate string, fn func(*bssopenapi.QueryInstanceBillResponse) bool) error {
  54. pageNum := 1
  55. processedItem := 0 // setting default here to hit the API for the first time
  56. totalItem := 1
  57. for processedItem < totalItem {
  58. log.Debugf("QueryBoaPaginated: query for BSS Open API for billing date: %s with pageNum: %d", billingDate, pageNum)
  59. response, err := bq.QueryInstanceBill(client, isBillingItem, invocationScheme, granularity, billingCycle, billingDate, pageNum)
  60. if err != nil {
  61. return fmt.Errorf("QueryBoaPaginated for billing cycle : %s, billing date: %s, page num %d: %v", billingCycle, billingDate, pageNum, err)
  62. }
  63. fn(response)
  64. totalItem = response.Data.TotalCount
  65. processedItem += response.Data.PageSize
  66. pageNum += 1
  67. }
  68. return nil
  69. }
  70. // GetBoaQueryInstanceBillFunc gives the item to the handler function in boaIntegration.go to process
  71. // computeItem, topNItem and aggregatedItem
  72. func GetBoaQueryInstanceBillFunc(fn func(bssopenapi.Item) error, billingDate string) func(output *bssopenapi.QueryInstanceBillResponse) bool {
  73. processBOAItems := func(output *bssopenapi.QueryInstanceBillResponse) bool {
  74. // This could be connection error were unable to fetch response output from Client
  75. if output == nil {
  76. log.Errorf("BoaQuerier: No Response from the ALibaba BSS Open API client for billing Date: %s", billingDate)
  77. return false
  78. }
  79. // These infer that the rest call was successful but the Cloud Usage resource for those days were 0
  80. if output.Data.TotalCount == 0 {
  81. log.Warnf("BoaQuerier: Total Item Count is 0 for billing Date: %s ", billingDate)
  82. return false
  83. }
  84. for _, item := range output.Data.Items.Item {
  85. fn(item)
  86. }
  87. return true
  88. }
  89. return processBOAItems
  90. }
  91. // SelectAlibabaCategory processes the Alibaba service to associated Kubecost category
  92. func SelectAlibabaCategory(item bssopenapi.Item) string {
  93. // Provider ID has prefix "i-" for node in Alibaba
  94. if strings.HasPrefix(item.InstanceID, boaIsNode) {
  95. return opencost.ComputeCategory
  96. }
  97. // Provider ID for disk start with "d-" for storage type in Alibaba
  98. if strings.HasPrefix(item.InstanceID, boaIsDisk) {
  99. return opencost.StorageCategory
  100. }
  101. // Network has the highest priority and is based on the usage type of "piece" in Alibaba
  102. if item.UsageUnit == boaIsNetwork {
  103. return opencost.NetworkCategory
  104. }
  105. // Alibaba CUR integration report has service lower case mostly unlike AWS
  106. // TO-DO: Can investigate further product codes but bare minimal differentiation for start
  107. switch strings.ToLower(item.ProductCode) {
  108. case "slb", "eip", "nis", "gtm":
  109. return opencost.NetworkCategory
  110. case "ecs", "eds", "sas":
  111. return opencost.ComputeCategory
  112. case "ack":
  113. return opencost.ManagementCategory
  114. case "ebs", "oss", "scu":
  115. return opencost.StorageCategory
  116. default:
  117. return opencost.OtherCategory
  118. }
  119. }