Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove logging from azblob reader #73

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions azblob/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ package azblob

import (
"context"
"errors"
"fmt"
"io"
"net/http"

azStorageBlob "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"

"github.com/datatrails/go-datatrails-common/logger"
)

const (
Expand All @@ -29,25 +28,20 @@ func (azp *Storer) getTags(
) (map[string]string, error) {

var err error
logger.Sugar.Debugf("getTags BlockBlob URL %s", identity)

blobClient, err := azp.containerClient.NewBlobClient(identity)
if err != nil {
logger.Sugar.Debugf("getTags BlockBlob Client %s error: %v", identity, err)
return nil, ErrorFromError(err)
}

resp, err := blobClient.GetTags(ctx, nil)
if err != nil {
logger.Sugar.Debugf("getTags BlockBlob URL %s error: %v", identity, err)
return nil, ErrorFromError(err)
}
logger.Sugar.Debugf("getTags BlockBlob tagSet: %v", resp.BlobTagSet)
tags := make(map[string]string, len(resp.BlobTagSet))
for _, tag := range resp.BlobTagSet {
tags[*tag.Key] = *tag.Value
}
logger.Sugar.Debugf("getTags BlockBlob URL %s tags: %v", identity, tags)
return tags, nil
}

Expand All @@ -56,7 +50,6 @@ func (azp *Storer) getMetadata(
ctx context.Context,
identity string,
) (map[string]string, error) {
logger.Sugar.Debugf("getMetadata BlockBlob URL %s", identity)

blobClient, err := azp.containerClient.NewBlobClient(identity)
if err != nil {
Expand All @@ -68,7 +61,6 @@ func (azp *Storer) getMetadata(
if err != nil {
return nil, ErrorFromError(err)
}
logger.Sugar.Debugf("getMetadata BlockBlob URL %v", resp.Metadata)
return resp.Metadata, nil
}

Expand All @@ -86,22 +78,18 @@ func (azp *Storer) Reader(
opt(options)
}

logger.Sugar.Debugf("Reader BlockBlob URL %s", identity)

resp := &ReaderResponse{}
blobAccessConditions, err := storerOptionConditions(options)
if err != nil {
return nil, err
}

if len(options.tags) > 0 || options.getTags {
logger.Sugar.Debugf("Get tags")
tags, tagsErr := azp.getTags(
ctx,
identity,
)
if tagsErr != nil {
logger.Sugar.Infof("cannot get tags: %v", tagsErr)
return nil, tagsErr
}
resp.Tags = tags
Expand All @@ -113,11 +101,9 @@ func (azp *Storer) Reader(
for k, requiredValue := range options.tags {
blobValue, ok := resp.Tags[k]
if !ok {
logger.Sugar.Infof("tag %s is not specified on blob", k)
return nil, NewStatusError(fmt.Sprintf("tag %s is not specified on blob", k), http.StatusNotFound)
}
if blobValue != requiredValue {
logger.Sugar.Infof("blob has different Tag %s than required %s", blobValue, requiredValue)
return nil, NewStatusError(fmt.Sprintf("blob has different Tag %s than required %s", blobValue, requiredValue), http.StatusNotFound)
}
}
Expand All @@ -130,7 +116,6 @@ func (azp *Storer) Reader(
identity,
)
if metadataErr != nil {
logger.Sugar.Infof("cannot get metadata: %v", metadataErr)
return nil, metadataErr
}
if parseErr := readerResponseMetadata(resp, metaData); parseErr != nil {
Expand All @@ -142,7 +127,12 @@ func (azp *Storer) Reader(
return resp, nil
}

logger.Sugar.Debugf("Creating New io.Reader")
// check if there is a container client
// as the storer may just have a service client
if azp.containerClient == nil {
return nil, errors.New("no container client available for reader")
}

resp.BlobClient, err = azp.containerClient.NewBlobClient(identity)
if err != nil {
return nil, ErrorFromError(err)
Expand All @@ -157,7 +147,6 @@ func (azp *Storer) Reader(
)

if err != nil && err == io.EOF { // nolint
logger.Sugar.Infof("cannot get blob body: %v", err)
return nil, ErrorFromError(err)
}

Expand Down
52 changes: 26 additions & 26 deletions azblob/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

azStorageBlob "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/datatrails/go-datatrails-common/logger"
)

/**
Expand All @@ -26,39 +25,38 @@ type Reader interface {

// NewReaderNoAuth is a azure blob reader client that has no credentials.
//
// The provided accountName is only used for logging purposes and may be empty
// The pro
//
// Paramaters:
//
// accountName: used only for logging purposes and may be empty
// url: The root path for the blob store requests, must not be empty
// container: To use the container client this must be provided. If absent only storage account level apis can be used
// accountName: used only for logging purposes and may be empty
// url: The root path for the blob store requests, must not be empty
// opts: optional arguments specific to creating a reader with no auth
//
// * WithContainer() - specifies the azblob container for point get in the container
// * WithAccountName() - specifies the azblob account name for logging
//
// NOTE: due to having no credentials, this can only read from public blob storage.
// or proxied private blob storage.
//
// NOTE: if no optional container is specified than the Reader() method on the interface
// will error, as we cannot create a container client reader.
//
// example:
//
// accountName: jitavid3b5fc07b9ae06f4e
// url: https://jitavid3b5fc07b9ae06f4e.blob.core.windows.net
// container: merklebuilder
func NewReaderNoAuth(accountName string, url string, container string) (Reader, error) {
logger.Sugar.Infof(
"New Reader for url: %s, with accountName: %s, for container: %s",
url, accountName, container,
)
// url: https://app.datatrails.ai/verifiabledata
func NewReaderNoAuth(url string, opts ...ReaderOption) (Reader, error) {

var err error
if url == "" {
return nil, errors.New("url is a required parameter and cannot be empty")
}

readerOptions := ParseReaderOptions(opts...)

azp := &Storer{
AccountName: accountName, // just for logging
ResourceGroup: "", // just for logging
Subscription: "", // just for logging
Container: container,
AccountName: readerOptions.accountName, // just for logging
ResourceGroup: "", // just for logging
Subscription: "", // just for logging
Container: readerOptions.container,
credential: nil,
rootURL: url,
}
Expand All @@ -67,23 +65,25 @@ func NewReaderNoAuth(accountName string, url string, container string) (Reader,
nil,
)
if err != nil {
logger.Sugar.Infof("unable to create serviceclient %s: %v", url, err)
return nil, err
honourfish marked this conversation as resolved.
Show resolved Hide resolved
}

if container == "" {
logger.Sugar.Infof("container not provided, container client not created")
return nil, nil
// check if we need the container client as well,
// if the container is an empty string just return the storer with
// the service client
//
// NOTE: Only listing is available to the serviceClient
if readerOptions.container == "" {
return azp, nil
}

azp.containerURL = fmt.Sprintf(
"%s%s",
url,
container,
readerOptions.container,
)
azp.containerClient, err = azp.serviceClient.NewContainerClient(container)
azp.containerClient, err = azp.serviceClient.NewContainerClient(readerOptions.container)
if err != nil {
logger.Sugar.Infof("unable to create containerclient %s: %v", container, err)
return nil, err
}

Expand Down
37 changes: 37 additions & 0 deletions azblob/readeroptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package azblob

// ReaderOptions - optional args for specifying optional behaviour for
//
// azblob readers
type ReaderOptions struct {
accountName string

container string
}

type ReaderOption func(*ReaderOptions)

// WithAccountName sets the azblob account name for the reader
func WithAccountName(accountName string) ReaderOption {
return func(a *ReaderOptions) {
a.accountName = accountName
}
}

// WithContainer sets the azblob container
func WithContainer(container string) ReaderOption {
return func(a *ReaderOptions) {
a.container = container
}
}

// ParseReaderOptions parses the given options into a ReaderOptions struct
func ParseReaderOptions(options ...ReaderOption) ReaderOptions {
readerOptions := ReaderOptions{}

for _, option := range options {
option(&readerOptions)
}

return readerOptions
}
Loading