storageconnection.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package azure
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
  10. "github.com/opencost/opencost/core/pkg/log"
  11. "github.com/opencost/opencost/pkg/cloud"
  12. )
  13. // StorageConnection provides access to Azure Storage
  14. type StorageConnection struct {
  15. StorageConfiguration
  16. ConnectionStatus cloud.ConnectionStatus
  17. }
  18. func (sc *StorageConnection) GetStatus() cloud.ConnectionStatus {
  19. // initialize status if it has not done so; this can happen if the integration is inactive
  20. if sc.ConnectionStatus.String() == "" {
  21. sc.ConnectionStatus = cloud.InitialStatus
  22. }
  23. return sc.ConnectionStatus
  24. }
  25. func (sc *StorageConnection) Equals(config cloud.Config) bool {
  26. thatConfig, ok := config.(*StorageConnection)
  27. if !ok {
  28. return false
  29. }
  30. return sc.StorageConfiguration.Equals(&thatConfig.StorageConfiguration)
  31. }
  32. // getBlobURLTemplate returns the correct BlobUrl for whichever Cloud storage account is specified by the AzureCloud configuration
  33. // defaults to the Public Cloud template
  34. func (sc *StorageConnection) getBlobURLTemplate() string {
  35. // Use gov cloud blob url if gov is detected in AzureCloud
  36. if strings.Contains(strings.ToLower(sc.Cloud), "gov") {
  37. return "https://%s.blob.core.usgovcloudapi.net/%s"
  38. } else if strings.Contains(strings.ToLower(sc.Cloud), "china") {
  39. // Use China cloud blob url if china is detected in AzureCloud
  40. return "https://%s.blob.core.chinacloudapi.cn/%s"
  41. }
  42. // default to Public Cloud template
  43. return "https://%s.blob.core.windows.net/%s"
  44. }
  45. // DownloadBlob downloads the Azure Billing CSV into a byte slice
  46. func (sc *StorageConnection) DownloadBlob(blobName string, client *azblob.Client, ctx context.Context) ([]byte, error) {
  47. log.Infof("Azure Storage: retrieving blob: %v", blobName)
  48. downloadResponse, err := client.DownloadStream(ctx, sc.Container, blobName, nil)
  49. if err != nil {
  50. return nil, fmt.Errorf("Azure: DownloadBlob: failed to download %w", err)
  51. }
  52. // NOTE: automatically retries are performed if the connection fails
  53. retryReader := downloadResponse.NewRetryReader(ctx, &azblob.RetryReaderOptions{})
  54. defer retryReader.Close()
  55. // read the body into a buffer
  56. downloadedData := bytes.Buffer{}
  57. _, err = downloadedData.ReadFrom(retryReader)
  58. if err != nil {
  59. return nil, fmt.Errorf("Azure: DownloadBlob: failed to read downloaded data %w", err)
  60. }
  61. return downloadedData.Bytes(), nil
  62. }
  63. // StreamBlob returns an io.Reader for the given blob which uses a re-usable double buffer approach to stream directly
  64. // from blob storage.
  65. func (sc *StorageConnection) StreamBlob(blobName string, client *azblob.Client) (*StreamReader, error) {
  66. return NewStreamReader(client, sc.Container, blobName)
  67. }
  68. // DownloadBlobToFile downloads the Azure Billing CSV to a local file
  69. func (sc *StorageConnection) DownloadBlobToFile(localFilePath string, blobName string, client *azblob.Client, ctx context.Context) error {
  70. // If file exists, don't download it again
  71. if _, err := os.Stat(localFilePath); err == nil {
  72. log.DedupedInfof(3, "CloudCost: Azure: DownloadBlobToFile: file %v already exists, not downloading %v", localFilePath, blobName)
  73. return nil
  74. }
  75. // Create filepath
  76. dir := filepath.Dir(localFilePath)
  77. if err := os.MkdirAll(dir, os.ModePerm); err != nil {
  78. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to create directory %w", err)
  79. }
  80. fp, err := os.Create(localFilePath)
  81. if err != nil {
  82. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to create file %w", err)
  83. }
  84. defer fp.Close()
  85. // Download newest Azure Billing CSV to disk
  86. log.Infof("CloudCost: Azure: DownloadBlobToFile: retrieving blob: %v", blobName)
  87. filesize, err := client.DownloadFile(ctx, sc.Container, blobName, fp, nil)
  88. if err != nil {
  89. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to download %w", err)
  90. }
  91. log.Infof("CloudCost: Azure: DownloadBlobToFile: retrieved %v of size %dMB", blobName, filesize/1024/1024)
  92. return nil
  93. }