Skip to content

Commit 4a1731c

Browse files
client Request Id for Runbook cmdlets
1 parent 6e04678 commit 4a1731c

File tree

5 files changed

+179
-100
lines changed

5 files changed

+179
-100
lines changed

src/Common/Commands.ScenarioTest/Resources/Automation/AutomationTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Checks whether the first string contains the second one
1818
#>
1919

20-
$accountName='safeer'
20+
$accountName='account'
2121
$location = "East US"
2222

2323
function AssertContains

src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
<DesignTime>True</DesignTime>
196196
<DependentUpon>Resources.resx</DependentUpon>
197197
</Compile>
198+
<Compile Include="RequestSettings.cs" />
198199
</ItemGroup>
199200
<ItemGroup>
200201
<Content Include="Microsoft.Azure.Commands.Automation.dll-help.xml">

src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs

Lines changed: 148 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,35 @@ public IEnumerable<Runbook> ListRunbooks(string automationAccountName)
181181
public Runbook CreateRunbookByName(string automationAccountName, string runbookName, string description,
182182
string[] tags)
183183
{
184-
var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName);
185-
if (runbookModel != null)
184+
using (var request = new RequestSettings(this.automationManagementClient))
186185
{
187-
throw new ResourceCommonException(typeof(Runbook),
188-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyExists, runbookName));
189-
}
186+
var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName);
187+
if (runbookModel != null)
188+
{
189+
throw new ResourceCommonException(typeof (Runbook),
190+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyExists, runbookName));
191+
}
190192

191-
var rdcprop = new RunbookCreateDraftProperties()
192-
{
193-
Description = description,
194-
RunbookType = RunbookTypeEnum.Script,
195-
Draft = new RunbookDraft(),
196-
ServiceManagementTags = (tags != null) ? string.Join(Constants.RunbookTagsSeparatorString, tags) : null
197-
};
193+
var rdcprop = new RunbookCreateDraftProperties()
194+
{
195+
Description = description,
196+
RunbookType = RunbookTypeEnum.Script,
197+
Draft = new RunbookDraft(),
198+
ServiceManagementTags =
199+
(tags != null) ? string.Join(Constants.RunbookTagsSeparatorString, tags) : null
200+
};
198201

199-
var rdcparam = new RunbookCreateDraftParameters() { Name = runbookName, Properties = rdcprop, Tags = null };
202+
var rdcparam = new RunbookCreateDraftParameters()
203+
{
204+
Name = runbookName,
205+
Properties = rdcprop,
206+
Tags = null
207+
};
200208

201-
this.automationManagementClient.Runbooks.CreateWithDraft(automationAccountName, rdcparam);
209+
this.automationManagementClient.Runbooks.CreateWithDraft(automationAccountName, rdcparam);
202210

203-
return this.GetRunbook(automationAccountName, runbookName);
211+
return this.GetRunbook(automationAccountName, runbookName);
212+
}
204213
}
205214

206215
public Runbook CreateRunbookByPath(string automationAccountName, string runbookPath, string description,
@@ -209,31 +218,37 @@ public Runbook CreateRunbookByPath(string automationAccountName, string runbookP
209218

210219
var runbookName = Path.GetFileNameWithoutExtension(runbookPath);
211220

212-
var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName);
213-
if (runbookModel != null)
221+
using (var request = new RequestSettings(this.automationManagementClient))
214222
{
215-
throw new ResourceCommonException(typeof(Runbook),
216-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyExists, runbookName));
217-
}
223+
var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName);
224+
if (runbookModel != null)
225+
{
226+
throw new ResourceCommonException(typeof (Runbook),
227+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyExists, runbookName));
228+
}
218229

219-
var runbook = this.CreateRunbookByName(automationAccountName, runbookName, description, tags);
230+
var runbook = this.CreateRunbookByName(automationAccountName, runbookName, description, tags);
220231

221-
var rduprop = new RunbookDraftUpdateParameters()
222-
{
223-
Name = runbookName,
224-
Stream = File.ReadAllText(runbookPath)
225-
};
232+
var rduprop = new RunbookDraftUpdateParameters()
233+
{
234+
Name = runbookName,
235+
Stream = File.ReadAllText(runbookPath)
236+
};
226237

227-
this.automationManagementClient.RunbookDraft.Update(automationAccountName, rduprop);
238+
this.automationManagementClient.RunbookDraft.Update(automationAccountName, rduprop);
228239

229-
return runbook;
240+
return runbook;
241+
}
230242
}
231243

232244
public void DeleteRunbook(string automationAccountName, string runbookName)
233245
{
234246
try
235247
{
236-
this.automationManagementClient.Runbooks.Delete(automationAccountName, runbookName);
248+
using (var request = new RequestSettings(this.automationManagementClient))
249+
{
250+
this.automationManagementClient.Runbooks.Delete(automationAccountName, runbookName);
251+
}
237252
}
238253
catch (CloudException cloudException)
239254
{
@@ -250,119 +265,153 @@ public void DeleteRunbook(string automationAccountName, string runbookName)
250265
public Runbook UpdateRunbook(string automationAccountName, string runbookName, string description,
251266
string[] tags, bool? logProgress, bool? logVerbose)
252267
{
253-
var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName);
254-
if (runbookModel == null)
268+
using (var request = new RequestSettings(this.automationManagementClient))
255269
{
256-
throw new ResourceCommonException(typeof(Runbook),
257-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName));
258-
}
270+
var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName);
271+
if (runbookModel == null)
272+
{
273+
throw new ResourceCommonException(typeof (Runbook),
274+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName));
275+
}
259276

260-
var runbookUpdateParameters = new RunbookUpdateParameters();
261-
runbookUpdateParameters.Name = runbookName;
262-
runbookUpdateParameters.Tags = null;
277+
var runbookUpdateParameters = new RunbookUpdateParameters();
278+
runbookUpdateParameters.Name = runbookName;
279+
runbookUpdateParameters.Tags = null;
263280

264-
runbookUpdateParameters.Properties = new RunbookUpdateProperties();
265-
runbookUpdateParameters.Properties.Description = description ?? runbookModel.Properties.Description;
266-
runbookUpdateParameters.Properties.LogProgress = (logProgress.HasValue) ? logProgress.Value : runbookModel.Properties.LogProgress;
267-
runbookUpdateParameters.Properties.LogVerbose = (logVerbose.HasValue) ? logVerbose.Value : runbookModel.Properties.LogVerbose;
268-
runbookUpdateParameters.Properties.ServiceManagementTags = (tags != null)
269-
? string.Join(Constants.RunbookTagsSeparatorString, tags)
270-
: runbookModel.Properties.ServiceManagementTags;
281+
runbookUpdateParameters.Properties = new RunbookUpdateProperties();
282+
runbookUpdateParameters.Properties.Description = description ?? runbookModel.Properties.Description;
283+
runbookUpdateParameters.Properties.LogProgress = (logProgress.HasValue)
284+
? logProgress.Value
285+
: runbookModel.Properties.LogProgress;
286+
runbookUpdateParameters.Properties.LogVerbose = (logVerbose.HasValue)
287+
? logVerbose.Value
288+
: runbookModel.Properties.LogVerbose;
289+
runbookUpdateParameters.Properties.ServiceManagementTags = (tags != null)
290+
? string.Join(Constants.RunbookTagsSeparatorString, tags)
291+
: runbookModel.Properties.ServiceManagementTags;
271292

272-
var runbook = this.automationManagementClient.Runbooks.Update(automationAccountName, runbookUpdateParameters).Runbook;
293+
var runbook =
294+
this.automationManagementClient.Runbooks.Update(automationAccountName, runbookUpdateParameters)
295+
.Runbook;
273296

274-
return new Runbook(automationAccountName, runbook);
297+
return new Runbook(automationAccountName, runbook);
298+
}
275299
}
276300

277301
public RunbookDefinition UpdateRunbookDefinition(string automationAccountName, string runbookName,
278302
string runbookPath, bool overwrite)
279303
{
280-
var runbook = this.TryGetRunbookModel(automationAccountName, runbookName);
281-
if (runbook == null)
304+
using (var request = new RequestSettings(this.automationManagementClient))
282305
{
283-
throw new ResourceNotFoundException(typeof(Runbook),
284-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName));
285-
}
306+
var runbook = this.TryGetRunbookModel(automationAccountName, runbookName);
307+
if (runbook == null)
308+
{
309+
throw new ResourceNotFoundException(typeof (Runbook),
310+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName));
311+
}
286312

287-
if ((0 !=
288-
String.Compare(runbook.Properties.State, RunbookState.Published, CultureInfo.InvariantCulture,
289-
CompareOptions.IgnoreCase) && overwrite == false))
290-
{
291-
throw new ResourceCommonException(typeof(Runbook),
292-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyHasDraft, runbookName));
293-
}
313+
if ((0 !=
314+
String.Compare(runbook.Properties.State, RunbookState.Published, CultureInfo.InvariantCulture,
315+
CompareOptions.IgnoreCase) && overwrite == false))
316+
{
317+
throw new ResourceCommonException(typeof (Runbook),
318+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyHasDraft, runbookName));
319+
}
294320

295-
this.automationManagementClient.RunbookDraft.Update(automationAccountName,
296-
new RunbookDraftUpdateParameters { Name = runbookName, Stream = File.ReadAllText(runbookPath) });
321+
this.automationManagementClient.RunbookDraft.Update(automationAccountName,
322+
new RunbookDraftUpdateParameters {Name = runbookName, Stream = File.ReadAllText(runbookPath)});
297323

298-
var content =
299-
this.automationManagementClient.RunbookDraft.Content(automationAccountName, runbookName).Stream;
324+
var content =
325+
this.automationManagementClient.RunbookDraft.Content(automationAccountName, runbookName).Stream;
300326

301-
return new RunbookDefinition(automationAccountName, runbook, content, Constants.Draft);
327+
return new RunbookDefinition(automationAccountName, runbook, content, Constants.Draft);
328+
}
302329
}
303330

304331
public IEnumerable<RunbookDefinition> ListRunbookDefinitionsByRunbookName(string automationAccountName,
305332
string runbookName, bool? isDraft)
306333
{
307334
var ret = new List<RunbookDefinition>();
308-
309-
var runbook = this.TryGetRunbookModel(automationAccountName, runbookName);
310-
if (runbook == null)
335+
using (var request = new RequestSettings(this.automationManagementClient))
311336
{
312-
throw new ResourceNotFoundException(typeof(Runbook),
313-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName));
314-
}
315-
316-
var draftContent = String.Empty;
317-
var publishedContent = String.Empty;
337+
var runbook = this.TryGetRunbookModel(automationAccountName, runbookName);
338+
if (runbook == null)
339+
{
340+
throw new ResourceNotFoundException(typeof (Runbook),
341+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName));
342+
}
318343

319-
if (0 != String.Compare(runbook.Properties.State, RunbookState.Published, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) && (!isDraft.HasValue || isDraft.Value))
320-
{
321-
draftContent = this.automationManagementClient.RunbookDraft.Content(automationAccountName, runbookName).Stream;
322-
}
323-
if (0 != String.Compare(runbook.Properties.State, RunbookState.New, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) && (!isDraft.HasValue || !isDraft.Value))
324-
{
325-
publishedContent = this.automationManagementClient.Runbooks.Content(automationAccountName, runbookName).Stream;
326-
}
344+
var draftContent = String.Empty;
345+
var publishedContent = String.Empty;
327346

328-
// if no slot specified return both draft and publish content
329-
if (false == isDraft.HasValue)
330-
{
331-
if (false == String.IsNullOrEmpty(draftContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, draftContent, Constants.Draft));
332-
if (false == String.IsNullOrEmpty(publishedContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, publishedContent, Constants.Published));
333-
}
334-
else
335-
{
336-
if (true == isDraft.Value)
347+
if (0 !=
348+
String.Compare(runbook.Properties.State, RunbookState.Published, CultureInfo.InvariantCulture,
349+
CompareOptions.IgnoreCase) && (!isDraft.HasValue || isDraft.Value))
337350
{
351+
draftContent =
352+
this.automationManagementClient.RunbookDraft.Content(automationAccountName, runbookName).Stream;
353+
}
354+
if (0 !=
355+
String.Compare(runbook.Properties.State, RunbookState.New, CultureInfo.InvariantCulture,
356+
CompareOptions.IgnoreCase) && (!isDraft.HasValue || !isDraft.Value))
357+
{
358+
publishedContent =
359+
this.automationManagementClient.Runbooks.Content(automationAccountName, runbookName).Stream;
360+
}
338361

339-
if (String.IsNullOrEmpty(draftContent)) throw new ResourceCommonException(typeof(Runbook),
340-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoDraftVersion, runbookName));
341-
if (false == String.IsNullOrEmpty(draftContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, draftContent, Constants.Draft));
362+
// if no slot specified return both draft and publish content
363+
if (false == isDraft.HasValue)
364+
{
365+
if (false == String.IsNullOrEmpty(draftContent))
366+
ret.Add(new RunbookDefinition(automationAccountName, runbook, draftContent, Constants.Draft));
367+
if (false == String.IsNullOrEmpty(publishedContent))
368+
ret.Add(new RunbookDefinition(automationAccountName, runbook, publishedContent,
369+
Constants.Published));
342370
}
343371
else
344372
{
345-
if (String.IsNullOrEmpty(publishedContent)) throw new ResourceCommonException(typeof(Runbook),
346-
string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoPublishedVersion, runbookName));
373+
if (true == isDraft.Value)
374+
{
347375

348-
if (false == String.IsNullOrEmpty(publishedContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, publishedContent, Constants.Published));
376+
if (String.IsNullOrEmpty(draftContent))
377+
throw new ResourceCommonException(typeof (Runbook),
378+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoDraftVersion,
379+
runbookName));
380+
if (false == String.IsNullOrEmpty(draftContent))
381+
ret.Add(new RunbookDefinition(automationAccountName, runbook, draftContent, Constants.Draft));
382+
}
383+
else
384+
{
385+
if (String.IsNullOrEmpty(publishedContent))
386+
throw new ResourceCommonException(typeof (Runbook),
387+
string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoPublishedVersion,
388+
runbookName));
389+
390+
if (false == String.IsNullOrEmpty(publishedContent))
391+
ret.Add(new RunbookDefinition(automationAccountName, runbook, publishedContent,
392+
Constants.Published));
393+
}
349394
}
350-
}
351395

352-
return ret;
396+
return ret;
397+
}
353398
}
354399

355400
public Runbook PublishRunbook(string automationAccountName, string runbookName)
356401
{
357-
this.automationManagementClient.RunbookDraft.Publish(
402+
using (var request = new RequestSettings(this.automationManagementClient))
403+
{
404+
this.automationManagementClient.RunbookDraft.Publish(
358405
automationAccountName,
359406
new RunbookDraftPublishParameters
360407
{
361408
Name = runbookName,
362409
PublishedBy = Constants.ClientIdentity
363410
});
364411

365-
return this.GetRunbook(automationAccountName, runbookName);
412+
return this.GetRunbook(automationAccountName, runbookName);
413+
}
414+
366415
}
367416

368417
public Job StartRunbook(string automationAccountName, string runbookName, IDictionary parameters)

src/ServiceManagement/Automation/Commands.Automation/Common/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ public class AutomationAccountState
5353

5454
public const int PsCommandValueDepth = 10;
5555

56+
public const string ClientRequestIdHeaderName = "x-ms-client-request-id";
57+
5658
}
5759
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.Azure.Commands.Automation.Common;
7+
using Microsoft.WindowsAzure.Management.Automation;
8+
9+
namespace Microsoft.Azure.Commands.Automation
10+
{
11+
public class RequestSettings : IDisposable
12+
{
13+
private readonly AutomationManagementClient client;
14+
15+
public RequestSettings(IAutomationManagementClient automationClient)
16+
{
17+
client = ((AutomationManagementClient)automationClient);
18+
client.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName);
19+
client.HttpClient.DefaultRequestHeaders.Add(Constants.ClientRequestIdHeaderName, Guid.NewGuid().ToString());
20+
}
21+
22+
public void Dispose()
23+
{
24+
client.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)