diff --git a/src/Storage/Storage.Management.Test/ScenarioTests/StorageDataPlaneTests.ps1 b/src/Storage/Storage.Management.Test/ScenarioTests/StorageDataPlaneTests.ps1 index 8ee3d115becb..a885da945da0 100644 --- a/src/Storage/Storage.Management.Test/ScenarioTests/StorageDataPlaneTests.ps1 +++ b/src/Storage/Storage.Management.Test/ScenarioTests/StorageDataPlaneTests.ps1 @@ -110,12 +110,25 @@ function Test-File Assert-AreEqual $localFileProperties.Attributes.ToString() $file[0].FileProperties.SmbProperties.FileAttributes.ToString() } - Remove-AzStorageFile -ShareName $shareName -Path $objectName1 -Context $storageContext + $fileName1 = "new" + $objectName1 + $file = Get-AzStorageFile -ShareName $shareName -Path $objectName1 -Context $storageContext + + $file2 = Rename-AzStorageFile -ShareName $shareName -SourcePath $objectName1 -DestinationPath $fileName1 -Context $storageContext + Assert-AreEqual $file2.Name $fileName1 + Assert-AreEqual $file.FileProperties.ContentType $file2.FileProperties.ContentType + Assert-AreEqual $file.FileProperties.ContentLength $file2.FileProperties.ContentLength + + $file3 = $file2 | Rename-AzStorageFile -DestinationPath $fileName1 -Context $storageContext -Force + Assert-AreEqual $file3.Name $fileName1 + Assert-AreEqual $file2.FileProperties.ContentType $file3.FileProperties.ContentType + Assert-AreEqual $file2.FileProperties.ContentLength $file3.FileProperties.ContentLength + + Remove-AzStorageFile -ShareName $shareName -Path $fileName1 -Context $storageContext $file = Get-AzStorageFile -ShareName $shareName -Context $storageContext Assert-AreEqual $file.Count 1 Assert-AreEqual $file[0].Name $objectName2 - $dirName = "filetestdir" + $dirName = "filetestdir" New-AzStorageDirectory -ShareName $shareName -Path $dirName -Context $storageContext $file = Get-AzStorageShare -Name $shareName -Context $storageContext | Get-AzStorageFile -ExcludeExtendedInfo Assert-AreEqual $file.Count 2 @@ -125,11 +138,25 @@ function Test-File Assert-AreEqual $file[1].Name $objectName2 Assert-AreEqual $file[1].GetType().Name "AzureStorageFile" Assert-Null $file[1].ListFileProperties.Properties.ETag - Get-AzStorageFile -ShareName $shareName -Path $dirName -Context $storageContext | Remove-AzStorageDirectory + + $newDir = "new" + $dirName + $dir = Get-AzStorageFile -ShareName $shareName -Path $dirName -Context $storageContext + $dir2 = Rename-AzStorageDirectory -ShareName $shareName -SourcePath $dirName -DestinationPath $newDir -Context $storageContext + Assert-AreEqual $newDir $dir2.Name + Assert-AreEqual $dir.ListFileProperties.IsDirectory $dir2.ListFileProperties.IsDirectory + Assert-AreEqual $dir.ListFileProperties.FileAttributes $dir2.ListFileProperties.FileAttributes + + $newDir2 = "new2" + $dirName + $dir3 = $dir2 | Rename-AzStorageDirectory -DestinationPath $newDir2 -Context $storageContext + Assert-AreEqual $newDir2 $dir3.Name + Assert-AreEqual $dir2.ListFileProperties.IsDirectory $dir3.ListFileProperties.IsDirectory + Assert-AreEqual $dir2.ListFileProperties.FileAttributes $dir3.ListFileProperties.FileAttributes + + $dir3 | Remove-AzStorageDirectory $file = Get-AzStorageFile -ShareName $shareName -Context $storageContext Assert-AreEqual $file.Count 1 Assert-AreEqual $file[0].Name $objectName2 - Assert-AreEqual $file[0].GetType().Name "AzureStorageFile" + Assert-AreEqual $file[0].GetType().Name "AzureStorageFile" # Clean Storage Account Remove-AzStorageShare -Name $shareName -Force -Context $storageContext diff --git a/src/Storage/Storage.Management/Az.Storage.psd1 b/src/Storage/Storage.Management/Az.Storage.psd1 index 89b115507685..42c5171796fa 100644 --- a/src/Storage/Storage.Management/Az.Storage.psd1 +++ b/src/Storage/Storage.Management/Az.Storage.psd1 @@ -206,7 +206,8 @@ CmdletsToExport = 'Get-AzStorageAccount', 'Get-AzStorageAccountKey', 'New-AzStorageLocalUserSshPublicKey', 'New-AzStorageLocalUserPermissionScope', 'Get-AzDataLakeGen2DeletedItem', - 'Restore-AzDataLakeGen2DeletedItem' + 'Restore-AzDataLakeGen2DeletedItem', + 'Rename-AzStorageFile', 'Rename-AzStorageDirectory' # Variables to export from this module # VariablesToExport = @() diff --git a/src/Storage/Storage.Management/ChangeLog.md b/src/Storage/Storage.Management/ChangeLog.md index 02e4ddf0c834..ef373b2317b7 100644 --- a/src/Storage/Storage.Management/ChangeLog.md +++ b/src/Storage/Storage.Management/ChangeLog.md @@ -18,6 +18,9 @@ - Additional information about change #1 --> ## Upcoming Release +* Supported rename file and directory + - `Rename-AzStorageFile` + - `Rename-AzStorageDirectory` * Added a warning message for an upcoming breaking change when getting a single blob - `Get-AzStorageBlob` * Fixed the issue of listing blobs with leading slashes diff --git a/src/Storage/Storage.Management/help/Rename-AzStorageDirectory.md b/src/Storage/Storage.Management/help/Rename-AzStorageDirectory.md new file mode 100644 index 000000000000..4805ba141bbd --- /dev/null +++ b/src/Storage/Storage.Management/help/Rename-AzStorageDirectory.md @@ -0,0 +1,259 @@ +--- +external help file: Microsoft.Azure.PowerShell.Cmdlets.Storage.dll-Help.xml +Module Name: Az.Storage +online version: https://learn.microsoft.com/powershell/module/az.storage/rename-azstoragedirectory +schema: 2.0.0 +--- + +# Rename-AzStorageDirectory + +## SYNOPSIS +Renames a directory. + +## SYNTAX + +### ShareName +``` +Rename-AzStorageDirectory [-ShareName] [-SourcePath] [[-DestinationPath] ] + [-Permission ] [-Force] [-AsJob] [-Context ] + [-DefaultProfile ] [-IgnoreReadonly] [] +``` + +### ShareObject +``` +Rename-AzStorageDirectory [-ShareClient] [-SourcePath] [[-DestinationPath] ] + [-Permission ] [-Force] [-AsJob] [-Context ] + [-DefaultProfile ] [-IgnoreReadonly] [] +``` + +### DirecotryObject +``` +Rename-AzStorageDirectory [-ShareDirectoryClient] [[-DestinationPath] ] + [-Permission ] [-Force] [-AsJob] [-Context ] + [-DefaultProfile ] [-IgnoreReadonly] [] +``` + +## DESCRIPTION +The **Rename-AzStorageDirectory** cmdlet renames a directory from a file share. + +## EXAMPLES + +### Example 1 : Rename a directory +```powershell +Rename-AzStorageDirectory -ShareName myshare -SourcePath testdir1 -DestinationPath testdir2 +``` + +```output + Directory: https://myaccount.file.core.windows.net/myshare + +Type Length Name +---- ------ ---- +Directory 1 testdir2 +``` + +This command renames a directory from testdir1 to testdir2. + +### Example 2 : Rename a directory using pipeline +```powershell +Get-AzStorageFile -ShareName myshare -Path testdir1 | Rename-AzStorageDirectory -DestinationPath testdir2 +``` + +```output + Directory: https://myaccount.file.core.windows.net/myshare + +Type Length Name +---- ------ ---- +Directory 1 testdir2 +``` + +This command gets a directory from a file share first, and then rename the directory from testdir1 to testdir2 using pipeline. + +## PARAMETERS + +### -AsJob +Run cmdlet in the background + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Context +Azure Storage Context Object + +```yaml +Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -DefaultProfile +The credentials, account, tenant, and subscription used for communication with Azure. + +```yaml +Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer +Parameter Sets: (All) +Aliases: AzureRmContext, AzureCredential + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -DestinationPath +The destination path to rename the directory to. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: 2 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Force +Force to overwrite the existing file. + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -IgnoreReadonly +Optional. +Specifies whether the ReadOnly attribute on a preexisting destination file should be respected. +If true, the rename will succeed, otherwise, a previous file at the destination with the ReadOnly attribute set will cause the rename to fail. + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Permission +If specified the permission (security descriptor) shall be set for the directory/file. +Default value: Inherit. +If SDDL is specified as input, it must have owner, group and dacl. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -ShareClient +ShareClienr indicated the share where the directory would be listed. + +```yaml +Type: Azure.Storage.Files.Shares.ShareClient +Parameter Sets: ShareObject +Aliases: + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -ShareDirectoryClient +Source directory instance + +```yaml +Type: Azure.Storage.Files.Shares.ShareDirectoryClient +Parameter Sets: DirecotryObject +Aliases: + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -ShareName +Name of the file share where the directory would be listed. + +```yaml +Type: System.String +Parameter Sets: ShareName +Aliases: + +Required: True +Position: 0 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SourcePath +Path to an existing directory. + +```yaml +Type: System.String +Parameter Sets: ShareName, ShareObject +Aliases: + +Required: True +Position: 1 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +### Azure.Storage.Files.Shares.ShareClient + +### Azure.Storage.Files.Shares.ShareDirectoryClient + +### Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext + +## OUTPUTS + +### Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileDirectory + +## NOTES + +## RELATED LINKS diff --git a/src/Storage/Storage.Management/help/Rename-AzStorageFile.md b/src/Storage/Storage.Management/help/Rename-AzStorageFile.md new file mode 100644 index 000000000000..da38b528ad4d --- /dev/null +++ b/src/Storage/Storage.Management/help/Rename-AzStorageFile.md @@ -0,0 +1,300 @@ +--- +external help file: Microsoft.Azure.PowerShell.Cmdlets.Storage.dll-Help.xml +Module Name: Az.Storage +online version: https://learn.microsoft.com/powershell/module/az.storage/rename-azstoragefile + +schema: 2.0.0 +--- + +# Rename-AzStorageFile + +## SYNOPSIS +Renames a file. + +## SYNTAX + +### ShareName +``` +Rename-AzStorageFile [-ShareName] [-SourcePath] [[-DestinationPath] ] + [-ContentType ] [-Permission ] [-Force] [-AsJob] [-Context ] + [-DefaultProfile ] [-IgnoreReadonly] [] +``` + +### FileObject +``` +Rename-AzStorageFile [-ShareFileClient] [[-DestinationPath] ] [-ContentType ] + [-Permission ] [-Force] [-AsJob] [-Context ] + [-DefaultProfile ] [-IgnoreReadonly] [] +``` + +### ShareObject +``` +Rename-AzStorageFile [-ShareClient] [-SourcePath] [[-DestinationPath] ] + [-ContentType ] [-Permission ] [-Force] [-AsJob] [-Context ] + [-DefaultProfile ] [-IgnoreReadonly] [] +``` + +### DirecotryObject +``` +Rename-AzStorageFile [-ShareDirectoryClient] [-SourcePath] + [[-DestinationPath] ] [-ContentType ] [-Permission ] [-Force] [-AsJob] + [-Context ] [-DefaultProfile ] [-IgnoreReadonly] [] +``` + +## DESCRIPTION +The **Rename-AzStorageFile** cmdlet renames a directory from a file share. + +## EXAMPLES + +### Example 1 : Rename a file from a file share +```powershell +Rename-AzStorageFile -SourcePath testfile1 -DestinationPath testfile2 -ShareName myshare +``` + +```output +Directory: https://myaccount.file.core.windows.net/myshare + +Type Length Name +---- ------ ---- +File 512 testfile2 +``` + +This command renames a file from testfile1 to testfile2 under file share myshare. + +### Example 2 : Rename a file from a file share using pipeline +```powershell +Get-AzStorageFile -ShareName myshare -Path testfile1 | Rename-AzStorageFile -DestinationPath testfile2 +``` + +```output +Directory: https://myaccount.file.core.windows.net/myshare + +Type Length Name +---- ------ ---- +File 512 testfile2 +``` + +This command gets a file client object first, and the rename the file from testfile1 to testfile2 using pipeline. + +## PARAMETERS + +### -AsJob +Run cmdlet in the background + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -ContentType +Sets the MIME content type of the file. +The default type is 'application/octet-stream'. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Context +Azure Storage Context Object + +```yaml +Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -DefaultProfile +The credentials, account, tenant, and subscription used for communication with Azure. + +```yaml +Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer +Parameter Sets: (All) +Aliases: AzureRmContext, AzureCredential + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -DestinationPath +The destination path to rename the file to. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: 1 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Force +Force to overwrite the existing file. + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -IgnoreReadonly +Optional. +Specifies whether the ReadOnly attribute on a preexisting destination file should be respected. +If true, the rename will succeed, otherwise, a previous file at the destination with the ReadOnly attribute set will cause the rename to fail. + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Permission +If specified the permission (security descriptor) shall be set for the directory/file. +Default value: Inherit. +If SDDL is specified as input, it must have owner, group and dacl. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -ShareClient +ShareClient indicated the share where the file would be listed. + +```yaml +Type: Azure.Storage.Files.Shares.ShareClient +Parameter Sets: ShareObject +Aliases: + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -ShareDirectoryClient +ShareDirectoryClient indicated the share where the file would be listed. + +```yaml +Type: Azure.Storage.Files.Shares.ShareDirectoryClient +Parameter Sets: DirecotryObject +Aliases: + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -ShareFileClient +Source file instance + +```yaml +Type: Azure.Storage.Files.Shares.ShareFileClient +Parameter Sets: FileObject +Aliases: + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -ShareName +Name of the file share where the file would be listed. + +```yaml +Type: System.String +Parameter Sets: ShareName +Aliases: + +Required: True +Position: 0 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SourcePath +Path to an existing file. + +```yaml +Type: System.String +Parameter Sets: ShareName, ShareObject, DirecotryObject +Aliases: + +Required: True +Position: 1 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +### Azure.Storage.Files.Shares.ShareFileClient + +### Azure.Storage.Files.Shares.ShareClient + +### Azure.Storage.Files.Shares.ShareDirectoryClient + +### Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext + +## OUTPUTS + +### Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFile + +## NOTES + +## RELATED LINKS diff --git a/src/Storage/Storage/File/Cmdlet/RenameAzureStorageDirectory.cs b/src/Storage/Storage/File/Cmdlet/RenameAzureStorageDirectory.cs new file mode 100644 index 000000000000..20f0125b5ce9 --- /dev/null +++ b/src/Storage/Storage/File/Cmdlet/RenameAzureStorageDirectory.cs @@ -0,0 +1,151 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using Azure.Storage.Files.Shares; +using Azure.Storage.Files.Shares.Models; +using Azure.Storage.Files.Shares.Specialized; +using Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel; +using Microsoft.WindowsAzure.Commands.Storage.Common; +using System.Globalization; +using System.Management.Automation; + +namespace Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet +{ + [Cmdlet("Rename", Azure.Commands.ResourceManager.Common.AzureRMConstants.AzurePrefix + "StorageDirectory", SupportsShouldProcess = true, DefaultParameterSetName = ShareNameParameterSet)] + [OutputType(typeof(AzureStorageFileDirectory))] + public class RenameAzureStorageDirectory : StorageFileDataManagementCmdletBase + { + private const string ShareNameParameterSet = "ShareName"; + private const string DirectoryObjectParameterSet = "DirecotryObject"; + private const string ShareObjectParameterSet = "ShareObject"; + + [Parameter( + Position = 0, + Mandatory = true, + ParameterSetName = ShareNameParameterSet, + HelpMessage = "Name of the file share where the directory would be listed.")] + [ValidateNotNullOrEmpty] + public string ShareName { get; set; } + + [Parameter( + Position = 0, + Mandatory = true, + ValueFromPipeline = true, + ValueFromPipelineByPropertyName = true, + ParameterSetName = ShareObjectParameterSet, + HelpMessage = "ShareClienr indicated the share where the directory would be listed.")] + [ValidateNotNull] + public ShareClient ShareClient { get; set; } + + [Parameter( + Position = 0, + Mandatory = true, + ValueFromPipeline = true, + ValueFromPipelineByPropertyName = true, + ParameterSetName = DirectoryObjectParameterSet, + HelpMessage = "Source directory instance")] + public ShareDirectoryClient ShareDirectoryClient { get; set; } + + [Parameter( + Position = 1, + Mandatory = true, + ParameterSetName = ShareNameParameterSet, + HelpMessage = "Path to an existing directory.")] + [Parameter( + Position = 1, + Mandatory = true, + ParameterSetName = ShareObjectParameterSet, + HelpMessage = "Path to an existing directory.")] + [ValidateNotNullOrEmpty] + public string SourcePath { get; set; } + + [Parameter( + Position = 2, + ParameterSetName = ShareNameParameterSet, + HelpMessage = "The destination path to rename the directory to.")] + [Parameter( + Position = 2, + ParameterSetName = ShareObjectParameterSet, + HelpMessage = "The destination path to rename the directory to.")] + [Parameter( + Position = 2, + ParameterSetName = DirectoryObjectParameterSet, + HelpMessage = "The destination path to rename the directory to.")] + public string DestinationPath { get; set; } + + [Parameter(Mandatory = false, + HelpMessage = "Optional. Specifies whether the ReadOnly attribute on a preexisting destination " + + "file should be respected. If true, the rename will succeed, otherwise, a previous file at the destination with " + + "the ReadOnly attribute set will cause the rename to fail.")] + public SwitchParameter IgnoreReadonly; + + [Parameter(Mandatory = false, + HelpMessage = "If specified the permission (security descriptor) shall be set for the directory/file. " + + "Default value: Inherit. If SDDL is specified as input, it must have owner, group and dacl.")] + public string Permission { get; set; } + + public override int? ClientTimeoutPerRequest { get; set; } + public override int? ServerTimeoutPerRequest { get; set; } + public override int? ConcurrentTaskCount { get; set; } + + public override void ExecuteCmdlet() + { + ShareDirectoryClient srcDirectoryClient; + ShareFileClient destFileClient; + + ShareServiceClient fileServiceClient = Util.GetTrack2FileServiceClient((AzureStorageContext)this.Context, ClientOptions); + + switch (ParameterSetName) + { + case ShareNameParameterSet: + srcDirectoryClient = fileServiceClient.GetShareClient(this.ShareName).GetDirectoryClient(this.SourcePath); + destFileClient = fileServiceClient.GetShareClient(this.ShareName).GetRootDirectoryClient().GetFileClient(this.DestinationPath); + break; + case ShareObjectParameterSet: + srcDirectoryClient = this.ShareClient.GetDirectoryClient(this.SourcePath); + destFileClient = this.ShareClient.GetRootDirectoryClient().GetFileClient(this.DestinationPath); + break; + case DirectoryObjectParameterSet: + srcDirectoryClient = this.ShareDirectoryClient; + destFileClient = srcDirectoryClient.GetParentShareClient().GetRootDirectoryClient().GetFileClient(this.DestinationPath); + break; + default: + throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); + } + + if (ShouldProcess(srcDirectoryClient.Name, VerbsCommon.Rename)) + { + // Throw exception and quit if input path is not a directory + srcDirectoryClient.GetProperties(); + + bool destFileExist = destFileClient.Exists(); + + if (!destFileExist || this.Force || ShouldContinue(string.Format(Resources.OverwriteConfirmation, destFileClient.Uri.ToString()), null)) + { + ShareFileRenameOptions options = new ShareFileRenameOptions + { + ReplaceIfExists = true, + IgnoreReadOnly = this.IgnoreReadonly, + FilePermission = this.Permission, + }; + + ShareDirectoryClient destDirectoryClient = srcDirectoryClient.Rename(this.DestinationPath, options, this.CmdletCancellationToken); + + ShareDirectoryProperties dirProperties = destDirectoryClient.GetProperties(this.CmdletCancellationToken).Value; + WriteObject(new AzureStorageFileDirectory(destDirectoryClient, (AzureStorageContext)this.Context, dirProperties, ClientOptions)); + } + } + } + } +} diff --git a/src/Storage/Storage/File/Cmdlet/RenameAzureStorageFile.cs b/src/Storage/Storage/File/Cmdlet/RenameAzureStorageFile.cs new file mode 100644 index 000000000000..8831b6972e7a --- /dev/null +++ b/src/Storage/Storage/File/Cmdlet/RenameAzureStorageFile.cs @@ -0,0 +1,180 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using Azure.Storage.Files.Shares; +using Azure.Storage.Files.Shares.Models; +using Azure.Storage.Files.Shares.Specialized; +using Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel; +using Microsoft.WindowsAzure.Commands.Storage.Common; +using System.Globalization; +using System.Management.Automation; + +namespace Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet +{ + [Cmdlet("Rename", Azure.Commands.ResourceManager.Common.AzureRMConstants.AzurePrefix + "StorageFile", SupportsShouldProcess = true, DefaultParameterSetName = ShareNameParameterSet)] + [OutputType(typeof(AzureStorageFile))] + public class RenameAzureStorageFile : StorageFileDataManagementCmdletBase + { + private const string ShareNameParameterSet = "ShareName"; + private const string FileObjectParameterSet = "FileObject"; + private const string DirectoryObjectParameterSet = "DirecotryObject"; + private const string ShareObjectParameterSet = "ShareObject"; + + [Parameter( + Position = 0, + Mandatory = true, + ParameterSetName = ShareNameParameterSet, + HelpMessage = "Name of the file share where the file would be listed.")] + [ValidateNotNullOrEmpty] + public string ShareName { get; set; } + + [Parameter( + Position = 0, + Mandatory = true, + ValueFromPipeline = true, + ValueFromPipelineByPropertyName = true, + ParameterSetName = FileObjectParameterSet, + HelpMessage = "Source file instance")] + [ValidateNotNull] + public ShareFileClient ShareFileClient { get; set; } + + [Parameter( + Mandatory = true, + Position = 0, + ValueFromPipeline = true, + ValueFromPipelineByPropertyName = true, + ParameterSetName = ShareObjectParameterSet, + HelpMessage = "ShareClient indicated the share where the file would be listed.")] + [ValidateNotNull] + public ShareClient ShareClient { get; set; } + + [Parameter( + Mandatory = true, + Position = 0, + ValueFromPipeline = true, + ValueFromPipelineByPropertyName = true, + ParameterSetName = DirectoryObjectParameterSet, + HelpMessage = "ShareDirectoryClient indicated the share where the file would be listed.")] + [ValidateNotNull] + public ShareDirectoryClient ShareDirectoryClient { get; set; } + + [Parameter( + Position = 1, + Mandatory = true, + ParameterSetName = ShareNameParameterSet, + HelpMessage = "Path to an existing file.")] + [Parameter( + Position = 1, + Mandatory = true, + ParameterSetName = ShareObjectParameterSet, + HelpMessage = "Path to an existing file.")] + [Parameter( + Position = 1, + Mandatory = true, + ParameterSetName = DirectoryObjectParameterSet, + HelpMessage = "Path to an existing file.")] + [ValidateNotNullOrEmpty] + public string SourcePath { get; set; } + + [Parameter( + Position = 1, + ParameterSetName = FileObjectParameterSet, + HelpMessage = "The destination path to rename the file to.")] + [Parameter( + Position = 2, + ParameterSetName = ShareNameParameterSet, + HelpMessage = "The destination path to rename the file to.")] + [Parameter( + Position = 2, + ParameterSetName = ShareObjectParameterSet, + HelpMessage = "The destination path to rename the file to.")] + [Parameter( + Position = 2, + ParameterSetName = DirectoryObjectParameterSet, + HelpMessage = "The destination path to rename the file to.")] + public string DestinationPath { get; set; } + + [Parameter(Mandatory = false, + HelpMessage = "Optional. Specifies whether the ReadOnly attribute on a preexisting destination " + + "file should be respected. If true, the rename will succeed, otherwise, a previous file at the destination with " + + "the ReadOnly attribute set will cause the rename to fail.")] + public SwitchParameter IgnoreReadonly; + + [Parameter(Mandatory = false, + HelpMessage = "Sets the MIME content type of the file. The default type is 'application/octet-stream'.")] + public string ContentType { get; set; } + + [Parameter(Mandatory = false, + HelpMessage = "If specified the permission (security descriptor) shall be set for the directory/file. " + + "Default value: Inherit. If SDDL is specified as input, it must have owner, group and dacl.")] + public string Permission { get; set; } + + public override int? ClientTimeoutPerRequest { get; set; } + public override int? ServerTimeoutPerRequest { get; set; } + public override int? ConcurrentTaskCount { get; set; } + + public override void ExecuteCmdlet() + { + ShareFileClient srcFileClient; + ShareFileClient destFileClient; + + ShareServiceClient fileServiceClient = Util.GetTrack2FileServiceClient((AzureStorageContext)this.Context, ClientOptions); + + switch (ParameterSetName) + { + case ShareNameParameterSet: + srcFileClient = fileServiceClient.GetShareClient(this.ShareName).GetRootDirectoryClient().GetFileClient(this.SourcePath); + destFileClient = fileServiceClient.GetShareClient(this.ShareName).GetRootDirectoryClient().GetFileClient(this.DestinationPath); + break; + case FileObjectParameterSet: + srcFileClient = this.ShareFileClient; + destFileClient = srcFileClient.GetParentShareClient().GetRootDirectoryClient().GetFileClient(this.DestinationPath); + break; + case ShareObjectParameterSet: + srcFileClient = this.ShareClient.GetRootDirectoryClient().GetFileClient(this.SourcePath); + destFileClient = this.ShareClient.GetRootDirectoryClient().GetFileClient(this.DestinationPath); + break; + case DirectoryObjectParameterSet: + srcFileClient = this.ShareDirectoryClient.GetFileClient(this.SourcePath); + destFileClient = srcFileClient.GetParentShareClient().GetRootDirectoryClient().GetFileClient(this.DestinationPath); + break; + default: + throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); + } + + if (ShouldProcess(srcFileClient.Name, VerbsCommon.Rename)) + { + // Throw exception and quit directly if input path is not a file + srcFileClient.GetProperties(this.CmdletCancellationToken); + + bool destExist = destFileClient.Exists(); + if (!destExist || this.Force || ShouldContinue(string.Format(Resources.OverwriteConfirmation, destFileClient.Uri.ToString()), null)) + { + ShareFileRenameOptions options = new ShareFileRenameOptions + { + ReplaceIfExists = true, + IgnoreReadOnly = this.IgnoreReadonly, + ContentType = this.ContentType, + FilePermission = this.Permission, + }; + + destFileClient = srcFileClient.Rename(this.DestinationPath, options, this.CmdletCancellationToken); + + ShareFileProperties fileProperties = destFileClient.GetProperties(this.CmdletCancellationToken).Value; + WriteObject(new AzureStorageFile(destFileClient, (AzureStorageContext)this.Context, fileProperties, ClientOptions)); + } + } + } + } +}