Skip to content

Commit db8bd30

Browse files
Ids instead of objects.
1 parent 37909d0 commit db8bd30

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class AsyncOperationVisitor : IResourceConfigVisitor<Task<object>>
1818

1919
public async Task<object> GetOrAddUntyped(IResourceConfig config)
2020
=> await TaskMap.GetOrAdd(
21-
config,
21+
config.DefaultIdStr(),
2222
async _ =>
2323
{
2424
var model = await config.Apply(this);
@@ -49,7 +49,7 @@ public abstract Task<object> Visit<Model, ParentModel>(
4949
where Model : class
5050
where ParentModel : class;
5151

52-
ConcurrentDictionary<IResourceConfig, Task<object>> TaskMap { get; }
53-
= new ConcurrentDictionary<IResourceConfig, Task<object>>();
52+
ConcurrentDictionary<string, Task<object>> TaskMap { get; }
53+
= new ConcurrentDictionary<string, Task<object>>();
5454
}
5555
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,8 @@ public static ResourceConfig<Model> CreateConfig<Model>(
7777

7878
public static string IdToString(this IEnumerable<string> id)
7979
=> "/" + string.Join("/", id);
80+
81+
public static string DefaultIdStr(this IResourceConfig config)
82+
=> config.GetId(string.Empty).IdToString();
8083
}
8184
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ sealed class State : IState
77
{
88
public Model GetOrNull<Model>(IResourceConfig<Model> config)
99
where Model : class
10-
=> Map.GetOrNull(config) as Model;
10+
=> Map.GetOrNull(config.DefaultIdStr()) as Model;
1111

1212
public Model GetOrAdd<Model>(IResourceConfig<Model> config, Func<Model> f)
1313
where Model : class
1414
=> GetOrAddUntyped(config, f) as Model;
1515

1616
public object GetOrAddUntyped(IResourceConfig config, Func<object> f)
17-
=> Map.GetOrAdd(config, _ => f());
17+
=> Map.GetOrAdd(config.DefaultIdStr(), _ => f());
1818

19-
ConcurrentDictionary<IResourceConfig, object> Map { get; }
20-
= new ConcurrentDictionary<IResourceConfig, object>();
19+
ConcurrentDictionary<string, object> Map { get; }
20+
= new ConcurrentDictionary<string, object>();
2121
}
2222
}

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
5656
Mandatory = true,
5757
Position = 0,
5858
ValueFromPipelineByPropertyName = true)]
59+
[Parameter(
60+
ParameterSetName = StrategyParameterSet,
61+
Mandatory = false)]
5962
[ValidateNotNullOrEmpty]
6063
public string ResourceGroupName { get; set; }
6164

@@ -64,6 +67,9 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
6467
Mandatory = true,
6568
Position = 1,
6669
ValueFromPipelineByPropertyName = true)]
70+
[Parameter(
71+
ParameterSetName = StrategyParameterSet,
72+
Mandatory = false)]
6773
[ValidateNotNullOrEmpty]
6874
public string Location { get; set; }
6975

@@ -78,9 +84,10 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
7884
public PSVirtualMachine VM { get; set; }
7985

8086
[Parameter(
81-
Mandatory = false,
82-
Position = 3,
83-
ValueFromPipelineByPropertyName = true)]
87+
ParameterSetName = DefaultParameterSet,
88+
Mandatory = false,
89+
Position = 3,
90+
ValueFromPipelineByPropertyName = true)]
8491
[ValidateNotNullOrEmpty]
8592
public string[] Zone { get; set; }
8693

@@ -154,7 +161,12 @@ public T GetClient<T>()
154161

155162
public void StrategyExecuteCmdlet()
156163
{
157-
var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(Name);
164+
if (ResourceGroupName == null)
165+
{
166+
ResourceGroupName = Name;
167+
}
168+
169+
var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
158170
var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(Name, AddressPrefix);
159171
var subnet = virtualNetwork.CreateSubnet(Name, SubnetAddressPrefix);
160172
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(Name);
@@ -173,12 +185,17 @@ public void StrategyExecuteCmdlet()
173185
.GetAsync(client, new CancellationToken())
174186
.GetAwaiter()
175187
.GetResult();
176-
var location = state.GetLocation(virtualMachine);
177-
if (location == null)
188+
189+
if (Location == null)
178190
{
179-
location = "eastus";
191+
Location = state.GetLocation(virtualMachine);
192+
if (Location == null)
193+
{
194+
Location = "eastus";
195+
}
180196
}
181-
var target = virtualMachine.GetTargetState(client.SubscriptionId, location);
197+
198+
var target = virtualMachine.GetTargetState(client.SubscriptionId, Location);
182199
var result = virtualMachine
183200
.CreateOrUpdateAsync(client, state, target, new CancellationToken())
184201
.GetAwaiter()

0 commit comments

Comments
 (0)