Skip to content

Commit 7566d0d

Browse files
committed
Reference cmdlet
1 parent cc0a07c commit 7566d0d

File tree

4 files changed

+101
-20
lines changed

4 files changed

+101
-20
lines changed

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupCmdletBase.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
using System.Collections.Generic;
1818
using System.Xml;
1919
using Microsoft.WindowsAzure.Commands.Utilities.Common;
20-
using Microsoft.WindowsAzure.Management.BackupServices;
2120
using Microsoft.Azure.Common.Authentication;
2221
using Microsoft.Azure.Common.Authentication.Models;
2322
using System.Threading;
2423
using Hyak.Common;
2524
using Microsoft.Azure.Commands.AzureBackup.Properties;
2625
using System.Net;
26+
using Microsoft.WindowsAzure.Management.Scheduler;
27+
using Microsoft.Azure.Management.BackupServices;
28+
using Microsoft.Azure.Management.BackupServices.Models;
2729

2830
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2931
{
@@ -32,17 +34,17 @@ public abstract class AzureBackupCmdletBase : AzurePSCmdlet
3234
/// <summary>
3335
/// ResourceGroup context for the operation
3436
/// </summary>
35-
protected string ResourceGroupName { get; set; }
37+
private string resourceGroupName { get; set; }
3638

3739
/// <summary>
3840
/// Resource context for the operation
3941
/// </summary>
40-
protected string ResourceName { get; set; }
42+
private string resourceName { get; set; }
4143

4244
/// <summary>
4345
/// Client request id.
4446
/// </summary>
45-
protected string clientRequestId;
47+
private string clientRequestId;
4648

4749
/// <summary>
4850
/// Azure backup client.
@@ -64,24 +66,25 @@ protected BackupServicesManagementClient AzureBackupClient
6466
{
6567
if (this.azureBackupClient == null)
6668
{
67-
this.azureBackupClient = AzureSession.ClientFactory.CreateClient<BackupServicesManagementClient>(Profile, Profile.Context.Subscription, AzureEnvironment.Endpoint.ResourceManager);
68-
// this.azureBackupClient.ResourceGroupName = resourceGroupName;
69-
// this.azureBackupClient.ResourceName = resourceName;
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);
7074
}
7175

7276
return this.azureBackupClient;
7377
}
7478
}
7579

76-
public override void ExecuteCmdlet()
80+
public void InitializeAzureBackupCmdlet(string rgName, string rName)
7781
{
78-
base.ExecuteCmdlet();
79-
80-
// Vaildate RGName, RName?
82+
resourceGroupName = rgName;
83+
resourceName = rName;
8184

8285
clientRequestId = Guid.NewGuid().ToString() + "-" + DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ssZ") + "-PS";
8386

84-
WriteVerbose(string.Format("ClientRequestId: {0}", this.clientRequestId));
87+
WriteDebug(string.Format("Initialized AzureBackup Cmdlet, ClientRequestId: {0}, ResourceGroupName: {1}, ResourceName : {2}", this.clientRequestId, resourceGroupName, resourceName));
8588

8689
CmdletCancellationToken = cancellationTokenSource.Token;
8790
}
@@ -155,6 +158,17 @@ private void HandleException(Exception exception)
155158
WriteError(errorRecord);
156159
}
157160
}
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+
}
158172
}
159173
}
160174

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
1717

1818
internal static class AzureBackupCmdletHelpMessage
1919
{
20-
public const string ResourceGroupName = "The resource group name.";
21-
public const string ResourceName = "The resource name.";
20+
public const string Vault = "The vault details";
2221
public const string PolicyName = "The protection policy name.";
22+
public const string ResourceGroupName = "The ResourceGroup name.";
23+
public const string ResourceName = "The Resource name.";
2324
}
2425
}
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+

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/ProtectionPolicy/GetAzureBackupProtectionPolicy.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
using System.Management.Automation;
1717
using System.Collections.Generic;
1818
using System.Xml;
19-
using Microsoft.WindowsAzure.Management.BackupServices.Models;
19+
using System.Linq;
20+
using Microsoft.Azure.Management.BackupServices.Models;
2021

2122
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2223
{
2324
/// <summary>
2425
/// Get list of containers
2526
/// </summary>
26-
[Cmdlet(VerbsCommon.Get, "AzureBackupProtectionPolicy"), OutputType(typeof(ProtectionPolicyInfo), typeof(List<ProtectionPolicyInfo>))]
27-
public class GetAzureBackupProtectionPolicy : AzureBackupCmdletBase
27+
[Cmdlet(VerbsCommon.Get, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy), typeof(List<AzureBackupProtectionPolicy>))]
28+
public class GetAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
2829
{
2930
[Parameter(Position = 2, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
3031
[ValidateNotNullOrEmpty]
@@ -36,9 +37,24 @@ public override void ExecuteCmdlet()
3637

3738
ExecutionBlock(() =>
3839
{
39-
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync("1234", CmdletCancellationToken).Result;
40+
WriteVerbose("Making client call");
4041

41-
WriteAzureBackupProtectionPolicy(policyListResponse.ProtectionPolicies.Objects);
42+
ProtectionPolicyListResponse policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
43+
44+
WriteVerbose("Received policy response");
45+
46+
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
47+
if (Name != null)
48+
{
49+
policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
50+
}
51+
else
52+
{
53+
policyObjects = policyListResponse.ProtectionPolicies.Objects;
54+
}
55+
56+
WriteVerbose("Converting response");
57+
WriteAzureBackupProtectionPolicy(policyObjects);
4258
});
4359
}
4460

@@ -47,7 +63,7 @@ public void WriteAzureBackupProtectionPolicy(ProtectionPolicyInfo sourcePolicy)
4763
this.WriteObject(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, sourcePolicy));
4864
}
4965

50-
public void WriteAzureBackupProtectionPolicy(IList<ProtectionPolicyInfo> sourcePolicyList)
66+
public void WriteAzureBackupProtectionPolicy(IEnumerable<ProtectionPolicyInfo> sourcePolicyList)
5167
{
5268
List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>();
5369

0 commit comments

Comments
 (0)