storagebillingparser.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package azure
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/csv"
  6. "fmt"
  7. "io"
  8. "strings"
  9. "time"
  10. "github.com/Azure/azure-storage-blob-go/azblob"
  11. "github.com/opencost/opencost/pkg/cloud"
  12. "github.com/opencost/opencost/pkg/log"
  13. )
  14. // AzureStorageBillingParser accesses billing data stored in CSV files in Azure Storage
  15. type AzureStorageBillingParser struct {
  16. StorageConnection
  17. }
  18. func (asbp *AzureStorageBillingParser) Equals(config cloud.Config) bool {
  19. thatConfig, ok := config.(*AzureStorageBillingParser)
  20. if !ok {
  21. return false
  22. }
  23. return asbp.StorageConnection.Equals(&thatConfig.StorageConnection)
  24. }
  25. type AzureBillingResultFunc func(*BillingRowValues) error
  26. func (asbp *AzureStorageBillingParser) ParseBillingData(start, end time.Time, resultFn AzureBillingResultFunc) error {
  27. err := asbp.Validate()
  28. if err != nil {
  29. asbp.ConnectionStatus = cloud.InvalidConfiguration
  30. return err
  31. }
  32. containerURL, err := asbp.getContainer()
  33. if err != nil {
  34. asbp.ConnectionStatus = cloud.FailedConnection
  35. return err
  36. }
  37. ctx := context.Background()
  38. blobNames, err := asbp.getMostRecentBlobs(start, end, containerURL, ctx)
  39. if err != nil {
  40. asbp.ConnectionStatus = cloud.FailedConnection
  41. return err
  42. }
  43. if len(blobNames) == 0 && asbp.ConnectionStatus != cloud.SuccessfulConnection {
  44. asbp.ConnectionStatus = cloud.MissingData
  45. return nil
  46. }
  47. for _, blobName := range blobNames {
  48. blobBytes, err2 := asbp.DownloadBlob(blobName, containerURL, ctx)
  49. if err2 != nil {
  50. asbp.ConnectionStatus = cloud.FailedConnection
  51. return err2
  52. }
  53. err2 = asbp.parseCSV(start, end, csv.NewReader(bytes.NewReader(blobBytes)), resultFn)
  54. if err2 != nil {
  55. asbp.ConnectionStatus = cloud.ParseError
  56. return err2
  57. }
  58. }
  59. asbp.ConnectionStatus = cloud.SuccessfulConnection
  60. return nil
  61. }
  62. func (asbp *AzureStorageBillingParser) parseCSV(start, end time.Time, reader *csv.Reader, resultFn AzureBillingResultFunc) error {
  63. headers, err := reader.Read()
  64. if err != nil {
  65. return err
  66. }
  67. abp, err := NewBillingParseSchema(headers)
  68. if err != nil {
  69. return err
  70. }
  71. for {
  72. var record, err = reader.Read()
  73. if err == io.EOF {
  74. break
  75. }
  76. if err != nil {
  77. return err
  78. }
  79. abv := abp.ParseRow(start, end, record)
  80. if abv == nil {
  81. continue
  82. }
  83. err = resultFn(abv)
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. func (asbp *AzureStorageBillingParser) getMostRecentBlobs(start, end time.Time, containerURL *azblob.ContainerURL, ctx context.Context) ([]string, error) {
  91. log.Infof("Azure Storage: retrieving most recent reports from: %v - %v", start, end)
  92. // Get list of month substrings for months contained in the start to end range
  93. monthStrs, err := asbp.getMonthStrings(start, end)
  94. if err != nil {
  95. return nil, err
  96. }
  97. mostResentBlobs := make(map[string]azblob.BlobItemInternal)
  98. for marker := (azblob.Marker{}); marker.NotDone(); {
  99. // Get a result segment starting with the blob indicated by the current Marker.
  100. listBlob, err := containerURL.ListBlobsFlatSegment(ctx, marker, azblob.ListBlobsSegmentOptions{})
  101. if err != nil {
  102. return nil, err
  103. }
  104. // ListBlobs returns the start of the next segment; you MUST use this to get
  105. // the next segment (after processing the current result segment).
  106. marker = listBlob.NextMarker
  107. // Using the list of months strings find the most resent blob for each month in the range
  108. for _, blobInfo := range listBlob.Segment.BlobItems {
  109. for _, month := range monthStrs {
  110. if strings.Contains(blobInfo.Name, month) {
  111. // If Container Path configuration exists, check if it is in the blobs name
  112. if asbp.Path != "" && !strings.Contains(blobInfo.Name, asbp.Path) {
  113. continue
  114. }
  115. if prevBlob, ok := mostResentBlobs[month]; ok {
  116. if prevBlob.Properties.CreationTime.After(*blobInfo.Properties.CreationTime) {
  117. continue
  118. }
  119. }
  120. mostResentBlobs[month] = blobInfo
  121. }
  122. }
  123. }
  124. }
  125. // convert blob names into blob urls and move from map into ordered list of blob names
  126. var blobNames []string
  127. for _, month := range monthStrs {
  128. if blob, ok := mostResentBlobs[month]; ok {
  129. blobNames = append(blobNames, blob.Name)
  130. }
  131. }
  132. return blobNames, nil
  133. }
  134. func (asbp *AzureStorageBillingParser) getMonthStrings(start, end time.Time) ([]string, error) {
  135. if start.After(end) {
  136. return []string{}, fmt.Errorf("start date must be before end date")
  137. }
  138. if end.After(time.Now()) {
  139. end = time.Now()
  140. }
  141. var monthStrs []string
  142. monthStr := asbp.timeToMonthString(start)
  143. endStr := asbp.timeToMonthString(end)
  144. monthStrs = append(monthStrs, monthStr)
  145. currMonth := start.AddDate(0, 0, -start.Day()+1)
  146. for monthStr != endStr {
  147. currMonth = currMonth.AddDate(0, 1, 0)
  148. monthStr = asbp.timeToMonthString(currMonth)
  149. monthStrs = append(monthStrs, monthStr)
  150. }
  151. return monthStrs, nil
  152. }
  153. func (asbp *AzureStorageBillingParser) timeToMonthString(input time.Time) string {
  154. format := "20060102"
  155. startOfMonth := input.AddDate(0, 0, -input.Day()+1)
  156. endOfMonth := input.AddDate(0, 1, -input.Day())
  157. return startOfMonth.Format(format) + "-" + endOfMonth.Format(format)
  158. }