Skip to content

Commit c3707f7

Browse files
authored
Merge pull request #5264 from corquiri/ReservedInstance
Reserved instance auto register provider
2 parents 2f0fb9f + c574ce4 commit c3707f7

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

src/ResourceManager/Reservations/ChangeLog.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<!--
1+
2+
<!--
23
Please leave this section at the top of the change log.
34
45
Changes for the current release should go under the section titled "Current Release", and should adhere to the following format:
@@ -33,4 +34,8 @@
3334
* New Cmdlet Get-AzureRmReservationOrderId
3435
- cmdlet to retrieve list of reservation order ids that are applicable to subscription.
3536
* New Cmdlet Get-AzureRmReservationCatalog
36-
- cmdlet to retrieve available reservation catalog for the subscription.
37+
- cmdlet to retrieve available reservation catalog for the subscription.
38+
* Enable subscription Auto-Registration for the reservations provider
39+
40+
## Version 0.1.0
41+
* Initial Release of Resrvations cmdlets

src/ResourceManager/Reservations/Commands.Reservations/Cmdlets/PatchReservation.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
using System.Management.Automation;
77
using Newtonsoft.Json;
88
using Microsoft.Azure.Management.Reservations;
9+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
10+
using Microsoft.Azure.Management.Internal.Resources;
11+
using Microsoft.Azure.Commands.Common.Authentication;
12+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
13+
using Microsoft.Azure.Management.Internal.Resources.Models;
14+
using Microsoft.Rest.Azure;
915

1016
namespace Microsoft.Azure.Commands.Reservations.Cmdlets
1117
{
@@ -53,6 +59,10 @@ public override void ExecuteCmdlet()
5359
Patch Patch;
5460
if (AppliedScope != null)
5561
{
62+
//Pre-register for Microsoft.Compute
63+
string subscriptionId = ValidateAndGetAppliedSubscription();
64+
PreRegister(subscriptionId);
65+
5666
Patch = new Patch(AppliedScopeType, new List<string>() { AppliedScope });
5767
}
5868
else
@@ -63,5 +73,69 @@ public override void ExecuteCmdlet()
6373
WriteObject(response);
6474
}
6575
}
76+
77+
private void PreRegister(string subscriptionId)
78+
{
79+
try
80+
{
81+
IAzureContext context;
82+
if (TryGetDefaultContext(out context)
83+
&& context.Account != null
84+
&& context.Subscription != null)
85+
{
86+
var client = new ResourceManagementClient(
87+
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
88+
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager));
89+
client.SubscriptionId = subscriptionId;
90+
91+
string ComputeProviderNamespace = "Microsoft.Compute";
92+
var maxRetryCount = 10;
93+
94+
var provider = client.Providers.Get(ComputeProviderNamespace);
95+
if (provider.RegistrationState != RegistrationState.Registered)
96+
{
97+
short retryCount = 0;
98+
do
99+
{
100+
if (retryCount++ > maxRetryCount)
101+
{
102+
throw new TimeoutException();
103+
}
104+
provider = client.Providers.Register(ComputeProviderNamespace);
105+
TestMockSupport.Delay(2000);
106+
} while (provider.RegistrationState != RegistrationState.Registered);
107+
}
108+
}
109+
}
110+
catch (Exception e)
111+
{
112+
if (e.Message?.IndexOf("does not have authorization") >= 0 && e.Message?.IndexOf("register/action",
113+
StringComparison.InvariantCultureIgnoreCase) >= 0)
114+
{
115+
throw new CloudException(e.Message);
116+
}
117+
}
118+
}
119+
120+
private string ValidateAndGetAppliedSubscription()
121+
{
122+
string subscriptionId = AppliedScope;
123+
string prefix = "/subscriptions/";
124+
if (subscriptionId.IndexOf(prefix, StringComparison.InvariantCultureIgnoreCase) >= 0
125+
&& subscriptionId.Length > prefix.Length)
126+
{
127+
subscriptionId = subscriptionId.Substring(prefix.Length);
128+
}
129+
130+
Guid result;
131+
if (Guid.TryParse(subscriptionId, out result))
132+
{
133+
return result.ToString();
134+
}
135+
else
136+
{
137+
throw new PSArgumentException("Invalid applied scope provided");
138+
}
139+
}
66140
}
67141
}

src/ResourceManager/Reservations/Commands.Reservations/help/Update-AzureRmReservation.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ Update a `Reservation`.
1515
### CommandLine (Default)
1616
```
1717
Update-AzureRmReservation -ReservationOrderId <String> -ReservationId <String> -AppliedScopeType <String>
18-
[-AppliedScope <String>] [-WhatIf] [-Confirm] [<CommonParameters>]
18+
[-AppliedScope <String>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
1919
```
2020

2121
### PipeObject
2222
```
2323
Update-AzureRmReservation -AppliedScopeType <String> [-AppliedScope <String>] -Reservation <PSReservation>
24-
[-WhatIf] [-Confirm] [<CommonParameters>]
24+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2525
```
2626

2727
## DESCRIPTION
@@ -31,7 +31,7 @@ Updates the applied scopes of the `Reservation`.
3131

3232
### Example 1
3333
```
34-
PS C:\> Update-AzureRmReservation -ReservationOrderId "11111111-1111-1111-1111-1111111111" -ReservationId "00000000-1111-1111-1111-0000000000" -appliedScopeType "Single" -appliedscope "1111aaaa-b1b2-c0c2-d0d2-00000fffff"
34+
PS C:\> Update-AzureRmReservation -ReservationOrderId "11111111-1111-1111-1111-1111111111" -ReservationId "00000000-1111-1111-1111-0000000000" -appliedScopeType "Single" -appliedscope "/subscriptions/1111aaaa-b1b2-c0c2-d0d2-00000fffff"
3535
```
3636

3737
Updates the AppliedScopeType of the specified reservation to Single
@@ -76,6 +76,21 @@ Accept pipeline input: False
7676
Accept wildcard characters: False
7777
```
7878

79+
### -DefaultProfile
80+
The credentials, account, tenant, and subscription used for communication with Azure.
81+
82+
```yaml
83+
Type: IAzureContextContainer
84+
Parameter Sets: (All)
85+
Aliases: AzureRmContext, AzureCredential
86+
87+
Required: False
88+
Position: Named
89+
Default value: None
90+
Accept pipeline input: False
91+
Accept wildcard characters: False
92+
```
93+
7994
### -Reservation
8095
Pipe object parameter for `Reservation`
8196

0 commit comments

Comments
 (0)