Skip to content

Commit 7605325

Browse files
committed
Merge pull request #11 from MabOneSdk/pikumar3
Added Get-AzureBackupItem cmdlet
2 parents d61bcc4 + b782f4f commit 7605325

File tree

7 files changed

+210
-1
lines changed

7 files changed

+210
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
27+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
28+
{
29+
public abstract class AzureBackupContainerCmdletBase : AzureBackupCmdletBase
30+
{
31+
// ToDO:
32+
// Correct Help message and other attributes related to paameters
33+
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ResourceGroupName, ValueFromPipelineByPropertyName = true)]
34+
[ValidateNotNullOrEmpty]
35+
public AzureBackupContainer AzureBackupContainer { get; set; }
36+
37+
public override void ExecuteCmdlet()
38+
{
39+
base.ExecuteCmdlet();
40+
41+
WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}", AzureBackupContainer.ResourceGroupName, AzureBackupContainer.ResourceName));
42+
43+
InitializeAzureBackupCmdlet(AzureBackupContainer.ResourceGroupName, AzureBackupContainer.ResourceName);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.DataSource
8+
{
9+
class Disable_AzureBackupProtection
10+
{
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.DataSource
8+
{
9+
class Enable_AzureBackupProtection
10+
{
11+
}
12+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 System.Linq;
20+
using Microsoft.Azure.Management.BackupServices.Models;
21+
22+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
23+
{
24+
/// <summary>
25+
/// Get list of containers
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Get, "AzureBackupItem"), OutputType(typeof(AzureBackupItem), typeof(List<AzureBackupItem>))]
28+
public class GetAzureBackupItem : AzureBackupContainerCmdletBase
29+
{
30+
[Parameter(Position = 2, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
31+
[ValidateNotNullOrEmpty]
32+
public string Id { get; set; }
33+
34+
public override void ExecuteCmdlet()
35+
{
36+
base.ExecuteCmdlet();
37+
38+
ExecutionBlock(() =>
39+
{
40+
WriteVerbose("Making client call");
41+
42+
var azureBackupDatasourceListResponse = AzureBackupClient.DataSource.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
43+
var azureBackupPOListResponse = AzureBackupClient.ProtectableObject. ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
44+
45+
WriteVerbose("Received policy response");
46+
WriteVerbose("Received policy response2");
47+
IEnumerable<DataSourceInfo> azureBackupDatasourceObjects = null;
48+
IEnumerable<ProtectableObjectInfo> azureBackupPOObjects = null;
49+
50+
if (Id != null)
51+
{
52+
azureBackupDatasourceObjects = azureBackupDatasourceListResponse.DataSources.Objects.Where(x => x.InstanceId.Equals(Id, System.StringComparison.InvariantCultureIgnoreCase));
53+
azureBackupPOObjects = azureBackupPOListResponse.ProtectableObject.Objects.Where(x => x.InstanceId.Equals(Id, System.StringComparison.InvariantCultureIgnoreCase));
54+
}
55+
else
56+
{
57+
azureBackupDatasourceObjects = azureBackupDatasourceListResponse.DataSources.Objects;
58+
azureBackupPOObjects = azureBackupPOListResponse.ProtectableObject.Objects;
59+
}
60+
61+
WriteVerbose("Converting response");
62+
WriteAzureBackupProtectionPolicy(azureBackupDatasourceObjects, azureBackupPOObjects, AzureBackupContainer);
63+
});
64+
}
65+
66+
public void WriteAzureBackupProtectionPolicy(DataSourceInfo sourceItem, AzureBackupContainer azureBackupItem)
67+
{
68+
this.WriteObject(new AzureBackupItem(sourceItem, azureBackupItem));
69+
}
70+
71+
public void WriteAzureBackupProtectionPolicy(IEnumerable<DataSourceInfo> sourceDataSourceList,IEnumerable<ProtectableObjectInfo> sourcePOList, AzureBackupContainer azureBackupContainer)
72+
{
73+
List<AzureBackupItem> targetList = new List<AzureBackupItem>();
74+
75+
foreach (var item in sourceDataSourceList)
76+
{
77+
targetList.Add(new AzureBackupItem(item, azureBackupContainer));
78+
}
79+
80+
foreach (var item in sourcePOList)
81+
{
82+
targetList.Add(new AzureBackupItem(item, azureBackupContainer));
83+
}
84+
85+
this.WriteObject(targetList, true);
86+
}
87+
88+
}
89+
}

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

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

15+
using Microsoft.Azure.Management.BackupServices.Models;
1516
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
1617
{
1718
public class AzureBackupVaultContextObject
@@ -66,6 +67,13 @@ public AzureBackupContainerContextObject(AzureBackupContainerContextObject azure
6667
ContainerName = azureBackupContainerContextObject.ContainerName;
6768
ContainerId = azureBackupContainerContextObject.ContainerId;
6869
}
70+
public AzureBackupContainerContextObject(AzureBackupContainer azureBackupContainer)
71+
: base(azureBackupContainer.ResourceGroupName, azureBackupContainer.ResourceName)
72+
{
73+
ContainerType = azureBackupContainer.ContainerType;
74+
ContainerName = azureBackupContainer.ContainerName;
75+
ContainerId = azureBackupContainer.ContainerId;
76+
}
6977
}
7078

7179
public class AzureBackupItemContextObject : AzureBackupContainerContextObject
@@ -91,5 +99,18 @@ public AzureBackupItemContextObject(AzureBackupItemContextObject azureBackupItem
9199
DataSourceId = azureBackupItemContextObject.DataSourceId;
92100
DataSourceType = azureBackupItemContextObject.DataSourceType;
93101
}
102+
103+
public AzureBackupItemContextObject(DataSourceInfo item, AzureBackupContainer azureBackupContainer)
104+
: base(azureBackupContainer)
105+
{
106+
DataSourceId = item.InstanceId;
107+
DataSourceType = item.Type;
108+
}
109+
110+
public AzureBackupItemContextObject(ProtectableObjectInfo item, AzureBackupContainer azureBackupContainer)
111+
: base(azureBackupContainer)
112+
{
113+
DataSourceType = item.Type;
114+
}
94115
}
95116
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2323
/// <summary>
2424
/// Represents Azure Backup Container
2525
/// </summary>
26-
public class AzureBackupContainer : AzureBackupVaultContextObject
26+
public class AzureBackupContainer : AzureBackupContainerContextObject
2727
{
2828
/// <summary>
2929
/// Type of the Azure Backup container

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq;
1818
using System.Text;
1919
using System.Threading.Tasks;
20+
using Microsoft.Azure.Management.BackupServices.Models;
2021

2122
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2223
{
@@ -79,5 +80,33 @@ public class AzureBackupItem : AzureBackupItemContextObject
7980
/// Last Backup Job Id for the Azure Backup Item
8081
/// </summary>
8182
public string LastBackupJobId { get; set; }
83+
84+
public AzureBackupItem()
85+
: base()
86+
{
87+
}
88+
89+
public AzureBackupItem(DataSourceInfo datasource, AzureBackupContainer azureBackupContainer)
90+
: base(datasource, azureBackupContainer)
91+
{
92+
Status = datasource.Status;
93+
ProtectionStatus = datasource.ProtectionStatus;
94+
ProtectableObjectName = datasource.ProtectableObjectName;
95+
ProtectionPolicyName = datasource.ProtectionPolicyName;
96+
ProtectionPolicyId = datasource.ProtectionPolicyId;
97+
PolicyInconsistent = datasource.PolicyInconsistent;
98+
RecoveryPointsCount = datasource.RecoveryPointsCount;
99+
LastRecoveryPoint = datasource.LastRecoveryPoint;
100+
LastBackupTime = datasource.LastBackupTime;
101+
LastBackupStatus = datasource.LastBackupStatus;
102+
LastBackupJobId = datasource.LastBackupJobId;
103+
}
104+
105+
public AzureBackupItem(ProtectableObjectInfo pPOItem, AzureBackupContainer azureBackupContainer)
106+
: base(pPOItem, azureBackupContainer)
107+
{
108+
ProtectionStatus = pPOItem.ProtectionStatus;
109+
ProtectableObjectName = pPOItem.Name;
110+
}
82111
}
83112
}

0 commit comments

Comments
 (0)