Skip to content

Commit c6cb271

Browse files
author
dragonfly91
committed
Get-AzureBackupContainer cmdlet part1
1 parent b9ca714 commit c6cb271

File tree

8 files changed

+339
-2
lines changed

8 files changed

+339
-2
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Management.Automation;
17+
using System.Collections.Generic;
18+
using System.Xml;
19+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
20+
using Microsoft.Azure.Common.Authentication;
21+
using Microsoft.Azure.Common.Authentication.Models;
22+
using System.Threading;
23+
using Hyak.Common;
24+
using Microsoft.Azure.Commands.AzureBackup.Properties;
25+
using System.Net;
26+
using System.Linq;
27+
using Microsoft.WindowsAzure.Management.Scheduler;
28+
using Microsoft.Azure.Management.BackupServices;
29+
using Microsoft.Azure.Management.BackupServices.Models;
30+
using Microsoft.Azure.Commands.AzureBackup.Models;
31+
32+
namespace Microsoft.Azure.Commands.AzureBackup.ClientAdapter
33+
{
34+
public partial class AzureBackupClientAdapter
35+
{
36+
/// <summary>
37+
/// Gets all MARS containers in the vault
38+
/// </summary>
39+
/// <returns></returns>
40+
public IEnumerable<MarsContainerResponse> ListMachineContainers()
41+
{
42+
var listResponse = AzureBackupVaultClient.Container.ListMarsContainersByType(MarsContainerType.Machine, GetCustomRequestHeaders());
43+
return listResponse.ListMarsContainerResponse.Value;
44+
}
45+
46+
/// <summary>
47+
/// Gets all MARS containers in the vault which match the friendly name
48+
/// </summary>
49+
/// <param name="friendlyName">The friendly name of the container</param>
50+
/// <returns></returns>
51+
public IEnumerable<MarsContainerResponse> ListMachineContainers(string friendlyName)
52+
{
53+
var listResponse = AzureBackupVaultClient.Container.ListMarsContainersByTypeAndFriendlyName(MarsContainerType.Machine, friendlyName, GetCustomRequestHeaders());
54+
return listResponse.ListMarsContainerResponse.Value;
55+
}
56+
57+
///// <summary>
58+
///// Register container
59+
///// </summary>
60+
///// <param name="containerName"></param>
61+
///// <returns></returns>
62+
//public Guid RegisterContainer(string containerName)
63+
//{
64+
// var response = AzureBackupVaultClient.Container.RegisterAsync(containerName, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
65+
// return response.OperationId;
66+
//}
67+
68+
///// <summary>
69+
///// UnRegister container
70+
///// </summary>
71+
///// <param name="containerName"></param>
72+
///// <returns></returns>
73+
//public Guid UnRegisterContainer(string containerName)
74+
//{
75+
// var response = AzureBackupVaultClient.Container.UnregisterAsync(containerName, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
76+
// return response.OperationId;
77+
//}
78+
}
79+
}

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupCmdletHelpMessage.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ internal static class AzureBackupCmdletHelpMessage
2222
public const string Location = "The resource location.";
2323
public const string TargetLocation = "The directory where the credentials file will be saved. This must be an absolute path.";
2424
public const string StorageType = "The vault back-end storage type.";
25+
public const string ManagedResourceName = "The resource name of the resource being managed by the Azure Backup service (ex: resource name of the VM).";
26+
public const string ManagedResourceGroupName = "The resource group name of the resource being managed by the Azure Backup service (ex: resource group name of the VM).";
27+
public const string ContainerRegistrationStatus = "The container registration status.";
28+
public const string ContainerType = "The container type.";
2529
}
2630
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.AzureBackup.Helpers;
16+
using Microsoft.Azure.Commands.AzureBackup.Library;
17+
using Microsoft.Azure.Commands.AzureBackup.Models;
18+
using Microsoft.Azure.Management.BackupServices.Models;
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq;
22+
using System.Management.Automation;
23+
using System.Text;
24+
using System.Threading.Tasks;
25+
26+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
27+
{
28+
/// <summary>
29+
/// Get list of containers
30+
/// </summary>
31+
[Cmdlet(VerbsCommon.Get, "AzureBackupContainer"), OutputType(typeof(AzureBackupContainer), typeof(List<AzureBackupContainer>))]
32+
public class GetAzureBackupContainer : AzureBackupVaultCmdletBase
33+
{
34+
//[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ManagedResourceGroupName)]
35+
//[ValidateNotNullOrEmpty]
36+
//public string ManagedResourceGroupName { get; set; }
37+
38+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ManagedResourceName)]
39+
[ValidateNotNullOrEmpty]
40+
public string ManagedResourceName { get; set; }
41+
42+
//[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ContainerRegistrationStatus)]
43+
//[ValidateNotNullOrEmpty]
44+
//public AzureBackupContainerStatusInput Status { get; set; }
45+
46+
[Parameter(Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ContainerType)]
47+
[ValidateNotNullOrEmpty]
48+
public AzureBackupContainerTypeInput Type { get; set; }
49+
50+
public override void ExecuteCmdlet()
51+
{
52+
ExecutionBlock(() =>
53+
{
54+
base.ExecuteCmdlet();
55+
56+
List<AzureBackupContainer> containers = new List<AzureBackupContainer>();
57+
58+
switch (Type)
59+
{
60+
case AzureBackupContainerTypeInput.Windows:
61+
case AzureBackupContainerTypeInput.SCDPM:
62+
containers.AddRange(GetMachineContainers());
63+
break;
64+
default:
65+
break;
66+
}
67+
68+
WriteObject(containers);
69+
});
70+
}
71+
72+
private List<AzureBackupContainer> GetMachineContainers()
73+
{
74+
List<MarsContainerResponse> marsContainerResponses = new List<MarsContainerResponse>();
75+
if (!string.IsNullOrEmpty(ManagedResourceName))
76+
{
77+
marsContainerResponses.AddRange(AzureBackupClient.ListMachineContainers());
78+
}
79+
else
80+
{
81+
marsContainerResponses.AddRange(AzureBackupClient.ListMachineContainers(ManagedResourceName));
82+
}
83+
84+
return marsContainerResponses.ConvertAll<AzureBackupContainer>(marsContainerResponse =>
85+
{
86+
return new AzureBackupContainer(Vault, marsContainerResponse);
87+
}).Where(container => container.ContainerType == GetCustomerType().ToString()).ToList();
88+
}
89+
90+
private CustomerType GetCustomerType()
91+
{
92+
CustomerType customerType = CustomerType.Invalid;
93+
94+
switch (Type)
95+
{
96+
case AzureBackupContainerTypeInput.Windows:
97+
customerType = CustomerType.OBS;
98+
break;
99+
case AzureBackupContainerTypeInput.SCDPM:
100+
customerType = CustomerType.DPM;
101+
break;
102+
default:
103+
break;
104+
}
105+
106+
return customerType;
107+
}
108+
}
109+
}

src/ResourceManager/AzureBackup/Commands.AzureBackup/Commands.AzureBackup.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
<Reference Include="Microsoft.Azure.Common.NetFramework">
5757
<HintPath>..\..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
5858
</Reference>
59-
<Reference Include="Microsoft.Azure.Management.BackupServicesManagement, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
59+
<Reference Include="Microsoft.Azure.Management.BackupServicesManagement, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
6060
<SpecificVersion>False</SpecificVersion>
61-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.BackupServices.1.0.0-preview\lib\net40\Microsoft.Azure.Management.BackupServicesManagement.dll</HintPath>
61+
<HintPath>Resources\Microsoft.Azure.Management.BackupServicesManagement.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory">
6464
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.14.201151115\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
@@ -116,10 +116,12 @@
116116
</ItemGroup>
117117
<ItemGroup>
118118
<Compile Include="AzureBackupClientAdapter\AzureBackupClientAdapter.cs" />
119+
<Compile Include="AzureBackupClientAdapter\ContainerAdapter.cs" />
119120
<Compile Include="AzureBackupClientAdapter\VaultAdapter.cs" />
120121
<Compile Include="AzureBackupVaultCmdletBase.cs" />
121122
<Compile Include="AzureBackupCmdletBase.cs" />
122123
<Compile Include="AzureBackupCmdletHelpMessage.cs" />
124+
<Compile Include="Cmdlets\Container\GetAzureBackupContainer.cs" />
123125
<Compile Include="Cmdlets\VaultCredentials\GetAzureBackupVaultCredentials.cs" />
124126
<Compile Include="Cmdlets\Vault\RemoveAzureBackupVault.cs" />
125127
<Compile Include="Cmdlets\Vault\GetAzureBackupVault.cs" />
@@ -128,6 +130,7 @@
128130
<Compile Include="Helpers\VaultHelpers.cs" />
129131
<Compile Include="Library\CertUtils.cs" />
130132
<Compile Include="Models\AzureBackupBaseObjects.cs" />
133+
<Compile Include="Models\AzureBackupContainer.cs" />
131134
<Compile Include="Models\AzureBackupEnums.cs" />
132135
<Compile Include="Models\AzurePSBackupVault.cs" />
133136
<Compile Include="Models\VaultCredentials.cs" />
@@ -160,6 +163,7 @@
160163
<Content Include="Microsoft.Azure.Commands.AzureBackup.dll-help.xml">
161164
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
162165
</Content>
166+
<Content Include="Resources\Microsoft.Azure.Management.BackupServicesManagement.dll" />
163167
<Content Include="Resources\Microsoft.WindowsAzure.Management.Common.dll" />
164168
<Content Include="Resources\Security.Cryptography.dll" />
165169
</ItemGroup>

src/ResourceManager/AzureBackup/Commands.AzureBackup/Models/AzureBackupBaseObjects.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Management.BackupServices.Models;
16+
1517
namespace Microsoft.Azure.Commands.AzureBackup.Models
1618
{
1719
public class AzureBackupVaultContextObject
@@ -43,4 +45,46 @@ public AzureBackupVaultContextObject(string resourceGroupName, string resourceNa
4345
public AzureBackupVaultContextObject(AzurePSBackupVault vault)
4446
: this(vault.ResourceGroupName, vault.Name, vault.Region) { }
4547
}
48+
49+
/// <summary>
50+
/// This class encapsulates all the properties of the container object
51+
/// that are needed by higher level objects (data source, recovery point etc).
52+
/// </summary>
53+
public class AzureBackupContainerContextObject : AzureBackupVaultContextObject
54+
{
55+
/// <summary>
56+
/// Type of the Azure Backup container
57+
/// </summary>
58+
public string ContainerType { get; set; }
59+
60+
/// <summary>
61+
/// Unique name of the Azure Backup Container
62+
/// </summary>
63+
public string ContainerUniqueName { get; set; }
64+
65+
public AzureBackupContainerContextObject()
66+
: base()
67+
{
68+
}
69+
70+
public AzureBackupContainerContextObject(AzureBackupContainerContextObject azureBackupContainerContextObject)
71+
: base(azureBackupContainerContextObject.ResourceGroupName, azureBackupContainerContextObject.ResourceName, azureBackupContainerContextObject.Location)
72+
{
73+
ContainerType = azureBackupContainerContextObject.ContainerType;
74+
ContainerUniqueName = azureBackupContainerContextObject.ContainerUniqueName;
75+
}
76+
public AzureBackupContainerContextObject(AzureBackupContainer azureBackupContainer)
77+
: base(azureBackupContainer.ResourceGroupName, azureBackupContainer.ResourceName, azureBackupContainer.Location)
78+
{
79+
ContainerType = azureBackupContainer.ContainerType;
80+
ContainerUniqueName = azureBackupContainer.ContainerUniqueName;
81+
}
82+
83+
public AzureBackupContainerContextObject(AzurePSBackupVault vault, MarsContainerResponse marsContainerResponse)
84+
: base(vault.ResourceGroupName, vault.Name, vault.Region)
85+
{
86+
ContainerType = marsContainerResponse.Properties.CustomerType;
87+
ContainerUniqueName = marsContainerResponse.UniqueName;
88+
}
89+
}
4690
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.AzureBackup.Helpers;
16+
using Microsoft.Azure.Management.BackupServices.Models;
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using System.Text;
21+
using System.Threading.Tasks;
22+
23+
namespace Microsoft.Azure.Commands.AzureBackup.Models
24+
{
25+
/// <summary>
26+
/// Represents Azure Backup Container
27+
/// </summary>
28+
public class AzureBackupContainer : AzureBackupContainerContextObject
29+
{
30+
///// <summary>
31+
///// Resource group name of the resource (ex: resource group name of the VM) being managed by Azure Backup service.
32+
///// </summary>
33+
//public string ManagedResourceGroupName { get; set; }
34+
35+
/// <summary>
36+
/// Resource name of the resource (ex: resource name of the VM) being managed by the Azure Backup service.
37+
/// </summary>
38+
public string ManagedResourceName { get; set; }
39+
40+
///// <summary>
41+
///// Status of health of the Azure Backup container
42+
///// </summary>
43+
//public string HealthStatus { get; set; }
44+
45+
///// <summary>
46+
///// Status of registration of the container
47+
///// </summary>
48+
//public string RegistrationStatus { get; set; }
49+
50+
public long ContainerId { get; set; }
51+
52+
public string ContainerStampUri { get; set; }
53+
54+
public Guid ContainerStampId { get; set; }
55+
56+
public bool CanReRegister { get; set; }
57+
58+
public AzureBackupContainer() : base() { }
59+
60+
public AzureBackupContainer(AzurePSBackupVault vault, MarsContainerResponse marsContainerResponse)
61+
: base(vault, marsContainerResponse)
62+
{
63+
//ManagedResourceGroupName = ContainerHelpers.GetRGNameFromId(marsContainerResponse.Properties.ParentContainerId);
64+
ManagedResourceName = marsContainerResponse.Properties.FriendlyName;
65+
ContainerId = marsContainerResponse.Properties.ContainerId;
66+
ContainerStampId = marsContainerResponse.Properties.ContainerStampId;
67+
ContainerStampUri = marsContainerResponse.Properties.ContainerStampUri;
68+
CanReRegister = marsContainerResponse.Properties.CanReRegister;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)