Skip to content

Commit

Permalink
fix the problem in automation that stream is still used after being c…
Browse files Browse the repository at this point in the history
…losed (#33907)
  • Loading branch information
Yao725 committed Feb 8, 2023
1 parent 2cd0c88 commit b6e3931
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30309.148
# Visual Studio Version 17
VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{78106D72-2E2E-4433-9188-E0703DBE21CD}") = "Azure.ResourceManager.Automation", "src\Azure.ResourceManager.Automation.csproj", "{5403181A-1EE5-4B7D-8FDD-785014F94FFD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.ResourceManager.Automation", "src\Azure.ResourceManager.Automation.csproj", "{5403181A-1EE5-4B7D-8FDD-785014F94FFD}"
EndProject
Project("{78106D72-2E2E-4433-9188-E0703DBE21CD}") = "Azure.ResourceManager.Automation.Tests", "tests\Azure.ResourceManager.Automation.Tests.csproj", "{1E8D0EFB-9AE9-4688-936D-98B4CCFF353E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.ResourceManager.Automation.Tests", "tests\Azure.ResourceManager.Automation.Tests.csproj", "{1E8D0EFB-9AE9-4688-936D-98B4CCFF353E}"
EndProject
Project("{78106D72-2E2E-4433-9188-E0703DBE21CD}") = "Azure.ResourceManager.Automation.Samples", "samples\Azure.ResourceManager.Automation.Samples.csproj", "{01968879-950A-4318-834B-66ADDF7EE015}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.ResourceManager.Automation.Samples", "samples\Azure.ResourceManager.Automation.Samples.csproj", "{01968879-950A-4318-834B-66ADDF7EE015}"
EndProject
Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C8191F65-A208-4F62-8F53-02148C12457D}
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -49,5 +43,23 @@ Global
{1E8D0EFB-9AE9-4688-936D-98B4CCFF353E}.Release|x64.Build.0 = Release|Any CPU
{1E8D0EFB-9AE9-4688-936D-98B4CCFF353E}.Release|x86.ActiveCfg = Release|Any CPU
{1E8D0EFB-9AE9-4688-936D-98B4CCFF353E}.Release|x86.Build.0 = Release|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Debug|x64.ActiveCfg = Debug|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Debug|x64.Build.0 = Debug|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Debug|x86.ActiveCfg = Debug|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Debug|x86.Build.0 = Debug|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Release|Any CPU.Build.0 = Release|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Release|x64.ActiveCfg = Release|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Release|x64.Build.0 = Release|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Release|x86.ActiveCfg = Release|Any CPU
{01968879-950A-4318-834B-66ADDF7EE015}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C8191F65-A208-4F62-8F53-02148C12457D}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;

namespace Azure.ResourceManager.Automation
{
/// <summary>
/// A Class representing an AutomationRunbook along with the instance operations that can be performed on it.
/// If you have a <see cref="ResourceIdentifier" /> you can construct an <see cref="AutomationRunbookResource" />
/// from an instance of <see cref="ArmClient" /> using the GetAutomationRunbookResource method.
/// Otherwise you can get one from its parent resource <see cref="AutomationAccountResource" /> using the GetAutomationRunbook method.
/// </summary>
public partial class AutomationRunbookResource
{
/// <summary>
/// Replaces the runbook draft content.
/// <list type="bullet">
/// <item>
/// <term>Request Path</term>
/// <description>/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks/{runbookName}/draft/content</description>
/// </item>
/// <item>
/// <term>Operation Id</term>
/// <description>RunbookDraft_ReplaceContent</description>
/// </item>
/// </list>
/// </summary>
/// <param name="waitUntil"> <see cref="WaitUntil.Completed"/> if the method should wait to return until the long-running operation has completed on the service; <see cref="WaitUntil.Started"/> if it should return after starting the operation. For more information on long-running operations, please see <see href="https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/LongRunningOperations.md"> Azure.Core Long-Running Operation samples</see>. </param>
/// <param name="runbookContent"> The runbook draft content. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentNullException"> <paramref name="runbookContent"/> is null. </exception>
public virtual async Task<ArmOperation> ReplaceContentRunbookDraftAsync(WaitUntil waitUntil, Stream runbookContent, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(runbookContent, nameof(runbookContent));

using var scope = _runbookDraftClientDiagnostics.CreateScope("AutomationRunbookResource.ReplaceContentRunbookDraft");
scope.Start();
try
{
var response = await _runbookDraftRestClient.ReplaceContentAsync(Id.SubscriptionId, Id.ResourceGroupName, Id.Parent.Name, Id.Name, runbookContent, cancellationToken).ConfigureAwait(false);
var operation = new AutomationArmOperation(_runbookDraftClientDiagnostics, Pipeline, _runbookDraftRestClient.CreateReplaceContentRequest(Id.SubscriptionId, Id.ResourceGroupName, Id.Parent.Name, Id.Name, runbookContent, true).Request, response, OperationFinalStateVia.Location);
if (waitUntil == WaitUntil.Completed)
await operation.WaitForCompletionResponseAsync(cancellationToken).ConfigureAwait(false);
return operation;
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

/// <summary>
/// Replaces the runbook draft content.
/// <list type="bullet">
/// <item>
/// <term>Request Path</term>
/// <description>/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks/{runbookName}/draft/content</description>
/// </item>
/// <item>
/// <term>Operation Id</term>
/// <description>RunbookDraft_ReplaceContent</description>
/// </item>
/// </list>
/// </summary>
/// <param name="waitUntil"> <see cref="WaitUntil.Completed"/> if the method should wait to return until the long-running operation has completed on the service; <see cref="WaitUntil.Started"/> if it should return after starting the operation. For more information on long-running operations, please see <see href="https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/LongRunningOperations.md"> Azure.Core Long-Running Operation samples</see>. </param>
/// <param name="runbookContent"> The runbook draft content. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentNullException"> <paramref name="runbookContent"/> is null. </exception>
public virtual ArmOperation ReplaceContentRunbookDraft(WaitUntil waitUntil, Stream runbookContent, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(runbookContent, nameof(runbookContent));

using var scope = _runbookDraftClientDiagnostics.CreateScope("AutomationRunbookResource.ReplaceContentRunbookDraft");
scope.Start();
try
{
var response = _runbookDraftRestClient.ReplaceContent(Id.SubscriptionId, Id.ResourceGroupName, Id.Parent.Name, Id.Name, runbookContent, cancellationToken);
var operation = new AutomationArmOperation(_runbookDraftClientDiagnostics, Pipeline, _runbookDraftRestClient.CreateReplaceContentRequest(Id.SubscriptionId, Id.ResourceGroupName, Id.Parent.Name, Id.Name, runbookContent, true).Request, response, OperationFinalStateVia.Location);
if (waitUntil == WaitUntil.Completed)
operation.WaitForCompletionResponse(cancellationToken);
return operation;
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using System.IO;
using Azure.Core;

namespace Azure.ResourceManager.Automation
{
[CodeGenSuppress("CreateReplaceContentRequest", typeof(string), typeof(string), typeof(string), typeof(string), typeof(Stream))]
internal partial class RunbookDraftRestOperations
{
internal HttpMessage CreateReplaceContentRequest(string subscriptionId, string resourceGroupName, string automationAccountName, string runbookName, Stream runbookContent, bool isContentSkipped = false)
{
var message = _pipeline.CreateMessage();
var request = message.Request;
request.Method = RequestMethod.Put;
var uri = new RawRequestUriBuilder();
uri.Reset(_endpoint);
uri.AppendPath("/subscriptions/", false);
uri.AppendPath(subscriptionId, true);
uri.AppendPath("/resourceGroups/", false);
uri.AppendPath(resourceGroupName, true);
uri.AppendPath("/providers/Microsoft.Automation/automationAccounts/", false);
uri.AppendPath(automationAccountName, true);
uri.AppendPath("/runbooks/", false);
uri.AppendPath(runbookName, true);
uri.AppendPath("/draft/content", false);
uri.AppendQuery("api-version", _apiVersion, true);
request.Uri = uri;
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Content-Type", "text/powershell");
if (!isContentSkipped)
{
request.Content = RequestContent.Create(runbookContent);
}
_userAgent.Apply(message);
return message;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b6e3931

Please sign in to comment.