Skip to content
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

[Accounts] Add Long-Running Operation (LRO) Support to Invoke-AzRestMethod #26225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
78 changes: 30 additions & 48 deletions src/Accounts/Accounts/Rest/InvokeAzRestMethodCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ public class InvokeAzRestMethodCommand : AzureRMCmdlet
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Wait till the long-running operation completes before returning the result.")]
public SwitchParameter WaitTillCompletion { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Specifies where to poll for long-running operation status.")]
[ValidateSet("AzureAsyncLocation", "Location", "OriginalUri", "OperationLocation", IgnoreCase = true)]
public string PollFrom { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Specifies where to get the final result after the long-running operation completes.")]
[ValidateSet("FinalStateVia", "Location", "OriginalUri", "OperationLocation", IgnoreCase = true)]
public string FinalResultFrom { get; set; }

#endregion

IAzureContext context;
Expand All @@ -100,7 +111,6 @@ public override void ExecuteCmdlet()
this.ValidateParameters();

context = DefaultContext;
AzureOperationResponse<string> response = null;

if (ByParameters.Equals(this.ParameterSetName))
{
Expand All @@ -111,17 +121,31 @@ public override void ExecuteCmdlet()
this.Path = Uri.PathAndQuery;
}


IAzureRestClient serviceClient = this.InitializeServiceClient();

RestRequestHandler handler = new RestRequestHandler(this, serviceClient, Method);
AzureOperationResponse<string> response = handler.ExecuteRestRequest(Path, ApiVersion, Payload);

WriteObject("This is New DEV TESTINGG VERSION!!!!");
WriteObject(new PSHttpResponse(response));
}


private IAzureRestClient InitializeServiceClient()
{
IAzureRestClient serviceClient = null;

if (ByPath.Equals(this.ParameterSetName) || ByParameters.Equals(this.ParameterSetName))
{
serviceClient = AzureSession.Instance.ClientFactory.CreateArmClient<AzureRestClient>(context, AzureEnvironment.Endpoint.ResourceManager);
}
else if (ByURI.Equals(this.ParameterSetName))
{
string targetResourceIdKey = null;
string resourceId = this.IsParameterBound(c => c.ResourceId) ? ResourceId.ToString() : null;
if(this.IsParameterBound(c => c.ResourceId)
&& context.Environment.ActiveDirectoryServiceEndpointResourceId.Equals(resourceId)
string resourceId = this.IsParameterBound(c => c.ResourceId) ? ResourceId.ToString() : null;
if (this.IsParameterBound(c => c.ResourceId)
&& context.Environment.ActiveDirectoryServiceEndpointResourceId.Equals(resourceId)
&& !HasSameEndpoint(Uri.Authority, context.Environment.ResourceManagerUrl))
{
throw new AzPSArgumentException("The resource ID of Azure Resource Manager cannot be used for other endpoint. Please make sure to input the correct resource ID that matches the request URI.",
Expand Down Expand Up @@ -150,51 +174,9 @@ public override void ExecuteCmdlet()
WriteErrorWithTimestamp("Parameter set is not implemented");
}

switch (this.Method.ToUpper())
{
case "GET":
response = serviceClient
.Operations
.GetResourceWithFullResponse(this.Path, this.ApiVersion);
break;
case "POST":
if (this.ShouldProcess(Path, "POST"))
{
response = serviceClient
.Operations
.PostResourceWithFullResponse(this.Path, this.ApiVersion, this.Payload);
}
break;
case "PUT":
if (this.ShouldProcess(Path, "PUT"))
{
response = serviceClient
.Operations
.PutResourceWithFullResponse(this.Path, this.ApiVersion, this.Payload);
}
break;
case "PATCH":
if (this.ShouldProcess(Path, "PATCH"))
{
response = serviceClient
.Operations
.PatchResourceWithFullResponse(this.Path, this.ApiVersion, this.Payload);
}
break;
case "DELETE":
if (this.ShouldProcess(Path, "DELETE"))
{
response = serviceClient
.Operations
.DeleteResourceWithFullResponse(this.Path, this.ApiVersion);
}
break;
default:
throw new AzPSArgumentException("Invalid HTTP Method", nameof(Method));
}
return serviceClient;
}

WriteObject(new PSHttpResponse(response));
}

private string MatchResourceId(IAzureContext context, string authority, out string targetResourceIdKey)
{
Expand Down
77 changes: 77 additions & 0 deletions src/Accounts/Accounts/Rest/RestRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using Microsoft.Azure.Commands.Common.Exceptions;
using System.IO;
using System.Management.Automation;
using Microsoft.Rest.Azure;
using Microsoft.Azure.Internal.Common;

namespace Microsoft.Azure.Commands.Profile.Rest
{
public class RestRequestHandler
{
private string Method;
private IAzureRestClient serviceClient;
private InvokeAzRestMethodCommand Parent;

public RestRequestHandler(InvokeAzRestMethodCommand parent, IAzureRestClient serviceClient, string Method)
{
this.Parent = parent;
this.Method = Method;
this.serviceClient = serviceClient;
}

public AzureOperationResponse<string> ExecuteRestRequest(string Path, string ApiVersion, string Payload)
{
AzureOperationResponse<string> response = null;

switch (this.Method.ToUpper())
{
case "GET":
response = serviceClient
.Operations
.GetResourceWithFullResponse(Path, ApiVersion);
break;
case "POST":
if (Parent.ShouldProcess(Path, "POST"))
{
response = serviceClient
.Operations
.PostResourceWithFullResponse(Path, ApiVersion, Payload);
}
break;
case "PUT":
if (Parent.ShouldProcess(Path, "PUT"))
{
response = serviceClient
.Operations
.PutResourceWithFullResponse(Path, ApiVersion, Payload);
}
break;
case "PATCH":
if (Parent.ShouldProcess(Path, "PATCH"))
{
response = serviceClient
.Operations
.PatchResourceWithFullResponse(Path, ApiVersion, Payload);
}
break;
case "DELETE":
if (Parent.ShouldProcess(Path, "DELETE"))
{
response = serviceClient
.Operations
.DeleteResourceWithFullResponse(Path, ApiVersion);
}
break;
default:
throw new AzPSArgumentException("Invalid HTTP Method", nameof(Method));
}


return response;
}


}
}