Skip to content

Commit

Permalink
create new sub directory (#22177)
Browse files Browse the repository at this point in the history
* Implement NewSubdirectoryClient

* Lowercase d in subDirectoryName

Co-authored-by: Gauri Prasad <51212198+gapra-msft@users.noreply.github.com>

* Make test recorded and add API calls

* Extend testing

* Add recorded tests

* Update recorded test

* doc

* Update assets.json

* create subdirectory client

* changelog

* revert to unrecorded test

---------

Co-authored-by: Adele Reed <adreed@microsoft.com>
Co-authored-by: adreed-msft <49764384+adreed-msft@users.noreply.github.com>
Co-authored-by: Gauri Prasad <51212198+gapra-msft@users.noreply.github.com>
  • Loading branch information
4 people committed Jan 2, 2024
1 parent 2cd07a1 commit 50cdcbb
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
3 changes: 2 additions & 1 deletion sdk/storage/azdatalake/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features Added
* Encryption Scope For SAS
* CPK for Datalake
* Create SubDirectory Client

### Breaking Changes

Expand All @@ -22,4 +23,4 @@

### Features Added

* This is the initial preview release of the `azdatalake` library
* This is the initial preview release of the `azdatalake` library
2 changes: 1 addition & 1 deletion sdk/storage/azdatalake/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/storage/azdatalake",
"Tag": "go/storage/azdatalake_c883370a69"
"Tag": "go/storage/azdatalake_ba5bfaa832"
}
23 changes: 23 additions & 0 deletions sdk/storage/azdatalake/directory/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,29 @@ func (d *Client) NewFileClient(fileName string) (*file.Client, error) {
return (*file.Client)(base.NewPathClient(fileURL, newBlobURL, newBlobClient, d.generatedDirClientWithDFS().InternalClient().WithClientName(shared.FileClient), d.sharedKey(), d.identityCredential(), d.getClientOptions())), nil
}

// NewSubdirectoryClient creates a new directory.Client object by concatenating subdirectoryName to the end of this Client's URL.
// The new directory.Client uses the same request policy pipeline as the Client.
func (d *Client) NewSubdirectoryClient(subdirectoryName string) (*Client, error) {
subdirectoryName = url.PathEscape(strings.TrimRight(subdirectoryName, "/"))
subDirectoryURL := runtime.JoinPaths(d.DFSURL(), subdirectoryName)
newBlobURL, subDirectoryURL := shared.GetURLs(subDirectoryURL)
var newBlobClient *blockblob.Client
clientOptions := &blockblob.ClientOptions{ClientOptions: d.getClientOptions().ClientOptions}
var err error
if d.identityCredential() != nil {
newBlobClient, err = blockblob.NewClient(newBlobURL, *d.identityCredential(), clientOptions)
} else if d.sharedKey() != nil {
blobSharedKey, _ := exported.ConvertToBlobSharedKey(d.sharedKey())
newBlobClient, err = blockblob.NewClientWithSharedKeyCredential(newBlobURL, blobSharedKey, clientOptions)
} else {
newBlobClient, err = blockblob.NewClientWithNoCredential(newBlobURL, clientOptions)
}
if err != nil {
return nil, exported.ConvertToDFSError(err)
}
return (*Client)(base.NewPathClient(subDirectoryURL, newBlobURL, newBlobClient, d.generatedDirClientWithDFS().InternalClient().WithClientName(shared.DirectoryClient), d.sharedKey(), d.identityCredential(), d.getClientOptions())), nil
}

// Create creates a new directory.
func (d *Client) Create(ctx context.Context, options *CreateOptions) (CreateResponse, error) {
lac, mac, httpHeaders, createOpts, cpkOpts := options.format()
Expand Down
65 changes: 64 additions & 1 deletion sdk/storage/azdatalake/directory/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ type UnrecordedTestSuite struct {
}

func (s *UnrecordedTestSuite) TestCreateDirAndDeleteWithConnectionString() {

_require := require.New(s.T())
testName := s.T().Name()

Expand Down Expand Up @@ -202,6 +201,70 @@ func (s *RecordedTestSuite) TestGetAndCreateFileClient() {
_require.NoError(err)
}

func (s *RecordedTestSuite) TestCreateNewSubdirectoryClient() {
_require := require.New(s.T())
testName := s.T().Name()

accountName, _ := testcommon.GetGenericAccountInfo(testcommon.TestAccountDatalake)
_require.Greater(len(accountName), 0)

svcClient, err := testcommon.GetServiceClient(s.T(), testcommon.TestAccountDatalake, nil)
_require.NoError(err)

fsName := testcommon.GenerateFileSystemName(testName)
fsClient := svcClient.NewFileSystemClient(fsName)

_, err = fsClient.Create(context.Background(), nil)
_require.NoError(err)

defer testcommon.DeleteFileSystem(context.Background(), _require, fsClient)

dirName := testcommon.GenerateDirName(testName)
dirClient := fsClient.NewDirectoryClient(dirName)

_, err = dirClient.Create(context.Background(), nil)
_require.NoError(err)

subdirName := testcommon.GenerateSubDirName(testName)
subdirClient, err := dirClient.NewSubdirectoryClient(subdirName)
_require.NoError(err)

perm := "r-xr-x---"

_, err = subdirClient.Create(context.Background(), &directory.CreateOptions{
Permissions: &perm,
CPKInfo: &testcommon.TestCPKByValue,
})
_require.NoError(err)

resp, err := subdirClient.GetProperties(context.Background(), &directory.GetPropertiesOptions{CPKInfo: &testcommon.TestCPKByValue})
_require.NoError(err)
_require.NotNil(resp.Permissions)
_require.Equal(perm, *resp.Permissions)
_require.Equal(*(resp.IsServerEncrypted), true)
_require.Equal(resp.EncryptionKeySHA256, testcommon.TestCPKByValue.EncryptionKeySHA256)

// Create a file under the new directory just to make sure we're not secretly targeting the parent
fileName := testcommon.GenerateFileName("newFile")
subdirFileClient, err := subdirClient.NewFileClient(fileName)
_require.NoError(err)

_, err = subdirFileClient.Create(context.Background(), nil)
_require.NoError(err)

fileContents := []byte(testcommon.DefaultData)
err = subdirFileClient.UploadBuffer(context.Background(), fileContents, nil)
_require.NoError(err)

// check for the file at the parent directory
dirFileClient, err := dirClient.NewFileClient(fileName)
_require.NoError(err)

_, err = dirFileClient.GetProperties(context.Background(), nil)
_require.Error(err) // we should get back a 404
_require.True(datalakeerror.HasCode(err, datalakeerror.PathNotFound))
}

func (s *RecordedTestSuite) TestCreateDirWithNilAccessConditions() {
_require := require.New(s.T())
testName := s.T().Name()
Expand Down
5 changes: 5 additions & 0 deletions sdk/storage/azdatalake/internal/testcommon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
FileSystemPrefix = "gofs"
FilePrefix = "gotestfile"
DirPrefix = "gotestdir"
SubDirPrefix = "gotestsubdir"
DefaultData = "Godatalakedata"
InvalidHeaderErrorSubstring = "invalid header field" // error thrown by the http client
)
Expand All @@ -44,6 +45,10 @@ func GenerateDirName(testName string) string {
return DirPrefix + GenerateEntityName(testName)
}

func GenerateSubDirName(testName string) string {
return SubDirPrefix + GenerateEntityName(testName)
}

func GenerateEntityName(testName string) string {
return strings.ReplaceAll(strings.ReplaceAll(strings.ToLower(testName), "/", ""), "test", "")
}
Expand Down

0 comments on commit 50cdcbb

Please sign in to comment.