Skip to content

Commit

Permalink
Merge pull request #399 from OctopusDeploy/enh-taskUserMethods
Browse files Browse the repository at this point in the history
Helper methods to Tasks and Users
  • Loading branch information
droyad authored Mar 14, 2019
2 parents c91c9be + 43357bc commit 0c6c27b
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4159,6 +4159,14 @@ Octopus.Client.Model
Nullable<DateTimeOffset> StartTime { get; set; }
Octopus.Client.Model.TaskState State { get; set; }
}
class TaskResourceCollection
Octopus.Client.Extensibility.IResource
Octopus.Client.Model.IAuditedResource
Octopus.Client.Model.ResourceCollection<TaskResource>
{
.ctor(IEnumerable<TaskResource>, Octopus.Client.Extensibility.LinkCollection)
Dictionary<String, Int32> TotalCounts { get; set; }
}
abstract class TaskRestrictedTo
{
static System.String DeploymentTargets
Expand Down Expand Up @@ -5961,7 +5969,9 @@ Octopus.Client.Repositories
Octopus.Client.Model.TaskResource ExecuteCommunityActionTemplatesSynchronisation(String)
Octopus.Client.Model.TaskResource ExecuteHealthCheck(String, Int32, Int32, String, String[], String, String, String[])
Octopus.Client.Model.TaskResource ExecuteTentacleUpgrade(String, String, String[], String, String, String[])
Octopus.Client.Model.TaskResourceCollection GetActiveWithSummary(Int32, Int32)
List<TaskResource> GetAllActive(Int32)
Octopus.Client.Model.TaskResourceCollection GetAllWithSummary(Int32, Int32)
Octopus.Client.Model.TaskDetailsResource GetDetails(Octopus.Client.Model.TaskResource, Nullable<Boolean>, Nullable<Int32>)
IReadOnlyList<TaskResource> GetQueuedBehindTasks(Octopus.Client.Model.TaskResource)
String GetRawOutputLog(Octopus.Client.Model.TaskResource)
Expand Down Expand Up @@ -6026,6 +6036,7 @@ Octopus.Client.Repositories
Octopus.Client.Model.UserResource Create(String, String, String, String)
Octopus.Client.Model.ApiKeyResource CreateApiKey(Octopus.Client.Model.UserResource, String)
Octopus.Client.Model.UserResource CreateServiceAccount(String, String)
Octopus.Client.Model.UserResource FindByUsername(String)
List<ApiKeyResource> GetApiKeys(Octopus.Client.Model.UserResource)
Octopus.Client.Model.UserResource GetCurrent()
Octopus.Client.Model.SpaceResource[] GetSpaces(Octopus.Client.Model.UserResource)
Expand Down Expand Up @@ -6538,7 +6549,9 @@ Octopus.Client.Repositories.Async
Task<TaskResource> ExecuteCommunityActionTemplatesSynchronisation(String)
Task<TaskResource> ExecuteHealthCheck(String, Int32, Int32, String, String[], String, String, String[])
Task<TaskResource> ExecuteTentacleUpgrade(String, String, String[], String, String, String[])
Task<TaskResourceCollection> GetActiveWithSummary(Int32, Int32)
Task<List<TaskResource>> GetAllActive(Int32)
Task<TaskResourceCollection> GetAllWithSummary(Int32, Int32)
Task<TaskDetailsResource> GetDetails(Octopus.Client.Model.TaskResource, Nullable<Boolean>, Nullable<Int32>)
Task<IReadOnlyList<TaskResource>> GetQueuedBehindTasks(Octopus.Client.Model.TaskResource)
Task<String> GetRawOutputLog(Octopus.Client.Model.TaskResource)
Expand Down Expand Up @@ -6604,6 +6617,7 @@ Octopus.Client.Repositories.Async
Task<UserResource> Create(String, String, String, String)
Task<ApiKeyResource> CreateApiKey(Octopus.Client.Model.UserResource, String)
Task<UserResource> CreateServiceAccount(String, String)
Task<UserResource> FindByUsername(String)
Task<List<ApiKeyResource>> GetApiKeys(Octopus.Client.Model.UserResource)
Task<UserResource> GetCurrent()
Task<SpaceResource[]> GetSpaces(Octopus.Client.Model.UserResource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4178,6 +4178,14 @@ Octopus.Client.Model
Nullable<DateTimeOffset> StartTime { get; set; }
Octopus.Client.Model.TaskState State { get; set; }
}
class TaskResourceCollection
Octopus.Client.Extensibility.IResource
Octopus.Client.Model.IAuditedResource
Octopus.Client.Model.ResourceCollection<TaskResource>
{
.ctor(IEnumerable<TaskResource>, Octopus.Client.Extensibility.LinkCollection)
Dictionary<String, Int32> TotalCounts { get; set; }
}
abstract class TaskRestrictedTo
{
static System.String DeploymentTargets
Expand Down Expand Up @@ -5983,7 +5991,9 @@ Octopus.Client.Repositories
Octopus.Client.Model.TaskResource ExecuteCommunityActionTemplatesSynchronisation(String)
Octopus.Client.Model.TaskResource ExecuteHealthCheck(String, Int32, Int32, String, String[], String, String, String[])
Octopus.Client.Model.TaskResource ExecuteTentacleUpgrade(String, String, String[], String, String, String[])
Octopus.Client.Model.TaskResourceCollection GetActiveWithSummary(Int32, Int32)
List<TaskResource> GetAllActive(Int32)
Octopus.Client.Model.TaskResourceCollection GetAllWithSummary(Int32, Int32)
Octopus.Client.Model.TaskDetailsResource GetDetails(Octopus.Client.Model.TaskResource, Nullable<Boolean>, Nullable<Int32>)
IReadOnlyList<TaskResource> GetQueuedBehindTasks(Octopus.Client.Model.TaskResource)
String GetRawOutputLog(Octopus.Client.Model.TaskResource)
Expand Down Expand Up @@ -6048,6 +6058,7 @@ Octopus.Client.Repositories
Octopus.Client.Model.UserResource Create(String, String, String, String)
Octopus.Client.Model.ApiKeyResource CreateApiKey(Octopus.Client.Model.UserResource, String)
Octopus.Client.Model.UserResource CreateServiceAccount(String, String)
Octopus.Client.Model.UserResource FindByUsername(String)
List<ApiKeyResource> GetApiKeys(Octopus.Client.Model.UserResource)
Octopus.Client.Model.UserResource GetCurrent()
Octopus.Client.Model.SpaceResource[] GetSpaces(Octopus.Client.Model.UserResource)
Expand Down Expand Up @@ -6560,7 +6571,9 @@ Octopus.Client.Repositories.Async
Task<TaskResource> ExecuteCommunityActionTemplatesSynchronisation(String)
Task<TaskResource> ExecuteHealthCheck(String, Int32, Int32, String, String[], String, String, String[])
Task<TaskResource> ExecuteTentacleUpgrade(String, String, String[], String, String, String[])
Task<TaskResourceCollection> GetActiveWithSummary(Int32, Int32)
Task<List<TaskResource>> GetAllActive(Int32)
Task<TaskResourceCollection> GetAllWithSummary(Int32, Int32)
Task<TaskDetailsResource> GetDetails(Octopus.Client.Model.TaskResource, Nullable<Boolean>, Nullable<Int32>)
Task<IReadOnlyList<TaskResource>> GetQueuedBehindTasks(Octopus.Client.Model.TaskResource)
Task<String> GetRawOutputLog(Octopus.Client.Model.TaskResource)
Expand Down Expand Up @@ -6626,6 +6639,7 @@ Octopus.Client.Repositories.Async
Task<UserResource> Create(String, String, String, String)
Task<ApiKeyResource> CreateApiKey(Octopus.Client.Model.UserResource, String)
Task<UserResource> CreateServiceAccount(String, String)
Task<UserResource> FindByUsername(String)
Task<List<ApiKeyResource>> GetApiKeys(Octopus.Client.Model.UserResource)
Task<UserResource> GetCurrent()
Task<SpaceResource[]> GetSpaces(Octopus.Client.Model.UserResource)
Expand Down
14 changes: 14 additions & 0 deletions source/Octopus.Client/Model/TaskResourceCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Octopus.Client.Extensibility;

namespace Octopus.Client.Model
{
public class TaskResourceCollection : ResourceCollection<TaskResource>
{
public TaskResourceCollection(IEnumerable<TaskResource> items, LinkCollection links) : base(items, links)
{
}

public Dictionary<string, int> TotalCounts { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected virtual void EnrichSpaceId(TResource resource)
}
}

async Task<string> ResolveLink()
protected async Task<string> ResolveLink()
{
if (CollectionLinkName == null && getCollectionLinkName != null)
CollectionLinkName = await getCollectionLinkName(Repository).ConfigureAwait(false);
Expand Down
34 changes: 29 additions & 5 deletions source/Octopus.Client/Repositories/Async/TaskRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,30 @@ public interface ITaskRepository : IPaginate<TaskResource>, IGet<TaskResource>,
Task<TaskDetailsResource> GetDetails(TaskResource resource, bool? includeVerboseOutput = null, int? tail = null);
Task<TaskResource> ExecuteActionTemplate(ActionTemplateResource resource, Dictionary<string, PropertyValueResource> properties, string[] machineIds = null, string[] environmentIds = null, string[] targetRoles = null, string description = null);
Task<TaskResource> ExecuteCommunityActionTemplatesSynchronisation(string description = null);

/// <summary>
/// Gets all the active tasks (optionally limited to pageSize)
/// </summary>
/// <param name="pageSize">Number of items per page, setting to less than the total items still retreives all items, but uses multiple requests reducing memory load on the server</param>
/// <returns></returns>
Task<List<TaskResource>> GetAllActive(int pageSize = int.MaxValue);

/// <summary>
/// Returns all active tasks (optionally limited to pageSize) along with a count of all tasks in each status
/// </summary>
/// <param name="pageSize"></param>
/// <param name="skip"></param>
/// <returns></returns>
Task<TaskResourceCollection> GetActiveWithSummary(int pageSize = int.MaxValue, int skip = 0);

/// <summary>
/// Returns all tasks (optionally limited to pageSize) along with a count of all tasks in each status
/// </summary>
/// <param name="pageSize"></param>
/// <param name="skip"></param>
/// <returns></returns>
Task<TaskResourceCollection> GetAllWithSummary(int pageSize = int.MaxValue, int skip = 0);

Task<string> GetRawOutputLog(TaskResource resource);
Task<TaskTypeResource[]> GetTaskTypes();
Task Rerun(TaskResource resource);
Expand Down Expand Up @@ -261,13 +284,14 @@ public async Task WaitForCompletion(TaskResource[] tasks, int pollIntervalSecond
}
}

/// <summary>
///
/// </summary>
/// <param name="pageSize">Number of items per page, setting to less than the total items still retreives all items, but uses multiple requests reducing memory load on the server</param>
/// <returns></returns>
public Task<List<TaskResource>> GetAllActive(int pageSize = int.MaxValue) => FindAll(pathParameters: new { active = true, take = pageSize });

public async Task<TaskResourceCollection> GetActiveWithSummary(int pageSize = int.MaxValue, int skip = 0)
=> await Client.Get<TaskResourceCollection>(await ResolveLink(), new {active = true, take = pageSize, skip});

public async Task<TaskResourceCollection> GetAllWithSummary(int pageSize = int.MaxValue, int skip = 0)
=> await Client.Get<TaskResourceCollection>(await ResolveLink(), new {take = pageSize, skip});

public ITaskRepository UsingContext(SpaceContext spaceContext)
{
return new TaskRepository(Repository, spaceContext);
Expand Down
4 changes: 4 additions & 0 deletions source/Octopus.Client/Repositories/Async/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IUserRepository :
IDelete<UserResource>,
ICreate<UserResource>
{
Task<UserResource> FindByUsername(string username);
Task<UserResource> Create(string username, string displayName, string password = null, string emailAddress = null);
Task<UserResource> CreateServiceAccount(string username, string displayName);
Task<UserResource> Register(RegisterCommand registerCommand);
Expand Down Expand Up @@ -40,6 +41,9 @@ public UserRepository(IOctopusAsyncRepository repository)
invitations = new LegacyInvitationRepository(Repository);
}

public Task<UserResource> FindByUsername(string username)
=> FindOne(u => u.Username.Equals(username, StringComparison.CurrentCultureIgnoreCase), pathParameters: new {filter = username});

public Task<UserResource> Create(string username, string displayName, string password = null, string emailAddress = null)
{
return Create(new UserResource
Expand Down
2 changes: 1 addition & 1 deletion source/Octopus.Client/Repositories/BasicRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ protected virtual void EnrichSpaceId(TResource resource)
}
}

string ResolveLink()
protected string ResolveLink()
{
if (CollectionLinkName == null && getCollectionLinkName != null)
CollectionLinkName = getCollectionLinkName(Repository);
Expand Down
58 changes: 41 additions & 17 deletions source/Octopus.Client/Repositories/TaskRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,30 @@ public interface ITaskRepository : IPaginate<TaskResource>, IGet<TaskResource>,
TaskResource ExecuteAdHocScript(string scriptBody, string[] machineIds = null, string[] environmentIds = null, string[] targetRoles = null, string description = null, string syntax = "PowerShell");
TaskResource ExecuteActionTemplate(ActionTemplateResource resource, Dictionary<string, PropertyValueResource> properties, string[] machineIds = null, string[] environmentIds = null, string[] targetRoles = null, string description = null);
TaskResource ExecuteCommunityActionTemplatesSynchronisation(string description = null);

/// <summary>
/// Gets all the active tasks (optionally limited to pageSize)
/// </summary>
/// <param name="pageSize">Number of items per page, setting to less than the total items still retreives all items, but uses multiple requests reducing memory load on the server</param>
/// <returns></returns>
List<TaskResource> GetAllActive(int pageSize = Int32.MaxValue);

/// <summary>
/// Returns all active tasks (optionally limited to pageSize) along with a count of all tasks in each status
/// </summary>
/// <param name="pageSize"></param>
/// <param name="skip"></param>
/// <returns></returns>
TaskResourceCollection GetActiveWithSummary(int pageSize = int.MaxValue, int skip = 0);

/// <summary>
/// Returns all tasks (optionally limited to pageSize) along with a count of all tasks in each status
/// </summary>
/// <param name="pageSize"></param>
/// <param name="skip"></param>
/// <returns></returns>
TaskResourceCollection GetAllWithSummary(int pageSize = int.MaxValue, int skip = 0);

TaskDetailsResource GetDetails(TaskResource resource, bool? includeVerboseOutput = null, int? tail = null);
string GetRawOutputLog(TaskResource resource);
TaskTypeResource[] GetTaskTypes();
Expand All @@ -45,7 +68,7 @@ public TaskRepository(IOctopusRepository repository)
}

public TaskResource ExecuteHealthCheck(
string description = null, int timeoutAfterMinutes = 5, int machineTimeoutAfterMinutes = 1, string environmentId = null, string[] machineIds = null,
string description = null, int timeoutAfterMinutes = 5, int machineTimeoutAfterMinutes = 1, string environmentId = null, string[] machineIds = null,
string restrictTo = null, string workerpoolId = null, string[] workerIds = null)
{
EnsureSingleSpaceContext();
Expand Down Expand Up @@ -140,7 +163,7 @@ public TaskResource ExecuteActionTemplate(ActionTemplateResource template, Dicti
{
if (string.IsNullOrEmpty(template?.Id)) throw new ArgumentException("The step template was either null, or has no ID");

var resource = new TaskResource(){SpaceId = template.SpaceId};
var resource = new TaskResource() {SpaceId = template.SpaceId};
resource.Name = BuiltInTasks.AdHocScript.Name;
resource.Description = string.IsNullOrWhiteSpace(description) ? "Run step template: " + template.Name : description;
resource.Arguments = new Dictionary<string, object>
Expand Down Expand Up @@ -191,19 +214,19 @@ public TaskTypeResource[] GetTaskTypes()
public void Rerun(TaskResource resource)
{
EnsureTaskCanRunInTheCurrentContext(resource);
Client.Post(resource.Link("Rerun"), (TaskResource)null);
Client.Post(resource.Link("Rerun"), (TaskResource) null);
}

public void Cancel(TaskResource resource)
{
EnsureTaskCanRunInTheCurrentContext(resource);
Client.Post(resource.Link("Cancel"), (TaskResource)null);
Client.Post(resource.Link("Cancel"), (TaskResource) null);
}

public void ModifyState(TaskResource resource, TaskState newState, string reason)
{
EnsureTaskCanRunInTheCurrentContext(resource);
Client.Post(resource.Link("State"), new { state = newState, reason = reason });
Client.Post(resource.Link("State"), new {state = newState, reason = reason});
}

public IReadOnlyList<TaskResource> GetQueuedBehindTasks(TaskResource resource)
Expand All @@ -213,24 +236,24 @@ public IReadOnlyList<TaskResource> GetQueuedBehindTasks(TaskResource resource)

public void WaitForCompletion(TaskResource task, int pollIntervalSeconds = 4, int timeoutAfterMinutes = 0, Action<TaskResource[]> interval = null)
{
WaitForCompletion(new[] { task }, pollIntervalSeconds, timeoutAfterMinutes, interval);
WaitForCompletion(new[] {task}, pollIntervalSeconds, timeoutAfterMinutes, interval);
}

public void WaitForCompletion(TaskResource[] tasks, int pollIntervalSeconds = 4, int timeoutAfterMinutes = 0, Action<TaskResource[]> interval = null)
=> WaitForCompletion(tasks, pollIntervalSeconds, TimeSpan.FromMinutes(timeoutAfterMinutes), interval);

public void WaitForCompletion(TaskResource[] tasks, int pollIntervalSeconds = 4, TimeSpan? timeoutAfter = null, Action<TaskResource[]> interval = null)
{
{
var start = Stopwatch.StartNew();
if (tasks == null || tasks.Length == 0)
return;

while (true)
{
var stillRunning =
(from task in tasks
let currentStatus = Client.Get<TaskResource>(task.Link("Self"), AdditionalQueryParameters)
select currentStatus).ToArray();
(from task in tasks
let currentStatus = Client.Get<TaskResource>(task.Link("Self"), AdditionalQueryParameters)
select currentStatus).ToArray();

interval?.Invoke(stillRunning);

Expand All @@ -246,12 +269,13 @@ public void WaitForCompletion(TaskResource[] tasks, int pollIntervalSeconds = 4,
}
}

/// <summary>
///
/// </summary>
/// <param name="pageSize">Number of items per page, setting to less than the total items still retreives all items, but uses multiple requests reducing memory load on the server</param>
/// <returns></returns>
public List<TaskResource> GetAllActive(int pageSize = int.MaxValue) => FindAll(pathParameters: new { active = true, take = pageSize });
public List<TaskResource> GetAllActive(int pageSize = int.MaxValue) => FindAll(pathParameters: new {active = true, take = pageSize});

public TaskResourceCollection GetActiveWithSummary(int pageSize = int.MaxValue, int skip = 0)
=> Client.Get<TaskResourceCollection>(ResolveLink(), new {active = true, take = pageSize, skip});

public TaskResourceCollection GetAllWithSummary(int pageSize = int.MaxValue, int skip = 0)
=> Client.Get<TaskResourceCollection>(ResolveLink(), new {take = pageSize, skip});

public ITaskRepository UsingContext(SpaceContext userDefinedSpaceContext)
{
Expand All @@ -263,7 +287,7 @@ void EnsureTaskCanRunInTheCurrentContext(TaskResource task)
if (string.IsNullOrEmpty(task.SpaceId))
return;
var spaceContext = GetCurrentSpaceContext();

spaceContext.ApplySpaceSelection(spaces =>
{
if (spaces.All(space => space.Id != task.SpaceId))
Expand Down
Loading

0 comments on commit 0c6c27b

Please sign in to comment.