Skip to content

Commit fe83f27

Browse files
committed
My changes
1 parent 7605325 commit fe83f27

File tree

6 files changed

+291
-0
lines changed

6 files changed

+291
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ internal static class AzureBackupCmdletHelpMessage
2525
public const string ContainerId = "The container ID.";
2626
public const string ContainerRegistrationStatus = "The container registration status.";
2727
public const string ContainerType = "The container type.";
28+
public const string VirtualMachine = "Virtual Machine.";
2829
}
2930
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.Collections.Generic;
17+
using System.Linq;
18+
using System.Management.Automation;
19+
using System.Text;
20+
using System.Threading.Tasks;
21+
using Microsoft.Azure.Commands.Compute;
22+
using Microsoft.Azure.Management.BackupServices.Models;
23+
using MBS = Microsoft.Azure.Management.BackupServices;
24+
using Microsoft.Azure.Commands.Compute.Models;
25+
26+
27+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
28+
{
29+
/// <summary>
30+
/// Get list of containers
31+
/// </summary>
32+
[Cmdlet(VerbsLifecycle.Register, "AzureBackupContainer"), OutputType(typeof(Guid))]
33+
public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase
34+
{
35+
[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
36+
[ValidateNotNullOrEmpty]
37+
public PSVirtualMachineInstanceView VirtualMachine { get; set; }
38+
39+
public override void ExecuteCmdlet()
40+
{
41+
base.ExecuteCmdlet();
42+
43+
ExecutionBlock(() =>
44+
{
45+
string vmName = VirtualMachine.Name;
46+
string rgName = VirtualMachine.ResourceGroupName;
47+
Guid jobId = Guid.Empty;
48+
bool isDiscoveryNeed = false;
49+
MBS.OperationResponse operationResponse;
50+
51+
ContainerInfo container = null;
52+
isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container);
53+
if(isDiscoveryNeed)
54+
{
55+
RefreshContainer();
56+
}
57+
58+
isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container);
59+
if((isDiscoveryNeed == false) && (container == null))
60+
{
61+
//Container is not discovered. Throw exception
62+
throw new NotImplementedException();
63+
}
64+
else
65+
{
66+
//Container is discovered. Register the container
67+
List<string> containerNameList = new List<string>();
68+
containerNameList.Add(container.Name);
69+
RegisterContainerRequest registrationRequest = new RegisterContainerRequest(containerNameList, "IaasVMContainer"); //TODO: Container type from enum
70+
operationResponse = AzureBackupClient.Container.RegisterAsync(registrationRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
71+
72+
//TODO fix the OperationResponse to JobID conversion
73+
jobId = operationResponse.OperationId;
74+
WriteObject(jobId);
75+
}
76+
});
77+
}
78+
79+
private void RefreshContainer()
80+
{
81+
MBS.OperationResponse opResponse =
82+
AzureBackupClient.Container.RefreshAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
83+
84+
//Now wait for the operation to Complete
85+
86+
//If operat
87+
throw new NotImplementedException();
88+
}
89+
90+
private bool IsDiscoveryNeeded(string vmName, string rgName, out ContainerInfo container)
91+
{
92+
bool isDiscoveryNeed = false;
93+
//First check if container is discoverd or not
94+
ListContainerQueryParameter queryParams = new ListContainerQueryParameter();
95+
queryParams.ContainerFriendlyNameField = vmName;
96+
ListContainerResponse containers = AzureBackupClient.Container.ListAsync(queryParams,
97+
GetCustomRequestHeaders(), CmdletCancellationToken).Result;
98+
if (containers.Objects.Count() == 0)
99+
{
100+
//Container is not discover
101+
WriteVerbose("Container is not discovered");
102+
container = null;
103+
isDiscoveryNeed = true;
104+
}
105+
106+
else
107+
{
108+
//We can have multiple container with same friendly name.
109+
//Look for resourceGroup name in the container unoque name
110+
container = containers.Objects.Where(c => c.ParentContainerFriendlyName.ToLower().Equals(rgName.ToLower())).FirstOrDefault();
111+
if (container == null)
112+
{
113+
//Container is not in list of registered container
114+
isDiscoveryNeed = true;
115+
}
116+
}
117+
return isDiscoveryNeed;
118+
}
119+
}
120+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.Collections.Generic;
17+
using System.Linq;
18+
using System.Management.Automation;
19+
using System.Text;
20+
using System.Threading.Tasks;
21+
using Microsoft.Azure.Commands.Compute;
22+
using Microsoft.Azure.Management.BackupServices.Models;
23+
using MBS = Microsoft.Azure.Management.BackupServices;
24+
using Microsoft.Azure.Commands.Compute.Models;
25+
26+
27+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
28+
{
29+
/// <summary>
30+
/// Get list of containers
31+
/// </summary>
32+
[Cmdlet(VerbsLifecycle.Unregister, "AzureBackupContainer"), OutputType(typeof(Guid))]
33+
public class UnregisterAzureBackupContainer : AzureBackupVaultCmdletBase
34+
{
35+
[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
36+
[ValidateNotNullOrEmpty]
37+
public PSVirtualMachineInstanceView VirtualMachine { get; set; }
38+
39+
public override void ExecuteCmdlet()
40+
{
41+
base.ExecuteCmdlet();
42+
43+
ExecutionBlock(() =>
44+
{
45+
string vmName = VirtualMachine.Name;
46+
string rgName = VirtualMachine.ResourceGroupName;
47+
Guid jobId = Guid.Empty;
48+
49+
ListContainerQueryParameter queryParams = new ListContainerQueryParameter();
50+
queryParams.ContainerStatusField = "Registered"; //TODO: Use enum
51+
queryParams.ContainerFriendlyNameField = vmName;
52+
53+
ListContainerResponse containers =
54+
AzureBackupClient.Container.ListAsync(queryParams,
55+
GetCustomRequestHeaders(), CmdletCancellationToken).Result;
56+
if(containers.Objects.Count() == 0)
57+
{
58+
WriteVerbose("Container is not in the registered list");
59+
jobId = Guid.Empty;
60+
}
61+
62+
else
63+
{
64+
//We can havemultiple container with same friendly name.
65+
//Look for resourceGroup name in the ParentFriendlyName
66+
ContainerInfo containerToUnreg = containers.Objects.Where(c => c.ParentContainerFriendlyName.ToLower().Equals(rgName.ToLower())).FirstOrDefault();
67+
if (containerToUnreg == null)
68+
{
69+
//Container is not in list of registered container
70+
jobId = Guid.Empty;
71+
}
72+
else
73+
{
74+
UnregisterContainerRequest unregRequest = new UnregisterContainerRequest(containerToUnreg.Name, "IaasVMContainer"); //TODO: Use enum
75+
MBS.OperationResponse operationResponse = AzureBackupClient.Container.UnregisterAsync(unregRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
76+
jobId = operationResponse.OperationId; //TODO: Fix it once PiyushKa publish the rest APi to get jobId based on operationId
77+
}
78+
}
79+
80+
WriteObject(jobId);
81+
});
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Management.Automation;
7+
8+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.RegisterContainer
9+
{
10+
[Cmdlet(VerbsCommon.Get, ProfileNouns.VirtualMachine, DefaultParameterSetName = ListAllVirtualMachinesParamSet)]
11+
class RegisterAzureBackupContainer
12+
{
13+
}
14+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
<Compile Include="AzureBackupCmdletHelpMessage.cs" />
123123
<Compile Include="Cmdlets\BackUp\TriggerBackUp.cs" />
124124
<Compile Include="Cmdlets\Container\GetAzureBackupContainer.cs" />
125+
<Compile Include="Cmdlets\Container\RegisterAzureBackupContainer.cs" />
126+
<Compile Include="Cmdlets\Container\UnregisterAzureBackupContainer.cs" />
125127
<Compile Include="Cmdlets\Jobs\GetAzureBackupJob.cs" />
126128
<Compile Include="Cmdlets\ProtectionPolicy\GetAzureBackupProtectionPolicy.cs" />
127129
<Compile Include="Cmdlets\VaultCredentials\AcsNamespace.cs" />
@@ -148,6 +150,10 @@
148150
<Project>{5ee72c53-1720-4309-b54b-5fb79703195f}</Project>
149151
<Name>Commands.Common</Name>
150152
</ProjectReference>
153+
<ProjectReference Include="..\..\Compute\Commands.Compute\Commands.Compute.csproj">
154+
<Project>{52643bd5-6378-49bd-9f6e-dac9dd8a867b}</Project>
155+
<Name>Commands.Compute</Name>
156+
</ProjectReference>
151157
</ItemGroup>
152158
<ItemGroup>
153159
<None Include="Microsoft.Azure.Commands.AzureBackup.format.ps1xml">
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.Management.BackupServices.Models;
16+
using System;
17+
using System.Collections.Generic;
18+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
19+
{
20+
/// <summary>
21+
/// Represents ProtectionPolicy object
22+
/// </summary>
23+
public class AzureBackupRegisterContainer : AzureBackupVaultContextObject
24+
{
25+
/// <summary>
26+
/// InstanceId of the azurebackup object
27+
/// </summary>
28+
public string InstanceId { get; set; }
29+
30+
/// <summary>
31+
/// Name of the azurebackup object
32+
/// </summary>
33+
public string Name { get; set; }
34+
35+
public string WorkloadType { get; set; }
36+
37+
public string BackupType { get; set; }
38+
39+
public DateTime ScheduleStartTime { get; set; }
40+
41+
public IList<DateTime> ScheduleRunTimes { get; set; }
42+
43+
public string RetentionType { get; set; }
44+
45+
public int RetentionDuration { get; set; }
46+
47+
public AzureBackupRegisterContainer()
48+
{
49+
}
50+
51+
public AzureBackupRegisterContainer(string resourceGroupName, string resourceName, ProtectionPolicyInfo sourcePolicy)
52+
: base(resourceGroupName, resourceName)
53+
{
54+
InstanceId = sourcePolicy.InstanceId;
55+
Name = sourcePolicy.Name;
56+
WorkloadType = sourcePolicy.WorkloadType;
57+
58+
BackupType = sourcePolicy.Schedule.BackupType;
59+
ScheduleStartTime = sourcePolicy.Schedule.ScheduleStartTime;
60+
ScheduleRunTimes = sourcePolicy.Schedule.ScheduleRunTimes;
61+
62+
RetentionType = sourcePolicy.Schedule.RetentionPolicy.RetentionType.ToString();
63+
RetentionDuration = sourcePolicy.Schedule.RetentionPolicy.RetentionDuration;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)