Skip to content

Commit 73a750a

Browse files
Runbook cmdlet get
1 parent a90585d commit 73a750a

File tree

9 files changed

+255
-7
lines changed

9 files changed

+255
-7
lines changed

src/ServiceManagement/Automation/Commands.Automation/Cmdlet/AzureAutomationBaseCmdlet.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections.Generic;
1617
using System.Globalization;
1718
using System.IO;
1819
using System.Management.Automation;
@@ -57,8 +58,7 @@ public IAutomationClient AutomationClient
5758
/// <summary>
5859
/// Gets or sets the automation account name.
5960
/// </summary>
60-
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true,
61-
HelpMessage = "The automation account name.")]
61+
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")]
6262
public string AutomationAccountName { get; set; }
6363

6464
protected virtual void AutomationExecuteCmdlet()
@@ -93,6 +93,24 @@ public override void ExecuteCmdlet()
9393
}
9494
}
9595

96+
protected bool GenerateCmdletOutput(IEnumerable<object> results)
97+
{
98+
var ret = true;
99+
foreach (var result in results)
100+
{
101+
try
102+
{
103+
WriteObject(result);
104+
}
105+
catch (PipelineStoppedException)
106+
{
107+
ret = false;
108+
}
109+
}
110+
111+
return ret;
112+
}
113+
96114
private string ParseErrorMessage(string errorMessage)
97115
{
98116
// The errorMessage is expected to be the error details in JSON format.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using System.Security.Permissions;
19+
using Microsoft.Azure.Commands.Automation.Common;
20+
using Microsoft.Azure.Commands.Automation.Model;
21+
22+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
23+
{
24+
/// <summary>
25+
/// Gets azure automation schedules for a given account.
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Get, "AzureAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)]
28+
[OutputType(typeof(Runbook))]
29+
public class GetAzureAutomationRunbook : AzureAutomationBaseCmdlet
30+
{
31+
/// <summary>
32+
/// Gets or sets the runbook name.
33+
/// </summary>
34+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")]
35+
[ValidateNotNullOrEmpty]
36+
public string Name { get; set; }
37+
38+
/// <summary>
39+
/// Execute this cmdlet.
40+
/// </summary>
41+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
42+
protected override void AutomationExecuteCmdlet()
43+
{
44+
IEnumerable<Runbook> ret = null;
45+
if (this.ParameterSetName == AutomationCmdletParameterSets.ByAll)
46+
{
47+
ret = new List<Runbook>
48+
{
49+
this.AutomationClient.GetRunbook(this.AutomationAccountName, this.Name)
50+
};
51+
}
52+
else if (this.ParameterSetName == AutomationCmdletParameterSets.ByName)
53+
{
54+
ret = this.AutomationClient.ListRunbooks(this.AutomationAccountName);
55+
}
56+
57+
this.GenerateCmdletOutput(ret);
58+
}
59+
}
60+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<ItemGroup>
5555
<Reference Include="Microsoft.Azure.Management.Automation, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5656
<SpecificVersion>False</SpecificVersion>
57-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Automation.1.0.0-preview\lib\net40\Microsoft.Azure.Management.Automation.dll</HintPath>
57+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Automation.2.0.0-preview\lib\net40\Microsoft.Azure.Management.Automation.dll</HintPath>
5858
</Reference>
5959
<Reference Include="Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
6060
<SpecificVersion>False</SpecificVersion>
@@ -100,15 +100,18 @@
100100
</ItemGroup>
101101
<ItemGroup>
102102
<Compile Include="Cmdlet\AzureAutomationBaseCmdlet.cs" />
103+
<Compile Include="Cmdlet\GetAzureAutomationRunbook.cs" />
103104
<Compile Include="Cmdlet\GetAzureAutomationSchedule.cs" />
104105
<Compile Include="Common\AutomationClient.cs" />
106+
<Compile Include="Common\AutomationCmdletParameterSet.cs" />
105107
<Compile Include="Common\IAutomationClient.cs" />
106108
<Compile Include="Common\Requires.cs" />
107109
<Compile Include="Common\RequiresExtensions.cs" />
108110
<Compile Include="Common\ResourceNotFoundException.cs" />
109111
<Compile Include="DataContract\ErrorResponse.cs" />
110112
<Compile Include="DataContract\OdataError.cs" />
111113
<Compile Include="DataContract\OdataErrorMessage.cs" />
114+
<Compile Include="Model\Runbook.cs" />
112115
<Compile Include="Model\Schedule.cs" />
113116
<Compile Include="Model\ScheduleFrequency.cs" />
114117
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -137,6 +140,7 @@
137140
<EmbeddedResource Include="Properties\Resources.resx">
138141
<Generator>ResXFileCodeGenerator</Generator>
139142
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
143+
<SubType>Designer</SubType>
140144
</EmbeddedResource>
141145
</ItemGroup>
142146
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
using System.Collections;
1717
using System.Collections.Generic;
1818
using System.Globalization;
19-
using System.IO;
2019
using System.Linq;
21-
using System.Text;
2220
using Microsoft.Azure.Commands.Automation.Model;
2321
using Microsoft.Azure.Commands.Automation.Properties;
2422
using Microsoft.Azure.Management.Automation;
2523
using Microsoft.WindowsAzure.Commands.Common;
2624
using Microsoft.WindowsAzure.Commands.Common.Models;
27-
using Newtonsoft.Json;
2825

2926
namespace Microsoft.Azure.Commands.Automation.Common
3027
{
@@ -79,6 +76,32 @@ public IEnumerable<Schedule> ListSchedules(string automationAccountName)
7976
return scheduleModels.Select(this.CreateScheduleFromScheduleModel);
8077
}
8178

79+
public Runbook GetRunbook(string automationAccountName, string name)
80+
{
81+
var sdkRunbook = this.automationManagementClient.Runbooks.Get(
82+
automationAccountName, name).Runbook;
83+
84+
if (sdkRunbook == null)
85+
{
86+
throw new ResourceNotFoundException(typeof(Runbook), string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, name));
87+
}
88+
89+
return new Runbook(sdkRunbook);
90+
}
91+
92+
public IEnumerable<Runbook> ListRunbooks(string automationAccountName)
93+
{
94+
return AutomationManagementClient
95+
.ContinuationTokenHandler(
96+
skipToken =>
97+
{
98+
var response = this.automationManagementClient.Runbooks.List(
99+
automationAccountName, skipToken);
100+
return new ResponseWithSkipToken<AutomationManagement.Models.Runbook>(
101+
response, response.Runbooks);
102+
}).Select(c => new Runbook(c));
103+
}
104+
82105
#endregion
83106

84107
#region Private Methods
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
21+
namespace Microsoft.Azure.Commands.Automation.Common
22+
{
23+
internal static class AutomationCmdletParameterSets
24+
{
25+
internal const string ByAll = "ByAll";
26+
internal const string ByName = "ByName";
27+
}
28+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ public interface IAutomationClient
2727
Schedule GetSchedule(string automationAccountName, string scheduleName);
2828

2929
IEnumerable<Schedule> ListSchedules(string automationAccountName);
30+
31+
Runbook GetRunbook(string automationAccountName, string runbookName);
32+
33+
IEnumerable<Runbook> ListRunbooks(string automationAccountName);
3034
}
3135
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using Microsoft.Azure.Commands.Automation.Common;
17+
18+
namespace Microsoft.Azure.Commands.Automation.Model
19+
{
20+
using AutomationManagement = Management.Automation;
21+
22+
/// <summary>
23+
/// The Runbook.
24+
/// </summary>
25+
public class Runbook
26+
{
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="Runbook"/> class.
29+
/// </summary>
30+
/// <param name="runbook">
31+
/// The runbook.
32+
/// </param>
33+
/// <exception cref="System.ArgumentException">
34+
/// </exception>
35+
public Runbook(AutomationManagement.Models.Runbook runbook)
36+
{
37+
Requires.Argument("runbook", runbook).NotNull();
38+
39+
this.Name = runbook.Name;
40+
this.CreationTime = runbook.Properties.CreationTime.ToLocalTime();
41+
this.LastModifiedTime = runbook.Properties.LastModifiedTime.ToLocalTime();
42+
this.LastModifiedBy = runbook.Properties.LastModifiedBy;
43+
this.Description = runbook.Properties.Description;
44+
// this.Tags = runbook.Tags != null ? runbook.Tags.Split(Constants.RunbookTagsSeparatorChar) : new string[] { };
45+
this.LogVerbose = runbook.Properties.LogVerbose;
46+
this.LogProgress = runbook.Properties.LogProgress;
47+
this.State = runbook.Properties.State;
48+
}
49+
50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="Runbook"/> class.
52+
/// </summary>
53+
public Runbook()
54+
{
55+
}
56+
57+
/// <summary>
58+
/// Gets or sets the name.
59+
/// </summary>
60+
public string Name { get; set; }
61+
62+
/// <summary>
63+
/// Gets or sets the creation time.
64+
/// </summary>
65+
public DateTimeOffset CreationTime { get; set; }
66+
67+
/// <summary>
68+
/// Gets or sets the last modified time.
69+
/// </summary>
70+
public DateTimeOffset LastModifiedTime { get; set; }
71+
72+
/// <summary>
73+
/// Gets or sets the last modified by.
74+
/// </summary>
75+
public string LastModifiedBy { get; set; }
76+
77+
/// <summary>
78+
/// Gets or sets the description.
79+
/// </summary>
80+
public string Description { get; set; }
81+
82+
/// <summary>
83+
/// Gets or sets the tags.
84+
/// </summary>
85+
public string[] Tags { get; set; }
86+
87+
/// <summary>
88+
/// Gets or sets a value indicating whether log verbose is enabled.
89+
/// </summary>
90+
public bool LogVerbose { get; set; }
91+
92+
/// <summary>
93+
/// Gets or sets a value indicating whether log progress is enabled.
94+
/// </summary>
95+
public bool LogProgress { get; set; }
96+
97+
/// <summary>
98+
/// Gets or sets the state of runbook.
99+
/// </summary>
100+
public string State { get; set; }
101+
}
102+
}

src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServiceManagement/Automation/Commands.Automation/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Azure.Management.Automation" version="0.12.1-preview" targetFramework="net45" />
3+
<package id="Microsoft.Azure.Management.Automation" version="2.0.0-preview" targetFramework="net45" />
44
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
55
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
66
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />

0 commit comments

Comments
 (0)