Skip to content

Commit 44fd921

Browse files
Client.
1 parent cad25fb commit 44fd921

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

src/ResourceManager/Common/Commands.Common.Strategies/Compute/ComputeStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class ComputePolicy
99
{
1010
public static ResourceStrategy<Model> Create<Model, Operations>(
1111
string header,
12-
Func<IComputeManagementClient, Operations> getOperations,
12+
Func<ComputeManagementClient, Operations> getOperations,
1313
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
1414
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync)
1515
where Model : Resource
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System;
1+
using Microsoft.Rest;
2+
using System;
23

34
namespace Microsoft.Azure.Commands.Common.Strategies
45
{
56
public interface IClient
67
{
78
T GetClient<T>()
8-
where T : class, IDisposable;
9+
where T : ServiceClient<T>;
910
}
1011
}

src/ResourceManager/Common/Commands.Common.Strategies/Network/NetworkStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class NetworkStrategy
99
{
1010
public static ResourceStrategy<Model> Create<Model, Operations>(
1111
string header,
12-
Func<INetworkManagementClient, Operations> getOperations,
12+
Func<NetworkManagementClient, Operations> getOperations,
1313
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
1414
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync)
1515
where Model : Resource

src/ResourceManager/Common/Commands.Common.Strategies/ResourceManager/ResourceGroupStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.Azure.Commands.Common.Strategies.ResourceManager
77
public static class ResourceGroupStrategy
88
{
99
public static ResourceStrategy<ResourceGroup> Strategy { get; }
10-
= ResourceStrategy.Create<ResourceGroup, IResourceManagementClient, IResourceGroupsOperations>(
10+
= ResourceStrategy.Create<ResourceGroup, ResourceManagementClient, IResourceGroupsOperations>(
1111
_ => Enumerable.Empty<string>(),
1212
client => client.ResourceGroups,
1313
p => p.Operations.GetAsync(p.Name, p.CancellationToken),

src/ResourceManager/Common/Commands.Common.Strategies/ResourceStrategy.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.Rest;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -41,7 +42,7 @@ public static ResourceStrategy<Model> Create<Model, Client, Operations>(
4142
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync,
4243
Func<Model, string> getLocation,
4344
Action<Model, string> setLocation)
44-
where Client : class, IDisposable
45+
where Client : ServiceClient<Client>
4546
{
4647
Func<IClient, Operations> toOperations = client => getOperations(client.GetClient<Client>());
4748
return new ResourceStrategy<Model>(
@@ -61,7 +62,7 @@ public static ResourceStrategy<Model> Create<Model, Client, Operations>(
6162
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync,
6263
Func<Model, string> getLocation,
6364
Action<Model, string> setLocation)
64-
where Client : class, IDisposable
65+
where Client : ServiceClient<Client>
6566
=> Create(
6667
name => new[] { "providers" }.Concat(headers).Concat(new[] { name }),
6768
getOperations,

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616
using Microsoft.Azure.Commands.Common.Authentication;
1717
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1818
using Microsoft.Azure.Commands.Common.Authentication.Models;
19+
using Microsoft.Azure.Commands.Common.Strategies;
1920
using Microsoft.Azure.Commands.Common.Strategies.Compute;
2021
using Microsoft.Azure.Commands.Common.Strategies.Network;
2122
using Microsoft.Azure.Commands.Common.Strategies.ResourceManager;
2223
using Microsoft.Azure.Commands.Compute.Common;
2324
using Microsoft.Azure.Commands.Compute.Models;
2425
using Microsoft.Azure.Management.Compute;
2526
using Microsoft.Azure.Management.Compute.Models;
27+
using Microsoft.Azure.Management.Network;
28+
using Microsoft.Azure.Management.ResourceManager;
2629
using Microsoft.Azure.Management.Storage;
2730
using Microsoft.Azure.Management.Storage.Models;
31+
using Microsoft.Rest;
2832
using System;
2933
using System.Collections;
3034
using System.Linq;
3135
using System.Management.Automation;
3236
using System.Reflection;
37+
using System.Threading;
3338
using CM = Microsoft.Azure.Management.Compute.Models;
3439

3540
namespace Microsoft.Azure.Commands.Compute
@@ -38,14 +43,19 @@ namespace Microsoft.Azure.Commands.Compute
3843
[OutputType(typeof(PSAzureOperationResponse))]
3944
public class NewAzureVMCommand : VirtualMachineBaseCmdlet
4045
{
46+
public const string DefaultParameterSet = "DefaultParameterSet";
47+
public const string StrategyParameterSet = "StrategyParameterSet";
48+
4149
[Parameter(
50+
ParameterSetName = DefaultParameterSet,
4251
Mandatory = true,
4352
Position = 0,
4453
ValueFromPipelineByPropertyName = true)]
4554
[ValidateNotNullOrEmpty]
4655
public string ResourceGroupName { get; set; }
4756

4857
[Parameter(
58+
ParameterSetName = DefaultParameterSet,
4959
Mandatory = true,
5060
Position = 1,
5161
ValueFromPipelineByPropertyName = true)]
@@ -54,6 +64,7 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
5464

5565
[Alias("VMProfile")]
5666
[Parameter(
67+
ParameterSetName = DefaultParameterSet,
5768
Mandatory = true,
5869
Position = 2,
5970
ValueFromPipeline = true,
@@ -62,23 +73,24 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
6273
public PSVirtualMachine VM { get; set; }
6374

6475
[Parameter(
76+
ParameterSetName = DefaultParameterSet,
6577
Position = 3,
6678
HelpMessage = "Disable BG Info Extension")]
6779
public SwitchParameter DisableBginfoExtension { get; set; }
6880

6981
[Parameter(
82+
ParameterSetName = DefaultParameterSet,
7083
Mandatory = false,
7184
ValueFromPipelineByPropertyName = true)]
7285
public Hashtable Tags { get; set; }
7386

7487
[Parameter(
88+
ParameterSetName = DefaultParameterSet,
7589
Mandatory = false,
7690
ValueFromPipelineByPropertyName = false)]
7791
[ValidateNotNullOrEmpty]
7892
public string LicenseType { get; set; }
7993

80-
public const string StrategyParameterSet = "StrategyParameterSet";
81-
8294
[Parameter(
8395
ParameterSetName = StrategyParameterSet,
8496
Mandatory = true)]
@@ -94,7 +106,7 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
94106
Mandatory = false)]
95107
public string SubnetAddressPrefix { get; } = "192.168.1.0/24";
96108

97-
[Parameter(ParameterSetName = StrategyParameterSet, Mandatory = false)]
109+
[Parameter(ParameterSetName = StrategyParameterSet, Mandatory = true)]
98110
public PSCredential Credential { get; }
99111

100112
public override void ExecuteCmdlet()
@@ -110,6 +122,24 @@ public override void ExecuteCmdlet()
110122
}
111123
}
112124

125+
private sealed class Client : IClient
126+
{
127+
public string SubscriptionId { get; }
128+
129+
IAzureContext Context { get; }
130+
131+
public Client(IAzureContext context)
132+
{
133+
Context = context;
134+
SubscriptionId = Context.Subscription.Id;
135+
}
136+
137+
public T GetClient<T>()
138+
where T : ServiceClient<T>
139+
=> AzureSession.Instance.ClientFactory.CreateArmClient<T>(
140+
Context, AzureEnvironment.Endpoint.ResourceManager);
141+
}
142+
113143
public void StrategyExecuteCmdlet()
114144
{
115145
var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(Name);
@@ -124,6 +154,19 @@ public void StrategyExecuteCmdlet()
124154
networkInterface,
125155
Credential.UserName,
126156
new System.Net.NetworkCredential(string.Empty, Credential.Password).Password);
157+
158+
//
159+
var client = new Client(DefaultProfile.DefaultContext);
160+
var state = virtualMachine
161+
.GetAsync(client, new CancellationToken())
162+
.GetAwaiter()
163+
.GetResult();
164+
var location = state.GetLocation(virtualMachine);
165+
var target = virtualMachine.GetTargetState(client.SubscriptionId, location);
166+
var result = virtualMachine
167+
.CreateOrUpdateAsync(client, state, target, new CancellationToken())
168+
.GetAwaiter()
169+
.GetResult();
127170
}
128171

129172
public void DefaultExecuteCmdlet()

0 commit comments

Comments
 (0)