Skip to content

Commit 2feee8c

Browse files
committed
Fixes to invoke-action
1 parent 19e8291 commit 2feee8c

File tree

12 files changed

+96
-19
lines changed

12 files changed

+96
-19
lines changed

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Components/LongRunningOperationHelper.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal LongRunningOperationHelper(string activityName, Func<ResourceManagerRes
7676
/// Waits for the operation to complete.
7777
/// </summary>
7878
/// <param name="operationResult">The operation result.</param>
79-
internal JToken WaitOnOperation(OperationResult operationResult)
79+
internal string WaitOnOperation(OperationResult operationResult)
8080
{
8181
// TODO: Re-factor this mess.
8282
this.ProgressTrackerObject.UpdateProgress("Starting", 0);
@@ -161,10 +161,11 @@ private TrackingOperationResult HandleOperationResponse(OperationResult operatio
161161
/// <param name="operationResult">The result of the operation.</param>
162162
private TrackingOperationResult HandleCreateOrUpdateResponse(OperationResult operationResult)
163163
{
164-
var resource = operationResult.Value == null
165-
? null
166-
: operationResult.Value
167-
.ToObject<Resource<InsensitiveDictionary<JToken>>>(JsonExtensions.JsonObjectTypeSerializer);
164+
Resource<InsensitiveDictionary<JToken>> resource;
165+
if (!operationResult.Value.TryConvertTo<Resource<InsensitiveDictionary<JToken>>>(out resource))
166+
{
167+
return null;
168+
}
168169

169170
if(resource == null && operationResult.HttpStatusCode == HttpStatusCode.Created)
170171
{
@@ -444,11 +445,12 @@ private string GetAzureAsyncOperationState(OperationResult operationResult)
444445
/// <param name="operationResult">The operation result.</param>
445446
private static string GetResourceState(OperationResult operationResult)
446447
{
447-
var resource = operationResult.Value == null
448-
? null
449-
: operationResult.Value
450-
.ToObject<Resource<InsensitiveDictionary<JToken>>>(JsonExtensions.JsonObjectTypeSerializer);
451-
448+
Resource<InsensitiveDictionary<JToken>> resource;
449+
if (!operationResult.Value.TryConvertTo<Resource<InsensitiveDictionary<JToken>>>(out resource))
450+
{
451+
return null;
452+
}
453+
452454
if (resource == null)
453455
{
454456
return null;

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Components/OperationResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ public class OperationResult
5656
/// <summary>
5757
/// Gets or sets the value.
5858
/// </summary>
59-
public JObject Value { get; set; }
59+
public string Value { get; set; }
6060
}
6161
}

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Components/ResourceIdUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static string GetResourceId(string resourceId, string extensionResourceTy
4040
resourceIdBuilder.Append(ResourceIdUtility.ProcessResourceTypeAndName(resourceType: extensionResourceType, resourceName: extensionResourceName));
4141
}
4242

43-
return resourceId.ToString();
43+
return resourceIdBuilder.ToString();
4444
}
4545

4646
/// <summary>

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Extensions/HttpMessageExtensions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,36 @@ public static async Task<T> ReadContentAsJsonAsync<T>(this HttpResponseMessage m
5050
}
5151
}
5252
}
53+
54+
{
55+
/// <summary>
56+
/// Reads the JSON content from the http response message.
57+
/// </summary>
58+
/// <typeparam name="T">The type of object contained in the JSON.</typeparam>
59+
/// <param name="message">The response message to be read.</param>
60+
/// <param name="rewindContentStream">Rewind content stream if set to true.</param>
61+
/// <returns>An object of type T instantiated from the response message's body.</returns>
62+
public static async Task<string> ReadContentAsStringAsync(this HttpResponseMessage message, bool rewindContentStream = false)
63+
{
64+
using (var stream = await message.Content
65+
.ReadAsStreamAsync()
66+
.ConfigureAwait(continueOnCapturedContext: false))
67+
using (var streamReader = new StreamReader(stream))
68+
{
69+
var streamPosition = stream.Position;
70+
try
71+
{
72+
73+
return streamReader.ReadToEnd();
74+
}
75+
finally
76+
{
77+
if (stream.CanSeek && streamPosition != stream.Position && rewindContentStream)
78+
{
79+
stream.Seek(streamPosition, SeekOrigin.Begin);
80+
}
81+
}
82+
}
83+
}
5384
}
5485
}

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Extensions/JsonExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,32 @@ public static bool CanConvertTo<TType>(this JToken jobject)
147147
return jobject.TryConvertTo(out ignored);
148148
}
149149

150+
/// <summary>
151+
/// Checks if a conversion from the supplied <see cref="JToken"/> to a <typeparamref name="TType"/> can be made.
152+
/// </summary>
153+
/// <typeparam name="TType">The type to convert to.</typeparam>
154+
/// <param name="str">The string.</param>
155+
/// <param name="result">The result.</param>
156+
public static bool TryConvertTo<TType>(this string str, out TType result)
157+
{
158+
JToken tmp = null;
159+
try
160+
{
161+
tmp = str.ToJToken();
162+
}
163+
catch (FormatException)
164+
{
165+
}
166+
catch (ArgumentException)
167+
{
168+
}
169+
catch (JsonException)
170+
{
171+
}
172+
173+
return tmp.TryConvertTo<TType>(out result);
174+
}
175+
150176
/// <summary>
151177
/// Checks if a conversion from the supplied <see cref="JToken"/> to a <typeparamref name="TType"/> can be made.
152178
/// </summary>

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/InvokeAzureResourceActionCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ protected override void OnProcessRecord()
8080
odataQuery: this.ODataQuery);
8181

8282
var activity = string.Format("POST {0}", managementUri.PathAndQuery);
83-
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false)
83+
var resultString = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false)
8484
.WaitOnOperation(operationResult: operationResult);
8585

86-
this.WriteObject(result, ResourceObjectFormat.New);
86+
this.TryConvertAndWriteObject(resultString);
8787
});
8888
}
8989

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/MoveAzureResourceCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private void RunCmdlet()
173173
isResourceCreateOrUpdate: false)
174174
.WaitOnOperation(operationResult: operationResult);
175175

176-
this.WriteObject(result, ResourceObjectFormat.New);
176+
this.TryConvertAndWriteObject(result, ResourceObjectFormat.New);
177177
}
178178
else
179179
{

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected override void OnProcessRecord()
129129
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
130130
.WaitOnOperation(operationResult: operationResult);
131131

132-
this.WriteObject(result, this.OutputObjectFormat.Value);
132+
this.TryConvertAndWriteObject(result, this.OutputObjectFormat.Value);
133133
});
134134
}
135135

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/RemoveAzureResourceCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected override void OnProcessRecord()
6060
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false)
6161
.WaitOnOperation(operationResult: operationResult);
6262

63-
this.WriteObject(result, ResourceObjectFormat.New);
63+
this.TryConvertAndWriteObject(result, ResourceObjectFormat.New);
6464
});
6565
}
6666
}

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/ResourceManagerCmdletBase.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,24 @@ public ResourceManagerRestRestClient GetResourcesClient()
259259
headerValues: AzureSession.ClientFactory.UserAgents));
260260
}
261261

262+
/// <summary>
263+
/// Writes the object
264+
/// </summary>
265+
/// <param name="resultString">The result as a string</param>
266+
/// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param>
267+
protected void TryConvertAndWriteObject(string resultString, ResourceObjectFormat objectFormat)
268+
{
269+
JToken resultJToken;
270+
if (resultString.TryConvertTo<JToken>(out resultJToken))
271+
{
272+
this.WriteObject(resultJToken, objectFormat);
273+
}
274+
else
275+
{
276+
this.WriteObject(resultString);
277+
}
278+
}
279+
262280
/// <summary>
263281
/// Writes a <see cref="JToken"/> object as a <see cref="PSObject"/>.
264282
/// </summary>

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/SetAzureResourceCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected override void OnProcessRecord()
124124
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
125125
.WaitOnOperation(operationResult: operationResult);
126126

127-
this.WriteObject(result, this.OutputObjectFormat.Value);
127+
this.TryConvertAndWriteObject(result, this.OutputObjectFormat.Value);
128128
});
129129
}
130130

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/RestClients/ResourceManagerRestClientBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private async Task<OperationResult> GetOperationResult(HttpResponseMessage respo
293293
var operationResult = new OperationResult
294294
{
295295
Value = await response
296-
.ReadContentAsJsonAsync<JObject>()
296+
.ReadContentAsStringAsync()
297297
.ConfigureAwait(continueOnCapturedContext: false),
298298
};
299299

0 commit comments

Comments
 (0)