storageconnection.go 3.4 KB

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