Skip to content

Check deployment exists before getting operations. #2468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
##2016.06.17 version 1.5.1
* Azure Resource Manager
- Fix a bug in New-AzureRmResourceGroupDeployment. In some deployments the cmdlet throws an exception with "Deployment 'deploymentName' could not be found." and causes the cmdlet to fail. The fix makes sure the deployment is created before getting operations.
##2016.06.01 version 1.5.0
* Azure Resource Manager
- (Get/Set/New/Remove)-AzureRmResourceGroup cmdlets will now use the new autorest generated ARM assembly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,27 @@ private List<DeploymentOperation> GetNewOperations(List<DeploymentOperation> old
Enum.TryParse<HttpStatusCode>(operation.Properties.StatusCode, out statusCode);
if (!statusCode.IsClientFailureRequest())
{
List<DeploymentOperation> newNestedOperations = new List<DeploymentOperation>();
var resourceGroupName = ResourceIdUtility.GetResourceGroupName(operation.Properties.TargetResource.Id);
var deploymentName = operation.Properties.TargetResource.ResourceName;

var result = ResourceManagementClient.DeploymentOperations.List(
resourceGroupName: ResourceIdUtility.GetResourceGroupName(operation.Properties.TargetResource.Id),
deploymentName: operation.Properties.TargetResource.ResourceName);
if (ResourceManagementClient.Deployments.CheckExistence(resourceGroupName, deploymentName) == true)
{
List<DeploymentOperation> newNestedOperations = new List<DeploymentOperation>();

newNestedOperations = GetNewOperations(operations, result);
var result = ResourceManagementClient.DeploymentOperations.List(
resourceGroupName: resourceGroupName,
deploymentName: deploymentName);

foreach (DeploymentOperation op in newNestedOperations)
{
DeploymentOperation nestedOperationWithSameIdAndProvisioningState = newOperations.Find(o => o.OperationId.Equals(op.OperationId) && o.Properties.ProvisioningState.Equals(op.Properties.ProvisioningState));
newNestedOperations = GetNewOperations(operations, result);

if (nestedOperationWithSameIdAndProvisioningState == null)
foreach (DeploymentOperation op in newNestedOperations)
{
newOperations.Add(op);
DeploymentOperation nestedOperationWithSameIdAndProvisioningState = newOperations.Find(o => o.OperationId.Equals(op.OperationId) && o.Properties.ProvisioningState.Equals(op.Properties.ProvisioningState));

if (nestedOperationWithSameIdAndProvisioningState == null)
{
newOperations.Add(op);
}
}
}
}
Expand Down Expand Up @@ -798,7 +804,7 @@ public virtual PSResourceGroupDeployment ExecuteDeployment(PSCreateResourceGroup
WriteVerbose(ProjectResources.TemplateValid);
}

ResourceManagementClient.Deployments.CreateOrUpdateAsync(parameters.ResourceGroupName, parameters.DeploymentName, deployment);
ResourceManagementClient.Deployments.BeginCreateOrUpdate(parameters.ResourceGroupName, parameters.DeploymentName, deployment);
WriteVerbose(string.Format(ProjectResources.CreatedDeployment, parameters.DeploymentName));
DeploymentExtended result = ProvisionDeploymentStatus(parameters.ResourceGroupName, parameters.DeploymentName, deployment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.Azure.Commands.Resources.Models;
Expand All @@ -23,16 +34,6 @@
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using Moq;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.Commands.Resources.Test.Models
Expand Down Expand Up @@ -392,7 +393,7 @@ public void NewResourceGroupUsesDeploymentNameForDeploymentName()
}))
.Callback((string rg, string dn, Deployment d, Dictionary<string, List<string>> customHeaders, CancellationToken c) => { deploymentFromValidate = d; });

deploymentsMock.Setup(f => f.CreateOrUpdateWithHttpMessagesAsync(
deploymentsMock.Setup(f => f.BeginCreateOrUpdateWithHttpMessagesAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<Deployment>(),
Expand All @@ -408,6 +409,16 @@ public void NewResourceGroupUsesDeploymentNameForDeploymentName()
.Callback((string name, string dName, Deployment bDeploy, Dictionary<string, List<string>> customHeaders, CancellationToken token) =>
{ deploymentFromGet = bDeploy; deploymentName = dName; });

deploymentsMock.Setup(f => f.CheckExistenceWithHttpMessagesAsync(
It.IsAny<string>(),
It.IsAny<string>(),
null,
new CancellationToken()))
.Returns(Task.Factory.StartNew(() => new AzureOperationResponse<bool?>()
{
Body = true
}));

SetupListForResourceGroupAsync(parameters.ResourceGroupName, new List<GenericResource>
{
CreateGenericResource(null, null, "website")
Expand Down Expand Up @@ -537,6 +548,176 @@ public void NewResourceGroupUsesDeploymentNameForDeploymentName()
Times.Once());
}

[Fact]
public void NewResourceGroupDeploymentWithDelay()
{
string deploymentName = "abc123";
ConcurrentBag<string> deploymentNames = new ConcurrentBag<string>();

PSCreateResourceGroupParameters parameters = new PSCreateResourceGroupParameters()
{
ResourceGroupName = resourceGroupName,
Location = resourceGroupLocation,
DeploymentName = deploymentName,
ConfirmAction = ConfirmAction,
TemplateFile = "http://path/file.html"
};

deploymentsMock.Setup(f => f.ValidateWithHttpMessagesAsync(
parameters.ResourceGroupName,
parameters.DeploymentName,
It.IsAny<Deployment>(),
null,
new CancellationToken()))
.Returns(Task.Factory.StartNew(() =>
new AzureOperationResponse<DeploymentValidateResult>()
{
Body = new DeploymentValidateResult
{
}
}));

deploymentsMock.Setup(f => f.GetWithHttpMessagesAsync(
parameters.ResourceGroupName,
parameters.DeploymentName,
null,
new CancellationToken()))
.Returns<string, string, Dictionary<string, List<string>>, CancellationToken>(
async (getResourceGroupName, getDeploymentName, customHeaders, cancellationToken) =>
{
await Task.Delay(100, cancellationToken);

if (deploymentNames.Contains(getDeploymentName))
{
return new AzureOperationResponse<DeploymentExtended>()
{
Body = new DeploymentExtended()
{
Name = getDeploymentName,
Id = requestId,
Properties = new DeploymentPropertiesExtended()
{
Mode = DeploymentMode.Incremental,
CorrelationId = "123",
ProvisioningState = "Succeeded"
},
}
};
}

throw new CloudException(String.Format("Deployment '{0}' could not be found.", getDeploymentName));
});

deploymentsMock.Setup(f => f.BeginCreateOrUpdateWithHttpMessagesAsync(
parameters.ResourceGroupName,
parameters.DeploymentName,
It.IsAny<Deployment>(),
null,
new CancellationToken()))
.Returns<string, string, Deployment, Dictionary<string, List<string>>, CancellationToken>(
async (craeteResourceGroupName, createDeploymentName, createDeployment, customHeaders, cancellationToken) =>
{
await Task.Delay(500, cancellationToken);

deploymentNames.Add(createDeploymentName);

return new AzureOperationResponse<DeploymentExtended>()
{
Body = new DeploymentExtended
{
Id = requestId
}
};
});

deploymentsMock.Setup(f => f.CreateOrUpdateWithHttpMessagesAsync(
parameters.ResourceGroupName,
parameters.DeploymentName,
It.IsAny<Deployment>(),
null,
new CancellationToken()))
.Returns<string, string, Deployment, Dictionary<string, List<string>>, CancellationToken>(
async (craeteResourceGroupName, createDeploymentName, createDeployment, customHeaders, cancellationToken) =>
{
await Task.Delay(10000, cancellationToken);

deploymentNames.Add(createDeploymentName);

return new AzureOperationResponse<DeploymentExtended>()
{
Body = new DeploymentExtended
{
Id = requestId
}
};
});

deploymentsMock.Setup(f => f.CheckExistenceWithHttpMessagesAsync(
parameters.ResourceGroupName,
parameters.DeploymentName,
null,
new CancellationToken()))
.Returns(Task.Factory.StartNew(() => new AzureOperationResponse<bool?>()
{
Body = true
}));

SetupListForResourceGroupAsync(parameters.ResourceGroupName, new List<GenericResource>
{
CreateGenericResource(null, null, "website")
});

var operationId = Guid.NewGuid().ToString();
var operationQueue = new Queue<DeploymentOperation>();
operationQueue.Enqueue(
new DeploymentOperation()
{
OperationId = operationId,
Properties = new DeploymentOperationProperties()
{
ProvisioningState = "Succeeded",
TargetResource = new TargetResource()
{
ResourceType = "Microsoft.Website",
ResourceName = resourceName
}
}
}
);
deploymentOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(
parameters.ResourceGroupName,
parameters.DeploymentName,
null,
null,
new CancellationToken()))
.Returns<string, string, int?, Dictionary<string, List<string>>, CancellationToken>(
async (getResourceGroupName, getDeploymentName, top, customHeaders, cancellationToken) =>
{
await Task.Delay(100, cancellationToken);

if (deploymentNames.Contains(getDeploymentName))
{
return new AzureOperationResponse<IPage<DeploymentOperation>>()
{
Body = GetPagableType(
new List<DeploymentOperation>()
{
operationQueue.Dequeue()
})
};
}

throw new CloudException(String.Format("Deployment '{0}' could not be found.", getDeploymentName));
});

Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroupDeployment result = resourcesClient.ExecuteDeployment(parameters);
Assert.Equal(deploymentName, result.DeploymentName);
Assert.Equal("Succeeded", result.ProvisioningState);
progressLoggerMock.Verify(
f => f(string.Format("Resource {0} '{1}' provisioning status is {2}", "Microsoft.Website", resourceName, "Succeeded".ToLower())),
Times.Once());
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NewResourceGroupWithDeploymentSucceeds()
Expand Down Expand Up @@ -573,7 +754,7 @@ public void NewResourceGroupWithDeploymentSucceeds()
Body = new ResourceGroup() { Location = resourceGroupLocation }
}));

deploymentsMock.Setup(f => f.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, deploymentName, It.IsAny<Deployment>(), null, new CancellationToken()))
deploymentsMock.Setup(f => f.BeginCreateOrUpdateWithHttpMessagesAsync(resourceGroupName, deploymentName, It.IsAny<Deployment>(), null, new CancellationToken()))
.Returns(Task.Factory.StartNew(() => new AzureOperationResponse<DeploymentExtended>()
{
Body = new DeploymentExtended
Expand Down Expand Up @@ -604,6 +785,15 @@ public void NewResourceGroupWithDeploymentSucceeds()
{
})))
.Callback((string rg, string dn, Deployment d, Dictionary<string, List<string>> customHeaders, CancellationToken c) => { deploymentFromValidate = d; });
deploymentsMock.Setup(f => f.CheckExistenceWithHttpMessagesAsync(
It.IsAny<string>(),
It.IsAny<string>(),
null,
new CancellationToken()))
.Returns(Task.Factory.StartNew(() => new AzureOperationResponse<bool?>()
{
Body = true
}));

SetupListForResourceGroupAsync(parameters.ResourceGroupName, new List<GenericResource>() { CreateGenericResource(null, null, "website") });
deploymentOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(resourceGroupName, deploymentName, null, null, new CancellationToken()))
Expand All @@ -626,7 +816,7 @@ public void NewResourceGroupWithDeploymentSucceeds()

PSResourceGroup result = resourcesClient.CreatePSResourceGroup(parameters);
Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroupDeployment deploymentResult = resourcesClient.ExecuteDeployment(parameters);
deploymentsMock.Verify((f => f.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, deploymentName, deploymentFromGet, null, new CancellationToken())), Times.Once());
deploymentsMock.Verify((f => f.BeginCreateOrUpdateWithHttpMessagesAsync(resourceGroupName, deploymentName, deploymentFromGet, null, new CancellationToken())), Times.Once());
Assert.Equal(parameters.ResourceGroupName, deploymentResult.ResourceGroupName);

Assert.Equal(DeploymentMode.Incremental, deploymentFromGet.Properties.Mode);
Expand Down
Loading