Skip to content

Commit cc0a07c

Browse files
committed
Change Set 1
1 parent a58af04 commit cc0a07c

15 files changed

+1594
-0
lines changed

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: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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.WindowsAzure.Management.BackupServices;
21+
using Microsoft.Azure.Common.Authentication;
22+
using Microsoft.Azure.Common.Authentication.Models;
23+
using System.Threading;
24+
using Hyak.Common;
25+
using Microsoft.Azure.Commands.AzureBackup.Properties;
26+
using System.Net;
27+
28+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
29+
{
30+
public abstract class AzureBackupCmdletBase : AzurePSCmdlet
31+
{
32+
/// <summary>
33+
/// ResourceGroup context for the operation
34+
/// </summary>
35+
protected string ResourceGroupName { get; set; }
36+
37+
/// <summary>
38+
/// Resource context for the operation
39+
/// </summary>
40+
protected string ResourceName { get; set; }
41+
42+
/// <summary>
43+
/// Client request id.
44+
/// </summary>
45+
protected string clientRequestId;
46+
47+
/// <summary>
48+
/// Azure backup client.
49+
/// </summary>
50+
private BackupServicesManagementClient azureBackupClient;
51+
52+
/// <summary>
53+
/// Cancellation Token Source
54+
/// </summary>
55+
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
56+
protected CancellationToken CmdletCancellationToken;
57+
58+
/// <summary>
59+
/// Get Azure backup client.
60+
/// </summary>
61+
protected BackupServicesManagementClient AzureBackupClient
62+
{
63+
get
64+
{
65+
if (this.azureBackupClient == null)
66+
{
67+
this.azureBackupClient = AzureSession.ClientFactory.CreateClient<BackupServicesManagementClient>(Profile, Profile.Context.Subscription, AzureEnvironment.Endpoint.ResourceManager);
68+
// this.azureBackupClient.ResourceGroupName = resourceGroupName;
69+
// this.azureBackupClient.ResourceName = resourceName;
70+
}
71+
72+
return this.azureBackupClient;
73+
}
74+
}
75+
76+
public override void ExecuteCmdlet()
77+
{
78+
base.ExecuteCmdlet();
79+
80+
// Vaildate RGName, RName?
81+
82+
clientRequestId = Guid.NewGuid().ToString() + "-" + DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ssZ") + "-PS";
83+
84+
WriteVerbose(string.Format("ClientRequestId: {0}", this.clientRequestId));
85+
86+
CmdletCancellationToken = cancellationTokenSource.Token;
87+
}
88+
89+
protected void ExecutionBlock(Action execAction)
90+
{
91+
try
92+
{
93+
execAction();
94+
}
95+
catch (Exception exception)
96+
{
97+
WriteDebug(String.Format("Caught exception, type: {0}", exception.GetType()));
98+
HandleException(exception);
99+
}
100+
}
101+
102+
/// <summary>
103+
/// Handles set of exceptions thrown by client
104+
/// </summary>
105+
/// <param name="ex"></param>
106+
private void HandleException(Exception exception)
107+
{
108+
if (exception is AggregateException && ((AggregateException)exception).InnerExceptions != null
109+
&& ((AggregateException)exception).InnerExceptions.Count != 0)
110+
{
111+
WriteDebug("Handling aggregate exception");
112+
foreach (var innerEx in ((AggregateException)exception).InnerExceptions)
113+
{
114+
HandleException(innerEx);
115+
}
116+
}
117+
else
118+
{
119+
Exception targetEx = exception;
120+
string targetErrorId = String.Empty;
121+
ErrorCategory targetErrorCategory = ErrorCategory.NotSpecified;
122+
123+
if (exception is CloudException)
124+
{
125+
var cloudEx = exception as CloudException;
126+
if (cloudEx.Response != null && cloudEx.Response.StatusCode == HttpStatusCode.NotFound)
127+
{
128+
WriteDebug(String.Format("Received CloudException, StatusCode: {0}", cloudEx.Response.StatusCode));
129+
130+
targetEx = new Exception(Resources.ResourceNotFoundMessage);
131+
targetErrorCategory = ErrorCategory.InvalidArgument;
132+
}
133+
else if (cloudEx.Error != null)
134+
{
135+
WriteDebug(String.Format("Received CloudException, ErrorCode: {0}, Message: {1}", cloudEx.Error.Code, cloudEx.Error.Message));
136+
137+
targetErrorId = cloudEx.Error.Code;
138+
targetErrorCategory = ErrorCategory.InvalidOperation;
139+
}
140+
}
141+
else if (exception is WebException)
142+
{
143+
var webEx = exception as WebException;
144+
WriteDebug(string.Format("Received WebException, Response: {0}, Status: {1}", webEx.Response, webEx.Status));
145+
146+
targetErrorCategory = ErrorCategory.ConnectionError;
147+
}
148+
else if (exception is ArgumentException || exception is ArgumentNullException)
149+
{
150+
WriteDebug(string.Format("Received ArgumentException"));
151+
targetErrorCategory = ErrorCategory.InvalidArgument;
152+
}
153+
154+
var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null);
155+
WriteError(errorRecord);
156+
}
157+
}
158+
}
159+
}
160+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 ResourceGroupName = "The resource group name.";
21+
public const string ResourceName = "The resource name.";
22+
public const string PolicyName = "The protection policy name.";
23+
}
24+
}
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.Management.BackupServices.Models;
20+
21+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
22+
{
23+
/// <summary>
24+
/// Get list of containers
25+
/// </summary>
26+
[Cmdlet(VerbsCommon.Get, "AzureBackupProtectionPolicy"), OutputType(typeof(ProtectionPolicyInfo), typeof(List<ProtectionPolicyInfo>))]
27+
public class GetAzureBackupProtectionPolicy : AzureBackupCmdletBase
28+
{
29+
[Parameter(Position = 2, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
30+
[ValidateNotNullOrEmpty]
31+
public string Name { get; set; }
32+
33+
public override void ExecuteCmdlet()
34+
{
35+
base.ExecuteCmdlet();
36+
37+
ExecutionBlock(() =>
38+
{
39+
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync("1234", CmdletCancellationToken).Result;
40+
41+
WriteAzureBackupProtectionPolicy(policyListResponse.ProtectionPolicies.Objects);
42+
});
43+
}
44+
45+
public void WriteAzureBackupProtectionPolicy(ProtectionPolicyInfo sourcePolicy)
46+
{
47+
this.WriteObject(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, sourcePolicy));
48+
}
49+
50+
public void WriteAzureBackupProtectionPolicy(IList<ProtectionPolicyInfo> sourcePolicyList)
51+
{
52+
List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>();
53+
54+
foreach (var sourcePolicy in sourcePolicyList)
55+
{
56+
targetList.Add(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, sourcePolicy));
57+
}
58+
59+
this.WriteObject(targetList, true);
60+
}
61+
}
62+
}
63+

0 commit comments

Comments
 (0)