Skip to content

Commit

Permalink
NestedResource bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 27, 2017
1 parent eb69566 commit c6b0f02
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public interface IState
TModel Get<TModel>(ResourceConfig<TModel> config)
where TModel : class;

bool Contains(IEntityConfig config);
bool Contains(IResourceConfig config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TModel GetOrAdd<TModel>(ResourceConfig<TModel> config, Func<TModel> f)
where TModel : class
=> _Map.GetOrAddWithCast(config.DefaultIdStr(), f);

public bool Contains(IEntityConfig config)
public bool Contains(IResourceConfig config)
=> _Map.ContainsKey(config.DefaultIdStr());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Microsoft.Azure.Commands.Common.Strategies
using System;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class StateExtensions
{
Expand Down Expand Up @@ -28,6 +30,15 @@ public static TModel GetDispatch<TModel>(
where TModel : class
=> config.Accept(new GetVisitor<TModel>(), state);

public static bool Contains<TModel, TParentModel>(
this IState state, NestedResourceConfig<TModel, TParentModel> config)
where TModel : class
where TParentModel : class
=> state.Get(config) != null;

public static bool ContainsDispatch(this IState state, IEntityConfig config)
=> config.Accept(new ContainsDispatchVisitor(), state);

sealed class GetVisitor<TModel> : IEntityConfigVisitor<TModel, IState, TModel>
where TModel : class
{
Expand All @@ -39,5 +50,18 @@ public TModel Visit<TParentModel>(
where TParentModel : class
=> state.Get(config);
}

sealed class ContainsDispatchVisitor : IEntityConfigVisitor<IState, bool>
{
public bool Visit<TModel>(ResourceConfig<TModel> config, IState context)
where TModel : class
=> context.Contains(config);

public bool Visit<TModel, TParentModel>(
NestedResourceConfig<TModel, TParentModel> config, IState context)
where TModel : class
where TParentModel : class
=> context.Contains(config);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Context(IState current, string subscription, string location)

public void AddIfRequired(IEntityConfig config)
{
if (!Current.Contains(config))
if (!Current.ContainsDispatch(config))
{
config.Accept(new AddVisitor(), this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public void StrategyExecuteCmdlet()
adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password,
image: image.Image);

//
var client = new Client(DefaultProfile.DefaultContext);
var current = virtualMachine
.GetStateAsync(client, new CancellationToken())
Expand All @@ -240,10 +239,15 @@ public void StrategyExecuteCmdlet()
}

var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location);
var result = virtualMachine
.UpdateStateAsync(client, target, new CancellationToken())
.GetAwaiter()
.GetResult();

if (ShouldProcess(Name, VerbsCommon.New))
{
var result = virtualMachine
.UpdateStateAsync(client, target, new CancellationToken())
.GetAwaiter()
.GetResult();
WriteObject(result);
}
}

public void DefaultExecuteCmdlet()
Expand Down Expand Up @@ -310,7 +314,8 @@ public void DefaultExecuteCmdlet()
AutoUpgradeMinorVersion = true,
};

typeof(CM.Resource).GetRuntimeProperty("Name").SetValue(extensionParameters, VirtualMachineBGInfoExtensionContext.ExtensionDefaultName);
typeof(CM.Resource).GetRuntimeProperty("Name")
.SetValue(extensionParameters, VirtualMachineBGInfoExtensionContext.ExtensionDefaultName);
typeof(CM.Resource).GetRuntimeProperty("Type")
.SetValue(extensionParameters, VirtualMachineExtensionType);

Expand Down

0 comments on commit c6b0f02

Please sign in to comment.