Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
- Show runbook state in List runbooks (Issue #48) (#59)
Browse files Browse the repository at this point in the history
- Fix issue when trying to start a runbook not published (Issue #55)
  • Loading branch information
ejadib authored and dtzar committed Jun 24, 2016
1 parent 131ca01 commit 2e12c9d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
7 changes: 7 additions & 0 deletions AzureBot.Azure.Management/Models/RunBook.cs
Expand Up @@ -10,6 +10,13 @@ public class Runbook

public string RunbookId { get; set; }

public string RunbookState { get; set; }

public IEnumerable<RunbookParameter> RunbookParameters { get; set; }

public override string ToString()
{
return $"{this.RunbookName} (State: {this.RunbookState})";
}
}
}
Expand Up @@ -83,7 +83,7 @@ public async Task<IEnumerable<AutomationAccount>> ListAutomationAccountsAsync(st
}
}

public async Task<IEnumerable<AutomationAccount>> ListRunbooksAsync(string accessToken, string subscriptionId, params string[] runbooksStateFilter)
public async Task<IEnumerable<AutomationAccount>> ListRunbooksAsync(string accessToken, string subscriptionId)
{
var credentials = new TokenCredentials(subscriptionId, accessToken);

Expand All @@ -94,7 +94,7 @@ public async Task<IEnumerable<AutomationAccount>> ListRunbooksAsync(string acces
automationAccountsResult.Select(
async account =>
{
account.Runbooks = await this.ListAutomationRunbooks(accessToken, subscriptionId, account.ResourceGroup, account.AutomationAccountName, runbooksStateFilter);
account.Runbooks = await this.ListAutomationRunbooks(accessToken, subscriptionId, account.ResourceGroup, account.AutomationAccountName);
return account;
}).ToList());
Expand Down Expand Up @@ -260,6 +260,7 @@ private async Task<IEnumerable<Runbook>> ListAutomationRunbooks(string accessTok
{
RunbookId = runbook.Id,
RunbookName = runbook.Name,
RunbookState = runbook.Properties.State,
RunbookParameters = await this.ListAutomationRunbookParameters(accessToken, subscriptionId, resourceGroupName, automationAccountName, runbook.Name)
}).ToList());

Expand Down
25 changes: 22 additions & 3 deletions AzureBot/Dialogs/ActionDialog.cs
Expand Up @@ -247,7 +247,7 @@ public async Task ListRunbooksAsync(IDialogContext context, LuisResult result)
string.Empty,
(currentRunbooks, nextRunbook) =>
{
return currentRunbooks += $"\n\r{nextRunbook.RunbookName}";
return currentRunbooks += $"\n\r{nextRunbook}";
});
return current += singleAutomationAccount ? innerRunbooksText : $"\n\r {next.AutomationAccountName}" + innerRunbooksText;
Expand Down Expand Up @@ -312,7 +312,7 @@ public async Task StartRunbookAsync(IDialogContext context, LuisResult result)

var subscriptionId = context.GetSubscriptionId();

var availableAutomationAccounts = await new AzureRepository().ListRunbooksAsync(accessToken, subscriptionId, "Published");
var availableAutomationAccounts = await new AzureRepository().ListRunbooksAsync(accessToken, subscriptionId);

// check if the user specified a runbook name in the command
if (result.TryFindEntity("Runbook", out runbookEntity))
Expand All @@ -336,14 +336,23 @@ public async Task StartRunbookAsync(IDialogContext context, LuisResult result)
return;
}

var runbook = selectedAutomationAccount.Runbooks.SingleOrDefault(x => x.RunbookName.Equals(runbookName, StringComparison.InvariantCultureIgnoreCase));

// ensure that the runbook exists in the specified automation account
if (!selectedAutomationAccount.Runbooks.Any(x => x.RunbookName.Equals(runbookName, StringComparison.InvariantCultureIgnoreCase)))
if (runbook == null)
{
await context.PostAsync($"The '{runbookName}' runbook was not found in the '{automationAccountName}' automation account.");
context.Wait(this.MessageReceived);
return;
}

if (!runbook.RunbookState.Equals("Published", StringComparison.InvariantCultureIgnoreCase))
{
await context.PostAsync($"The '{runbookName}' runbook that you are trying to run is not published (State: {runbook.RunbookState}). Please go the Azure Portal and publish the runbook.");
context.Wait(this.MessageReceived);
return;
}

runbookEntity.Entity = runbookName;
runbookEntity.Type = "RunbookName";

Expand All @@ -362,6 +371,16 @@ public async Task StartRunbookAsync(IDialogContext context, LuisResult result)
return;
}

var runbooks = selectedAutomationAccounts.SelectMany(x => x.Runbooks.Where(r => r.RunbookName.Equals(runbookName, StringComparison.InvariantCultureIgnoreCase)
&& r.RunbookState.Equals("Published", StringComparison.InvariantCultureIgnoreCase)));

if (runbooks == null || !runbooks.Any())
{
await context.PostAsync($"The '{runbookName}' runbook that you are trying to run is not Published. Please go the Azure Portal and publish the runbook.");
context.Wait(this.MessageReceived);
return;
}

runbookEntity.Entity = runbookName;
runbookEntity.Type = "RunbookName";

Expand Down
2 changes: 1 addition & 1 deletion AzureBot/Forms/EntityForms.cs
Expand Up @@ -97,7 +97,7 @@ public static IForm<RunbookFormState> BuildRunbookForm()
}
foreach (var runbook in state.AvailableAutomationAccounts.Single(
a => a.AutomationAccountName == state.AutomationAccountName).Runbooks)
a => a.AutomationAccountName == state.AutomationAccountName).Runbooks.Where(x => x.RunbookState.Equals("Published", StringComparison.InvariantCultureIgnoreCase)))
{
field
.AddDescription(runbook.RunbookName, runbook.RunbookName)
Expand Down

0 comments on commit 2e12c9d

Please sign in to comment.