storageconnection.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package azure
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net/url"
  7. "strings"
  8. "github.com/Azure/azure-storage-blob-go/azblob"
  9. "github.com/opencost/opencost/pkg/cloud"
  10. cloudconfig "github.com/opencost/opencost/pkg/cloud/config"
  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 cloudconfig.Config) bool {
  26. thatConfig, ok := config.(*StorageConnection)
  27. if !ok {
  28. return false
  29. }
  30. return sc.StorageConfiguration.Equals(&thatConfig.StorageConfiguration)
  31. }
  32. func (sc *StorageConnection) getContainer() (*azblob.ContainerURL, error) {
  33. credential, err := sc.Authorizer.GetBlobCredentials()
  34. if err != nil {
  35. return nil, err
  36. }
  37. p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  38. // From the Azure portal, get your storage account blob service URL endpoint.
  39. URL, _ := url.Parse(
  40. fmt.Sprintf(sc.getBlobURLTemplate(), sc.Account, sc.Container))
  41. // Create a ContainerURL object that wraps the container URL and a request
  42. // pipeline to make requests.
  43. containerURL := azblob.NewContainerURL(*URL, p)
  44. return &containerURL, nil
  45. }
  46. // getBlobURLTemplate returns the correct BlobUrl for whichever Cloud storage account is specified by the AzureCloud configuration
  47. // defaults to the Public Cloud template
  48. func (sc *StorageConnection) getBlobURLTemplate() string {
  49. // Use gov cloud blob url if gov is detected in AzureCloud
  50. if strings.Contains(strings.ToLower(sc.Cloud), "gov") {
  51. return "https://%s.blob.core.usgovcloudapi.net/%s"
  52. }
  53. // default to Public Cloud template
  54. return "https://%s.blob.core.windows.net/%s"
  55. }
  56. func (sc *StorageConnection) DownloadBlob(blobName string, containerURL *azblob.ContainerURL, ctx context.Context) ([]byte, error) {
  57. log.Infof("Azure Storage: retrieving blob: %v", blobName)
  58. blobURL := containerURL.NewBlobURL(blobName)
  59. downloadResponse, err := blobURL.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
  60. if err != nil {
  61. return nil, err
  62. }
  63. // NOTE: automatically retries are performed if the connection fails
  64. bodyStream := downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20})
  65. // read the body into a buffer
  66. downloadedData := bytes.Buffer{}
  67. _, err = downloadedData.ReadFrom(bodyStream)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return downloadedData.Bytes(), nil
  72. }