Skip to content

Commit 729dbb0

Browse files
Merge pull request #1 from MabOneSdk/piyushka
Infra for Azure Backup Services Powershell
2 parents a58af04 + 5fc6998 commit 729dbb0

17 files changed

+1685
-1
lines changed

src/AzurePowershell.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio 2013
2+
# Visual Studio Express 2013 for Web
33
VisualStudioVersion = 12.0.31101.0
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8531411A-0137-4E27-9C5E-49E07C245048}"
@@ -210,6 +210,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.ApiManagement", "R
210210
EndProject
211211
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.ApiManagement.Test", "ResourceManager\ApiManagement\Commands.ApiManagement.Test\Commands.ApiManagement.Test.csproj", "{BEC9ECE9-A3D6-4B24-A682-1FA890647D9D}"
212212
EndProject
213+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.AzureBackup", "ResourceManager\AzureBackup\Commands.AzureBackup\Commands.AzureBackup.csproj", "{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}"
214+
EndProject
213215
Global
214216
GlobalSection(SolutionConfigurationPlatforms) = preSolution
215217
Debug|Any CPU = Debug|Any CPU
@@ -512,6 +514,10 @@ Global
512514
{BEC9ECE9-A3D6-4B24-A682-1FA890647D9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
513515
{BEC9ECE9-A3D6-4B24-A682-1FA890647D9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
514516
{BEC9ECE9-A3D6-4B24-A682-1FA890647D9D}.Release|Any CPU.Build.0 = Release|Any CPU
517+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
518+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Debug|Any CPU.Build.0 = Debug|Any CPU
519+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Release|Any CPU.ActiveCfg = Release|Any CPU
520+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Release|Any CPU.Build.0 = Release|Any CPU
515521
EndGlobalSection
516522
GlobalSection(SolutionProperties) = preSolution
517523
HideSolutionNode = FALSE

src/ResourceManager.AzureBackup.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Express 2013 for Web
4+
VisualStudioVersion = 12.0.31101.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.AzureBackup", "ResourceManager\AzureBackup\Commands.AzureBackup\Commands.AzureBackup.csproj", "{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6C8D2337-C9D1-4F52-94B3-AB63A19F3453}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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 Microsoft.WindowsAzure.Management.Scheduler;
27+
using Microsoft.Azure.Management.BackupServices;
28+
using Microsoft.Azure.Management.BackupServices.Models;
29+
30+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
31+
{
32+
public abstract class AzureBackupCmdletBase : AzurePSCmdlet
33+
{
34+
/// <summary>
35+
/// ResourceGroup context for the operation
36+
/// </summary>
37+
private string resourceGroupName { get; set; }
38+
39+
/// <summary>
40+
/// Resource context for the operation
41+
/// </summary>
42+
private string resourceName { get; set; }
43+
44+
/// <summary>
45+
/// Client request id.
46+
/// </summary>
47+
private string clientRequestId;
48+
49+
/// <summary>
50+
/// Azure backup client.
51+
/// </summary>
52+
private BackupServicesManagementClient azureBackupClient;
53+
54+
/// <summary>
55+
/// Cancellation Token Source
56+
/// </summary>
57+
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
58+
protected CancellationToken CmdletCancellationToken;
59+
60+
/// <summary>
61+
/// Get Azure backup client.
62+
/// </summary>
63+
protected BackupServicesManagementClient AzureBackupClient
64+
{
65+
get
66+
{
67+
if (this.azureBackupClient == null)
68+
{
69+
// Temp code to be able to test internal env.
70+
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
71+
72+
var cloudServicesClient = AzureSession.ClientFactory.CreateClient<CloudServiceManagementClient>(Profile, Profile.Context.Subscription, AzureEnvironment.Endpoint.ResourceManager);
73+
this.azureBackupClient = AzureSession.ClientFactory.CreateCustomClient<BackupServicesManagementClient>(resourceName, resourceGroupName, cloudServicesClient.Credentials, cloudServicesClient.BaseUri);
74+
}
75+
76+
return this.azureBackupClient;
77+
}
78+
}
79+
80+
public void InitializeAzureBackupCmdlet(string rgName, string rName)
81+
{
82+
resourceGroupName = rgName;
83+
resourceName = rName;
84+
85+
clientRequestId = Guid.NewGuid().ToString() + "-" + DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ssZ") + "-PS";
86+
87+
WriteDebug(string.Format("Initialized AzureBackup Cmdlet, ClientRequestId: {0}, ResourceGroupName: {1}, ResourceName : {2}", this.clientRequestId, resourceGroupName, resourceName));
88+
89+
CmdletCancellationToken = cancellationTokenSource.Token;
90+
}
91+
92+
protected void ExecutionBlock(Action execAction)
93+
{
94+
try
95+
{
96+
execAction();
97+
}
98+
catch (Exception exception)
99+
{
100+
WriteDebug(String.Format("Caught exception, type: {0}", exception.GetType()));
101+
HandleException(exception);
102+
}
103+
}
104+
105+
/// <summary>
106+
/// Handles set of exceptions thrown by client
107+
/// </summary>
108+
/// <param name="ex"></param>
109+
private void HandleException(Exception exception)
110+
{
111+
if (exception is AggregateException && ((AggregateException)exception).InnerExceptions != null
112+
&& ((AggregateException)exception).InnerExceptions.Count != 0)
113+
{
114+
WriteDebug("Handling aggregate exception");
115+
foreach (var innerEx in ((AggregateException)exception).InnerExceptions)
116+
{
117+
HandleException(innerEx);
118+
}
119+
}
120+
else
121+
{
122+
Exception targetEx = exception;
123+
string targetErrorId = String.Empty;
124+
ErrorCategory targetErrorCategory = ErrorCategory.NotSpecified;
125+
126+
if (exception is CloudException)
127+
{
128+
var cloudEx = exception as CloudException;
129+
if (cloudEx.Response != null && cloudEx.Response.StatusCode == HttpStatusCode.NotFound)
130+
{
131+
WriteDebug(String.Format("Received CloudException, StatusCode: {0}", cloudEx.Response.StatusCode));
132+
133+
targetEx = new Exception(Resources.ResourceNotFoundMessage);
134+
targetErrorCategory = ErrorCategory.InvalidArgument;
135+
}
136+
else if (cloudEx.Error != null)
137+
{
138+
WriteDebug(String.Format("Received CloudException, ErrorCode: {0}, Message: {1}", cloudEx.Error.Code, cloudEx.Error.Message));
139+
140+
targetErrorId = cloudEx.Error.Code;
141+
targetErrorCategory = ErrorCategory.InvalidOperation;
142+
}
143+
}
144+
else if (exception is WebException)
145+
{
146+
var webEx = exception as WebException;
147+
WriteDebug(string.Format("Received WebException, Response: {0}, Status: {1}", webEx.Response, webEx.Status));
148+
149+
targetErrorCategory = ErrorCategory.ConnectionError;
150+
}
151+
else if (exception is ArgumentException || exception is ArgumentNullException)
152+
{
153+
WriteDebug(string.Format("Received ArgumentException"));
154+
targetErrorCategory = ErrorCategory.InvalidArgument;
155+
}
156+
157+
var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null);
158+
WriteError(errorRecord);
159+
}
160+
}
161+
162+
protected CustomRequestHeaders GetCustomRequestHeaders()
163+
{
164+
var hdrs = new CustomRequestHeaders()
165+
{
166+
// ClientRequestId is a unique ID for every request to backend service.
167+
ClientRequestId = this.clientRequestId,
168+
};
169+
170+
return hdrs;
171+
}
172+
}
173+
}
174+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
16+
{
17+
18+
internal static class AzureBackupCmdletHelpMessage
19+
{
20+
public const string Vault = "The vault details";
21+
public const string PolicyName = "The protection policy name.";
22+
public const string ResourceGroupName = "The ResourceGroup name.";
23+
public const string ResourceName = "The Resource name.";
24+
}
25+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 AzureBackupVaultCmdletBase : AzureBackupCmdletBase
30+
{
31+
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ResourceGroupName, ValueFromPipelineByPropertyName = true)]
32+
[ValidateNotNullOrEmpty]
33+
public string ResourceGroupName { get; set; }
34+
35+
[Parameter(Position = 1, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ResourceName, ValueFromPipelineByPropertyName = true)]
36+
[ValidateNotNullOrEmpty]
37+
public string ResourceName { get; set; }
38+
39+
40+
public override void ExecuteCmdlet()
41+
{
42+
base.ExecuteCmdlet();
43+
44+
WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}", ResourceGroupName, ResourceName));
45+
46+
InitializeAzureBackupCmdlet(ResourceGroupName, ResourceName);
47+
}
48+
}
49+
}
50+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
20+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
21+
{
22+
/// <summary>
23+
/// Get list of containers
24+
/// </summary>
25+
[Cmdlet(VerbsCommon.Get, "AzureBackupJob"), OutputType(typeof(string))]
26+
public class GetAzureBackupJob : AzureBackupCmdletBase
27+
{
28+
public override void ExecuteCmdlet()
29+
{
30+
}
31+
}
32+
}
33+

0 commit comments

Comments
 (0)