From 00a38970bee9dd01491efdf8a42a21f32770c795 Mon Sep 17 00:00:00 2001 From: Marco Amador Date: Wed, 2 Oct 2024 21:07:32 +0100 Subject: [PATCH] [azure-blob-storage] Make `container` interpolated Signed-off-by: Marco Amador --- .../pages/inputs/azure_blob_storage.adoc | 1 + internal/impl/azure/auth.go | 10 +++++++--- internal/impl/azure/input_blob_storage.go | 17 ++++++++++------- internal/impl/azure/output_blob_storage.go | 6 +----- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/docs/modules/components/pages/inputs/azure_blob_storage.adoc b/docs/modules/components/pages/inputs/azure_blob_storage.adoc index d6263bbe89..61281bc5e5 100644 --- a/docs/modules/components/pages/inputs/azure_blob_storage.adoc +++ b/docs/modules/components/pages/inputs/azure_blob_storage.adoc @@ -150,6 +150,7 @@ The storage account SAS token. This field is ignored if `storage_connection_stri === `container` The name of the container from which to download blobs. +This field supports xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions]. *Type*: `string` diff --git a/internal/impl/azure/auth.go b/internal/impl/azure/auth.go index afe4e42b3e..a46557b631 100644 --- a/internal/impl/azure/auth.go +++ b/internal/impl/azure/auth.go @@ -57,7 +57,7 @@ func azureComponentSpec(forBlobStorage bool) *service.ConfigSpec { return spec } -func blobStorageClientFromParsed(pConf *service.ParsedConfig, container string) (*azblob.Client, bool, error) { +func blobStorageClientFromParsed(pConf *service.ParsedConfig, container *service.InterpolatedString) (*azblob.Client, bool, error) { connectionString, err := pConf.FieldString(bscFieldStorageConnectionString) if err != nil { return nil, false, err @@ -84,7 +84,7 @@ const ( blobEndpointExp = "https://%s.blob.core.windows.net" ) -func getBlobStorageClient(storageConnectionString, storageAccount, storageAccessKey, storageSASToken, container string) (*azblob.Client, bool, error) { +func getBlobStorageClient(storageConnectionString, storageAccount, storageAccessKey, storageSASToken string, container *service.InterpolatedString) (*azblob.Client, bool, error) { var client *azblob.Client var err error var containerSASToken bool @@ -103,7 +103,11 @@ func getBlobStorageClient(storageConnectionString, storageAccount, storageAccess if strings.HasPrefix(storageSASToken, "sp=") { // container SAS token containerSASToken = true - serviceURL = fmt.Sprintf("%s/%s?%s", fmt.Sprintf(blobEndpointExp, storageAccount), container, storageSASToken) + c, err := container.TryString(service.NewMessage([]byte(""))) + if err != nil { + return nil, false, fmt.Errorf("error getting container: %w", err) + } + serviceURL = fmt.Sprintf("%s/%s?%s", fmt.Sprintf(blobEndpointExp, storageAccount), c, storageSASToken) } else { // storage account SAS token serviceURL = fmt.Sprintf("%s/%s", fmt.Sprintf(blobEndpointExp, storageAccount), storageSASToken) diff --git a/internal/impl/azure/input_blob_storage.go b/internal/impl/azure/input_blob_storage.go index 437f718899..8910b503a7 100644 --- a/internal/impl/azure/input_blob_storage.go +++ b/internal/impl/azure/input_blob_storage.go @@ -51,16 +51,20 @@ type bsiConfig struct { } func bsiConfigFromParsed(pConf *service.ParsedConfig) (conf bsiConfig, err error) { - if conf.Container, err = pConf.FieldString(bsiFieldContainer); err != nil { + var containerSASToken bool + container, err := pConf.FieldInterpolatedString(bsiFieldContainer) + if err != nil { return } - var containerSASToken bool - if conf.client, containerSASToken, err = blobStorageClientFromParsed(pConf, conf.Container); err != nil { + if conf.client, containerSASToken, err = blobStorageClientFromParsed(pConf, container); err != nil { return } if containerSASToken { - // when using a container SAS token, the container is already implicit - conf.Container = "" + // if using a container SAS token, the container is already implicit + container, _ = service.NewInterpolatedString("") + } + if conf.Container, err = container.TryString(service.NewMessage([]byte(""))); err != nil { + return } if conf.Prefix, err = pConf.FieldString(bsiFieldPrefix); err != nil { return @@ -119,7 +123,7 @@ This input adds the following metadata fields to each message: You can access these metadata fields using xref:configuration:interpolation.adoc#bloblang-queries[function interpolation].`). Fields( - service.NewStringField(bsiFieldContainer). + service.NewInterpolatedStringField(bsiFieldContainer). Description("The name of the container from which to download blobs."), service.NewStringField(bsiFieldPrefix). Description("An optional path prefix, if set only objects with the prefix are consumed."). @@ -367,7 +371,6 @@ func newAzureTargetBatchReader(ctx context.Context, conf bsiConfig) (*azureTarge } staticKeys.pager = pager } - return &staticKeys, nil } diff --git a/internal/impl/azure/output_blob_storage.go b/internal/impl/azure/output_blob_storage.go index 18c3f47854..cbad245b82 100644 --- a/internal/impl/azure/output_blob_storage.go +++ b/internal/impl/azure/output_blob_storage.go @@ -49,11 +49,7 @@ func bsoConfigFromParsed(pConf *service.ParsedConfig) (conf bsoConfig, err error return } var containerSASToken bool - c, err := conf.Container.TryString(service.NewMessage([]byte(""))) - if err != nil { - return - } - if conf.client, containerSASToken, err = blobStorageClientFromParsed(pConf, c); err != nil { + if conf.client, containerSASToken, err = blobStorageClientFromParsed(pConf, conf.Container); err != nil { return } if containerSASToken {