storageconnection.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/pkg/cloud"
  11. "github.com/opencost/opencost/pkg/log"
  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. }
  39. // default to Public Cloud template
  40. return "https://%s.blob.core.windows.net/%s"
  41. }
  42. func (sc *StorageConnection) DownloadBlob(blobName string, client *azblob.Client, ctx context.Context) ([]byte, error) {
  43. log.Infof("Azure Storage: retrieving blob: %v", blobName)
  44. downloadResponse, err := client.DownloadStream(ctx, sc.Container, blobName, nil)
  45. if err != nil {
  46. return nil, fmt.Errorf("Azure: DownloadBlob: failed to download %w", err)
  47. }
  48. // NOTE: automatically retries are performed if the connection fails
  49. retryReader := downloadResponse.NewRetryReader(ctx, &azblob.RetryReaderOptions{})
  50. defer retryReader.Close()
  51. // read the body into a buffer
  52. downloadedData := bytes.Buffer{}
  53. _, err = downloadedData.ReadFrom(retryReader)
  54. if err != nil {
  55. return nil, fmt.Errorf("Azure: DownloadBlob: failed to read downloaded data %w", err)
  56. }
  57. return downloadedData.Bytes(), nil
  58. }
  59. // DownloadBlobToFile downloads the Azure Billing CSV to a local file
  60. func (sc *StorageConnection) DownloadBlobToFile(localFilePath string, blobName string, client *azblob.Client, ctx context.Context) error {
  61. // If file exists, don't download it again
  62. if _, err := os.Stat(localFilePath); err == nil {
  63. log.DedupedInfof(3, "CloudCost: Azure: DownloadBlobToFile: file %v already exists, not downloading %v", localFilePath, blobName)
  64. return nil
  65. }
  66. // Create filepath
  67. dir := filepath.Dir(localFilePath)
  68. if err := os.MkdirAll(dir, os.ModePerm); err != nil {
  69. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to create directory %w", err)
  70. }
  71. fp, err := os.Create(localFilePath)
  72. if err != nil {
  73. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to create file %w", err)
  74. }
  75. defer fp.Close()
  76. // Download newest Azure Billing CSV to disk
  77. log.Infof("CloudCost: Azure: DownloadBlobToFile: retrieving blob: %v", blobName)
  78. filesize, err := client.DownloadFile(ctx, sc.Container, blobName, fp, nil)
  79. if err != nil {
  80. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to download %w", err)
  81. }
  82. log.Infof("CloudCost: Azure: DownloadBlobToFile: retrieved %v of size %dMB", blobName, filesize/1024/1024)
  83. return nil
  84. }